WvStreams
wvtask.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * A set of classes that provide co-operative multitasking support. By
6  * default there's no scheduler -- you have to provide it yourself. As it
7  * stands, this is just a convenient way to switch from one context to
8  * another when you know exactly what you want to do.
9  *
10  * This is mainly intended for use by WvStream, but that's probably not the
11  * only possible use... see also WvCont.
12  */
13 #ifndef __WVTASK_H
14 #define __WVTASK_H
15 
16 #ifdef _WIN32
17 
18 #include "wvwin32task.h"
19 
20 #else
21 
22 #include "wvstring.h"
23 #include "wvlinklist.h"
24 #include "wvstreamsdebugger.h"
25 #include "wvstringlist.h"
26 #include "setjmp.h"
27 #include <ucontext.h>
28 
29 #define WVTASK_MAGIC 0x123678
30 
31 class WvTaskMan;
32 
34 class WvTask
35 {
36  friend class WvTaskMan;
37 
38  // you might think it would be useful to have this return an int, since
39  // yield() and run() both return int. But that ends up being more
40  // confusing than you think, because if you call task1->run(), and he
41  // calls task2->run(), and task2 calls yield(), then task1->run() returns
42  // the value *task2* passed to yield! So we avoid the confusion by not
43  // using return values here, which discourages people from thinking of
44  // them as return values.
45  typedef void TaskFunc(void *userdata);
46 
47  static int taskcount, numtasks, numrunning;
48  int magic_number, *stack_magic;
49  WvString name;
50  int tid;
51 
52  size_t stacksize;
53  void *stack;
54  bool running, recycled;
55 
56  WvTaskMan &man;
57  ucontext_t mystate; // used for resuming the task
58  ucontext_t func_call, func_return;
59 
60  TaskFunc *func;
61  void *userdata;
62 
63  WvTask(WvTaskMan &_man, size_t _stacksize = 64*1024);
64 
65 public:
66  virtual ~WvTask();
67 
68  void start(WvStringParm _name, TaskFunc *_func, void *_userdata);
69  bool isrunning() const
70  { return running; }
71  void recycle();
72  int get_tid() const { return tid; }
73  WvString get_name() const { return name; }
74 };
75 
76 
77 DeclareWvList(WvTask);
78 
80 class WvTaskMan
81 {
82  friend class WvTask;
83 
84  static WvTaskMan *singleton;
85  static int links;
86 
87  static int magic_number;
88  static WvTaskList all_tasks, free_tasks;
89 
90  static void get_stack(WvTask &task, size_t size);
91  static void stackmaster();
92  static void _stackmaster();
93  static void do_task();
94  static void call_func(WvTask *task);
95 
96  static char *stacktop;
97  static ucontext_t stackmaster_task;
98 
99  static WvTask *stack_target;
100  static ucontext_t get_stack_return;
101 
102  static WvTask *current_task;
103  static ucontext_t toplevel;
104 
105  WvTaskMan();
106  virtual ~WvTaskMan();
107 
108 #ifdef ENABLE_DELETE_DETECTOR
109  friend void operator &&<WvTaskMan>(CheckIObject, const WvTaskMan *);
110 #endif
111 
112 public:
114  static WvTaskMan *get();
115  static void unlink();
116 
117  WvTask *start(WvStringParm name,
118  WvTask::TaskFunc *func, void *userdata,
119  size_t stacksize = 64*1024);
120 
121  // run() and yield() return the 'val' passed to run() when this task
122  // was started.
123  static int run(WvTask &task, int val = 1);
124  static int yield(int val = 1);
125 
126  static WvTask *whoami()
127  { return current_task; }
128 
129  static const void *current_top_of_stack();
130  static size_t current_stacksize_limit();
131 
132 private:
133  static WvString debugger_tasks_run_cb(WvStringParm, WvStringList &,
134  WvStreamsDebugger::ResultCallback, void *);
135 };
136 
137 
138 #endif // ifdef _WIN32
139 #endif // __WVTASK_H
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition: wvstring.h:93
Represents a single thread of control.
Definition: wvtask.h:34
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
Provides co-operative multitasking support among WvTask instances.
Definition: wvtask.h:80