diff options
| author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2012-02-19 06:14:24 +0000 |
|---|---|---|
| committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2012-02-19 06:14:24 +0000 |
| commit | 6fafeb146e9efd59140ea58cebd7dd38ae9a6379 (patch) | |
| tree | 7db89ba6a28932514b105d620bba4884ec332ec3 /libddekit/lock.c | |
| parent | 38c2c2458e3f4ecb329ff35621806252aac209b9 (diff) | |
| parent | 8df772b3c665e663f6f9d2a70f9c691590bd3f91 (diff) | |
Merge branch 'dde' into upstream-merged
Diffstat (limited to 'libddekit/lock.c')
| -rw-r--r-- | libddekit/lock.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/libddekit/lock.c b/libddekit/lock.c new file mode 100644 index 00000000..e534bebe --- /dev/null +++ b/libddekit/lock.c @@ -0,0 +1,54 @@ +#include <cthreads.h> + +#include "ddekit/lock.h" +#include "ddekit/memory.h" + +#define DDEKIT_DEBUG_LOCKS 0 + +struct ddekit_lock { + struct mutex lock; + cthread_t helder; +}; + +void _ddekit_lock_init(struct ddekit_lock **mtx) { + struct ddekit_lock *lock; + + lock = (struct ddekit_lock *) ddekit_simple_malloc (sizeof *lock); + mutex_init (&lock->lock); + lock->helder = NULL; + *mtx = lock; +} + +void _ddekit_lock_deinit(struct ddekit_lock **mtx) { + mutex_free (*mtx); + *mtx = NULL; +} + +void _ddekit_lock_lock(struct ddekit_lock **mtx) { + mutex_lock (&(*mtx)->lock); + (*mtx)->helder = cthread_self (); +} + +/* returns 0 on success, != 0 if it would block */ +int _ddekit_lock_try_lock(struct ddekit_lock **mtx) { + if (mutex_try_lock (&(*mtx)->lock)) { /* lock succeessfully */ + (*mtx)->helder = cthread_self (); + return 0; + } + return -1; +} + +void _ddekit_lock_unlock(struct ddekit_lock **mtx) { + // TODO I wonder if it can cause any trouble. + (*mtx)->helder = NULL; + mutex_unlock (&(*mtx)->lock); +} + + +int _ddekit_lock_owner(struct ddekit_lock **mtx) { + /* The return value is the address of the holder. + * I hope it will be OK. At least, it is OK + * for the current implementation of DDE Linux/BSD */ + return (int) (*mtx)->helder; +} + |
