LUMIERA.clone/src/lib/mutex.h

208 lines
5.7 KiB
C
Raw Normal View History

2007-09-02 17:52:30 +02:00
/*
mutex.h - mutal exclusion locking
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
#include "lib/locking.h"
/**
* @file
* Mutual exclusion locking, header.
*/
#define LUMIERA_MUTEX_SECTION(flag, handle, mutex) \
RESOURCE_HANDLE (rh_##__LINE__##_); \
lumiera_mutexacquirer lock_##__LINE__##_; \
RESOURCE_ENTER (flag, handle, "acquire mutex", &lock_##__LINE__##_, \
NOBUG_RESOURCE_EXCLUSIVE, rh_##__LINE__##_); \
for (lumiera_mutexacquirer_init_mutex (&lock_##__LINE__##_, mutex, LUMIERA_LOCKED); \
lock_##__LINE__##_.state == LUMIERA_LOCKED; \
lumiera_mutexacquirer_unlock (&lock_##__LINE__##_), \
({RESOURCE_LEAVE(flag, rh_##__LINE__##_);}))
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-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
2008-07-11 06:32:56 +02:00
/**
* Initialize a mutex variable
* @param self is a pointer to the mutex to be initialized
* @return self as given
*/
2008-03-10 06:09:44 +01:00
LumieraMutex
lumiera_mutex_init (LumieraMutex self);
2007-09-02 17:52:30 +02:00
2008-07-11 06:32:56 +02:00
/**
* Destroy a mutex variable
* @param self is a pointer to the mutex to be destroyed
* @return self as given
*/
2008-03-10 06:09:44 +01:00
LumieraMutex
lumiera_mutex_destroy (LumieraMutex self);
2007-09-02 17:52:30 +02:00
/**
2007-09-03 06:40:44 +02:00
* mutexacquirer used to manage the state of a mutex variable.
2007-09-02 17:52:30 +02:00
*/
2008-03-10 06:09:44 +01:00
struct lumiera_mutexacquirer_struct
2007-09-02 17:52:30 +02:00
{
2008-03-10 06:09:44 +01:00
LumieraMutex mutex;
enum lumiera_lockstate state;
2007-09-02 17:52:30 +02:00
};
2008-03-10 06:09:44 +01:00
typedef struct lumiera_mutexacquirer_struct lumiera_mutexacquirer;
typedef struct lumiera_mutexacquirer_struct* LumieraMutexacquirer;
2007-09-02 17:52:30 +02:00
/* helper function for nobug */
static inline void
2008-03-10 06:09:44 +01:00
lumiera_mutexacquirer_ensureunlocked (LumieraMutexacquirer self)
2007-09-02 17:52:30 +02:00
{
2008-03-10 06:09:44 +01:00
ENSURE (self->state == LUMIERA_UNLOCKED, "forgot to unlock mutex");
2007-09-02 17:52:30 +02:00
}
/* override with a macro to use the cleanup checker */
2008-03-10 06:09:44 +01:00
#define lumiera_mutexacquirer \
lumiera_mutexacquirer NOBUG_CLEANUP(lumiera_mutexacquirer_ensureunlocked)
2007-09-02 17:52:30 +02:00
2007-09-19 06:58:54 +02:00
/**
* initialize a mutexacquirer state without mutex.
* @param self mutexacquirer to be initialized, must be an automatic variable
* @return self as given
2008-03-10 06:09:44 +01:00
* This initialization is used when lumiera_mutexacquirer_try_mutex shall be used later
2007-09-19 06:58:54 +02:00
*/
2008-03-10 06:09:44 +01:00
static inline LumieraMutexacquirer
lumiera_mutexacquirer_init (LumieraMutexacquirer self)
2007-09-19 06:58:54 +02:00
{
REQUIRE (self);
self->mutex = NULL;
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_UNLOCKED;
2007-09-19 06:58:54 +02:00
return self;
}
2007-09-02 17:52:30 +02:00
/**
2007-09-03 06:40:44 +02:00
* initialize a mutexacquirer state
* @param self mutexacquirer to be initialized, must be an automatic variable
2007-09-02 17:52:30 +02:00
* @param mutex associated mutex
2008-03-10 06:09:44 +01:00
* @param state initial state of the mutex, either LUMIERA_LOCKED or LUMIERA_UNLOCKED
2007-09-02 17:52:30 +02:00
* @return self as given
* errors are fatal
*/
2008-03-10 06:09:44 +01:00
static inline LumieraMutexacquirer
lumiera_mutexacquirer_init_mutex (LumieraMutexacquirer self, LumieraMutex mutex, enum lumiera_lockstate state)
2007-09-02 17:52:30 +02:00
{
REQUIRE (self);
REQUIRE (mutex);
self->mutex = mutex;
self->state = state;
2008-03-10 06:09:44 +01:00
if (state == LUMIERA_LOCKED)
2007-09-02 17:52:30 +02:00
if (pthread_mutex_lock (&mutex->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 17:52:30 +02:00
return self;
}
2007-09-19 06:58:54 +02:00
2007-09-02 17:52:30 +02:00
/**
* lock the mutex.
* must not already be locked
2007-09-03 06:40:44 +02:00
* @param self mutexacquirer associated with a mutex variable
2007-09-02 17:52:30 +02:00
*/
static inline void
2008-03-10 06:09:44 +01:00
lumiera_mutexacquirer_lock (LumieraMutexacquirer self)
2007-09-02 17:52:30 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_UNLOCKED, "mutex already locked");
2007-09-02 17:52:30 +02:00
if (pthread_mutex_lock (&self->mutex->mutex))
LUMIERA_DIE (MUTEX_LOCK);
2007-09-02 17:52:30 +02:00
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_LOCKED;
2007-09-02 17:52:30 +02:00
}
2007-09-19 06:58:54 +02:00
/**
* get the state of a lock.
* @param self mutexacquirer associated with a mutex variable
2008-03-10 06:09:44 +01:00
* @return LUMIERA_LOCKED when the mutex is locked by this thead
2007-09-19 06:58:54 +02:00
*/
2008-03-10 06:09:44 +01:00
static inline enum lumiera_lockstate
lumiera_mutexacquirer_state (LumieraMutexacquirer self)
2007-09-19 06:58:54 +02:00
{
REQUIRE (self);
return self->state;
}
/**
* try to lock a mutex.
* must not already be locked
* @param self mutexacquirer associated with a mutex variable
* @param mutex pointer to a mutex which should be tried
2008-03-10 06:09:44 +01:00
* @return LUMIERA_LOCKED when the mutex got locked
2007-09-19 06:58:54 +02:00
*/
2008-03-10 06:09:44 +01:00
static inline enum lumiera_lockstate
lumiera_mutexacquirer_try_mutex (LumieraMutexacquirer self, LumieraMutex mutex)
2007-09-19 06:58:54 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_UNLOCKED, "mutex already locked");
2007-09-19 06:58:54 +02:00
self->mutex=mutex;
switch (pthread_mutex_trylock (&self->mutex->mutex))
{
case 0:
2008-03-10 06:09:44 +01:00
return self->state = LUMIERA_LOCKED;
2007-09-19 06:58:54 +02:00
case EBUSY:
2008-03-10 06:09:44 +01:00
return LUMIERA_UNLOCKED;
2007-09-19 06:58:54 +02:00
default:
LUMIERA_DIE (MUTEX_LOCK);
2007-09-19 06:58:54 +02:00
}
}
2007-09-02 17:52:30 +02:00
/**
* release mutex.
2007-09-03 06:40:44 +02:00
* a mutexacquirer must be unlocked before leaving scope
* @param self mutexacquirer associated with a mutex variable
2007-09-02 17:52:30 +02:00
*/
2007-09-19 06:58:54 +02:00
static inline void
2008-03-10 06:09:44 +01:00
lumiera_mutexacquirer_unlock (LumieraMutexacquirer self)
2007-09-02 17:52:30 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_LOCKED, "mutex was not locked");
2007-09-02 17:52:30 +02:00
if (pthread_mutex_unlock (&self->mutex->mutex))
LUMIERA_DIE (MUTEX_UNLOCK);
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_UNLOCKED;
2007-09-02 17:52:30 +02:00
}
#endif