1.5.1 (revision 4026)
otf2/OTF2_Pthread_Locks.h
Go to the documentation of this file.
00001 /*
00002  * This file is part of the Score-P software (http://www.score-p.org)
00003  *
00004  * Copyright (c) 2014,
00005  * Technische Universitaet Dresden, Germany
00006  *
00007  * This software may be modified and distributed under the terms of
00008  * a BSD-style license.  See the COPYING file in the package base
00009  * directory for details.
00010  *
00011  */
00012 
00013 
00014 
00023 #ifndef OTF2_PTHREAD_LOCKS_H
00024 #define OTF2_PTHREAD_LOCKS_H
00025 
00026 
00027 #include <otf2/otf2.h>
00028 
00029 
00030 #include <pthread.h>
00031 
00032 
00045 static OTF2_ErrorCode
00046 OTF2_Pthread_Archive_SetLockingCallbacks( OTF2_Archive*              archive,
00047                                           const pthread_mutexattr_t* mutexAttribute );
00048 
00049 
00062 static OTF2_ErrorCode
00063 OTF2_Pthread_Reader_SetLockingCallbacks( OTF2_Reader*               reader,
00064                                          const pthread_mutexattr_t* mutexAttribute );
00065 
00066 
00070 struct OTF2_LockObject
00071 {
00072     pthread_mutex_t mutex;
00073 };
00074 
00075 
00079 typedef struct OTF2_Pthread_UserData
00080 {
00081     const pthread_mutexattr_t* mutex_attribute;
00082 } OTF2_Pthread_UserData;
00083 
00084 
00086 static void
00087 otf2_pthread_lock_release( void* userData )
00088 {
00089     OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
00090 
00091     if ( user_data->mutex_attribute )
00092     {
00093         /* Ignore errors */
00094         pthread_mutexattr_destroy( ( pthread_mutexattr_t* )user_data->mutex_attribute );
00095     }
00096 
00097     free( user_data );
00098 }
00099 
00100 
00102 static OTF2_CallbackCode
00103 otf2_pthread_lock_create( void*      userData,
00104                           OTF2_Lock* lock )
00105 {
00106     OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
00107     int                    err;
00108 
00109     if ( !lock )
00110     {
00111         return OTF2_CALLBACK_ERROR;
00112     }
00113 
00114     *lock = ( OTF2_Lock )malloc( sizeof( **lock ) );
00115     if ( !*lock )
00116     {
00117         return OTF2_CALLBACK_ERROR;
00118     }
00119 
00120     err = pthread_mutex_init( &( *lock )->mutex, user_data->mutex_attribute );
00121     if ( 0 != err )
00122     {
00123         free( *lock );
00124         return OTF2_CALLBACK_ERROR;
00125     }
00126 
00127     return OTF2_CALLBACK_SUCCESS;
00128 }
00129 
00130 
00132 static OTF2_CallbackCode
00133 otf2_pthread_lock_destroy( void*     userData,
00134                            OTF2_Lock lock )
00135 {
00136     int err;
00137 
00138     ( void )userData;
00139 
00140     if ( !lock )
00141     {
00142         return OTF2_CALLBACK_ERROR;
00143     }
00144 
00145     err = pthread_mutex_destroy( &lock->mutex );
00146     free( lock );
00147 
00148     return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
00149 }
00150 
00151 
00153 static OTF2_CallbackCode
00154 otf2_pthread_lock_lock( void*     userData,
00155                         OTF2_Lock lock )
00156 {
00157     int err;
00158 
00159     ( void )userData;
00160 
00161     if ( !lock )
00162     {
00163         return OTF2_CALLBACK_ERROR;
00164     }
00165 
00166     err = pthread_mutex_lock( &lock->mutex );
00167 
00168     return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
00169 }
00170 
00171 
00173 static OTF2_CallbackCode
00174 otf2_pthread_lock_unlock( void*     userData,
00175                           OTF2_Lock lock )
00176 {
00177     int err;
00178 
00179     ( void )userData;
00180 
00181     if ( !lock )
00182     {
00183         return OTF2_CALLBACK_ERROR;
00184     }
00185 
00186     err = pthread_mutex_unlock( &lock->mutex );
00187 
00188     return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
00189 }
00190 
00191 
00193 static const OTF2_LockingCallbacks otf2_pthread_locking_callbacks =
00194 {
00195     otf2_pthread_lock_release,
00196     otf2_pthread_lock_create,
00197     otf2_pthread_lock_destroy,
00198     otf2_pthread_lock_lock,
00199     otf2_pthread_lock_unlock
00200 };
00201 
00202 
00203 static OTF2_ErrorCode
00204 OTF2_Pthread_Archive_SetLockingCallbacks( OTF2_Archive*              archive,
00205                                           const pthread_mutexattr_t* mutexAttribute )
00206 {
00207     OTF2_Pthread_UserData* user_data = NULL;
00208 
00209     ( void )OTF2_Pthread_Reader_SetLockingCallbacks;
00210 
00211     if ( !archive )
00212     {
00213         return OTF2_ERROR_INVALID_ARGUMENT;
00214     }
00215 
00216     user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
00217     if ( !user_data )
00218     {
00219         return OTF2_ERROR_MEM_ALLOC_FAILED;
00220     }
00221 
00222     user_data->mutex_attribute = mutexAttribute;
00223 
00224     return OTF2_Archive_SetLockingCallbacks( archive,
00225                                              &otf2_pthread_locking_callbacks,
00226                                              user_data );
00227 }
00228 
00229 
00230 static OTF2_ErrorCode
00231 OTF2_Pthread_Reader_SetLockingCallbacks( OTF2_Reader*               reader,
00232                                          const pthread_mutexattr_t* mutexAttribute )
00233 {
00234     OTF2_Pthread_UserData* user_data = NULL;
00235 
00236     ( void )OTF2_Pthread_Archive_SetLockingCallbacks;
00237 
00238     if ( !reader )
00239     {
00240         return OTF2_ERROR_INVALID_ARGUMENT;
00241     }
00242 
00243     user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
00244     if ( !user_data )
00245     {
00246         return OTF2_ERROR_MEM_ALLOC_FAILED;
00247     }
00248 
00249     user_data->mutex_attribute = mutexAttribute;
00250 
00251     return OTF2_Reader_SetLockingCallbacks( reader,
00252                                             &otf2_pthread_locking_callbacks,
00253                                             user_data );
00254 }
00255 
00256 
00257 #endif /* OTF2_PTHREAD_LOCKS_H */