summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZheng Da <zhengda1936@gmail.com>2009-11-21 06:40:42 +0100
committerZheng Da <zhengda1936@gmail.com>2009-11-21 06:40:42 +0100
commit7c5d267799af896d4bc9e7a03ece141a6764c12c (patch)
tree0fbdcbc7ddd688e4fbb6c7aca67ae87e2347b890
parentb537f6cede63b0e55e82a2ee1467d094200d953e (diff)
Record the thread who holds the lock by ourselves.
cthreads mutex might contain the holder only when WAITDEBUG is defined. I think it's better to implement it by ourselves.
-rw-r--r--libddekit/lock.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/libddekit/lock.c b/libddekit/lock.c
index f64d571b..bf643e65 100644
--- a/libddekit/lock.c
+++ b/libddekit/lock.c
@@ -6,11 +6,16 @@
struct ddekit_lock {
struct mutex lock;
+ cthread_t helder;
};
void _ddekit_lock_init(struct ddekit_lock **mtx) {
- *mtx = (struct ddekit_lock *) mutex_alloc ();
- mutex_init (*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) {
@@ -20,15 +25,22 @@ void _ddekit_lock_deinit(struct ddekit_lock **mtx) {
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) {
- return !mutex_try_lock (&(*mtx)->lock);
+ if (mutex_try_lock (&(*mtx)->lock)) { /* lock succeessfully */
+ (*mtx)->helder = cthread_self ();
+ return 0;
+ }
+ return -1;
}
void _ddekit_lock_unlock(struct ddekit_lock **mtx) {
mutex_unlock (&(*mtx)->lock);
+ // TODO I wonder if it can cause any trouble.
+ (*mtx)->helder = NULL;
}
@@ -36,6 +48,6 @@ 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)->lock.holder;
+ return (int) (*mtx)->holder;
}