22 #ifndef LUMIERA_RECMUTEX_H 23 #define LUMIERA_RECMUTEX_H 41 #define LUMIERA_RECMUTEX_SECTION(nobugflag, mtx) \ 42 for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \ 43 lumiera_lock_section_ = { \ 44 mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \ 45 NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \ 47 if (lumiera_lock_section_.lock) \ 48 lumiera_lock_section_.lock = \ 49 lumiera_recmutex_lock (mtx, &NOBUG_FLAG(nobugflag), \ 50 &lumiera_lock_section_.rh, NOBUG_CONTEXT); \ 51 lumiera_lock_section_.lock; \ 54 LUMIERA_RECMUTEX_SECTION_UNLOCK; \ 58 #define LUMIERA_RECMUTEX_SECTION_CHAIN(nobugflag, mtx) \ 59 for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_, \ 60 NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_lock_section_ = { \ 61 mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \ 62 NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \ 64 if (lumiera_lock_section_.lock) \ 66 REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \ 67 lumiera_lock_section_.lock = \ 68 lumiera_recmutex_lock (mtx, &NOBUG_FLAG(nobugflag), \ 69 lumiera_lock_section_.rh, NOBUG_CONTEXT); \ 70 LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_); \ 72 lumiera_lock_section_.lock; \ 75 LUMIERA_RECMUTEX_SECTION_UNLOCK; \ 79 #define LUMIERA_RECMUTEX_SECTION_UNLOCK \ 80 LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_) 90 pthread_mutex_t recmutex;
94 typedef lumiera_recmutex* LumieraRecmutex;
105 struct nobug_flag* flag,
106 const struct nobug_context ctx);
115 struct nobug_flag* flag,
116 const struct nobug_context ctx);
119 static inline LumieraRecmutex
120 lumiera_recmutex_lock (LumieraRecmutex
self,
121 struct nobug_flag* flag,
122 struct nobug_resource_user** handle,
123 const struct nobug_context ctx)
127 NOBUG_RESOURCE_WAIT_CTX (NOBUG_FLAG_RAW(flag), self->rh,
"acquire mutex", *handle, ctx)
129 if (pthread_mutex_lock (&self->recmutex))
132 NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) ;
140 static inline LumieraRecmutex
141 lumiera_recmutex_trylock (LumieraRecmutex
self,
142 struct nobug_flag* flag,
143 struct nobug_resource_user** handle,
144 const struct nobug_context ctx)
148 NOBUG_RESOURCE_TRY_CTX (NOBUG_FLAG_RAW(flag), self->rh,
"try acquire mutex", *handle, ctx)
150 int err = pthread_mutex_trylock (&self->recmutex);
154 NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) ;
158 NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx) ;
169 static inline LumieraRecmutex
170 lumiera_recmutex_timedlock (LumieraRecmutex
self,
171 const struct timespec* timeout,
172 struct nobug_flag* flag,
173 struct nobug_resource_user** handle,
174 const struct nobug_context ctx)
178 NOBUG_RESOURCE_TRY_CTX (NOBUG_FLAG_RAW(flag), self->rh,
"timed acquire mutex", *handle, ctx)
180 int err = pthread_mutex_timedlock (&self->recmutex, timeout);
184 NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) ;
188 NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx) ;
200 lumiera_recmutex_unlock (LumieraRecmutex
self,
201 struct nobug_flag* flag,
202 struct nobug_resource_user** handle,
203 const struct nobug_context ctx)
205 NOBUG_REQUIRE_CTX (
self, ctx);
207 NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx)
209 if (pthread_mutex_unlock (&self->recmutex))
definitions and declarations for error-handling on low-level locking
LumieraRecmutex lumiera_recmutex_destroy(LumieraRecmutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a recursive mutex variable.
Lumiera error handling (C interface).
#define LUMIERA_DIE(err)
Abort unconditionally with a 'Fatal Error!' message.
Mutual exclusion locking, header.
LumieraRecmutex lumiera_recmutex_init(LumieraRecmutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a recursive mutex variable Initialises a 'recursive' mutex which might be locked by the sa...
void lumiera_lockerror_set(int err, struct nobug_flag *flag, const struct nobug_context ctx)
Translate pthread error code into lumiera error.
Mutex state handle for locked code sections.