00001 #ifndef MTASKER_HH
00002 #define MTASKER_HH
00003 #include <signal.h>
00004 #include <ucontext.h>
00005 #include <queue>
00006 #include <vector>
00007 #include <map>
00008 #include <time.h>
00009
00011
00016 template<class EventKey=int, class EventVal=int> class MTasker
00017 {
00018 private:
00019 ucontext_t d_kernel;
00020 std::queue<int> d_runQueue;
00021 std::queue<int> d_zombiesQueue;
00022
00023 struct Waiter
00024 {
00025 ucontext_t *context;
00026 time_t ttd;
00027 int tid;
00028 };
00029
00030 typedef std::map<EventKey,Waiter> waiters_t;
00031 waiters_t d_waiters;
00032 std::map<int,ucontext_t*> d_threads;
00033 int d_tid;
00034 int d_maxtid;
00035 size_t d_stacksize;
00036
00037 EventVal d_waitval;
00038 enum {Error=-1,TimeOut=0,Answer} d_waitstatus;
00039
00040 public:
00042
00046 MTasker(size_t stacksize=8192) : d_stacksize(stacksize)
00047 {
00048 d_maxtid=0;
00049 }
00050
00051 typedef void tfunc_t(void *);
00052 int waitEvent(const EventKey &key, EventVal *val=0, unsigned int timeout=0);
00053 void yield();
00054 int sendEvent(const EventKey& key, const EventVal* val=0);
00055 void getEvents(std::vector<EventKey>& events);
00056 void makeThread(tfunc_t *start, void* val);
00057 bool schedule();
00058 bool noProcesses();
00059 unsigned int numProcesses();
00060 int getTid();
00061 private:
00062 static void threadWrapper(MTasker *self, tfunc_t *tf, int tid, void* val);
00063 };
00064 #include "mtasker.cc"
00065 #endif