semaphore.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "wizard/studio.h"
00045 #include "wizard/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/memory_.h"
00048 #include "wizard/semaphore.h"
00049 #include "wizard/semaphore-private.h"
00050 #include "wizard/string_.h"
00051 #include "wizard/thread_.h"
00052 #include "wizard/thread-private.h"
00053
00054
00055
00056
00057 struct SemaphoreInfo
00058 {
00059 WizardMutexType
00060 mutex;
00061
00062 WizardThreadType
00063 id;
00064
00065 ssize_t
00066 reference_count;
00067
00068 size_t
00069 signature;
00070 };
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 WizardExport void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
00095 {
00096 assert(semaphore_info != (SemaphoreInfo **) NULL);
00097 if (*semaphore_info == (SemaphoreInfo *) NULL)
00098 {
00099 LockWizardMutex();
00100 if (*semaphore_info == (SemaphoreInfo *) NULL)
00101 *semaphore_info=AllocateSemaphoreInfo();
00102 UnlockWizardMutex();
00103 }
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 WizardExport SemaphoreInfo *AllocateSemaphoreInfo(void)
00125 {
00126 SemaphoreInfo
00127 *semaphore_info;
00128
00129
00130
00131
00132 semaphore_info=(SemaphoreInfo *) AcquireAlignedMemory(1,
00133 sizeof(SemaphoreInfo));
00134 if (semaphore_info == (SemaphoreInfo *) NULL)
00135 ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00136 (void) ResetWizardMemory(semaphore_info,0,sizeof(SemaphoreInfo));
00137
00138
00139
00140 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00141 {
00142 int
00143 status;
00144
00145 pthread_mutexattr_t
00146 mutex_info;
00147
00148 status=pthread_mutexattr_init(&mutex_info);
00149 if (status != 0)
00150 {
00151 errno=status;
00152 ThrowFatalException(ResourceFatalError,
00153 "unable to instantiate semaphore `%s'");
00154 }
00155 status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
00156 if (status != 0)
00157 {
00158 errno=status;
00159 ThrowFatalException(ResourceFatalError,
00160 "unable to instantiate semaphore `%s'");
00161 }
00162 status=pthread_mutexattr_destroy(&mutex_info);
00163 if (status != 0)
00164 {
00165 errno=status;
00166 ThrowFatalException(ResourceFatalError,
00167 "unable to instantiate semaphore `%s'");
00168 }
00169 }
00170 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00171 InitializeCriticalSection(&semaphore_info->mutex);
00172 #endif
00173 semaphore_info->id=GetWizardThreadId();
00174 semaphore_info->reference_count=0;
00175 semaphore_info->signature=WizardSignature;
00176 return(semaphore_info);
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 WizardExport void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
00202 {
00203 assert(semaphore_info != (SemaphoreInfo **) NULL);
00204 assert((*semaphore_info) != (SemaphoreInfo *) NULL);
00205 assert((*semaphore_info)->signature == WizardSignature);
00206 LockWizardMutex();
00207 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00208 {
00209 int
00210 status;
00211
00212 status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
00213 if (status != 0)
00214 {
00215 errno=status;
00216 ThrowFatalException(ResourceFatalError,
00217 "unable to destroy semaphore `%s'");
00218 }
00219 }
00220 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00221 DeleteCriticalSection(&(*semaphore_info)->mutex);
00222 #endif
00223 (*semaphore_info)->signature=(~WizardSignature);
00224 *semaphore_info=(SemaphoreInfo *) RelinquishAlignedMemory(*semaphore_info);
00225 UnlockWizardMutex();
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00251 {
00252 assert(semaphore_info != (SemaphoreInfo *) NULL);
00253 assert(semaphore_info->signature == WizardSignature);
00254 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00255 {
00256 int
00257 status;
00258
00259 status=pthread_mutex_lock(&semaphore_info->mutex);
00260 if (status != 0)
00261 {
00262 errno=status;
00263 ThrowFatalException(ResourceFatalError,
00264 "unable to lock semaphore `%s'");
00265 }
00266 }
00267 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00268 EnterCriticalSection(&semaphore_info->mutex);
00269 #endif
00270 #if defined(WIZARDSTOOLKIT_DEBUG)
00271 if ((semaphore_info->reference_count > 0) &&
00272 (IsWizardThreadEqual(semaphore_info->id) != WizardFalse))
00273 {
00274 (void) fprintf(stderr,"Warning: unexpected recursive lock!\n");
00275 (void) fflush(stderr);
00276 }
00277 #endif
00278 semaphore_info->id=GetWizardThreadId();
00279 semaphore_info->reference_count++;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo *semaphore_info)
00305 {
00306 assert(semaphore_info != (SemaphoreInfo *) NULL);
00307 assert(semaphore_info->signature == WizardSignature);
00308 UnlockSemaphoreInfo(semaphore_info);
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 WizardExport WizardBooleanType SemaphoreComponentGenesis(void)
00330 {
00331 LockWizardMutex();
00332 UnlockWizardMutex();
00333 return(WizardTrue);
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 WizardExport void SemaphoreComponentTerminus(void)
00355 {
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
00381 {
00382 assert(semaphore_info != (SemaphoreInfo *) NULL);
00383 assert(semaphore_info->signature == WizardSignature);
00384 #if defined(WIZARDSTOOLKIT_DEBUG)
00385 assert(IsWizardThreadEqual(semaphore_info->id) != WizardFalse);
00386 if (semaphore_info->reference_count == 0)
00387 {
00388 (void) fprintf(stderr,"Warning: semaphore lock already unlocked!\n");
00389 (void) fflush(stderr);
00390 return;
00391 }
00392 semaphore_info->reference_count--;
00393 #endif
00394 #if defined(WIZARDSTOOLKIT_HAVE_PTHREAD)
00395 {
00396 int
00397 status;
00398
00399 status=pthread_mutex_unlock(&semaphore_info->mutex);
00400 if (status != 0)
00401 {
00402 errno=status;
00403 ThrowFatalException(ResourceFatalError,
00404 "unable to unlock semaphore `%s'");
00405 }
00406 }
00407 #elif defined(WIZARDSTOOLKIT_HAVE_WINTHREADS)
00408 LeaveCriticalSection(&semaphore_info->mutex);
00409 #endif
00410 }