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
31class WvTaskMan;
32
34class 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 volatile magic_number;
49 int *stack_magic;
50 WvString name;
51 int tid;
52
53 size_t stacksize;
54 void *stack;
55 bool running, recycled;
56
57 WvTaskMan &man;
58 ucontext_t mystate; // used for resuming the task
59 ucontext_t func_call, func_return;
60
61 TaskFunc *func;
62 void *userdata;
63
64 WvTask(WvTaskMan &_man, size_t _stacksize = 64*1024);
65
66public:
67 virtual ~WvTask();
68
69 void start(WvStringParm _name, TaskFunc *_func, void *_userdata);
70 bool isrunning() const
71 { return running; }
72 void recycle();
73 int get_tid() const { return tid; }
74 WvString get_name() const { return name; }
75};
76
77
78DeclareWvList(WvTask);
79
82{
83 friend class WvTask;
84
85 static WvTaskMan *singleton;
86 static int links;
87
88 static int volatile magic_number;
89 static WvTaskList all_tasks, free_tasks;
90
91 static void get_stack(WvTask &task, size_t size);
92 static void stackmaster();
93 static void _stackmaster();
94 static void do_task();
95 static void call_func(WvTask *task);
96
97 static char *stacktop;
98 static ucontext_t stackmaster_task;
99
100 static WvTask *stack_target;
101 static ucontext_t get_stack_return;
102
103 static WvTask *current_task;
104 static ucontext_t toplevel;
105
106 WvTaskMan();
107 virtual ~WvTaskMan();
108
109#ifdef ENABLE_DELETE_DETECTOR
110 friend void operator &&<WvTaskMan>(CheckIObject, const WvTaskMan *);
111#endif
112
113public:
115 static WvTaskMan *get();
116 static void unlink();
117
118 WvTask *start(WvStringParm name,
119 WvTask::TaskFunc *func, void *userdata,
120 size_t stacksize = 64*1024);
121
122 // run() and yield() return the 'val' passed to run() when this task
123 // was started.
124 static int run(WvTask &task, int val = 1);
125 static int yield(int val = 1);
126
127 static WvTask *whoami()
128 { return current_task; }
129
130 static const void *current_top_of_stack();
131 static size_t current_stacksize_limit();
132
133private:
134 static WvString debugger_tasks_run_cb(WvStringParm, WvStringList &,
135 WvStreamsDebugger::ResultCallback, void *);
136};
137
138
139#endif // ifdef _WIN32
140#endif // __WVTASK_H
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
This is a WvList of WvStrings, and is a really handy way to parse strings.
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330
Provides co-operative multitasking support among WvTask instances.
Definition wvtask.h:82
static WvTaskMan * get()
get/dereference the singleton global WvTaskMan
Definition wvtask.cc:139
Represents a single thread of control.
Definition wvtask.h:35