wvwin32task.h

00001 /* -*- Mode: C++ -*- */
00002 #ifndef __WVWIN32TASK_H
00003 #define __WVWIN32TASK_H
00004 
00005 #include "wvstring.h"
00006 #include "wvlinklist.h"
00007 
00008 #include <windows.h>
00009 
00010 #define WVTASK_MAGIC 0x123678
00011 
00012 class WvTaskMan;
00013 
00014 class WvTask
00015 {
00016     friend class WvTaskMan;
00017     typedef void TaskFunc(void *userdata);
00018     
00019     static int taskcount, numtasks, numrunning;
00020     int magic_number, *stack_magic;
00021     WvString name;
00022     int tid;
00023     
00024     size_t stacksize;
00025     bool running, recycled;
00026     
00027     WvTaskMan &man;
00028     LPVOID mystate;     // used for resuming the task
00029     
00030     TaskFunc *func;
00031     void *userdata;
00032 
00033     static VOID CALLBACK MyFiberProc(PVOID lpParameter);
00034     WvTask(WvTaskMan &_man, size_t _stacksize = 64*1024);
00035     
00036 public:
00037     virtual ~WvTask();
00038     
00039     void start(WvStringParm _name, TaskFunc *_func, void *_userdata);
00040     bool isrunning() const
00041         { return running; }
00042     void recycle();
00043 };
00044 
00045 
00046 DeclareWvList(WvTask);
00047 
00049 class WvTaskMan
00050 {
00051     friend class WvTask;
00052 
00053     static WvTaskMan *singleton;
00054     static int links;
00055 
00056     static int magic_number;
00057     static WvTaskList free_tasks;
00058     
00059     static void get_stack(WvTask &task, size_t size);
00060     static void stackmaster();
00061     static void _stackmaster();
00062     static void do_task();
00063     
00064     static WvTask *stack_target;
00065     
00066     static WvTask *current_task;
00067     static LPVOID toplevel;
00068     
00069     WvTaskMan();
00070     virtual ~WvTaskMan();
00071    
00072 public:
00074     static WvTaskMan *get();
00075     static void unlink();
00076   
00077     WvTask *start(WvStringParm name,
00078                   WvTask::TaskFunc *func, void *userdata,
00079                   size_t stacksize = 256*1024);
00080     
00081     // run() and yield() return the 'val' passed to run() when this task
00082     // was started.
00083     static int run(WvTask &task, int val = 1);
00084     static int yield(int val = 1);
00085     
00086     static WvTask *whoami()
00087         { return current_task; }
00088 };
00089 
00090 #endif // __WVWIN32TASK_H

Generated on Thu May 25 21:51:04 2006 for WvStreams by  doxygen 1.4.6