00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __synchronizer_h
00009 #define __synchronizer_h
00010
00011 #include <system/config.h>
00012 #include <cpu.h>
00013 #include <thread.h>
00014 #include "ep_cvec.h"
00015 #include <scheduler_disabler.h>
00016
00017 __BEGIN_SYS
00018
00023 class Synchronizer_Common {
00024 protected:
00025 Synchronizer_Common() {}
00026
00030 void wakeup() {
00031 DISABLE_SCHED {
00032 if(!_waitingThreads.isEmpty()) removeWaintingThreadAndResume();
00033 }
00034 }
00035
00039 void wakeupAll() {
00040 DISABLE_SCHED {
00041 while(!_waitingThreads.isEmpty()) removeWaintingThreadAndResume();
00042 }
00043 }
00044
00045 protected:
00049 inline bool tsl(volatile bool & lock) { return CPU::tsl(lock); }
00050
00054 inline int inc(volatile int & number) { return CPU::finc(number); }
00055
00059 inline int dec(volatile int & number) { return CPU::fdec(number); }
00060
00065 void sleep() {
00066 DISABLE_SCHED {
00067 _waitingThreads.pushBack(&Scheduler::getInstance().getRunning());
00068 Scheduler::getInstance().suspend(&Scheduler::getInstance().getRunning());
00069 }
00070 }
00071
00072 private:
00073 typedef CircularVector<Thread *> ThreadVector;
00074 ThreadVector _waitingThreads;
00075
00079 inline void removeWaintingThreadAndResume() {
00080 Thread *thread = _waitingThreads.front();
00081 _waitingThreads.popFront();
00082 Scheduler::getInstance().resume(thread);
00083 }
00084 };
00085
00086 __END_SYS
00087
00088 #endif