summaryrefslogtreecommitdiffstats
path: root/src/H5TS.c
blob: 37b7244fa05459fa0051d0b1549329a3f0a2b682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/****************************************************************************
* NCSA HDF                                                                 *
* Software Development Group                                               *
* National Center for Supercomputing Applications                          *
* University of Illinois at Urbana-Champaign                               *
* 605 E. Springfield, Champaign IL 61820                                   *
*                                                                          *
* For conditions of distribution and use, see the accompanying             *
* hdf/COPYING file.                                                        *
*                                                                          *
****************************************************************************/

#ifdef RCSID
static char             RcsId[] = "@(#)$Revision$";
#endif

/* $Id$ */

/* private headers */
#include <H5private.h>          /*library                 		*/
#include <H5Eprivate.h>         /*error handling          		*/

#ifdef H5_HAVE_THREADSAFE

/* Module specific data structures */

/* cancelability structure */
typedef struct H5TS_cancel_struct {
  int previous_state;
  unsigned int cancel_count;
} H5TS_cancel_t;

/* Global variable definitions */
pthread_once_t H5TS_first_init_g = PTHREAD_ONCE_INIT;
pthread_key_t H5TS_errstk_key_g;
pthread_key_t H5TS_cancel_key_g;
hbool_t H5TS_allow_concurrent_g = FALSE; /* concurrent APIs override this */

/* Local function definitions */
#ifdef NOT_USED
static void H5TS_mutex_init(H5TS_mutex_t *mutex);
#endif /* NOT_USED */

/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_first_thread_init
 * USAGE
 *    H5TS_first_thread_init()
 * 
 * RETURNS
 *
 * DESCRIPTION
 *   Initialization of global API lock, keys for per-thread error stacks
 *   and cancallability information. Called by the first thread that enters
 *   the library.
 *
 * PROGRAMMER: Chee Wai LEE
 *             May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
void
H5TS_first_thread_init(void)
{
    /* initialize global API mutex lock                      */
    H5_g.H5_libinit_g = FALSE;
    H5_g.init_lock.owner_thread = NULL;
    pthread_mutex_init(&H5_g.init_lock.atomic_lock, NULL);
    pthread_cond_init(&H5_g.init_lock.cond_var, NULL);
    H5_g.init_lock.lock_count = 0;

    /* initialize key for thread-specific error stacks       */
    pthread_key_create(&H5TS_errstk_key_g, NULL);

    /* initialize key for thread cancellability mechanism    */
    pthread_key_create(&H5TS_cancel_key_g, NULL);
}

#ifdef NOT_USED
/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_mutex_init
 * USAGE
 *    H5TS_mutex_init(&mutex_var)
 * 
 * RETURNS
 *
 * DESCRIPTION
 *  Recursive lock semantics for HDF5 (lock initialization) -
 *  Multiple acquisition of a lock by a thread is permitted with a
 *  corresponding unlock operation required.
 *
 * PROGRAMMER: Chee Wai LEE
 *             May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
static void
H5TS_mutex_init(H5TS_mutex_t *mutex)
{
    (*mutex).owner_thread = NULL;
    pthread_mutex_init(&(*mutex).atomic_lock, NULL);
    pthread_cond_init(&(*mutex).cond_var, NULL);
    (*mutex).lock_count = 0;
}
#endif /* NOT_USED */

/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_mutex_lock
 * USAGE
 *    H5TS_mutex_lock(&mutex_var)
 * 
 * RETURNS
 *
 * DESCRIPTION
 *  Recursive lock semantics for HDF5 (locking) -
 *  Multiple acquisition of a lock by a thread is permitted with a
 *  corresponding unlock operation required.
 *
 * PROGRAMMER: Chee Wai LEE
 *             May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
void
H5TS_mutex_lock(H5TS_mutex_t *mutex)
{
    pthread_mutex_lock(&(*mutex).atomic_lock);
    if (pthread_equal(pthread_self(), (*mutex).owner_thread)) {
        /* already owned by self - increment count */
        (*mutex).lock_count++;
    } else {
        if ((*mutex).owner_thread == NULL) {
            /* no one else has locked it - set owner and grab lock */
            (*mutex).owner_thread = pthread_self();
            (*mutex).lock_count = 1;
        } else {
            /* if already locked by someone else */
            while (1) {
                pthread_cond_wait(&(*mutex).cond_var, &(*mutex).atomic_lock);
                if ((*mutex).owner_thread == NULL) {
                    (*mutex).owner_thread = pthread_self();
                    (*mutex).lock_count = 1;
                    break;
                } /* else do nothing and loop back to wait on condition*/
            }
        }
    }
    pthread_mutex_unlock(&(*mutex).atomic_lock);
}

/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_mutex_unlock
 * USAGE
 *    H5TS_mutex_unlock(&mutex_var)
 * 
 * RETURNS
 *
 * DESCRIPTION
 *  Recursive lock semantics for HDF5 (unlocking) -
 *  Multiple acquisition of a lock by a thread is permitted with a
 *  corresponding unlock operation required.
 *
 * PROGRAMMER: Chee Wai LEE
 *             May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
void
H5TS_mutex_unlock(H5TS_mutex_t *mutex)
{
    pthread_mutex_lock(&(*mutex).atomic_lock);
    (*mutex).lock_count--;
    if ((*mutex).lock_count == 0) {
        (*mutex).owner_thread = NULL;
        pthread_cond_signal(&(*mutex).cond_var);
    }
    pthread_mutex_unlock(&(*mutex).atomic_lock);
}

/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_cancel_count_inc
 * USAGE
 *    H5TS_cancel_count_inc()
 * 
 * RETURNS
 *
 * DESCRIPTION
 *    Creates a cancelation counter for a thread if it is the first time
 *    the thread is entering the library.
 *
 *    if counter value is zero, then set cancelability type of the thread
 *    to PTHREAD_CANCEL_DISABLE as thread is entering the library and store
 *    the previous cancelability type into cancelation counter.
 *    Increase the counter value by 1.
 *
 * PROGRAMMER: Chee Wai LEE
 *            May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
void
H5TS_cancel_count_inc(void)
{
    H5TS_cancel_t *cancel_counter;

    if ((cancel_counter = pthread_getspecific(H5TS_cancel_key_g))!=NULL) {
        /* do nothing here */
    } else {
        /* first time thread calls library - create new counter and associate
         *  with key
         */
        cancel_counter = (H5TS_cancel_t *)malloc(sizeof(H5TS_cancel_t));
        cancel_counter->cancel_count = 0;
        pthread_setspecific(H5TS_cancel_key_g, (void *)cancel_counter);
    }

    if (cancel_counter->cancel_count == 0) {
        /* thread entering library */
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &(cancel_counter->previous_state));
    }
    cancel_counter->cancel_count++;
}

/*--------------------------------------------------------------------------
 * NAME
 *    H5TS_cancel_count_dec
 * USAGE
 *    H5TS_cancel_count_dec()
 *   
 * RETURNS
 *
 * DESCRIPTION
 *    if counter value is one, then set cancelability type of the thread
 *    to the previous cancelability type stored in the cancelation counter.
 *    (the thread is leaving the library).
 *
 *    Decrement the counter value by 1.
 *
 * PROGRAMMER: Chee Wai LEE
 *             May 2, 2000
 *
 * MODIFICATIONS:
 *
 *--------------------------------------------------------------------------
 */
void
H5TS_cancel_count_dec(void)
{
    H5TS_cancel_t *cancel_counter = pthread_getspecific(H5TS_cancel_key_g);

    if (cancel_counter->cancel_count == 1) {
        pthread_setcancelstate(cancel_counter->previous_state, NULL);
    }
    cancel_counter->cancel_count--;
}

#endif