split mutex.h again into mutex.h and recmutex.h
This commit is contained in:
parent
ec62ca073b
commit
4f29f302b2
7 changed files with 140 additions and 82 deletions
|
|
@ -1,5 +1,5 @@
|
|||
# Copyright (C) Lumiera.org
|
||||
# 2007, Christian Thaeter <ct@pipapo.org>
|
||||
# 2007, 2008, 2009 Christian Thaeter <ct@pipapo.org>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License as
|
||||
|
|
@ -52,6 +52,7 @@ noinst_HEADERS += \
|
|||
$(liblumiera_la_srcdir)/error.h \
|
||||
$(liblumiera_la_srcdir)/error.hpp \
|
||||
$(liblumiera_la_srcdir)/mutex.h \
|
||||
$(liblumiera_la_srcdir)/recmutex.h \
|
||||
$(liblumiera_la_srcdir)/rwlock.h \
|
||||
$(liblumiera_la_srcdir)/condition.h \
|
||||
$(liblumiera_la_srcdir)/reccondition.h \
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
mutex.c - mutex
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, Christian Thaeter <ct@pipapo.org>
|
||||
2008, 2009, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
mutex.h - mutal exclusion locking
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, Christian Thaeter <ct@pipapo.org>
|
||||
2008, 2009, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
|
|
@ -93,58 +93,6 @@
|
|||
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
||||
|
||||
|
||||
/**
|
||||
* Recursive Mutual exclusive section.
|
||||
*/
|
||||
#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 ( \
|
||||
({ \
|
||||
lumiera_lock_section_.lock = (mtx); \
|
||||
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); \
|
||||
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
||||
LUMIERA_DIE (LOCK_ACQUIRE); \
|
||||
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_RECURSIVE, lumiera_lock_section_.rh); \
|
||||
}); \
|
||||
lumiera_lock_section_.lock; \
|
||||
({ \
|
||||
LUMIERA_MUTEX_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_ = { \
|
||||
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
||||
lumiera_lock_section_.lock;) \
|
||||
for ( \
|
||||
({ \
|
||||
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
||||
lumiera_lock_section_.lock = (mtx); \
|
||||
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); \
|
||||
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
||||
LUMIERA_DIE (LOCK_ACQUIRE); \
|
||||
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; \
|
||||
}))
|
||||
|
||||
|
||||
#define LUMIERA_RECMUTEX_SECTION_UNLOCK \
|
||||
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mutex.
|
||||
*
|
||||
|
|
@ -157,14 +105,6 @@ struct lumiera_mutex_struct
|
|||
typedef struct lumiera_mutex_struct lumiera_mutex;
|
||||
typedef lumiera_mutex* LumieraMutex;
|
||||
|
||||
struct lumiera_recmutex_struct
|
||||
{
|
||||
pthread_mutex_t recmutex;
|
||||
RESOURCE_HANDLE (rh);
|
||||
};
|
||||
typedef struct lumiera_recmutex_struct lumiera_recmutex;
|
||||
typedef lumiera_recmutex* LumieraRecmutex;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize a mutex variable
|
||||
|
|
@ -184,24 +124,6 @@ LumieraMutex
|
|||
lumiera_mutex_destroy (LumieraMutex self, struct nobug_flag* flag);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return self as given
|
||||
*/
|
||||
LumieraRecmutex
|
||||
lumiera_recmutex_init (LumieraRecmutex self, const char* purpose, struct nobug_flag* flag);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Callback for unlocking mutexes.
|
||||
* @internal
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "lib/mutex.h"
|
||||
#include "lib/recmutex.h"
|
||||
|
||||
/**
|
||||
* @file
|
||||
|
|
|
|||
132
src/lib/recmutex.h
Normal file
132
src/lib/recmutex.h
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
recmutex.h - recursive mutal exclusion locking
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, 2009, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef LUMIERA_RECMUTEX_H
|
||||
#define LUMIERA_RECMUTEX_H
|
||||
|
||||
#include "lib/error.h"
|
||||
#include "lib/sectionlock.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_ = { \
|
||||
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
||||
lumiera_lock_section_.lock;) \
|
||||
for ( \
|
||||
({ \
|
||||
lumiera_lock_section_.lock = (mtx); \
|
||||
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); \
|
||||
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
||||
LUMIERA_DIE (LOCK_ACQUIRE); \
|
||||
RESOURCE_STATE (nobugflag, NOBUG_RESOURCE_RECURSIVE, lumiera_lock_section_.rh); \
|
||||
}); \
|
||||
lumiera_lock_section_.lock; \
|
||||
({ \
|
||||
LUMIERA_MUTEX_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_ = { \
|
||||
(void*)1, lumiera_mutex_unlock_cb NOBUG_ALPHA_COMMA_NULL NOBUG_ALPHA_COMMA_NULL}; \
|
||||
lumiera_lock_section_.lock;) \
|
||||
for ( \
|
||||
({ \
|
||||
REQUIRE (lumiera_lock_section_old_->lock, "section prematurely unlocked"); \
|
||||
lumiera_lock_section_.lock = (mtx); \
|
||||
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); \
|
||||
if (pthread_mutex_lock (&(mtx)->recmutex)) \
|
||||
LUMIERA_DIE (LOCK_ACQUIRE); \
|
||||
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; \
|
||||
}))
|
||||
|
||||
|
||||
#define LUMIERA_RECMUTEX_SECTION_UNLOCK \
|
||||
LUMIERA_SECTION_UNLOCK_(&lumiera_lock_section_)
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mutex.
|
||||
*
|
||||
*/
|
||||
struct lumiera_recmutex_struct
|
||||
{
|
||||
pthread_mutex_t recmutex;
|
||||
RESOURCE_HANDLE (rh);
|
||||
};
|
||||
typedef struct lumiera_recmutex_struct lumiera_recmutex;
|
||||
typedef lumiera_recmutex* LumieraRecmutex;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return self as given
|
||||
*/
|
||||
LumieraRecmutex
|
||||
lumiera_recmutex_init (LumieraRecmutex self, const char* purpose, struct nobug_flag* flag);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Callback for unlocking mutexes.
|
||||
* @internal
|
||||
*/
|
||||
int
|
||||
lumiera_mutex_unlock_cb (void* mutex);
|
||||
|
||||
|
||||
#endif
|
||||
/*
|
||||
// Local Variables:
|
||||
// mode: C
|
||||
// c-file-style: "gnu"
|
||||
// indent-tabs-mode: nil
|
||||
// End:
|
||||
*/
|
||||
|
|
@ -73,6 +73,7 @@
|
|||
|
||||
extern "C" {
|
||||
#include "lib/mutex.h"
|
||||
#include "lib/recmutex.h"
|
||||
#include "lib/condition.h"
|
||||
#include "lib/reccondition.h"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@
|
|||
|
||||
#include "tests/test.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "lib/recmutex.h"
|
||||
#include "lib/condition.h"
|
||||
#include "lib/reccondition.h"
|
||||
#include "lib/rwlock.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
|
|
|||
Loading…
Reference in a new issue