LUMIERA.clone/src/lib/rwlock.c

150 lines
3.4 KiB
C
Raw Normal View History

2007-09-03 06:35:13 +02:00
/*
rwlock.c - read/write locks
Copyright (C) Lumiera.org
2008, Christian Thaeter <ct@pipapo.org>
2007-09-03 06:35:13 +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.
*/
#include <errno.h>
#include "lib/rwlock.h"
2008-03-10 06:09:44 +01:00
LUMIERA_ERROR_DEFINE(RWLOCK_AGAIN, "maximum number of readlocks exceed");
LUMIERA_ERROR_DEFINE(RWLOCK_DEADLOCK, "deadlock detected");
LUMIERA_ERROR_DEFINE(RWLOCK_DESTROY, "destroy rwlock");
LUMIERA_ERROR_DEFINE(RWLOCK_UNLOCK, "unlock");
LUMIERA_ERROR_DEFINE(RWLOCK_RLOCK, "rlock");
LUMIERA_ERROR_DEFINE(RWLOCK_WLOCK, "wlock");
2007-09-03 06:35:13 +02:00
/**
* @file
* Read/write locks.
*/
2007-09-03 06:35:13 +02:00
2008-03-10 06:09:44 +01:00
LumieraRWLock
lumiera_rwlock_init (LumieraRWLock self)
2007-09-03 06:35:13 +02:00
{
if (self)
{
pthread_rwlock_init (&self->rwlock, NULL);
}
return self;
}
2008-03-10 06:09:44 +01:00
LumieraRWLock
lumiera_rwlock_destroy (LumieraRWLock self)
2007-09-03 06:35:13 +02:00
{
if (self)
{
if (pthread_rwlock_destroy (&self->rwlock))
LUMIERA_DIE (RWLOCK_DESTROY);
2007-09-03 06:35:13 +02:00
}
return self;
}
2008-03-10 06:09:44 +01:00
LumieraRWLockacquirer
lumiera_rwlockacquirer_init (LumieraRWLockacquirer self, LumieraRWLock rwlock, enum lumiera_lockstate state)
2007-09-03 06:35:13 +02:00
{
REQUIRE (self);
REQUIRE (rwlock);
2008-03-10 06:09:44 +01:00
REQUIRE (state != LUMIERA_LOCKED, "illegal state for rwlock");
2007-09-03 06:35:13 +02:00
self->rwlock = rwlock;
self->state = state;
if (state == LUMIERA_RDLOCKED)
switch (pthread_rwlock_rdlock (&rwlock->rwlock))
{
case 0:
break;
case EAGAIN:
lumiera_error_set (LUMIERA_ERROR_RWLOCK_AGAIN);
return NULL;
case EDEADLK:
lumiera_error_set (LUMIERA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
LUMIERA_DIE (RWLOCK_RLOCK);
}
else
switch (pthread_rwlock_wrlock (&rwlock->rwlock))
{
case 0:
break;
case EDEADLK:
lumiera_error_set (LUMIERA_ERROR_RWLOCK_DEADLOCK);
return NULL;
default:
LUMIERA_DIE (RWLOCK_WLOCK);
}
2007-09-03 06:35:13 +02:00
return self;
}
2008-03-10 06:09:44 +01:00
LumieraRWLockacquirer
lumiera_rwlockacquirer_rdlock (LumieraRWLockacquirer self)
2007-09-03 06:35:13 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_UNLOCKED, "rwlock already locked");
2007-09-03 06:35:13 +02:00
switch (pthread_rwlock_rdlock (&self->rwlock->rwlock))
{
case 0:
break;
case EAGAIN:
2008-03-10 06:09:44 +01:00
lumiera_error_set (LUMIERA_ERROR_RWLOCK_AGAIN);
2007-09-03 06:35:13 +02:00
return NULL;
case EDEADLK:
2008-03-10 06:09:44 +01:00
lumiera_error_set (LUMIERA_ERROR_RWLOCK_DEADLOCK);
2007-09-03 06:35:13 +02:00
return NULL;
default:
LUMIERA_DIE (RWLOCK_RLOCK);
2007-09-03 06:35:13 +02:00
}
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_RDLOCKED;
2007-09-03 06:35:13 +02:00
return self;
}
2008-03-10 06:09:44 +01:00
LumieraRWLockacquirer
lumiera_rwlockacquirer_wrlock (LumieraRWLockacquirer self)
2007-09-03 06:35:13 +02:00
{
REQUIRE (self);
2008-03-10 06:09:44 +01:00
REQUIRE (self->state == LUMIERA_UNLOCKED, "rwlock already locked");
2007-09-03 06:35:13 +02:00
switch (pthread_rwlock_wrlock (&self->rwlock->rwlock))
{
case 0:
break;
case EDEADLK:
2008-03-10 06:09:44 +01:00
lumiera_error_set (LUMIERA_ERROR_RWLOCK_DEADLOCK);
2007-09-03 06:35:13 +02:00
return NULL;
default:
LUMIERA_DIE (RWLOCK_WLOCK);
2007-09-03 06:35:13 +02:00
}
2008-03-10 06:09:44 +01:00
self->state = LUMIERA_WRLOCKED;
2007-09-03 06:35:13 +02:00
return self;
}