Skip to content

Commit

Permalink
Make new condition implementation work on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Nov 9, 2018
1 parent 9f6d533 commit 3d52f2d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions fs/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void tty_release(struct tty *tty) {
tty->driver->close(tty);
ttys[tty->type][tty->num] = NULL;
unlock(&tty->lock);
cond_destroy(&tty->produced);
free(tty);
} else {
unlock(&tty->lock);
Expand Down
1 change: 1 addition & 0 deletions kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static int reap_if_zombie(struct task *task, addr_t status_addr, addr_t rusage_a
return _EFAULT;

unlock(&task->group->lock);
cond_destroy(&task->group->child_exit);
free(task->group);
task_destroy(task);
return 1;
Expand Down
1 change: 1 addition & 0 deletions kernel/futex.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static struct futex *futex_get(addr_t addr) {
static void futex_put(struct futex *futex) {
unlock(&futex->lock);
if (--futex->refcount == 0) {
cond_destroy(&futex->cond);
lock(&futex_hash_lock);
list_remove(&futex->chain);
unlock(&futex_hash_lock);
Expand Down
1 change: 1 addition & 0 deletions kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void task_destroy(struct task *task) {
list_remove(&task->pgroup);
list_remove(&task->session);
pid_get(task->pid)->task = NULL;
cond_destroy(&task->vfork_cond);
free(task);
}

Expand Down
28 changes: 26 additions & 2 deletions util/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,31 @@ static int _sem_wait(_sem_t *sem) {
static void _sem_post(_sem_t *sem) {
sem_post(sem);
}
static void _sem_destroy(_sem_t *sem) {}

#else

static void _sem_init(_sem_t *sem) {
semaphore_create(main_task, &sem, SYNC_POLICY_FIFO, 0);
static task_t main_task;
__attribute__((constructor)) static void get_main_task() {
main_task = mach_task_self();
}

static void _sem_init(_sem_t *sem, int init) {
semaphore_create(main_task, sem, SYNC_POLICY_FIFO, init);
}
static int _sem_wait(_sem_t *sem) {
if (current != NULL && current->pending)
return 1; // signal is waiting
kern_return_t err = semaphore_wait(*sem);
if (err == KERN_ABORTED)
return 1;
return 0;
}
static void _sem_post(_sem_t *sem) {
semaphore_signal(*sem);
}
static void _sem_destroy(_sem_t *sem) {
semaphore_destroy(main_task, *sem);
}
#endif

Expand All @@ -34,6 +54,10 @@ void cond_init(cond_t *cond) {
_sem_init(&cond->waitsem, 0);
_sem_init(&cond->donesem, 0);
}
void cond_destroy(cond_t *cond) {
_sem_destroy(&cond->waitsem);
_sem_destroy(&cond->donesem);
}

int wait_for(cond_t *cond, lock_t *lock) {
lock(&cond->lock);
Expand Down
3 changes: 2 additions & 1 deletion util/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ typedef struct {

// Must call before using the condition
void cond_init(cond_t *cond);
// TODO we might need: void cond_destroy(cond_t *cond);
// Must call when finished with the condition (otherwise you get semaphore leaks on darwin)
void cond_destroy(cond_t *cond);
// Releases the lock, waits for the condition, and reacquires the lock. Return
// 0 if waiting stopped because another thread called notify, or 1 if waiting
// stopped because the thread received a signal.
Expand Down

0 comments on commit 3d52f2d

Please sign in to comment.