CS318 - Pintos
Pintos source browser for JHU CS318 course
synch.h
Go to the documentation of this file.
1 #ifndef THREADS_SYNCH_H
2 #define THREADS_SYNCH_H
3 
4 #include <list.h>
5 #include <stdbool.h>
6 
7 /** A counting semaphore. */
8 struct semaphore
9  {
10  unsigned value; /**< Current value. */
11  struct list waiters; /**< List of waiting threads. */
12  };
13 
14 void sema_init (struct semaphore *, unsigned value);
15 void sema_down (struct semaphore *);
16 bool sema_try_down (struct semaphore *);
17 void sema_up (struct semaphore *);
18 void sema_self_test (void);
19 
20 /** Lock. */
21 struct lock
22  {
23  struct thread *holder; /**< Thread holding lock (for debugging). */
24  struct semaphore semaphore; /**< Binary semaphore controlling access. */
25  };
26 
27 void lock_init (struct lock *);
28 void lock_acquire (struct lock *);
29 bool lock_try_acquire (struct lock *);
30 void lock_release (struct lock *);
31 bool lock_held_by_current_thread (const struct lock *);
32 
33 /** Condition variable. */
34 struct condition
35  {
36  struct list waiters; /**< List of waiting threads. */
37  };
38 
39 void cond_init (struct condition *);
40 void cond_wait (struct condition *, struct lock *);
41 void cond_signal (struct condition *, struct lock *);
42 void cond_broadcast (struct condition *, struct lock *);
43 
44 /** Optimization barrier.
45 
46  The compiler will not reorder operations across an
47  optimization barrier. See "Optimization Barriers" in the
48  reference guide for more information.*/
49 #define barrier() asm volatile ("" : : : "memory")
50 
51 #endif /**< threads/synch.h */
lock::holder
struct thread * holder
Thread holding lock (for debugging).
Definition: synch.h:23
semaphore
A counting semaphore.
Definition: synch.h:8
thread
A kernel thread or user process.
Definition: thread.h:83
lock_init
void lock_init(struct lock *)
Initializes LOCK.
Definition: synch.c:176
sema_self_test
void sema_self_test(void)
Self-test for semaphores that makes control "ping-pong" between a pair of threads.
Definition: synch.c:129
stdbool.h
cond_wait
void cond_wait(struct condition *, struct lock *)
Atomically releases LOCK and waits for COND to be signaled by some other piece of code.
Definition: synch.c:288
semaphore::waiters
struct list waiters
List of waiting threads.
Definition: synch.h:11
sema_down
void sema_down(struct semaphore *)
Down or "P" operation on a semaphore.
Definition: synch.c:61
sema_init
void sema_init(struct semaphore *, unsigned value)
This file is derived from source code for the Nachos instructional operating system.
Definition: synch.c:45
cond_signal
void cond_signal(struct condition *, struct lock *)
sema_up
void sema_up(struct semaphore *)
Up or "V" operation on a semaphore.
Definition: synch.c:109
value
A linked list element.
Definition: list.c:22
condition::waiters
struct list waiters
List of waiting threads.
Definition: synch.h:36
lock_acquire
void lock_acquire(struct lock *)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
lock_held_by_current_thread
bool lock_held_by_current_thread(const struct lock *)
Returns true if the current thread holds LOCK, false otherwise.
Definition: synch.c:242
semaphore::value
unsigned value
Current value.
Definition: synch.h:10
lock_try_acquire
bool lock_try_acquire(struct lock *)
Tries to acquires LOCK and returns true if successful or false on failure.
Definition: synch.c:210
list.h
lock_release
void lock_release(struct lock *)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
lock
Lock.
Definition: synch.h:21
sema_try_down
bool sema_try_down(struct semaphore *)
Down or "P" operation on a semaphore, but only if the semaphore is not already 0.
Definition: synch.c:84
condition
Condition variable.
Definition: synch.h:34
list
List.
Definition: list.h:97
cond_init
void cond_init(struct condition *)
Initializes condition variable COND.
Definition: synch.c:260
cond_broadcast
void cond_broadcast(struct condition *, struct lock *)
Wakes up all threads, if any, waiting on COND (protected by LOCK).
Definition: synch.c:331