Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
semaphore.h File Reference
#include <sys/queue.h>
#include <sys/spinlock.h>
Include dependency graph for semaphore.h:

Go to the source code of this file.

Data Structures

struct  Semaphore
 

Macros

#define SEMAPHORE_NAMELEN   32
 

Typedefs

typedef struct Semaphore Semaphore
 

Functions

void Semaphore_Init (Semaphore *sema, int count, const char *name)
 
void Semaphore_Destroy (Semaphore *sema)
 
void Semaphore_Acquire (Semaphore *sema)
 
void Semaphore_Release (Semaphore *sema)
 
bool Semaphore_TryAcquire (Semaphore *sema)
 

Macro Definition Documentation

◆ SEMAPHORE_NAMELEN

#define SEMAPHORE_NAMELEN   32

Definition at line 8 of file semaphore.h.

Typedef Documentation

◆ Semaphore

typedef struct Semaphore Semaphore

Function Documentation

◆ Semaphore_Acquire()

void Semaphore_Acquire ( Semaphore sema)

Definition at line 49 of file semaphore.c.

50{
51 Thread *cur = Sched_Current();
52
53 while (1) {
54 Spinlock_Lock(&sema->lock);
55 if (sema->count > 0) {
56 sema->count -= 1;
57 Spinlock_Unlock(&sema->lock);
58 Thread_Release(cur);
59 return;
60 }
61
62 // Add to sleeper list
63 TAILQ_INSERT_TAIL(&sema->waiters, cur, semaQueue);
65
66 Spinlock_Unlock(&sema->lock);
68 }
69}
void Sched_SetWaiting(Thread *thr)
Definition: sched.c:104
void Sched_Scheduler()
Definition: sched.c:189
void Thread_Release(Thread *thr)
Definition: thread.c:265
Thread * Sched_Current()
Definition: sched.c:56
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:641
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
Spinlock lock
Definition: semaphore.h:14
int count
Definition: semaphore.h:16
Definition: thread.h:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Semaphore_Destroy()

void Semaphore_Destroy ( Semaphore sema)

Definition at line 39 of file semaphore.c.

40{
42 LIST_REMOVE(sema, semaphoreList);
44
45 Spinlock_Destroy(&sema->lock);
46}
#define LIST_REMOVE(elm, field)
Definition: queue.h:465
Spinlock semaListLock
Definition: semaphore.c:19
void Spinlock_Destroy(Spinlock *lock)
Definition: spinlock.c:61
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Semaphore_Init()

void Semaphore_Init ( Semaphore sema,
int  count,
const char *  name 
)
Here is the caller graph for this function:

◆ Semaphore_Release()

void Semaphore_Release ( Semaphore sema)

Definition at line 72 of file semaphore.c.

73{
74 Thread *thr;
75
76 Spinlock_Lock(&sema->lock);
77 sema->count += 1;
78
79 // Wakeup thread
80 thr = TAILQ_FIRST(&sema->waiters);
81 if (thr != NULL) {
82 TAILQ_REMOVE(&sema->waiters, thr, semaQueue);
84 }
85 Spinlock_Unlock(&sema->lock);
86}
void Sched_SetRunnable(Thread *thr)
Definition: sched.c:77
#define TAILQ_FIRST(head)
Definition: queue.h:555
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:659
#define NULL
Definition: stddef.h:6
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Semaphore_TryAcquire()

bool Semaphore_TryAcquire ( Semaphore sema)

Definition at line 89 of file semaphore.c.

90{
91 Spinlock_Lock(&sema->lock);
92 if (sema->count > 0) {
93 sema->count -= 1;
94 Spinlock_Unlock(&sema->lock);
95 return true;
96 }
97 Spinlock_Unlock(&sema->lock);
98 return false;
99}
Here is the call graph for this function: