-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thread wakeup/wait/trap and the base semaphore
- Loading branch information
zhiping zhong
committed
May 7, 2012
1 parent
d5e8a6f
commit 8673b13
Showing
6 changed files
with
253 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include <irq.h> | ||
#include <thread.h> | ||
#include <sem.h> | ||
|
||
int sem_init(sem_t *sem, int init) | ||
{ | ||
if (!sem) | ||
return -1; | ||
memset(sem, 0, sizeof(*sem)); | ||
sem->val = init; | ||
return 0; | ||
} | ||
int sem_up(sem_t *sem, int up) | ||
{ | ||
int flags; | ||
|
||
if (up <= 0) | ||
up = 0; | ||
local_irq_save(&flags); | ||
spinlock_lock(sem->lock); | ||
|
||
sem->val += up; | ||
while (sem->wait) { | ||
struct waitlist *w = sem->wait; | ||
if (sem->val >= w->down || !up) { | ||
sem->val -= w->down; | ||
sem->wait = w->next; | ||
thread_wakeup(w->thread); | ||
free(w); | ||
} else | ||
break; | ||
} | ||
|
||
spinlock_unlock(sem->lock); | ||
local_irq_restore(&flags); | ||
return 0; | ||
} | ||
int sem_down(sem_t *sem, int down) | ||
{ | ||
int flags, sleep = 0; | ||
struct thread_task *cur; | ||
|
||
if (down <= 0) | ||
return -1; | ||
|
||
local_irq_save(&flags); | ||
spinlock_lock(sem->lock); | ||
|
||
cur = current_thread(smp_cpu_id()); | ||
if (sem->val < down) { | ||
struct waitlist **wp; | ||
for (wp = &sem->wait; *wp; wp = &(*wp)->next) ; | ||
*wp = malloc(sizeof(struct waitlist)); | ||
if (*wp) { | ||
cur->state = THREAD_STATE_SLEEP; | ||
(*wp)->down = down; | ||
(*wp)->thread = cur; | ||
(*wp)->next = NULL; | ||
sleep = 1; | ||
} | ||
} else { | ||
sem->val -= down; | ||
} | ||
|
||
spinlock_unlock(sem->lock); | ||
|
||
if (sleep) { | ||
cur->state = THREAD_STATE_SLEEP; | ||
thread_yield(); | ||
} | ||
|
||
local_irq_restore(&flags); | ||
return 0; | ||
} | ||
|
||
int sem_free(sem_t *sem) | ||
{ | ||
if (sem->wait) | ||
return -1; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
#ifndef __SEM_H__ | ||
#define __SEM_H__ | ||
|
||
#include <spinlock.h> | ||
struct waitlist { | ||
int down; | ||
thread_t *thread; | ||
struct waitlist *next; | ||
}; | ||
struct sem { | ||
int val; | ||
spinlock_t lock; | ||
struct waitlist *wait; | ||
}; | ||
typedef struct sem sem_t; | ||
|
||
int sem_init(sem_t *, int init); | ||
/* if up <= 0; up all ! */ | ||
int sem_up(sem_t *, int up); | ||
/* if down <= 0; error ! */ | ||
int sem_down(sem_t *, int down); | ||
int sem_free(sem_t *); | ||
|
||
#endif |
Oops, something went wrong.