This repository was archived by the owner on Sep 7, 2021. It is now read-only.
forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiber.cpp
More file actions
261 lines (203 loc) · 6.02 KB
/
Copy pathfiber.cpp
File metadata and controls
261 lines (203 loc) · 6.02 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "core.h"
#include "fiber.h"
#include <pthread.h>
////////////////////////////////////////////////////////////////////////////////
struct MCFiber
{
MCFiber *next;
pthread_t thread;
bool owns_thread;
bool finished;
uindex_t depth;
MCFiberCallback callback;
void *context;
MCFiberRef caller;
};
////////////////////////////////////////////////////////////////////////////////
static MCFiberRef s_fibers = nil;
static MCFiberRef s_fiber_current = nil;
static pthread_mutex_t s_fiber_mutex;
static pthread_cond_t s_fiber_condition;
void MCFiberInitialize(void)
{
s_fibers = nil;
s_fiber_current = nil;
pthread_mutex_init(&s_fiber_mutex, NULL);
pthread_cond_init(&s_fiber_condition, NULL);
}
void MCFiberFinalize(void)
{
pthread_cond_destroy(&s_fiber_condition);
pthread_mutex_destroy(&s_fiber_mutex);
s_fiber_current = nil;
s_fibers = nil;
}
static void MCFiberSwitch(MCFiberRef p_target)
{
// Lock the mutex and update the current fiber to the target.
pthread_mutex_lock(&s_fiber_mutex);
s_fiber_current = p_target;
pthread_mutex_unlock(&s_fiber_mutex);
// Signal the condition to wake up any of the fiber threads.
// This is where the switch actually happens.
pthread_cond_signal(&s_fiber_condition);
}
static void MCFiberDispatch(MCFiberRef p_current)
{
// Increase the nesting depth of the fiber.
p_current -> depth += 1;
// Loop until there are no more callback requests.
for(;;)
{
// Wait until we are made current.
pthread_mutex_lock(&s_fiber_mutex);
while(s_fiber_current != p_current)
pthread_cond_wait(&s_fiber_condition, &s_fiber_mutex);
pthread_mutex_unlock(&s_fiber_mutex);
// If there are no callbacks to this fiber then we are done.
if (p_current -> callback == nil)
break;
// We have a callback to invoke.
MCFiberCallback t_callback;
void *t_context;
t_callback = p_current -> callback;
t_context = p_current -> context;
// Get the calling fiber.
MCFiberRef t_target;
t_target = p_current -> caller;
// Clear the current fiber's callback fields as we are about to invoke it.
p_current -> callback = nil;
p_current -> context = nil;
p_current -> caller = nil;
// Invoke the callback.
t_callback(t_context);
// Switch to the calling fiber.
MCFiberSwitch(t_target);
}
// Decrease the nesting depth of the fiber.
p_current -> depth -= 1;
}
static void *MCFiberOwnedThreadRoutine(void *p_context)
{
MCFiberRef self;
self = (MCFiberRef)p_context;
MCFiberDispatch(self);
self -> finished = true;
return nil;
}
////////////////////////////////////////////////////////////////////////////////
bool MCFiberConvert(MCFiberRef& r_fiber)
{
MCFiberRef self;
if (!MCMemoryNew(self))
return false;
// If we have no fibers already, initialize ourselves.
if (s_fibers == nil)
MCFiberInitialize();
// Get the thread id of the fiber and link it into the fiber chain.
self -> thread = pthread_self();
self -> next = s_fibers;
s_fibers = self;
// As we are converting the current thread, the current fiber becomes
// self.
s_fiber_current = self;
// Return successfully.
r_fiber = self;
return true;
}
bool MCFiberCreate(size_t p_stack_size, MCFiberRef& r_fiber)
{
MCFiberRef self;
if (!MCMemoryNew(self))
return false;
if (s_fibers == nil)
MCFiberInitialize();
self -> thread = nil;
self -> next = s_fibers;
s_fibers = self;
if (pthread_create(&self -> thread, nil, MCFiberOwnedThreadRoutine, self) != 0)
{
MCFiberDestroy(self);
return false;
}
r_fiber = self;
return true;
}
void MCFiberDestroy(MCFiberRef self)
{
// A fiber can only be destroyed at zero depth.
MCAssert(self -> depth == 0);
// If the thread is owned by the fiber then wait on it to finish.
if (self -> owns_thread && self -> thread != nil)
{
// A fiber that owns its thread cannot destroy itself.
MCAssert(self != s_fiber_current);
// Loop until the thread is finished.
while(!self -> finished)
MCFiberMakeCurrent(self);
// Join to the thread.
pthread_join(self -> thread, nil);
// The thread is now gone.
self -> thread = nil;
}
// Remove the fiber record from the list.
if (s_fibers != self)
{
MCFiber *t_previous;
for(t_previous = s_fibers; t_previous -> next != self; t_previous = t_previous -> next)
;
t_previous -> next = self -> next;
}
else
s_fibers = self -> next;
// Delete the record.
MCMemoryDelete(self);
// If there are now no fibers, finalize our state.
if (s_fibers == nil)
MCFiberFinalize();
}
MCFiberRef MCFiberGetCurrent(void)
{
return s_fiber_current;
}
void MCFiberMakeCurrent(MCFiberRef p_target)
{
// If we are on the current fiber, then do nothing.
if (p_target == s_fiber_current)
return;
// Get the fiber we are currently on.
MCFiberRef t_current;
t_current = s_fiber_current;
// Jump to the new fiber.
MCFiberSwitch(p_target);
// Dispatch callbacks until no more are done, then we return to the caller.
MCFiberDispatch(t_current);
}
void MCFiberCall(MCFiberRef p_target, MCFiberCallback p_callback, void *p_context)
{
// If we are on the given fiber, call the callback directly.
if (p_target == s_fiber_current)
{
p_callback(p_context);
return;
}
// Otherwise, set the fields in the fiber's structure.
p_target -> callback = p_callback;
p_target -> context = p_context;
p_target -> caller = s_fiber_current;
// And switch to the fiber.
MCFiberMakeCurrent(p_target);
}
////////////////////////////////////////////////////////////////////////////////