blob: a76eecc4584b9bc1ae98a55e444e85f35a26b86a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#ifndef THREADS_SYNCH_H
#define THREADS_SYNCH_H
#include <list.h>
#include <stdbool.h>
/* A counting semaphore. */
struct semaphore
{
unsigned value; /* Current value. */
struct list waiters; /* List of waiting threads. */
};
void sema_init (struct semaphore *, unsigned value);
void sema_down (struct semaphore *);
bool sema_try_down (struct semaphore *);
void sema_up (struct semaphore *);
void sema_self_test (void);
/* Lock. */
struct lock
{
struct thread *holder; /* Thread holding lock (for debugging). */
struct semaphore semaphore; /* Binary semaphore controlling access. */
};
void lock_init (struct lock *);
void lock_acquire (struct lock *);
bool lock_try_acquire (struct lock *);
void lock_release (struct lock *);
bool lock_held_by_current_thread (const struct lock *);
/* Condition variable. */
struct condition
{
struct list waiters; /* List of waiting threads. */
};
void cond_init (struct condition *);
void cond_wait (struct condition *, struct lock *);
void cond_signal (struct condition *, struct lock *);
void cond_broadcast (struct condition *, struct lock *);
/* Readers-writers lock.
Implementation of "First readers-writers problem" from
https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem. */
struct rwlock
{
struct semaphore resource;
struct lock rmutex;
unsigned readcount;
};
void rwlock_init (struct rwlock *);
void rwlock_write_p (struct rwlock *);
void rwlock_write_v (struct rwlock *);
void rwlock_read_p (struct rwlock *);
void rwlock_read_v (struct rwlock *);
/* Optimization barrier.
The compiler will not reorder operations across an
optimization barrier. See "Optimization Barriers" in the
reference guide for more information.*/
#define barrier() asm volatile ("" : : : "memory")
#endif /* threads/synch.h */
|