source: mainline/uspace/lib/c/generic/fibril.c@ 204ba47

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 204ba47 was 4d11204, checked in by Jakub Jermar <jakub@…>, 11 years ago

Protect accesses to fibril_list by fibril_futex.

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2007 Jakub Jermar
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <adt/list.h>
37#include <fibril.h>
38#include <thread.h>
39#include <stack.h>
40#include <tls.h>
41#include <malloc.h>
42#include <abi/mm/as.h>
43#include <as.h>
44#include <unistd.h>
45#include <stdio.h>
46#include <libarch/barrier.h>
47#include <libarch/faddr.h>
48#include <futex.h>
49#include <assert.h>
50#include <async.h>
51#include <futex.h>
52
53#ifdef FUTEX_UPGRADABLE
54#include <rcu.h>
55#endif
56
57/**
58 * This futex serializes access to ready_list,
59 * serialized_list, manager_list and fibril_list.
60 */
61static futex_t fibril_futex = FUTEX_INITIALIZER;
62
63static LIST_INITIALIZE(ready_list);
64static LIST_INITIALIZE(serialized_list);
65static LIST_INITIALIZE(manager_list);
66static LIST_INITIALIZE(fibril_list);
67
68/** Number of threads that are executing a manager fibril. */
69static int threads_in_manager;
70
71/**
72 * Number of threads that are executing a manager fibril
73 * and are serialized. Protected by async_futex.
74 */
75static int serialized_threads;
76
77/** Fibril-local count of serialization. If > 0, we must not preempt */
78static fibril_local int serialization_count;
79
80/** Function that spans the whole life-cycle of a fibril.
81 *
82 * Each fibril begins execution in this function. Then the function implementing
83 * the fibril logic is called. After its return, the return value is saved.
84 * The fibril then switches to another fibril, which cleans up after it.
85 *
86 */
87static void fibril_main(void)
88{
89 fibril_t *fibril = __tcb_get()->fibril_data;
90
91#ifdef FUTEX_UPGRADABLE
92 rcu_register_fibril();
93#endif
94
95 /* Call the implementing function. */
96 fibril->retval = fibril->func(fibril->arg);
97
98 fibril_switch(FIBRIL_FROM_DEAD);
99 /* Not reached */
100}
101
102/** Setup fibril information into TCB structure
103 *
104 */
105fibril_t *fibril_setup(void)
106{
107 tcb_t *tcb = tls_make();
108 if (!tcb)
109 return NULL;
110
111 fibril_t *fibril = malloc(sizeof(fibril_t));
112 if (!fibril) {
113 tls_free(tcb);
114 return NULL;
115 }
116
117 tcb->fibril_data = fibril;
118 fibril->tcb = tcb;
119
120 fibril->func = NULL;
121 fibril->arg = NULL;
122 fibril->stack = NULL;
123 fibril->clean_after_me = NULL;
124 fibril->retval = 0;
125 fibril->flags = 0;
126
127 fibril->waits_for = NULL;
128
129 futex_lock(&fibril_futex);
130 list_append(&fibril->all_link, &fibril_list);
131 futex_unlock(&fibril_futex);
132
133 return fibril;
134}
135
136void fibril_teardown(fibril_t *fibril, bool locked)
137{
138 if (!locked)
139 futex_lock(&fibril_futex);
140 list_remove(&fibril->all_link);
141 if (!locked)
142 futex_unlock(&fibril_futex);
143 tls_free(fibril->tcb);
144 free(fibril);
145}
146
147/** Switch from the current fibril.
148 *
149 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
150 * held.
151 *
152 * @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
153 * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
154 * describes the circumstances of the switch.
155 *
156 * @return 0 if there is no ready fibril,
157 * @return 1 otherwise.
158 *
159 */
160int fibril_switch(fibril_switch_type_t stype)
161{
162 int retval = 0;
163
164 futex_lock(&fibril_futex);
165
166 if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
167 goto ret_0;
168
169 if (stype == FIBRIL_FROM_MANAGER) {
170 if ((list_empty(&ready_list)) && (list_empty(&serialized_list)))
171 goto ret_0;
172
173 /*
174 * Do not preempt if there is not enough threads to run the
175 * ready fibrils which are not serialized.
176 */
177 if ((list_empty(&serialized_list)) &&
178 (threads_in_manager <= serialized_threads)) {
179 goto ret_0;
180 }
181 }
182
183 /* If we are going to manager and none exists, create it */
184 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
185 while (list_empty(&manager_list)) {
186 futex_unlock(&fibril_futex);
187 async_create_manager();
188 futex_lock(&fibril_futex);
189 }
190 }
191
192 fibril_t *srcf = __tcb_get()->fibril_data;
193 if (stype != FIBRIL_FROM_DEAD) {
194
195 /* Save current state */
196 if (!context_save(&srcf->ctx)) {
197 if (serialization_count)
198 srcf->flags &= ~FIBRIL_SERIALIZED;
199
200 if (srcf->clean_after_me) {
201 /*
202 * Cleanup after the dead fibril from which we
203 * restored context here.
204 */
205 void *stack = srcf->clean_after_me->stack;
206 if (stack) {
207 /*
208 * This check is necessary because a
209 * thread could have exited like a
210 * normal fibril using the
211 * FIBRIL_FROM_DEAD switch type. In that
212 * case, its fibril will not have the
213 * stack member filled.
214 */
215 as_area_destroy(stack);
216 }
217 fibril_teardown(srcf->clean_after_me, true);
218 srcf->clean_after_me = NULL;
219 }
220
221 return 1; /* futex_unlock already done here */
222 }
223
224 /* Save myself to the correct run list */
225 if (stype == FIBRIL_PREEMPT)
226 list_append(&srcf->link, &ready_list);
227 else if (stype == FIBRIL_FROM_MANAGER) {
228 list_append(&srcf->link, &manager_list);
229 threads_in_manager--;
230 } else {
231 /*
232 * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
233 * any list, we should already be somewhere, or we will
234 * be lost.
235 */
236 }
237 }
238
239 /* Choose a new fibril to run */
240 fibril_t *dstf;
241 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
242 dstf = list_get_instance(list_first(&manager_list), fibril_t,
243 link);
244 if (serialization_count && stype == FIBRIL_TO_MANAGER) {
245 serialized_threads++;
246 srcf->flags |= FIBRIL_SERIALIZED;
247 }
248 threads_in_manager++;
249
250 if (stype == FIBRIL_FROM_DEAD)
251 dstf->clean_after_me = srcf;
252 } else {
253 if (!list_empty(&serialized_list)) {
254 dstf = list_get_instance(list_first(&serialized_list),
255 fibril_t, link);
256 serialized_threads--;
257 } else {
258 dstf = list_get_instance(list_first(&ready_list),
259 fibril_t, link);
260 }
261 }
262 list_remove(&dstf->link);
263
264 futex_unlock(&fibril_futex);
265
266#ifdef FUTEX_UPGRADABLE
267 if (stype == FIBRIL_FROM_DEAD) {
268 rcu_deregister_fibril();
269 }
270#endif
271
272 context_restore(&dstf->ctx);
273 /* not reached */
274
275ret_0:
276 futex_unlock(&fibril_futex);
277 return retval;
278}
279
280/** Create a new fibril.
281 *
282 * @param func Implementing function of the new fibril.
283 * @param arg Argument to pass to func.
284 * @param stksz Stack size in bytes.
285 *
286 * @return 0 on failure or TLS of the new fibril.
287 *
288 */
289fid_t fibril_create_generic(int (*func)(void *), void *arg, size_t stksz)
290{
291 fibril_t *fibril;
292
293 fibril = fibril_setup();
294 if (fibril == NULL)
295 return 0;
296
297 size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
298 stack_size_get() : stksz;
299 fibril->stack = as_area_create((void *) -1, stack_size,
300 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
301 AS_AREA_LATE_RESERVE);
302 if (fibril->stack == (void *) -1) {
303 fibril_teardown(fibril, false);
304 return 0;
305 }
306
307 fibril->func = func;
308 fibril->arg = arg;
309
310 context_save(&fibril->ctx);
311 context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
312 stack_size, fibril->tcb);
313
314 return (fid_t) fibril;
315}
316
317/** Delete a fibril that has never run.
318 *
319 * Free resources of a fibril that has been created with fibril_create()
320 * but never readied using fibril_add_ready().
321 *
322 * @param fid Pointer to the fibril structure of the fibril to be
323 * added.
324 */
325void fibril_destroy(fid_t fid)
326{
327 fibril_t *fibril = (fibril_t *) fid;
328
329 as_area_destroy(fibril->stack);
330 fibril_teardown(fibril, false);
331}
332
333/** Add a fibril to the ready list.
334 *
335 * @param fid Pointer to the fibril structure of the fibril to be
336 * added.
337 *
338 */
339void fibril_add_ready(fid_t fid)
340{
341 fibril_t *fibril = (fibril_t *) fid;
342
343 futex_lock(&fibril_futex);
344
345 if ((fibril->flags & FIBRIL_SERIALIZED))
346 list_append(&fibril->link, &serialized_list);
347 else
348 list_append(&fibril->link, &ready_list);
349
350 futex_unlock(&fibril_futex);
351}
352
353/** Add a fibril to the manager list.
354 *
355 * @param fid Pointer to the fibril structure of the fibril to be
356 * added.
357 *
358 */
359void fibril_add_manager(fid_t fid)
360{
361 fibril_t *fibril = (fibril_t *) fid;
362
363 futex_lock(&fibril_futex);
364 list_append(&fibril->link, &manager_list);
365 futex_unlock(&fibril_futex);
366}
367
368/** Remove one manager from the manager list. */
369void fibril_remove_manager(void)
370{
371 futex_lock(&fibril_futex);
372
373 if (!list_empty(&manager_list))
374 list_remove(list_first(&manager_list));
375
376 futex_unlock(&fibril_futex);
377}
378
379/** Return fibril id of the currently running fibril.
380 *
381 * @return fibril ID of the currently running fibril.
382 *
383 */
384fid_t fibril_get_id(void)
385{
386 return (fid_t) __tcb_get()->fibril_data;
387}
388
389/** Disable preemption
390 *
391 * If the fibril wants to send several message in a row and does not want to be
392 * preempted, it should start async_serialize_start() in the beginning of
393 * communication and async_serialize_end() in the end. If it is a true
394 * multithreaded application, it should protect the communication channel by a
395 * futex as well.
396 *
397 */
398void fibril_inc_sercount(void)
399{
400 serialization_count++;
401}
402
403/** Restore the preemption counter to the previous state. */
404void fibril_dec_sercount(void)
405{
406 serialization_count--;
407}
408
409int fibril_get_sercount(void)
410{
411 return serialization_count;
412}
413
414/** @}
415 */
Note: See TracBrowser for help on using the repository browser.