c13f95d8d429a762843824e8c4e70fc1f6d4a799
[pintos-anon] / src / threads / synch.h
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     char name[16];              /* Name (for debugging purposes only). */
11     unsigned value;             /* Current value. */
12     struct list waiters;        /* List of waiting threads. */
13   };
14
15 void sema_init (struct semaphore *, unsigned value, const char *name);
16 void sema_down (struct semaphore *);
17 void sema_up (struct semaphore *);
18 const char *sema_name (const struct semaphore *);
19 void sema_self_test (void);
20
21 /* Lock. */
22 struct lock 
23   {
24     struct thread *holder;      /* Thread holding lock (for debugging). */
25     struct semaphore semaphore; /* Binary semaphore controlling access. */
26   };
27
28 void lock_init (struct lock *, const char *name);
29 void lock_acquire (struct lock *);
30 void lock_release (struct lock *);
31 bool lock_held_by_current_thread (const struct lock *);
32 const char *lock_name (const struct lock *);
33
34 /* Condition variable. */
35 struct condition 
36   {
37     char name[16];              /* Name (for debugging purposes only). */
38     struct list waiters;        /* List of waiting threads. */
39   };
40
41 void cond_init (struct condition *, const char *name);
42 void cond_wait (struct condition *, struct lock *);
43 void cond_signal (struct condition *, struct lock *);
44 void cond_broadcast (struct condition *, struct lock *);
45 const char *cond_name (const struct condition *);
46
47 #endif /* threads/synch.h */