00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "lib/mutex.h"
00023
00029 LUMIERA_ERROR_DEFINE (MUTEX_LOCK, "Mutex locking failed");
00030 LUMIERA_ERROR_DEFINE (MUTEX_UNLOCK, "Mutex unlocking failed");
00031 LUMIERA_ERROR_DEFINE (MUTEX_DESTROY, "Mutex destroy failed");
00032
00033
00034 LumieraMutex
00035 lumiera_mutex_init (LumieraMutex self, const char* purpose, struct nobug_flag* flag)
00036 {
00037 if (self)
00038 {
00039 pthread_mutex_init (&self->mutex, NULL);
00040 NOBUG_RESOURCE_HANDLE_INIT (self->rh);
00041 NOBUG_RESOURCE_ANNOUNCE_RAW (flag, "mutex", purpose, self, self->rh);
00042 }
00043 return self;
00044 }
00045
00046
00047 static pthread_once_t recursive_mutexattr_once = PTHREAD_ONCE_INIT;
00048 static pthread_mutexattr_t recursive_mutexattr;
00049
00050 static void recursive_mutexattr_init()
00051 {
00052 pthread_mutexattr_init (&recursive_mutexattr);
00053 pthread_mutexattr_settype (&recursive_mutexattr, PTHREAD_MUTEX_RECURSIVE);
00054 }
00055
00056
00057 LumieraMutex
00058 lumiera_recmutex_init (LumieraMutex self, const char* purpose, struct nobug_flag* flag)
00059 {
00060 if (self)
00061 {
00062 if (recursive_mutexattr_once == PTHREAD_ONCE_INIT)
00063 pthread_once (&recursive_mutexattr_once, recursive_mutexattr_init);
00064
00065 pthread_mutex_init (&self->mutex, &recursive_mutexattr);
00066 NOBUG_RESOURCE_HANDLE_INIT (self->rh);
00067 NOBUG_RESOURCE_ANNOUNCE_RAW (flag, "recmutex", purpose, self, self->rh);
00068 }
00069 return self;
00070 }
00071
00072
00073 LumieraMutex
00074 lumiera_mutex_destroy (LumieraMutex self, struct nobug_flag* flag)
00075 {
00076 if (self)
00077 {
00078 NOBUG_RESOURCE_FORGET_RAW (flag, self->rh);
00079 if (pthread_mutex_destroy (&self->mutex))
00080 LUMIERA_DIE (MUTEX_DESTROY);
00081 }
00082 return self;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091