2
----------------------------------------------------------------------------
3
| Copyright (C) 2001 Emergent IT Inc. and Raytheon Systems Company |
5
| Permission to use, modify, and distribute this software and its |
6
| documentation for any purpose without fee is hereby granted, provided |
7
| that the above copyright notice appear in all copies and that both that |
8
| copyright notice and this permission notice appear in supporting |
10
----------------------------------------------------------------------------
13
#include <HE5_HdfEosDef.h>
15
#ifdef _HDFEOS5_THREADSAFE
17
/* Global Variable definition */
18
/* -------------------------- */
19
pthread_once_t HE5_HDFE_TS_FirstInit = PTHREAD_ONCE_INIT;
23
/*----------------------------------------------------------------------------
26
| FUNCTION: HE5_TSinitfirst |
28
| DESCRIPTION : This function initializes the mutex, once per run. |
31
| Return Value Type Units Description |
32
| ============ ====== ========= ===================================== |
43
| Date Programmer Description |
44
| ======== ============ =============================================== |
45
| 08/21/01 A.Muslimov Original development. |
46
| 08/27/01 A.Muslimov Added appropriate error handlings. |
49
----------------------------------------------------------------------------*/
53
#ifdef _HDFEOS5_THREADSAFE
56
char errbuf[HE5_HDFE_ERRBUFSIZE];
58
/* Initialize Global Mutex Variable */
59
/* -------------------------------- */
60
GlobalMutex.MasterThread = (pthread_t *)NULL;
62
/* Initialize mutex with default value */
63
/* ----------------------------------- */
64
status = pthread_mutex_init(&GlobalMutex.Lock, NULL);
65
if (status != SUCCEED)
68
sprintf(errbuf, "Failed to initialize the mutex.\n");
69
H5Epush(__FILE__, "HE5_TSinitfirst", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
72
/* Initialize condition variable with default attribute */
73
/* ---------------------------------------------------- */
74
status = pthread_cond_init(&GlobalMutex.CondVar, NULL);
75
if (status != SUCCEED)
78
sprintf(errbuf, "Failed to initialize the condition variable.\n");
79
H5Epush(__FILE__, "HE5_TSinitfirst", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
82
GlobalMutex.LockCount = 0;
88
/*----------------------------------------------------------------------------
91
| FUNCTION: HE5_TSmutexlock |
93
| DESCRIPTION : This function locks the mutex. |
96
| Return Value Type Units Description |
97
| ============ ====== ========= ===================================== |
98
| status herr_t none Return status variable |
101
| Mutex struct Input data structure |
110
| Date Programmer Description |
111
| ======== ============ =============================================== |
112
| 08/21/01 A.Muslimov Original development. |
113
| 08/27/01 A.Muslimov Added appropriate error handlings. |
116
----------------------------------------------------------------------------*/
118
HE5_TSmutexlock(HE5_HDFE_MutexStruct *Mutex)
120
herr_t status = SUCCEED;
121
#ifdef _HDFEOS5_THREADSAFE
122
char errbuf[HE5_HDFE_ERRBUFSIZE];
123
#ifdef _HDFEOS5_THREADSAFE_DEBUG
129
/* Initialize the mutex */
130
/* -------------------- */
131
HE5_FIRST_THREAD_INIT
133
/* Check the initialization */
134
/* ------------------------ */
135
if (status != SUCCEED)
138
sprintf(errbuf, "Failed to initialize the mutex.\n");
139
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
143
#ifdef _HDFEOS5_THREADSAFE_DEBUG
145
printf("Attempt to lock mutex ... Time = %lf \n", (double)lockTime);
150
status = pthread_mutex_lock(&Mutex->Lock);
151
if (status != SUCCEED)
154
sprintf(errbuf, "Failed to lock the mutex.\n");
155
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
159
/* Find out if the mutex is locked and who owns the lock */
160
/* ----------------------------------------------------- */
161
if (Mutex->MasterThread && pthread_equal(pthread_self(), *Mutex->MasterThread)){
162
/* already owned by self -- increment count */
163
/* ---------------------------------------- */
165
} else if (!Mutex->MasterThread) {
166
/* No one else has locked it -- set owner and get lock */
167
/* --------------------------------------------------- */
168
Mutex->MasterThread = (pthread_t *)malloc(sizeof(pthread_t));
169
if (Mutex->MasterThread == (pthread_t *)NULL)
172
sprintf(errbuf, "Cannot allocate memory for master thread ID.\n");
173
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_RESOURCE, H5E_BADRANGE, errbuf);
177
*Mutex->MasterThread = pthread_self();
178
Mutex->LockCount = 1;
180
/* if already locked by someone else */
181
/* --------------------------------- */
183
status = pthread_cond_wait(&Mutex->CondVar, &Mutex->Lock);
184
if (status != SUCCEED)
187
sprintf(errbuf, "Failed to block the thread on condition variable.\n");
188
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
192
if (!Mutex->MasterThread) {
193
Mutex->MasterThread = (pthread_t *)malloc(sizeof(pthread_t));
194
if (Mutex->MasterThread == (pthread_t *)NULL)
197
sprintf(errbuf, "Cannot allocate memory for master thread ID.\n");
198
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_RESOURCE, H5E_BADRANGE, errbuf);
201
#ifdef _HDFEOS5_THREADSAFE_DEBUG
203
printf("Locking mutex ... Time = %lf \n", (double)lockTime);
205
*Mutex->MasterThread = pthread_self();
206
Mutex->LockCount = 1;
213
/* Unlock the mutex */
214
/* ---------------- */
215
status = pthread_mutex_unlock(&Mutex->Lock);
216
if (status != SUCCEED)
219
sprintf(errbuf, "Failed to unlock the mutex.\n");
220
H5Epush(__FILE__, "HE5_TSmutexlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
222
#ifdef _HDFEOS5_THREADSAFE_DEBUG
224
printf("Locking mutex ... Time = %lf \n", (double)lockTime);
232
/*----------------------------------------------------------------------------
235
| FUNCTION: HE5_TSmutexunlock |
237
| DESCRIPTION : This function unlocks the mutex. |
240
| Return Value Type Units Description |
241
| ============ ====== ========= ===================================== |
242
| status herr_t Return status variable |
245
| Mutex struct data structure |
254
| Date Programmer Description |
255
| ======== ============ =============================================== |
256
| 08/21/01 A.Muslimov Original development. |
257
| 08/27/01 A.Muslimov Added appropriate error handlings. |
260
----------------------------------------------------------------------------*/
262
HE5_TSmutexunlock(HE5_HDFE_MutexStruct *Mutex)
264
herr_t status = SUCCEED;
265
#ifdef _HDFEOS5_THREADSAFE
266
char errbuf[HE5_HDFE_ERRBUFSIZE];
267
#ifdef _HDFEOS5_THREADSAFE_DEBUG
275
status = pthread_mutex_lock(&Mutex->Lock);
276
if (status != SUCCEED)
279
sprintf(errbuf, "Failed to unlock the mutex.\n");
280
H5Epush(__FILE__, "HE5_TSmutexunlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
285
if (Mutex->LockCount == 0)
287
free(Mutex->MasterThread);
288
Mutex->MasterThread = NULL;
289
status = pthread_cond_signal(&Mutex->CondVar);
290
if (status != SUCCEED)
293
sprintf(errbuf, "Failed to unblock the thread blocked on a condition variable.\n");
294
H5Epush(__FILE__, "HE5_TSmutexunlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
299
/* Unlock the mutex */
300
/* ---------------- */
301
status = pthread_mutex_unlock(&Mutex->Lock);
302
if (status != SUCCEED)
305
sprintf(errbuf, "Failed to unlock the mutex.\n");
306
H5Epush(__FILE__, "HE5_TSmutexunlock", __LINE__, H5E_FUNC, H5E_BADVALUE, errbuf);
308
#ifdef _HDFEOS5_THREADSAFE_DEBUG
310
printf("Unlocking mutex ... Time = %lf \n", (double)unlockTime);