Skip to content

Commit

Permalink
Replace turnstile with sleepqueue in mutex implementation
Browse files Browse the repository at this point in the history
* remove turnstile implementation
* adjust debugging tools to support new mutex implementation
  • Loading branch information
cahirwpz committed Jan 17, 2017
1 parent 68cce6f commit 5cec10f
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 66 deletions.
21 changes: 7 additions & 14 deletions debug/mutex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def get_mutex_owner(mtx):
state = mtx['mtx_state']
state = mtx['m_owner']
td_pointer = gdb.lookup_type('struct thread').pointer()
owner = state.cast(td_pointer)
if owner:
Expand All @@ -16,12 +16,9 @@ def get_mutex_owner(mtx):


def get_threads_blocked_on_mutex(mtx):
ts = mtx['turnstile']
return map(lambda t: t['td_name'].string(), tailq.collect_values(ts['td_queue'], 'td_lock'))


def pretty_list_of_string(lst):
return "[" + ", ".join(lst) + "]"
sq = gdb.parse_and_eval("sleepq_lookup((void *)%d)" % mtx.address)
return map(lambda n: n.string(),
tailq.collect_values(sq['sq_blocked'], 'td_name'))


class MutexPrettyPrinter():
Expand All @@ -30,14 +27,10 @@ def __init__(self, val):
self.val = val

def to_string(self):
res = []
if self.val['mtx_state'] == 0:
if self.val['m_owner'] == 0:
return "Mutex unowned"
else:
res.append("Owner = " + str(get_mutex_owner(self.val)))
res.append("Waiting for access = " +
pretty_list_of_string(get_threads_blocked_on_mutex(self.val)))
return "\n".join(res)
return "{owner = %s, blocked = %s}" % (
get_mutex_owner(self.val), get_threads_blocked_on_mutex(self.val))

def display_hint(self):
return 'map'
Expand Down
3 changes: 2 additions & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ noreturn void thread_exit();
thread_exit(); \
})

#ifdef DEBUG
#define log(FMT, ...) \
__extension__( \
{ kprintf("[%s:%d] " FMT "\n", __FILE__, __LINE__, ##__VA_ARGS__); })

#ifdef DEBUG
#define assert(EXPR) \
__extension__({ \
if (!(EXPR)) \
panic("Assertion '" __STRING(EXPR) "' failed!"); \
})
#else
#define log(...)
#define assert(expr)
#endif

Expand Down
4 changes: 1 addition & 3 deletions include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define _SYS_MUTEX_H_

#include <stdbool.h>
#include <turnstile.h>

typedef struct thread thread_t;

Expand All @@ -13,7 +12,6 @@ typedef struct mtx {
volatile thread_t *m_owner; /* stores address of the owner */
volatile unsigned m_count; /* Counter for recursive mutexes */
unsigned m_type; /* Normal or recursive mutex */
turnstile_t m_turnstile; /* FIFO Queue for blocked threads */
} mtx_t;

/* Initializes mutex. Note that EVERY mutex has to be initialized
Expand All @@ -24,7 +22,7 @@ void mtx_init(mtx_t *m, unsigned type);
bool mtx_owned(mtx_t *mtx);

/* Locks the mutex, if the mutex is already owned by someone,
* then this blocks on turnstile, otherwise it takes the mutex. */
* then this blocks on sleepqueue, otherwise it takes the mutex. */
void mtx_lock(mtx_t *m);

/* Unlocks the mutex. If some thread blocked for the mutex,
Expand Down
3 changes: 1 addition & 2 deletions include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ typedef struct thread {
TAILQ_ENTRY(thread) td_all; /* a link on all threads list */
TAILQ_ENTRY(thread) td_runq; /* a link on run queue */
TAILQ_ENTRY(thread) td_sleepq; /* a link on sleep queue */
TAILQ_ENTRY(thread) td_lock; /* a link on turnstile */
char *td_name;
tid_t td_tid; /* Thread ID*/
tid_t td_tid;
/* thread state */
enum { TDS_INACTIVE = 0x0, TDS_WAITING, TDS_READY, TDS_RUNNING } td_state;
uint32_t td_flags; /* TDF_* flags */
Expand Down
18 changes: 0 additions & 18 deletions include/turnstile.h

This file was deleted.

1 change: 0 additions & 1 deletion sys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ SOURCES_C = \
sync.c \
sysent.c \
thread.c \
turnstile.c \
uio.c \
vfs.c \
vfs_syscalls.c \
Expand Down
5 changes: 2 additions & 3 deletions sys/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ void mtx_init(mtx_t *m, unsigned type) {
m->m_owner = NULL;
m->m_count = 0;
m->m_type = type;
turnstile_init(&m->m_turnstile);
}

void mtx_lock(mtx_t *m) {
Expand All @@ -26,7 +25,7 @@ void mtx_lock(mtx_t *m) {

critical_enter();
while (m->m_owner != NULL)
turnstile_wait(&m->m_turnstile);
sleepq_wait(&m->m_owner, NULL);
m->m_owner = thread_self();
critical_leave();
}
Expand All @@ -41,6 +40,6 @@ void mtx_unlock(mtx_t *m) {

critical_enter();
m->m_owner = NULL;
turnstile_signal(&m->m_turnstile);
sleepq_signal(&m->m_owner);
critical_leave();
}
1 change: 1 addition & 0 deletions sys/sleepq.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#undef DEBUG
#include <common.h>
#include <queue.h>
#include <stdc.h>
Expand Down
24 changes: 0 additions & 24 deletions sys/turnstile.c

This file was deleted.

0 comments on commit 5cec10f

Please sign in to comment.