3.1-rc3 (revision d9ca08bb)
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, 2023,
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 <stdlib.h>
28 
29 
30 #include <otf2/otf2.h>
31 
32 
33 #include <pthread.h>
34 
35 
51 static OTF2_ErrorCode
53  const pthread_mutexattr_t* mutexAttribute );
54 
55 
71 static OTF2_ErrorCode
73  const pthread_mutexattr_t* mutexAttribute );
74 
75 
83 struct OTF2_LockObject
84 {
85  pthread_mutex_t mutex;
86 };
87 
88 
91 typedef struct OTF2_Pthread_UserData
92 {
93  const pthread_mutexattr_t* mutex_attribute;
94 } OTF2_Pthread_UserData;
95 
96 
97 static void
98 otf2_pthread_lock_release( void* userData )
99 {
100  OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
101 
102  if ( user_data->mutex_attribute )
103  {
104  /* Ignore errors */
105  pthread_mutexattr_destroy( ( pthread_mutexattr_t* )user_data->mutex_attribute );
106  }
107 
108  free( user_data );
109 }
110 
111 
112 static OTF2_CallbackCode
113 otf2_pthread_lock_create( void* userData,
114  OTF2_Lock* lock )
115 {
116  OTF2_Pthread_UserData* user_data = ( OTF2_Pthread_UserData* )userData;
117  int err;
118 
119  if ( !lock )
120  {
121  return OTF2_CALLBACK_ERROR;
122  }
123 
124  *lock = ( OTF2_Lock )malloc( sizeof( **lock ) );
125  if ( !*lock )
126  {
127  return OTF2_CALLBACK_ERROR;
128  }
129 
130  err = pthread_mutex_init( &( *lock )->mutex, user_data->mutex_attribute );
131  if ( 0 != err )
132  {
133  free( *lock );
134  return OTF2_CALLBACK_ERROR;
135  }
136 
137  return OTF2_CALLBACK_SUCCESS;
138 }
139 
140 
141 static OTF2_CallbackCode
142 otf2_pthread_lock_destroy( void* userData,
143  OTF2_Lock lock )
144 {
145  int err;
146 
147  ( void )userData;
148 
149  if ( !lock )
150  {
151  return OTF2_CALLBACK_ERROR;
152  }
153 
154  err = pthread_mutex_destroy( &lock->mutex );
155  free( lock );
156 
157  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
158 }
159 
160 
161 static OTF2_CallbackCode
162 otf2_pthread_lock_lock( void* userData,
163  OTF2_Lock lock )
164 {
165  int err;
166 
167  ( void )userData;
168 
169  if ( !lock )
170  {
171  return OTF2_CALLBACK_ERROR;
172  }
173 
174  err = pthread_mutex_lock( &lock->mutex );
175 
176  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
177 }
178 
179 
180 static OTF2_CallbackCode
181 otf2_pthread_lock_unlock( void* userData,
182  OTF2_Lock lock )
183 {
184  int err;
185 
186  ( void )userData;
187 
188  if ( !lock )
189  {
190  return OTF2_CALLBACK_ERROR;
191  }
192 
193  err = pthread_mutex_unlock( &lock->mutex );
194 
195  return 0 == err ? OTF2_CALLBACK_SUCCESS : OTF2_CALLBACK_ERROR;
196 }
197 
198 
199 static const OTF2_LockingCallbacks otf2_pthread_locking_callbacks =
200 {
201  otf2_pthread_lock_release,
202  otf2_pthread_lock_create,
203  otf2_pthread_lock_destroy,
204  otf2_pthread_lock_lock,
205  otf2_pthread_lock_unlock
206 };
207 
208 
209 static OTF2_ErrorCode
211  const pthread_mutexattr_t* mutexAttribute )
212 {
213  OTF2_ErrorCode ret;
214  OTF2_Pthread_UserData* user_data = NULL;
215 
217 
218  if ( !archive )
219  {
221  }
222 
223  user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
224  if ( !user_data )
225  {
227  }
228 
229  user_data->mutex_attribute = mutexAttribute;
230 
231  ret = OTF2_Archive_SetLockingCallbacks( archive,
232  &otf2_pthread_locking_callbacks,
233  user_data );
234  if ( OTF2_SUCCESS != ret )
235  {
236  free( user_data );
237  }
238  return ret;
239 }
240 
241 
242 static OTF2_ErrorCode
244  const pthread_mutexattr_t* mutexAttribute )
245 {
246  OTF2_ErrorCode ret;
247  OTF2_Pthread_UserData* user_data = NULL;
248 
250 
251  if ( !reader )
252  {
254  }
255 
256  user_data = ( OTF2_Pthread_UserData* )calloc( 1, sizeof( *user_data ) );
257  if ( !user_data )
258  {
260  }
261 
262  user_data->mutex_attribute = mutexAttribute;
263 
264  ret = OTF2_Reader_SetLockingCallbacks( reader,
265  &otf2_pthread_locking_callbacks,
266  user_data );
267  if ( OTF2_SUCCESS != ret )
268  {
269  free( user_data );
270  }
271  return ret;
272 }
273 
274 
280 #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:65
Definition: OTF2_ErrorCodes.h:230
Definition: OTF2_ErrorCodes.h:244
OTF2_ErrorCode
Definition: OTF2_ErrorCodes.h:53
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.