PKUOS - Pintos
Pintos source browser for PKU Operating System 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. */
8struct semaphore
9 {
10 unsigned value; /**< Current value. */
11 struct list waiters; /**< List of waiting threads. */
12 };
13
14void sema_init (struct semaphore *, unsigned value);
15void sema_down (struct semaphore *);
16bool sema_try_down (struct semaphore *);
17void sema_up (struct semaphore *);
18void sema_self_test (void);
19
20/** Lock. */
21struct lock
22 {
23 struct thread *holder; /**< Thread holding lock (for debugging). */
24 struct semaphore semaphore; /**< Binary semaphore controlling access. */
25 };
26
27void lock_init (struct lock *);
28void lock_acquire (struct lock *);
29bool lock_try_acquire (struct lock *);
30void lock_release (struct lock *);
31bool lock_held_by_current_thread (const struct lock *);
32
33/** Condition variable. */
34struct condition
35 {
36 struct list waiters; /**< List of waiting threads. */
37 };
38
39void cond_init (struct condition *);
40void cond_wait (struct condition *, struct lock *);
41void cond_signal (struct condition *, struct lock *);
42void 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 */
Condition variable.
Definition: synch.h:35
struct list waiters
List of waiting threads.
Definition: synch.h:36
List.
Definition: list.h:98
Lock.
Definition: synch.h:22
struct thread * holder
Thread holding lock (for debugging).
Definition: synch.h:23
A counting semaphore.
Definition: synch.h:9
unsigned value
Current value.
Definition: synch.h:10
struct list waiters
List of waiting threads.
Definition: synch.h:11
A kernel thread or user process.
Definition: thread.h:84
A linked list element.
Definition: list.c:23
bool lock_held_by_current_thread(const struct lock *)
Returns true if the current thread holds LOCK, false otherwise.
Definition: synch.c:242
void lock_release(struct lock *)
Releases LOCK, which must be owned by the current thread.
Definition: synch.c:229
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
void sema_up(struct semaphore *)
Up or "V" operation on a semaphore.
Definition: synch.c:109
void cond_init(struct condition *)
Initializes condition variable COND.
Definition: synch.c:260
void sema_down(struct semaphore *)
Down or "P" operation on a semaphore.
Definition: synch.c:61
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
void lock_acquire(struct lock *)
Acquires LOCK, sleeping until it becomes available if necessary.
Definition: synch.c:193
void sema_init(struct semaphore *, unsigned value)
This file is derived from source code for the Nachos instructional operating system.
Definition: synch.c:45
void cond_signal(struct condition *, struct lock *)
void sema_self_test(void)
Self-test for semaphores that makes control "ping-pong" between a pair of threads.
Definition: synch.c:129
void cond_broadcast(struct condition *, struct lock *)
Wakes up all threads, if any, waiting on COND (protected by LOCK).
Definition: synch.c:331
bool lock_try_acquire(struct lock *)
Tries to acquires LOCK and returns true if successful or false on failure.
Definition: synch.c:210
void lock_init(struct lock *)
Initializes LOCK.
Definition: synch.c:176