Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

wvtask.h

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * A set of classes that provide co-operative multitasking support.  By
00006  * default there's no scheduler -- you have to provide it yourself.  As it
00007  * stands, this is just a convenient way to switch from one context to
00008  * another when you know exactly what you want to do.
00009  * 
00010  * This is mainly intended for use by WvStream, but that's probably not the
00011  * only possible use...
00012  */
00013 #ifndef __WVTASK_H
00014 #define __WVTASK_H
00015 
00016 #include "wvstring.h"
00017 #include "wvlinklist.h"
00018 #include "setjmp.h"
00019 
00020 #define WVTASK_MAGIC 123678
00021 
00022 class WvTaskMan;
00023 
00024 class WvTask
00025 {
00026     friend class WvTaskMan;
00027     typedef void TaskFunc(void *userdata);
00028     
00029     static int taskcount, numtasks, numrunning;
00030     int magic_number;
00031     WvString name;
00032     int tid;
00033     
00034     size_t stacksize;
00035     bool running, recycled;
00036     
00037     WvTaskMan &man;
00038     jmp_buf mystate;    // used for resuming the task
00039     
00040     TaskFunc *func;
00041     void *userdata;
00042     
00043     WvTask(WvTaskMan &_man, size_t _stacksize = 64*1024);
00044     
00045 public:
00046     virtual ~WvTask();
00047     
00048     void start(const WvString &_name, TaskFunc *_func, void *_userdata);
00049     bool isrunning() const
00050         { return running; }
00051     void recycle();
00052 };
00053 
00054 
00055 DeclareWvList(WvTask);
00056 
00057 class WvTaskMan
00058 {
00059     friend class WvTask;
00060     int magic_number;
00061     WvTaskList free_tasks;
00062     
00063     void get_stack(WvTask &task, size_t size);
00064     void stackmaster();
00065     void _stackmaster();
00066     void do_task();
00067     jmp_buf stackmaster_task;
00068     
00069     WvTask *stack_target;
00070     jmp_buf get_stack_return;
00071     
00072     WvTask *current_task;
00073     jmp_buf toplevel;
00074     
00075 public:
00076     WvTaskMan();
00077     virtual ~WvTaskMan();
00078     
00079     WvTask *start(const WvString &name,
00080                   WvTask::TaskFunc *func, void *userdata,
00081                   size_t stacksize = 64*1024);
00082     
00083     // run() and yield() return the 'val' passed to run() when this task
00084     // was started.
00085     int run(WvTask &task, int val = 1);
00086     int yield(int val = 1);
00087     
00088     WvTask *whoami() const
00089         { return current_task; }
00090 };
00091 
00092 
00093 
00094 #endif // __WVTASK_H

Generated on Sun Mar 16 01:01:12 2003 for WvStreams by doxygen1.3-rc3