-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
executable file
·213 lines (164 loc) · 3.58 KB
/
thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* thread.c:
*
* The Thread class and related functions, including some
* miscellaneous thread-related script-evaluation functions.
*
* See the ``toi.h'' file for more extensive information on
* the various source files and their purposes.
*
*/
#include "toi.h"
VALUE cur_thr;
VALUE cThread;
static VALUE thr_crit;
static vector_t *thr_stk;
static int
thread_reap_check(void *ptr)
{
return object_refcount_down(ptr);
}
static void
thread_reap(VALUE thr)
{
Thread *th;
vector_t *v;
th = THREAD(thr);
v = th->obj_stk;
vector_foreach(v, thread_reap_check);
st_free_table(th->env_tbl);
vector_free(th->obj_stk);
vector_free(th->env_stk);
vector_free(th->self_stk);
vector_free(th->tok_stk);
vector_free(th->stack);
#ifdef SYMBOL_CACHE
vector_free(th->modified_syms);
#endif
}
VALUE
thread_new()
{
VALUE thr;
thr = NEW_OBJ(Thread);
OBJ_SETUP(thr, T_THREAD);
BASIC(thr).klass = cThread;
vector_push(thr_stk, (void*)thr);
THREAD(thr)->alive_p = 1;
if (cur_thr)
THREAD(thr)->branch = THREAD(cur_thr)->branch;
if ((thr != main_thread()) && (thr != cur_thr))
THREAD(thr)->recv = cur_thr;
THREAD(thr)->up = cur_thr;
THREAD(thr)->env_tbl = st_init_numtable();
THREAD(thr)->obj_stk = vector_new();
THREAD(thr)->self_stk = vector_new();
THREAD(thr)->env_stk = vector_new();
THREAD(thr)->tok_stk = vector_new();
THREAD(thr)->stack = vector_new();
#ifdef SYMBOL_CACHE
THREAD(thr)->modified_syms = vector_new();
#endif
return thr;
}
VALUE
push_scope()
{
VALUE thr;
thr = thread_new();
cur_thr = thr;
return thr;
}
VALUE
pop_scope()
{
VALUE tmp;
vector_t *stk = thr_stk;
VALUE thr;
thr = (VALUE)vector_pop(stk);
while ( (tmp = (VALUE)vector_pop(THREAD(thr)->obj_stk)) )
memdealloc(tmp);
cur_thr = THREAD(thr)->up;
return thr;
}
VALUE
thread_s_main()
{
VALUE thr;
return (VALUE)vector_first(thr_stk);
for (thr = cur_thr; TEST(thr); thr = THREAD(thr)->up)
if (! TEST(THREAD(thr)->up) )
return thr;
return thr;
}
VALUE
main_thread()
{
return thread_s_main();
}
VALUE
thread_s_current()
{
return (VALUE)cur_thr;
}
VALUE
thread_set_critical(VALUE val)
{
thr_crit = val;
return val;
}
VALUE
thread_get_critical()
{
return thr_crit;
}
static VALUE
thread_deadlock()
{
fail("thread deadlock");
}
/* FIXME: incompatible with current design of soft-method-calling */
VALUE
thread_schedule()
{
int i;
VALUE th;
/* don't increment round-robin count if in critical section */
if ( TEST(thr_crit) || (vector_length(thr_stk) <= 1) )
{
if (!THREAD(cur_thr)->alive_p)
thread_deadlock();
return cur_thr;
}
/* find the highest priority thread; lowest round-robin count otherwise */
for (i = 0; i < vector_length(thr_stk); i++)
{
th = (VALUE)vector_aref(thr_stk, i);
if ( (THREAD(th)->prio > THREAD(cur_thr)->prio) ||
(!(THREAD(th)->prio < THREAD(cur_thr)->prio) && (THREAD(th)->cnt < THREAD(cur_thr)->cnt)) )
{
if (THREAD(th)->alive_p)
cur_thr = (VALUE)vector_aref(thr_stk, i);
else
THREAD(th)->cnt++;
}
}
THREAD(cur_thr)->cnt++;
return cur_thr;
}
void
Init_thread()
{
if (!thr_stk)
thr_stk = vector_new();
if (!cur_thr)
cur_thr = thread_new();
cThread = define_class(intern("Thread"), cObject);
OBJ_SETUP(cur_thr, T_THREAD);
BASIC(cur_thr).klass = cThread;
define_method(cThread, intern("call"), eval_thread, 1);
define_singleton_method(cThread, intern("main"), main_thread, 0);
define_singleton_method(cThread, intern("current"), thread_s_current, 0);
define_singleton_method(cThread, intern("critical"), thread_get_critical, 0);
define_singleton_method(cThread, intern("setcritical"), thread_set_critical, 1);
}