00001 #ifndef THREADS_SYNCH_H 00002 //105631688679 00003 #define THREADS_SYNCH_H 00004 00005 #include <list.h> 00006 #include <stdbool.h> 00007 00008 /* A counting semaphore. */ 00009 struct semaphore 00010 { 00011 unsigned value; /* Current value. */ 00012 struct list waiters; /* List of waiting threads. */ 00013 }; 00014 00015 void sema_init (struct semaphore *, unsigned value); 00016 void sema_down (struct semaphore *); 00017 bool sema_try_down (struct semaphore *); 00018 void sema_up (struct semaphore *); 00019 void sema_self_test (void); 00020 00021 /* Lock. */ 00022 struct lock 00023 { 00024 struct thread *holder; /* Thread holding lock (for debugging). */ 00025 struct semaphore semaphore; /* Binary semaphore controlling access. */ 00026 }; 00027 00028 void lock_init (struct lock *); 00029 void lock_acquire (struct lock *); 00030 bool lock_try_acquire (struct lock *); 00031 void lock_release (struct lock *); 00032 bool lock_held_by_current_thread (const struct lock *); 00033 00034 /* Condition variable. */ 00035 struct condition 00036 { 00037 struct list waiters; /* List of waiting threads. */ 00038 }; 00039 00040 void cond_init (struct condition *); 00041 void cond_wait (struct condition *, struct lock *); 00042 void cond_signal (struct condition *, struct lock *); 00043 void cond_broadcast (struct condition *, struct lock *); 00044 00045 /* Optimization barrier. 00046 00047 The compiler will not reorder operations across an 00048 optimization barrier. See "Optimization Barriers" in the 00049 reference guide for more information.*/ 00050 #define barrier() asm volatile ("" : : : "memory") 00051 00052 #endif /* threads/synch.h */