SIONlib  1.7.7
Scalable I/O library for parallel access to task-local files
sion_lock.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
9 
14 #define _XOPEN_SOURCE 700
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include <assert.h>
21 #define sion_assert(expr, args) (assert(expr))
22 
23 #include "sion.h"
24 #include "sion_debug.h"
25 #include "sion_internal.h"
26 #include "sion_fd.h"
27 #include "sion_filedesc.h"
28 #include "sion_printts.h"
29 
30 #include "sion_lock.h"
31 
34  int lock_defined;
35  int (*user_lock)(void *);
36  int (*user_unlock)(void *);
37  void *user_lock_data;
38 };
40 static _sion_lock_user_cb_t _sion_lock_user_cb = {0, NULL, NULL, NULL};
41 
42 /* from configure */
43 #ifdef SION_PTHREADS
44 /* from Makefile */
45 #ifdef SION_USE_PTHREADS
46 #define _USE_PTHREADS
47 #endif
48 #else
49 #define _USE_NOT_PTHREADS
50 #endif
51 
52 #ifdef _USE_PTHREADS
53 #include <pthread.h>
54 #define SION_FDDATA_INITIALIZER {PTHREAD_MUTEX_INITIALIZER}
55 #else
56 #define SION_FDDATA_INITIALIZER {}
57 #endif
58 
59 #ifdef _USE_PTHREADS
60 static struct _sion_lockdata {
61  pthread_mutex_t lock;
62 } sion_lockdata = SION_FDDATA_INITIALIZER;
63 #endif
64 
65 int _sion_lock_register_lock_callbacks(int lock(void *), int unlock(void *), void *lock_data) {
66  int rc = SION_SUCCESS;
67 
68 
69  lock(lock_data);
70  DPRINTFP((2, "_sion_lock_register_lock_callbacks", _SION_DEFAULT_RANK, "enter\n"));
71  if(_sion_lock_user_cb.lock_defined==1) {
72  rc=SION_NOT_SUCCESS;
73  } else {
74  _sion_lock_user_cb.lock_defined=1;
75  _sion_lock_user_cb.user_lock=lock;
76  _sion_lock_user_cb.user_unlock=unlock;
77  _sion_lock_user_cb.user_lock_data=lock_data;
78  }
79  DPRINTFP((2, "_sion_lock_register_lock_callbacks", _SION_DEFAULT_RANK, "lock and unlock function defined, rc=%d\n",rc));
80  unlock(lock_data);
81 
82 
83  return(rc);
84 }
85 
86 int _sion_lock_user_callbacks_defined(void) {
87  int rc;
88  _sion_lock();
89  rc=_sion_lock_user_cb.lock_defined;
90  _sion_unlock();
91  DPRINTFP((2, "_sion_lock_user_callbacks_defined", _SION_DEFAULT_RANK, "check user lock cbs, rc=%d\n",rc));
92  return(rc);
93 };
94 
95 
96 #ifdef _USE_PTHREADS
97 int _sion_lock(void) {
98  int rc=SION_SUCCESS;
99  if(_sion_lock_user_cb.lock_defined==1) {
100  _sion_lock_user_cb.user_lock(_sion_lock_user_cb.user_lock_data);
101  } else {
102  if(pthread_mutex_lock(&sion_lockdata.lock)) {
103  rc=SION_NOT_SUCCESS;
104  };
105  }
106  return(rc);
107 };
108 
109 int _sion_unlock(void) {
110  int rc=SION_SUCCESS;
111  if(_sion_lock_user_cb.lock_defined==1) {
112  _sion_lock_user_cb.user_unlock(_sion_lock_user_cb.user_lock_data);
113  } else {
114  if(pthread_mutex_unlock(&sion_lockdata.lock)) {
115  rc=SION_NOT_SUCCESS;
116  }
117  }
118  return(rc);
119 }
120 
121 #else
122 
123 int _sion_lock(void) {
124  if(_sion_lock_user_cb.lock_defined==1) {
125  _sion_lock_user_cb.user_lock(_sion_lock_user_cb.user_lock_data);
126  }
127  return(SION_SUCCESS);
128 };
129 int _sion_unlock(void) {
130  if(_sion_lock_user_cb.lock_defined==1) {
131  _sion_lock_user_cb.user_unlock(_sion_lock_user_cb.user_lock_data);
132  }
133  return(SION_SUCCESS);
134 };
135 
136 #endif
callback wrappers for OMP thread safe fd manipulation
Sion Time Stamp Header.