* Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
213 lines
7.4 KiB
C
213 lines
7.4 KiB
C
/*
|
||
recmutex.h - recursive mutual exclusion locking
|
||
|
||
Copyright (C)
|
||
2008, 2009, Christian Thaeter <ct@pipapo.org>
|
||
|
||
**Lumiera** is free software; you can redistribute it and/or modify it
|
||
under the terms of the GNU General Public License as published by the
|
||
Free Software Foundation; either version 2 of the License, or (at your
|
||
option) any later version. See the file COPYING for further details.
|
||
*/
|
||
|
||
#ifndef LUMIERA_RECMUTEX_H
|
||
#define LUMIERA_RECMUTEX_H
|
||
|
||
#include "lib/error.h"
|
||
#include "lib/sectionlock.h"
|
||
#include "lib/lockerror.h"
|
||
#include "lib/mutex.h"
|
||
|
||
#include <pthread.h>
|
||
#include <nobug.h>
|
||
|
||
/**
|
||
* @file
|
||
* Mutual exclusion locking, header.
|
||
*/
|
||
|
||
/**
|
||
* Recursive Mutual exclusive section.
|
||
*/
|
||
#define LUMIERA_RECMUTEX_SECTION(nobugflag, mtx) \
|
||
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
|
||
lumiera_lock_section_ = { \
|
||
mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \
|
||
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
|
||
({ \
|
||
if (lumiera_lock_section_.lock) \
|
||
lumiera_lock_section_.lock = \
|
||
lumiera_recmutex_lock (mtx, &NOBUG_FLAG(nobugflag), \
|
||
&lumiera_lock_section_.rh, NOBUG_CONTEXT); \
|
||
lumiera_lock_section_.lock; \
|
||
}); \
|
||
({ \
|
||
LUMIERA_RECMUTEX_SECTION_UNLOCK; \
|
||
}))
|
||
|
||
|
||
#define LUMIERA_RECMUTEX_SECTION_CHAIN(nobugflag, mtx) \
|
||
for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_, \
|
||
NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_lock_section_ = { \
|
||
mtx, (lumiera_sectionlock_unlock_fn) lumiera_mutex_unlock \
|
||
NOBUG_ALPHA_COMMA(&NOBUG_FLAG(nobugflag)) NOBUG_ALPHA_COMMA_NULL}; \
|
||
({ \
|
||
if (lumiera_lock_section_.lock) \
|
||
{ \
|
||
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
||
lumiera_lock_section_.lock = \
|
||
lumiera_recmutex_lock (mtx, &NOBUG_FLAG(nobugflag), \
|
||
lumiera_lock_section_.rh, NOBUG_CONTEXT); \
|
||
LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_); \
|
||
} \
|
||
lumiera_lock_section_.lock; \
|
||
}); \
|
||
({ \
|
||
LUMIERA_RECMUTEX_SECTION_UNLOCK; \
|
||
}))
|
||
|
||
|
||
#define LUMIERA_RECMUTEX_SECTION_UNLOCK \
|
||
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
||
|
||
|
||
|
||
/**
|
||
* Recursive Mutex.
|
||
*
|
||
*/
|
||
struct lumiera_recmutex_struct
|
||
{
|
||
pthread_mutex_t recmutex;
|
||
RESOURCE_HANDLE (rh);
|
||
};
|
||
typedef struct lumiera_recmutex_struct lumiera_recmutex;
|
||
typedef lumiera_recmutex* LumieraRecmutex;
|
||
|
||
/**
|
||
* Initialise a recursive mutex variable
|
||
* Initialises a 'recursive' mutex which might be locked by the same thread multiple times.
|
||
* @param self is a pointer to the mutex to be initialised
|
||
* @return self as given
|
||
*/
|
||
LumieraRecmutex
|
||
lumiera_recmutex_init (LumieraRecmutex self,
|
||
const char* purpose,
|
||
struct nobug_flag* flag,
|
||
const struct nobug_context ctx);
|
||
|
||
/**
|
||
* Destroy a recursive mutex variable
|
||
* @param self is a pointer to the mutex to be destroyed
|
||
* @return self as given
|
||
*/
|
||
LumieraRecmutex
|
||
lumiera_recmutex_destroy (LumieraRecmutex self,
|
||
struct nobug_flag* flag,
|
||
const struct nobug_context ctx);
|
||
|
||
|
||
static inline LumieraRecmutex
|
||
lumiera_recmutex_lock (LumieraRecmutex self,
|
||
struct nobug_flag* flag,
|
||
struct nobug_resource_user** handle,
|
||
const struct nobug_context ctx)
|
||
{
|
||
if (self)
|
||
{
|
||
NOBUG_RESOURCE_WAIT_CTX (NOBUG_FLAG_RAW(flag), self->rh, "acquire mutex", *handle, ctx)
|
||
{
|
||
if (pthread_mutex_lock (&self->recmutex))
|
||
LUMIERA_DIE (LOCK_ACQUIRE); /* never reached (in a correct program) */
|
||
|
||
NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) /*{}*/;
|
||
}
|
||
}
|
||
|
||
return self;
|
||
}
|
||
|
||
|
||
static inline LumieraRecmutex
|
||
lumiera_recmutex_trylock (LumieraRecmutex self,
|
||
struct nobug_flag* flag,
|
||
struct nobug_resource_user** handle,
|
||
const struct nobug_context ctx)
|
||
{
|
||
if (self)
|
||
{
|
||
NOBUG_RESOURCE_TRY_CTX (NOBUG_FLAG_RAW(flag), self->rh, "try acquire mutex", *handle, ctx)
|
||
{
|
||
int err = pthread_mutex_trylock (&self->recmutex);
|
||
|
||
if (!err)
|
||
{
|
||
NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) /*{}*/;
|
||
}
|
||
else
|
||
{
|
||
NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx) /*{}*/;
|
||
lumiera_lockerror_set (err, flag, ctx);
|
||
self = NULL;
|
||
}
|
||
}
|
||
}
|
||
|
||
return self;
|
||
}
|
||
|
||
|
||
static inline LumieraRecmutex
|
||
lumiera_recmutex_timedlock (LumieraRecmutex self,
|
||
const struct timespec* timeout,
|
||
struct nobug_flag* flag,
|
||
struct nobug_resource_user** handle,
|
||
const struct nobug_context ctx)
|
||
{
|
||
if (self)
|
||
{
|
||
NOBUG_RESOURCE_TRY_CTX (NOBUG_FLAG_RAW(flag), self->rh, "timed acquire mutex", *handle, ctx)
|
||
{
|
||
int err = pthread_mutex_timedlock (&self->recmutex, timeout);
|
||
|
||
if (!err)
|
||
{
|
||
NOBUG_RESOURCE_STATE_CTX (NOBUG_FLAG_RAW(flag), NOBUG_RESOURCE_RECURSIVE, *handle, ctx) /*{}*/;
|
||
}
|
||
else
|
||
{
|
||
NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx) /*{}*/;
|
||
lumiera_lockerror_set (err, flag, ctx);
|
||
self = NULL;
|
||
}
|
||
}
|
||
}
|
||
|
||
return self;
|
||
}
|
||
|
||
|
||
static inline void
|
||
lumiera_recmutex_unlock (LumieraRecmutex self,
|
||
struct nobug_flag* flag,
|
||
struct nobug_resource_user** handle,
|
||
const struct nobug_context ctx)
|
||
{
|
||
NOBUG_REQUIRE_CTX (self, ctx);
|
||
|
||
NOBUG_RESOURCE_LEAVE_RAW_CTX (flag, *handle, ctx)
|
||
{
|
||
if (pthread_mutex_unlock (&self->recmutex))
|
||
LUMIERA_DIE (LOCK_RELEASE); /* never reached (in a correct program) */
|
||
}
|
||
}
|
||
|
||
|
||
#endif
|
||
/*
|
||
// Local Variables:
|
||
// mode: C
|
||
// c-file-style: "gnu"
|
||
// indent-tabs-mode: nil
|
||
// End:
|
||
*/
|