2007-09-02 17:52:30 +02:00
|
|
|
/*
|
|
|
|
|
mutex.h - mutal exclusion locking
|
|
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Christian Thaeter <ct@pipapo.org>
|
2007-09-02 17:52:30 +02:00
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-03-10 06:09:44 +01:00
|
|
|
#ifndef LUMIERA_MUTEX_H
|
|
|
|
|
#define LUMIERA_MUTEX_H
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2008-08-09 12:51:40 +02:00
|
|
|
#include "lib/error.h"
|
2009-01-25 14:39:18 +01:00
|
|
|
#include "lib/sectionlock.h"
|
2008-08-09 12:51:40 +02:00
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <nobug.h>
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2007-10-20 17:27:27 +02:00
|
|
|
/**
|
2008-04-10 02:40:34 +02:00
|
|
|
* @file
|
|
|
|
|
* Mutual exclusion locking, header.
|
2007-10-20 17:27:27 +02:00
|
|
|
*/
|
2008-08-09 09:33:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mutual exclusive section.
|
|
|
|
|
*/
|
2009-01-25 14:39:18 +01:00
|
|
|
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx) \
|
|
|
|
|
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
|
|
|
|
|
lumiera_lock_section_ = { \
|
|
|
|
|
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
|
|
|
|
lumiera_lock_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
2009-01-26 09:21:03 +01:00
|
|
|
lumiera_lock_section_.lock = (mtx); \
|
2009-01-25 14:39:18 +01:00
|
|
|
NOBUG_IF_ALPHA(lumiera_lock_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (mtx)->rh, "acquire mutex", &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_lock_section_.rh); \
|
2009-01-26 09:21:03 +01:00
|
|
|
if (pthread_mutex_lock (&(mtx)->mutex)) \
|
2009-01-26 04:21:58 +01:00
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
2009-01-25 14:39:18 +01:00
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_EXCLUSIVE, lumiera_lock_section_.rh); \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_lock_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_MUTEX_SECTION_UNLOCK; \
|
2008-08-09 09:33:14 +02:00
|
|
|
}))
|
2008-03-27 14:30:08 +01:00
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
|
2008-09-16 00:45:05 +02:00
|
|
|
/**
|
|
|
|
|
* Mutual exclusion chainbuilder section.
|
2008-09-27 04:19:43 +02:00
|
|
|
* Usage: LUMIERA_MUTEX_SECTION(a){LUMIERA_MUTEX_SECTION_CHAIN(b){run();}}
|
2008-09-16 00:45:05 +02:00
|
|
|
* calls lock(a); lock(b); unlock(a); run(); unlock(b);
|
|
|
|
|
* This macro should only be used inside LUMIERA_MUTEX_SECTION and should be
|
|
|
|
|
* called on the correct mutexes, period.
|
|
|
|
|
*/
|
2009-01-25 14:39:18 +01:00
|
|
|
|
|
|
|
|
#define LUMIERA_MUTEX_SECTION_CHAIN(nobugflag, mtx) \
|
|
|
|
|
for (lumiera_sectionlock *lumiera_lock_section_old_ = &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) lumiera_lock_section_ = { \
|
|
|
|
|
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
|
|
|
|
lumiera_lock_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
2009-01-26 09:21:03 +01:00
|
|
|
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
|
|
|
|
lumiera_lock_section_.lock = mtx; \
|
2009-01-25 14:39:18 +01:00
|
|
|
NOBUG_IF_ALPHA(lumiera_lock_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (mtx)->rh, "acquire mutex", &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_lock_section_.rh); \
|
2009-01-26 09:21:03 +01:00
|
|
|
if (pthread_mutex_lock (&(mtx)->mutex)) \
|
2009-01-26 04:21:58 +01:00
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
2009-01-25 14:39:18 +01:00
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_EXCLUSIVE, lumiera_lock_section_.rh); \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_); \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_lock_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_MUTEX_SECTION_UNLOCK; \
|
2008-09-16 00:45:05 +02:00
|
|
|
}))
|
2008-03-27 14:30:08 +01:00
|
|
|
|
2008-09-27 04:19:43 +02:00
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
#define LUMIERA_MUTEX_SECTION_UNLOCK \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
|
|
|
|
|
|
|
|
|
|
2008-09-27 04:19:43 +02:00
|
|
|
/**
|
|
|
|
|
* Recursive Mutual exclusive section.
|
|
|
|
|
*/
|
2009-01-25 14:39:18 +01:00
|
|
|
#define LUMIERA_RECMUTEX_SECTION(nobugflag, mtx) \
|
|
|
|
|
for (lumiera_sectionlock NOBUG_CLEANUP(lumiera_sectionlock_ensureunlocked) \
|
|
|
|
|
lumiera_lock_section_ = { \
|
|
|
|
|
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
|
|
|
|
lumiera_lock_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
2009-01-26 09:21:03 +01:00
|
|
|
lumiera_lock_section_.lock = (mtx); \
|
2009-01-25 14:39:18 +01:00
|
|
|
NOBUG_IF_ALPHA(lumiera_lock_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (mtx)->rh, "acquire recmutex", &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_lock_section_.rh); \
|
2009-01-26 09:21:03 +01:00
|
|
|
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
2009-01-26 04:21:58 +01:00
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
2009-01-25 14:39:18 +01:00
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_RECURSIVE, lumiera_lock_section_.rh); \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_lock_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_MUTEX_SECTION_UNLOCK; \
|
2008-09-27 04:19:43 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
#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_ = { \
|
|
|
|
|
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
|
|
|
|
lumiera_lock_section_.lock;) \
|
|
|
|
|
for ( \
|
|
|
|
|
({ \
|
2009-01-26 08:39:37 +01:00
|
|
|
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
2009-01-26 09:21:03 +01:00
|
|
|
lumiera_lock_section_.lock = (mtx); \
|
2009-01-25 14:39:18 +01:00
|
|
|
NOBUG_IF_ALPHA(lumiera_lock_section_.flag = &NOBUG_FLAG(nobugflag);) \
|
|
|
|
|
RESOURCE_ENTER (nobugflag, (mtx)->rh, "acquire recmutex", &lumiera_lock_section_, \
|
|
|
|
|
NOBUG_RESOURCE_WAITING, lumiera_lock_section_.rh); \
|
2009-01-26 09:21:03 +01:00
|
|
|
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
2009-01-26 04:21:58 +01:00
|
|
|
LUMIERA_DIE (LOCK_ACQUIRE); \
|
2009-01-25 14:39:18 +01:00
|
|
|
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_recursive, lumiera_lock_section_.rh); \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(lumiera_lock_section_old_) \
|
|
|
|
|
}); \
|
|
|
|
|
lumiera_lock_section_.lock; \
|
|
|
|
|
({ \
|
|
|
|
|
LUMIERA_MUTEX_SECTION_UNLOCK; \
|
2008-09-27 04:19:43 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
#define LUMIERA_RECMUTEX_SECTION_UNLOCK \
|
|
|
|
|
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-02 17:52:30 +02:00
|
|
|
/**
|
|
|
|
|
* Mutex.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2008-03-10 06:09:44 +01:00
|
|
|
struct lumiera_mutex_struct
|
2007-09-02 17:52:30 +02:00
|
|
|
{
|
|
|
|
|
pthread_mutex_t mutex;
|
2008-08-09 09:33:14 +02:00
|
|
|
RESOURCE_HANDLE (rh);
|
2007-09-02 17:52:30 +02:00
|
|
|
};
|
2008-03-10 06:09:44 +01:00
|
|
|
typedef struct lumiera_mutex_struct lumiera_mutex;
|
|
|
|
|
typedef lumiera_mutex* LumieraMutex;
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
struct lumiera_recmutex_struct
|
|
|
|
|
{
|
|
|
|
|
pthread_mutex_t recmutex;
|
|
|
|
|
RESOURCE_HANDLE (rh);
|
|
|
|
|
};
|
|
|
|
|
typedef struct lumiera_recmutex_struct lumiera_recmutex;
|
|
|
|
|
typedef lumiera_recmutex* LumieraRecMutex;
|
|
|
|
|
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2008-07-11 06:32:56 +02:00
|
|
|
/**
|
|
|
|
|
* Initialize a mutex variable
|
2008-09-27 04:19:43 +02:00
|
|
|
* This initializes a 'fast' default mutex which must not be locked recursively from one thread.
|
2008-07-11 06:32:56 +02:00
|
|
|
* @param self is a pointer to the mutex to be initialized
|
|
|
|
|
* @return self as given
|
|
|
|
|
*/
|
2008-03-10 06:09:44 +01:00
|
|
|
LumieraMutex
|
2008-08-09 16:15:29 +02:00
|
|
|
lumiera_mutex_init (LumieraMutex self, const char* purpose, struct nobug_flag* flag);
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2008-09-27 04:19:43 +02:00
|
|
|
/**
|
2009-01-25 14:39:18 +01:00
|
|
|
* Destroy a mutex variable
|
|
|
|
|
* @param self is a pointer to the mutex to be destroyed
|
2008-09-27 04:19:43 +02:00
|
|
|
* @return self as given
|
|
|
|
|
*/
|
|
|
|
|
LumieraMutex
|
2009-01-25 14:39:18 +01:00
|
|
|
lumiera_mutex_destroy (LumieraMutex self, struct nobug_flag* flag);
|
|
|
|
|
|
2008-09-27 04:19:43 +02:00
|
|
|
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2008-07-11 06:32:56 +02:00
|
|
|
/**
|
2009-01-25 14:39:18 +01:00
|
|
|
* Initialize a recursive mutex variable
|
|
|
|
|
* Initializes a 'recursive' mutex which might be locked by the same thread multiple times.
|
|
|
|
|
* @param self is a pointer to the mutex to be initialized
|
2008-07-11 06:32:56 +02:00
|
|
|
* @return self as given
|
|
|
|
|
*/
|
2009-01-25 14:39:18 +01:00
|
|
|
LumieraRecMutex
|
|
|
|
|
lumiera_recmutex_init (LumieraRecMutex self, const char* purpose, struct nobug_flag* flag);
|
2007-09-02 17:52:30 +02:00
|
|
|
|
2009-01-25 14:39:18 +01:00
|
|
|
/**
|
|
|
|
|
* 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);
|
2007-09-02 17:52:30 +02:00
|
|
|
|
|
|
|
|
/**
|
2009-01-25 14:39:18 +01:00
|
|
|
* Callback for unlocking mutexes.
|
|
|
|
|
* @internal
|
2007-09-02 17:52:30 +02:00
|
|
|
*/
|
2009-01-25 14:39:18 +01:00
|
|
|
int
|
|
|
|
|
lumiera_mutex_unlock_cb (void* mutex);
|
2007-09-02 17:52:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
2008-08-09 16:15:29 +02:00
|
|
|
/*
|
|
|
|
|
// Local Variables:
|
|
|
|
|
// mode: C
|
|
|
|
|
// c-file-style: "gnu"
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// End:
|
|
|
|
|
*/
|