WvStreams
wvtask.h
00001 /* -*- Mode: C++ -*-
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... see also WvCont.
00012  */
00013 #ifndef __WVTASK_H
00014 #define __WVTASK_H
00015 
00016 #ifdef _WIN32
00017 
00018 #include "wvwin32task.h"
00019 
00020 #else
00021 
00022 #include "wvstring.h"
00023 #include "wvlinklist.h"
00024 #include "wvstreamsdebugger.h"
00025 #include "wvstringlist.h"
00026 #include "setjmp.h"
00027 #include <ucontext.h>
00028 
00029 #define WVTASK_MAGIC 0x123678
00030 
00031 class WvTaskMan;
00032 
00034 class WvTask
00035 {
00036     friend class WvTaskMan;
00037     
00038     // you might think it would be useful to have this return an int, since
00039     // yield() and run() both return int.  But that ends up being more
00040     // confusing than you think, because if you call task1->run(), and he
00041     // calls task2->run(), and task2 calls yield(), then task1->run() returns
00042     // the value *task2* passed to yield!  So we avoid the confusion by not
00043     // using return values here, which discourages people from thinking of
00044     // them as return values.
00045     typedef void TaskFunc(void *userdata);
00046     
00047     static int taskcount, numtasks, numrunning;
00048     int volatile magic_number;
00049     int *stack_magic;
00050     WvString name;
00051     int tid;
00052     
00053     size_t stacksize;
00054     void *stack;
00055     bool running, recycled;
00056     
00057     WvTaskMan &man;
00058     ucontext_t mystate; // used for resuming the task
00059     ucontext_t func_call, func_return;
00060     
00061     TaskFunc *func;
00062     void *userdata;
00063     
00064     WvTask(WvTaskMan &_man, size_t _stacksize = 64*1024);
00065     
00066 public:
00067     virtual ~WvTask();
00068     
00069     void start(WvStringParm _name, TaskFunc *_func, void *_userdata);
00070     bool isrunning() const
00071         { return running; }
00072     void recycle();
00073     int get_tid() const { return tid; }
00074     WvString get_name() const { return name; }
00075 };
00076 
00077 
00078 DeclareWvList(WvTask);
00079 
00081 class WvTaskMan
00082 {
00083     friend class WvTask;
00084     
00085     static WvTaskMan *singleton;
00086     static int links;
00087     
00088     static int volatile magic_number;
00089     static WvTaskList all_tasks, free_tasks;
00090     
00091     static void get_stack(WvTask &task, size_t size);
00092     static void stackmaster();
00093     static void _stackmaster();
00094     static void do_task();
00095     static void call_func(WvTask *task);
00096 
00097     static char *stacktop;
00098     static ucontext_t stackmaster_task;
00099     
00100     static WvTask *stack_target;
00101     static ucontext_t get_stack_return;
00102     
00103     static WvTask *current_task;
00104     static ucontext_t toplevel;
00105     
00106     WvTaskMan();
00107     virtual ~WvTaskMan();
00108 
00109 #ifdef ENABLE_DELETE_DETECTOR
00110     friend void operator &&<WvTaskMan>(CheckIObject, const WvTaskMan *);
00111 #endif
00112     
00113 public:
00115     static WvTaskMan *get();
00116     static void unlink();
00117     
00118     WvTask *start(WvStringParm name,
00119                   WvTask::TaskFunc *func, void *userdata,
00120                   size_t stacksize = 64*1024);
00121     
00122     // run() and yield() return the 'val' passed to run() when this task
00123     // was started.
00124     static int run(WvTask &task, int val = 1);
00125     static int yield(int val = 1);
00126     
00127     static WvTask *whoami()
00128         { return current_task; }
00129 
00130     static const void *current_top_of_stack();
00131     static size_t current_stacksize_limit();
00132 
00133 private:
00134     static WvString debugger_tasks_run_cb(WvStringParm, WvStringList &,
00135             WvStreamsDebugger::ResultCallback, void *);
00136 };
00137 
00138 
00139 #endif // ifdef _WIN32
00140 #endif // __WVTASK_H