1.5.1 (revision 4026)
otf2/OTF2_OpenMP_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_OPENMP_LOCKS_H
00024 #define OTF2_OPENMP_LOCKS_H
00025 
00026 
00027 #include <otf2/otf2.h>
00028 
00029 
00030 #include <omp.h>
00031 
00032 
00041 static OTF2_ErrorCode
00042 OTF2_OpenMP_Archive_SetLockingCallbacks( OTF2_Archive* archive );
00043 
00044 
00053 static OTF2_ErrorCode
00054 OTF2_OpenMP_Reader_SetLockingCallbacks( OTF2_Reader* reader );
00055 
00056 
00060 struct OTF2_LockObject
00061 {
00062     omp_lock_t lock;
00063 };
00064 
00065 
00067 static OTF2_CallbackCode
00068 otf2_openmp_lock_create( void*      userData,
00069                          OTF2_Lock* lock )
00070 {
00071     if ( !lock )
00072     {
00073         return OTF2_CALLBACK_ERROR;
00074     }
00075 
00076     *lock = ( OTF2_Lock )malloc( sizeof( **lock ) );
00077     if ( !*lock )
00078     {
00079         return OTF2_CALLBACK_ERROR;
00080     }
00081 
00082     omp_init_lock( &( *lock )->lock );
00083 
00084     return OTF2_CALLBACK_SUCCESS;
00085 }
00086 
00087 
00089 static OTF2_CallbackCode
00090 otf2_openmp_lock_destroy( void*     userData,
00091                           OTF2_Lock lock )
00092 {
00093     if ( !lock )
00094     {
00095         return OTF2_CALLBACK_ERROR;
00096     }
00097 
00098     omp_destroy_lock( &lock->lock );
00099 
00100     return OTF2_CALLBACK_SUCCESS;
00101 }
00102 
00103 
00105 static OTF2_CallbackCode
00106 otf2_openmp_lock_lock( void*     userData,
00107                        OTF2_Lock lock )
00108 {
00109     if ( !lock )
00110     {
00111         return OTF2_CALLBACK_ERROR;
00112     }
00113 
00114     omp_set_lock( &lock->lock );
00115 
00116     return OTF2_CALLBACK_SUCCESS;
00117 }
00118 
00119 
00121 static OTF2_CallbackCode
00122 otf2_openmp_lock_unlock( void*     userData,
00123                          OTF2_Lock lock )
00124 {
00125     if ( !lock )
00126     {
00127         return OTF2_CALLBACK_ERROR;
00128     }
00129 
00130     omp_unset_lock( &lock->lock );
00131 
00132     return OTF2_CALLBACK_SUCCESS;
00133 }
00134 
00135 
00137 static const OTF2_LockingCallbacks otf2_openmp_locking_callbacks =
00138 {
00139     NULL,
00140     otf2_openmp_lock_create,
00141     otf2_openmp_lock_destroy,
00142     otf2_openmp_lock_lock,
00143     otf2_openmp_lock_unlock
00144 };
00145 
00146 
00147 static OTF2_ErrorCode
00148 OTF2_OpenMP_Archive_SetLockingCallbacks( OTF2_Archive* archive )
00149 {
00150     ( void )OTF2_OpenMP_Reader_SetLockingCallbacks;
00151 
00152     if ( !archive )
00153     {
00154         return OTF2_ERROR_INVALID_ARGUMENT;
00155     }
00156 
00157     return OTF2_Archive_SetLockingCallbacks( archive,
00158                                              &otf2_openmp_locking_callbacks,
00159                                              NULL );
00160 }
00161 
00162 
00163 static OTF2_ErrorCode
00164 OTF2_OpenMP_Reader_SetLockingCallbacks( OTF2_Reader* reader )
00165 {
00166     ( void )OTF2_OpenMP_Archive_SetLockingCallbacks;
00167 
00168     if ( !reader )
00169     {
00170         return OTF2_ERROR_INVALID_ARGUMENT;
00171     }
00172 
00173     return OTF2_Reader_SetLockingCallbacks( reader,
00174                                             &otf2_openmp_locking_callbacks,
00175                                             NULL );
00176 }
00177 
00178 
00179 #endif /* OTF2_OPENMP_LOCKS_H */