3.0-rc2 (revision 337012f1)
OTF2_Pthread_Locks.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Score-P software (http://www.score-p.org)
3  *
4  * Copyright (c) 2014,
5  * Technische Universitaet Dresden, Germany
6  *
7  * This software may be modified and distributed under the terms of
8  * a BSD-style license. See the COPYING file in the package base
9  * directory for details.
10  *
11  */
12 
13 
14 
23 #ifndef OTF2_PTHREAD_LOCKS_H
24 #define OTF2_PTHREAD_LOCKS_H
25 
26 
27 #include <otf2/otf2.h>
28 
29 
30 #include <pthread.h>
31 
32 
46 static OTF2_ErrorCode
48  const pthread_mutexattr_t* mutexAttribute );
49 
50 
64 static OTF2_ErrorCode
66  const pthread_mutexattr_t* mutexAttribute );
67 
68 
76 struct OTF2_LockObject
77 {
78  pthread_mutex_t mutex;
79 };
80 
81 
84 typedef struct OTF2_Pthread_UserData
85 {
86  const pthread_mutexattr_t* mutex_attribute;
87 } OTF2_Pthread_UserData;
88 
89 
90 static void
91 otf2_pthread_lock_release( void* userData )
92 {
93  OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
94 
95  if ( user_data->mutex_attribute )
96  {
97  /* Ignore errors */
98  pthread_mutexattr_destroy( ( pthread_mutexattr_t* )user_data->mutex_attribute );
99  }
100 
101  free( user_data );
102 }
103 
104 
105 static OTF2_CallbackCode
106 otf2_pthread_lock_create( void* userData,
107  OTF2_Lock* lock )
108 {
109  OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
110  int err;
111 
112  if ( !lock )
113  {
114  return OTF2_CALLBACK_ERROR;
115  }
116 
117  *lock = ( OTF2_Lock )malloc( sizeof( **lock ) );
118  if ( !*lock )
119  {
120  return OTF2_CALLBACK_ERROR;
121  }
122 
123  err = pthread_mutex_init( &( *lock )->mutex, user_data->mutex_attribute );
124  if ( 0 != err )
125  {
126  free( *lock );
127  return OTF2_CALLBACK_ERROR;
128  }
129 
130  return OTF2_CALLBACK_SUCCESS;
131 }
132 
133 
134 static OTF2_CallbackCode
135 otf2_pthread_lock_destroy( void* userData,
136  OTF2_Lock lock )
137 {
138  int err;
139 
140  ( void )userData;
141 
142  if ( !lock )
143  {
144  return OTF2_CALLBACK_ERROR;
145  }
146 
147  err = pthread_mutex_destroy( &lock->mutex );
148  free( lock );
149 
150  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
151 }
152 
153 
154 static OTF2_CallbackCode
155 otf2_pthread_lock_lock( void* userData,
156  OTF2_Lock lock )
157 {
158  int err;
159 
160  ( void )userData;
161 
162  if ( !lock )
163  {
164  return OTF2_CALLBACK_ERROR;
165  }
166 
167  err = pthread_mutex_lock( &lock->mutex );
168 
169  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
170 }
171 
172 
173 static OTF2_CallbackCode
174 otf2_pthread_lock_unlock( void* userData,
175  OTF2_Lock lock )
176 {
177  int err;
178 
179  ( void )userData;
180 
181  if ( !lock )
182  {
183  return OTF2_CALLBACK_ERROR;
184  }
185 
186  err = pthread_mutex_unlock( &lock->mutex );
187 
188  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
189 }
190 
191 
192 static const OTF2_LockingCallbacks otf2_pthread_locking_callbacks =
193 {
194  otf2_pthread_lock_release,
195  otf2_pthread_lock_create,
196  otf2_pthread_lock_destroy,
197  otf2_pthread_lock_lock,
198  otf2_pthread_lock_unlock
199 };
200 
201 
202 static OTF2_ErrorCode
204  const pthread_mutexattr_t* mutexAttribute )
205 {
206  OTF2_ErrorCode ret;
207  OTF2_Pthread_UserData* user_data = NULL;
208 
210 
211  if ( !archive )
212  {
214  }
215 
216  user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
217  if ( !user_data )
218  {
220  }
221 
222  user_data->mutex_attribute = mutexAttribute;
223 
224  ret = OTF2_Archive_SetLockingCallbacks( archive,
225  &otf2_pthread_locking_callbacks,
226  user_data );
227  if ( OTF2_SUCCESS != ret )
228  {
229  free( user_data );
230  }
231  return ret;
232 }
233 
234 
235 static OTF2_ErrorCode
237  const pthread_mutexattr_t* mutexAttribute )
238 {
239  OTF2_ErrorCode ret;
240  OTF2_Pthread_UserData* user_data = NULL;
241 
243 
244  if ( !reader )
245  {
247  }
248 
249  user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
250  if ( !user_data )
251  {
253  }
254 
255  user_data->mutex_attribute = mutexAttribute;
256 
257  ret = OTF2_Reader_SetLockingCallbacks( reader,
258  &otf2_pthread_locking_callbacks,
259  user_data );
260  if ( OTF2_SUCCESS != ret )
261  {
262  free( user_data );
263  }
264  return ret;
265 }
266 
267 
273 #endif /* OTF2_PTHREAD_LOCKS_H */
Record reading can continue.
Definition: OTF2_GeneralDefinitions.h:352
OTF2_ErrorCode OTF2_Archive_SetLockingCallbacks(OTF2_Archive *archive, const OTF2_LockingCallbacks *lockingCallbacks, void *lockingData)
Set the locking callbacks for the archive.
Main include file for applications using OTF2.
Definition: OTF2_ErrorCodes.h:66
Definition: OTF2_ErrorCodes.h:231
Definition: OTF2_ErrorCodes.h:245
OTF2_ErrorCode
Definition: OTF2_ErrorCodes.h:54
static OTF2_ErrorCode OTF2_Pthread_Archive_SetLockingCallbacks(OTF2_Archive *archive, const pthread_mutexattr_t *mutexAttribute)
Register callbacks to use Pthread mutexes for an OTF2 archive.
OTF2_CallbackCode
Return value to indicate that the record reading should be interrupted.
Definition: OTF2_GeneralDefinitions.h:349
struct OTF2_LockObject * OTF2_Lock
Opaque type for a locking object.
Definition: OTF2_Callbacks.h:552
Signaling an error in the callback.
Definition: OTF2_GeneralDefinitions.h:363
struct OTF2_Archive_struct OTF2_Archive
Keeps all meta-data for an OTF2 archive.
Definition: OTF2_Archive.h:220
struct OTF2_Reader_struct OTF2_Reader
Keeps all necessary information for the reader.
Definition: OTF2_Reader.h:193
Struct which holds all locking callbacks.
Definition: OTF2_Callbacks.h:643
static OTF2_ErrorCode OTF2_Pthread_Reader_SetLockingCallbacks(OTF2_Reader *reader, const pthread_mutexattr_t *mutexAttribute)
Register callbacks to use Pthread mutexes for an OTF2 reader.
OTF2_ErrorCode OTF2_Reader_SetLockingCallbacks(OTF2_Reader *reader, const OTF2_LockingCallbacks *lockingCallbacks, void *lockingData)
Set the locking callbacks for the reader.