00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __semaphore_h
00009 #define __semaphore_h
00010
00011 #include <common/synchronizer.h>
00012 #include <scheduler_disabler.h>
00013
00014 __BEGIN_SYS
00015
00020 class Semaphore: public Synchronizer_Common {
00021 public:
00026 inline Semaphore(int v = 1) : _value(v) {
00027 db<Semaphore>(TRC) << "Semaphore(value= " << _value << ")\n";
00028 }
00029
00033 inline ~Semaphore() {
00034 db<Semaphore>(TRC) << "~Semaphore()\n";
00035
00036 wakeupAll();
00037 }
00038
00042 inline void p() {
00043 db<Semaphore>(TRC) << "Semaphore::p(value=" << _value << ")\n";
00044 if(dec(_value) < 1) sleep();
00045 Scheduler::getInstance().getRunning().addSemaphore(this);
00046 }
00047
00051 inline void v() {
00052 db<Semaphore>(TRC) << "Semaphore::v(value=" << _value << ")\n";
00053 if(inc(_value) < 0) wakeup();
00054 Scheduler::getInstance().getRunning().removeSemaphore(this);
00055 }
00056
00060 inline void freeResources() {
00061 wakeupAll();
00062 }
00063
00068 static int init(System_Info *si);
00069
00070 private:
00072 static const Type_Id TYPE = Type<Semaphore>::TYPE;
00074 volatile int _value;
00075 };
00076
00077 __END_SYS
00078
00079 #endif