Skip to content

Commit

Permalink
Removed duplicated code, fixed comment
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-cherry committed Feb 20, 2017
1 parent b9bb22b commit 6cd3d34
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions core/rmutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define ENABLE_DEBUG (0)
#include "debug.h"

void rmutex_lock(rmutex_t *rmutex)
static int _lock(rmutex_t *rmutex, int trylock)
{
kernel_pid_t owner;

Expand Down Expand Up @@ -70,7 +70,7 @@ void rmutex_lock(rmutex_t *rmutex)
* Condition 2: holds
* rmutex->owner == thread_getpid()
*
* Note for Case 1:
* Note for Case 2:
*
* Because the mutex rmutex->owner is only written be the
* owner (me), rmutex->owner stays constant througout the
Expand All @@ -87,7 +87,12 @@ void rmutex_lock(rmutex_t *rmutex)
/* wait for the mutex */
DEBUG("rmutex %" PRIi16" : locking mutex\n", thread_getpid());

mutex_lock(&rmutex->mutex);
if (trylock) {
return 0;
}
else {
mutex_lock(&rmutex->mutex);
}
}
/* Case 2: Mutex is held be me (relock) */
/* Note: There is nothing to do for Case 2; refcount is incremented below */
Expand All @@ -105,34 +110,18 @@ void rmutex_lock(rmutex_t *rmutex)

/* increase the refcount */
rmutex->refcount++;

return 1;
}

int rmutex_trylock(rmutex_t *rmutex)
void rmutex_lock(rmutex_t *rmutex)
{
kernel_pid_t owner;

/* try to lock the mutex */
if (mutex_trylock(&rmutex->mutex) == 0) {
/* ensure that owner is read atomically, since I need a consistent value */
owner = atomic_load_explicit( &rmutex->owner, memory_order_relaxed);

/* Case 1: Mutex is not held by me */
if ( owner != thread_getpid() ) {
/* wait for the mutex */
return 0;
}
/* Case 2: Mutex is held be me (relock) */
/* Note: There is nothing to do for Case 2; refcount is incremented below */
}

/* I am are holding the recursive mutex */

/* ensure that owner is written atomically, since others need a consistent value */
atomic_store_explicit(&rmutex->owner, thread_getpid(), memory_order_relaxed);
_lock(rmutex, 0);
}

/* increase the refcount */
rmutex->refcount++;
return 1;
int rmutex_trylock(rmutex_t *rmutex)
{
return _lock(rmutex, 1);
}

void rmutex_unlock(rmutex_t *rmutex)
Expand Down

0 comments on commit 6cd3d34

Please sign in to comment.