source: mainline/uspace/lib/c/generic/thread/fibril.c@ 2cf8f994

Last change on this file since 2cf8f994 was d8cb48d, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Remove FIBRIL_DFLT_STK_SIZE macro

  • Property mode set to 100644
File size: 21.8 KB
RevLine 
[bc1f1c2]1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2007 Jakub Jermar
[514d561]4 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
[bc1f1c2]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup libc
32 * @{
33 */
34/** @file
35 */
36
[d9c8c81]37#include <adt/list.h>
[bc1f1c2]38#include <fibril.h>
[0aae87a6]39#include <stack.h>
[fa23560]40#include <tls.h>
[38d150e]41#include <stdlib.h>
[1107050]42#include <as.h>
[e0a4686]43#include <context.h>
[bc1f1c2]44#include <assert.h>
45
[514d561]46#include <mem.h>
47#include <str.h>
48#include <ipc/ipc.h>
49#include <libarch/faddr.h>
[6340b4d2]50
51#include "../private/thread.h"
[f787c8e]52#include "../private/futex.h"
[6340b4d2]53#include "../private/fibril.h"
54#include "../private/libc.h"
[bc1f1c2]55
[514d561]56#define DPRINTF(...) ((void)0)
[05208d9]57#undef READY_DEBUG
[514d561]58
59/** Member of timeout_list. */
60typedef struct {
61 link_t link;
[bd41ac52]62 struct timespec expires;
[514d561]63 fibril_event_t *event;
64} _timeout_t;
65
66typedef struct {
67 errno_t rc;
68 link_t link;
69 ipc_call_t *call;
70 fibril_event_t event;
71} _ipc_waiter_t;
72
73typedef struct {
74 errno_t rc;
75 link_t link;
76 ipc_call_t call;
77} _ipc_buffer_t;
78
79typedef enum {
80 SWITCH_FROM_DEAD,
81 SWITCH_FROM_HELPER,
82 SWITCH_FROM_YIELD,
83 SWITCH_FROM_BLOCKED,
84} _switch_type_t;
85
86static bool multithreaded = false;
87
88/* This futex serializes access to global data. */
[45c8eea]89static futex_t fibril_futex;
[508b0df1]90static futex_t ready_semaphore;
[05208d9]91static long ready_st_count;
[12f91130]92
[bc1f1c2]93static LIST_INITIALIZE(ready_list);
[c1b979a]94static LIST_INITIALIZE(fibril_list);
[514d561]95static LIST_INITIALIZE(timeout_list);
96
[45c8eea]97static futex_t ipc_lists_futex;
[514d561]98static LIST_INITIALIZE(ipc_waiter_list);
99static LIST_INITIALIZE(ipc_buffer_list);
100static LIST_INITIALIZE(ipc_buffer_free_list);
101
102/* Only used as unique markers for triggered events. */
103static fibril_t _fibril_event_triggered;
104static fibril_t _fibril_event_timed_out;
105#define _EVENT_INITIAL (NULL)
106#define _EVENT_TRIGGERED (&_fibril_event_triggered)
107#define _EVENT_TIMED_OUT (&_fibril_event_timed_out)
108
[05208d9]109static inline void _ready_debug_check(void)
110{
111#ifdef READY_DEBUG
112 assert(!multithreaded);
113 long count = (long) list_count(&ready_list) +
114 (long) list_count(&ipc_buffer_free_list);
115 assert(ready_st_count == count);
116#endif
117}
118
119static inline void _ready_up(void)
120{
121 if (multithreaded) {
122 futex_up(&ready_semaphore);
123 } else {
124 ready_st_count++;
125 _ready_debug_check();
126 }
127}
128
[bd41ac52]129static inline errno_t _ready_down(const struct timespec *expires)
[05208d9]130{
131 if (multithreaded)
132 return futex_down_timeout(&ready_semaphore, expires);
133
134 _ready_debug_check();
135 ready_st_count--;
136 return EOK;
137}
138
[508b0df1]139static atomic_int threads_in_ipc_wait;
[bc1f1c2]140
[596d65c]141/** Function that spans the whole life-cycle of a fibril.
142 *
143 * Each fibril begins execution in this function. Then the function implementing
144 * the fibril logic is called. After its return, the return value is saved.
145 * The fibril then switches to another fibril, which cleans up after it.
146 *
147 */
[514d561]148static void _fibril_main(void)
[bc1f1c2]149{
[514d561]150 /* fibril_futex is locked when a fibril is started. */
[899342e]151 futex_unlock(&fibril_futex);
152
[d73d992]153 fibril_t *fibril = fibril_self();
[d54b303]154
[596d65c]155 /* Call the implementing function. */
[514d561]156 fibril_exit(fibril->func(fibril->arg));
[a35b458]157
[596d65c]158 /* Not reached */
159}
[bc1f1c2]160
[40abf56]161/** Allocate a fibril structure and TCB, but don't do anything else with it. */
162fibril_t *fibril_alloc(void)
[596d65c]163{
[40abf56]164 tcb_t *tcb = tls_make(__progsymbols.elfstart);
[bc1f1c2]165 if (!tcb)
166 return NULL;
[a35b458]167
[d73d992]168 fibril_t *fibril = calloc(1, sizeof(fibril_t));
[596d65c]169 if (!fibril) {
[31399f3]170 tls_free(tcb);
[bc1f1c2]171 return NULL;
172 }
[a35b458]173
[596d65c]174 tcb->fibril_data = fibril;
175 fibril->tcb = tcb;
[40abf56]176 fibril->is_freeable = true;
[a35b458]177
[40abf56]178 fibril_setup(fibril);
[596d65c]179 return fibril;
[bc1f1c2]180}
181
[40abf56]182/**
183 * Put the fibril into fibril_list.
184 */
185void fibril_setup(fibril_t *f)
186{
187 futex_lock(&fibril_futex);
188 list_append(&f->all_link, &fibril_list);
189 futex_unlock(&fibril_futex);
190}
191
[514d561]192void fibril_teardown(fibril_t *fibril)
[1b20da0]193{
[514d561]194 futex_lock(&fibril_futex);
[c1b979a]195 list_remove(&fibril->all_link);
[514d561]196 futex_unlock(&fibril_futex);
[40abf56]197
198 if (fibril->is_freeable) {
199 tls_free(fibril->tcb);
200 free(fibril);
201 }
[bc1f1c2]202}
203
[514d561]204/**
205 * Event notification with a given reason.
[596d65c]206 *
[514d561]207 * @param reason Reason of the notification.
208 * Can be either _EVENT_TRIGGERED or _EVENT_TIMED_OUT.
[bc1f1c2]209 */
[514d561]210static fibril_t *_fibril_trigger_internal(fibril_event_t *event, fibril_t *reason)
[bc1f1c2]211{
[514d561]212 assert(reason != _EVENT_INITIAL);
213 assert(reason == _EVENT_TIMED_OUT || reason == _EVENT_TRIGGERED);
214
215 futex_assert_is_locked(&fibril_futex);
216
217 if (event->fibril == _EVENT_INITIAL) {
218 event->fibril = reason;
219 return NULL;
220 }
221
222 if (event->fibril == _EVENT_TIMED_OUT) {
223 assert(reason == _EVENT_TRIGGERED);
224 event->fibril = reason;
225 return NULL;
226 }
227
228 if (event->fibril == _EVENT_TRIGGERED) {
229 /* Already triggered. Nothing to do. */
230 return NULL;
231 }
232
233 fibril_t *f = event->fibril;
234 event->fibril = reason;
235
236 assert(f->sleep_event == event);
237 return f;
238}
239
[bd41ac52]240static errno_t _ipc_wait(ipc_call_t *call, const struct timespec *expires)
[514d561]241{
242 if (!expires)
243 return ipc_wait(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
244
245 if (expires->tv_sec == 0)
246 return ipc_wait(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
247
[bd41ac52]248 struct timespec now;
[514d561]249 getuptime(&now);
250
[bd41ac52]251 if (ts_gteq(&now, expires))
[514d561]252 return ipc_wait(call, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NON_BLOCKING);
253
[bd41ac52]254 return ipc_wait(call, NSEC2USEC(ts_sub_diff(expires, &now)),
255 SYNCH_FLAGS_NONE);
[514d561]256}
257
258/*
259 * Waits until a ready fibril is added to the list, or an IPC message arrives.
260 * Returns NULL on timeout and may also return NULL if returning from IPC
261 * wait after new ready fibrils are added.
262 */
[bd41ac52]263static fibril_t *_ready_list_pop(const struct timespec *expires, bool locked)
[514d561]264{
265 if (locked) {
266 futex_assert_is_locked(&fibril_futex);
267 assert(expires);
268 /* Must be nonblocking. */
269 assert(expires->tv_sec == 0);
270 } else {
271 futex_assert_is_not_locked(&fibril_futex);
272 }
273
[05208d9]274 errno_t rc = _ready_down(expires);
[514d561]275 if (rc != EOK)
276 return NULL;
277
278 /*
279 * Once we acquire a token from ready_semaphore, there are two options.
280 * Either there is a ready fibril in the list, or it's our turn to
281 * call `ipc_wait_cycle()`. There is one extra token on the semaphore
282 * for each entry of the call buffer.
283 */
284
285 if (!locked)
286 futex_lock(&fibril_futex);
287 fibril_t *f = list_pop(&ready_list, fibril_t, link);
288 if (!f)
[508b0df1]289 atomic_fetch_add_explicit(&threads_in_ipc_wait, 1,
290 memory_order_relaxed);
[514d561]291 if (!locked)
292 futex_unlock(&fibril_futex);
293
294 if (f)
295 return f;
296
297 if (!multithreaded)
298 assert(list_empty(&ipc_buffer_list));
299
300 /* No fibril is ready, IPC wait it is. */
301 ipc_call_t call = { 0 };
302 rc = _ipc_wait(&call, expires);
303
[508b0df1]304 atomic_fetch_sub_explicit(&threads_in_ipc_wait, 1,
305 memory_order_relaxed);
[514d561]306
307 if (rc != EOK && rc != ENOENT) {
308 /* Return token. */
[05208d9]309 _ready_up();
[514d561]310 return NULL;
311 }
312
313 /*
314 * We might get ENOENT due to a poke.
315 * In that case, we propagate the null call out of fibril_ipc_wait(),
316 * because poke must result in that call returning.
317 */
318
319 /*
320 * If a fibril is already waiting for IPC, we wake up the fibril,
321 * and return the token to ready_semaphore.
322 * If there is no fibril waiting, we pop a buffer bucket and
323 * put our call there. The token then returns when the bucket is
324 * returned.
325 */
326
327 if (!locked)
328 futex_lock(&fibril_futex);
329
330 futex_lock(&ipc_lists_futex);
331
332 _ipc_waiter_t *w = list_pop(&ipc_waiter_list, _ipc_waiter_t, link);
333 if (w) {
334 *w->call = call;
335 w->rc = rc;
336 /* We switch to the woken up fibril immediately if possible. */
337 f = _fibril_trigger_internal(&w->event, _EVENT_TRIGGERED);
338
339 /* Return token. */
[05208d9]340 _ready_up();
[514d561]341 } else {
342 _ipc_buffer_t *buf = list_pop(&ipc_buffer_free_list, _ipc_buffer_t, link);
343 assert(buf);
344 *buf = (_ipc_buffer_t) { .call = call, .rc = rc };
345 list_append(&buf->link, &ipc_buffer_list);
346 }
347
348 futex_unlock(&ipc_lists_futex);
349
350 if (!locked)
351 futex_unlock(&fibril_futex);
352
353 return f;
354}
355
356static fibril_t *_ready_list_pop_nonblocking(bool locked)
357{
[bd41ac52]358 struct timespec tv = { .tv_sec = 0, .tv_nsec = 0 };
[514d561]359 return _ready_list_pop(&tv, locked);
360}
361
362static void _ready_list_push(fibril_t *f)
363{
364 if (!f)
365 return;
366
367 futex_assert_is_locked(&fibril_futex);
368
369 /* Enqueue in ready_list. */
370 list_append(&f->link, &ready_list);
[05208d9]371 _ready_up();
[514d561]372
[508b0df1]373 if (atomic_load_explicit(&threads_in_ipc_wait, memory_order_relaxed)) {
[514d561]374 DPRINTF("Poking.\n");
375 /* Wakeup one thread sleeping in SYS_IPC_WAIT. */
376 ipc_poke();
377 }
378}
379
380/* Blocks the current fibril until an IPC call arrives. */
[bd41ac52]381static errno_t _wait_ipc(ipc_call_t *call, const struct timespec *expires)
[514d561]382{
383 futex_assert_is_not_locked(&fibril_futex);
384
385 futex_lock(&ipc_lists_futex);
386 _ipc_buffer_t *buf = list_pop(&ipc_buffer_list, _ipc_buffer_t, link);
387 if (buf) {
388 *call = buf->call;
389 errno_t rc = buf->rc;
390
391 /* Return to freelist. */
392 list_append(&buf->link, &ipc_buffer_free_list);
393 /* Return IPC wait token. */
[05208d9]394 _ready_up();
[514d561]395
396 futex_unlock(&ipc_lists_futex);
397 return rc;
398 }
399
400 _ipc_waiter_t w = { .call = call };
401 list_append(&w.link, &ipc_waiter_list);
402 futex_unlock(&ipc_lists_futex);
403
404 errno_t rc = fibril_wait_timeout(&w.event, expires);
405 if (rc == EOK)
406 return w.rc;
407
408 futex_lock(&ipc_lists_futex);
409 if (link_in_use(&w.link))
410 list_remove(&w.link);
411 else
412 rc = w.rc;
413 futex_unlock(&ipc_lists_futex);
414 return rc;
415}
416
417/** Fire all timeouts that expired. */
[bd41ac52]418static struct timespec *_handle_expired_timeouts(struct timespec *next_timeout)
[514d561]419{
[bd41ac52]420 struct timespec ts;
421 getuptime(&ts);
[ab6edb6]422
[df7cbc6]423 futex_lock(&fibril_futex);
[c721d26]424
[514d561]425 while (!list_empty(&timeout_list)) {
426 link_t *cur = list_first(&timeout_list);
427 _timeout_t *to = list_get_instance(cur, _timeout_t, link);
[e0a4686]428
[bd41ac52]429 if (ts_gt(&to->expires, &ts)) {
[514d561]430 *next_timeout = to->expires;
[ab6edb6]431 futex_unlock(&fibril_futex);
[514d561]432 return next_timeout;
[ab6edb6]433 }
[c721d26]434
[514d561]435 list_remove(&to->link);
[a35b458]436
[514d561]437 _ready_list_push(_fibril_trigger_internal(
438 to->event, _EVENT_TIMED_OUT));
[bc1f1c2]439 }
[ab6edb6]440
[514d561]441 futex_unlock(&fibril_futex);
442 return NULL;
443}
444
445/**
446 * Clean up after a dead fibril from which we restored context, if any.
447 * Called after a switch is made and fibril_futex is unlocked.
448 */
449static void _fibril_cleanup_dead(void)
450{
451 fibril_t *srcf = fibril_self();
452 if (!srcf->clean_after_me)
453 return;
454
455 void *stack = srcf->clean_after_me->stack;
456 assert(stack);
457 as_area_destroy(stack);
458 fibril_teardown(srcf->clean_after_me);
459 srcf->clean_after_me = NULL;
460}
461
462/** Switch to a fibril. */
463static void _fibril_switch_to(_switch_type_t type, fibril_t *dstf, bool locked)
464{
[2965d18]465 assert(fibril_self()->rmutex_locks == 0);
466
[514d561]467 if (!locked)
468 futex_lock(&fibril_futex);
469 else
470 futex_assert_is_locked(&fibril_futex);
471
472 fibril_t *srcf = fibril_self();
473 assert(srcf);
474 assert(dstf);
[a35b458]475
[514d561]476 switch (type) {
477 case SWITCH_FROM_YIELD:
478 _ready_list_push(srcf);
[e0a4686]479 break;
[514d561]480 case SWITCH_FROM_DEAD:
481 dstf->clean_after_me = srcf;
[e0a4686]482 break;
[514d561]483 case SWITCH_FROM_HELPER:
484 case SWITCH_FROM_BLOCKED:
[e0a4686]485 break;
486 }
487
[514d561]488 dstf->thread_ctx = srcf->thread_ctx;
489 srcf->thread_ctx = NULL;
490
491 /* Just some bookkeeping to allow better debugging of futex locks. */
[f6372be9]492 futex_give_to(&fibril_futex, dstf);
493
[e0a4686]494 /* Swap to the next fibril. */
495 context_swap(&srcf->ctx, &dstf->ctx);
496
[514d561]497 assert(srcf == fibril_self());
498 assert(srcf->thread_ctx);
[e0a4686]499
[514d561]500 if (!locked) {
501 /* Must be after context_swap()! */
502 futex_unlock(&fibril_futex);
503 _fibril_cleanup_dead();
504 }
505}
[899342e]506
[514d561]507/**
508 * Main function for a helper fibril.
509 * The helper fibril executes on threads in the lightweight fibril pool when
510 * there is no fibril ready to run. Its only purpose is to block until
511 * another fibril is ready, or a timeout expires, or an IPC message arrives.
512 *
513 * There is at most one helper fibril per thread.
514 *
515 */
516static errno_t _helper_fibril_fn(void *arg)
517{
518 /* Set itself as the thread's own context. */
519 fibril_self()->thread_ctx = fibril_self();
520
521 (void) arg;
522
[bd41ac52]523 struct timespec next_timeout;
[514d561]524 while (true) {
[bd41ac52]525 struct timespec *to = _handle_expired_timeouts(&next_timeout);
[514d561]526 fibril_t *f = _ready_list_pop(to, false);
527 if (f) {
528 _fibril_switch_to(SWITCH_FROM_HELPER, f, false);
[e0a4686]529 }
530 }
531
[514d561]532 return EOK;
[bc1f1c2]533}
534
535/** Create a new fibril.
536 *
[596d65c]537 * @param func Implementing function of the new fibril.
538 * @param arg Argument to pass to func.
[eceff5f]539 * @param stksz Stack size in bytes.
[596d65c]540 *
541 * @return 0 on failure or TLS of the new fibril.
[bc1f1c2]542 *
543 */
[b7fd2a0]544fid_t fibril_create_generic(errno_t (*func)(void *), void *arg, size_t stksz)
[bc1f1c2]545{
[596d65c]546 fibril_t *fibril;
[a35b458]547
[40abf56]548 fibril = fibril_alloc();
[596d65c]549 if (fibril == NULL)
[bc1f1c2]550 return 0;
[a35b458]551
[d8cb48d]552 fibril->stack_size = stksz;
[514d561]553 fibril->stack = as_area_create(AS_AREA_ANY, fibril->stack_size,
[1107050]554 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
[6aeca0d]555 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
[40abf56]556 if (fibril->stack == AS_MAP_FAILED) {
[514d561]557 fibril_teardown(fibril);
[bc1f1c2]558 return 0;
559 }
[a35b458]560
[596d65c]561 fibril->func = func;
562 fibril->arg = arg;
[7f122e3]563
[e0a4686]564 context_create_t sctx = {
[514d561]565 .fn = _fibril_main,
[e0a4686]566 .stack_base = fibril->stack,
[514d561]567 .stack_size = fibril->stack_size,
[e0a4686]568 .tls = fibril->tcb,
569 };
[bc1f1c2]570
[e0a4686]571 context_create(&fibril->ctx, &sctx);
[596d65c]572 return (fid_t) fibril;
[bc1f1c2]573}
574
[d8cb48d]575fid_t fibril_create(errno_t (*func)(void *), void *arg)
576{
577 return fibril_create_generic(func, arg, stack_size_get());
578}
579
[32d19f7]580/** Delete a fibril that has never run.
581 *
582 * Free resources of a fibril that has been created with fibril_create()
[514d561]583 * but never started using fibril_start().
[32d19f7]584 *
585 * @param fid Pointer to the fibril structure of the fibril to be
586 * added.
587 */
588void fibril_destroy(fid_t fid)
589{
590 fibril_t *fibril = (fibril_t *) fid;
[a35b458]591
[514d561]592 assert(!fibril->is_running);
593 assert(fibril->stack);
[1107050]594 as_area_destroy(fibril->stack);
[514d561]595 fibril_teardown(fibril);
596}
597
598static void _insert_timeout(_timeout_t *timeout)
599{
600 futex_assert_is_locked(&fibril_futex);
601 assert(timeout);
602
603 link_t *tmp = timeout_list.head.next;
604 while (tmp != &timeout_list.head) {
605 _timeout_t *cur = list_get_instance(tmp, _timeout_t, link);
606
[bd41ac52]607 if (ts_gteq(&cur->expires, &timeout->expires))
[514d561]608 break;
609
610 tmp = tmp->next;
611 }
612
613 list_insert_before(&timeout->link, tmp);
[32d19f7]614}
615
[514d561]616/**
617 * Same as `fibril_wait_for()`, except with a timeout.
[bc1f1c2]618 *
[514d561]619 * It is guaranteed that timing out cannot cause another thread's
620 * `fibril_notify()` to be lost. I.e. the function returns success if and
621 * only if `fibril_notify()` was called after the last call to
622 * wait/wait_timeout returned, and before the call timed out.
[596d65c]623 *
[514d561]624 * @return ETIMEOUT if timed out. EOK otherwise.
[bc1f1c2]625 */
[bd41ac52]626errno_t fibril_wait_timeout(fibril_event_t *event,
627 const struct timespec *expires)
[bc1f1c2]628{
[2965d18]629 assert(fibril_self()->rmutex_locks == 0);
630
[514d561]631 DPRINTF("### Fibril %p sleeping on event %p.\n", fibril_self(), event);
632
633 if (!fibril_self()->thread_ctx) {
634 fibril_self()->thread_ctx =
635 fibril_create_generic(_helper_fibril_fn, NULL, PAGE_SIZE);
636 if (!fibril_self()->thread_ctx)
637 return ENOMEM;
638 }
[a35b458]639
[df7cbc6]640 futex_lock(&fibril_futex);
[514d561]641
642 if (event->fibril == _EVENT_TRIGGERED) {
643 DPRINTF("### Already triggered. Returning. \n");
644 event->fibril = _EVENT_INITIAL;
645 futex_unlock(&fibril_futex);
646 return EOK;
647 }
648
649 assert(event->fibril == _EVENT_INITIAL);
650
651 fibril_t *srcf = fibril_self();
652 fibril_t *dstf = NULL;
653
654 /*
655 * We cannot block here waiting for another fibril becoming
656 * ready, since that would require unlocking the fibril_futex,
657 * and that in turn would allow another thread to restore
658 * the source fibril before this thread finished switching.
659 *
660 * Instead, we switch to an internal "helper" fibril whose only
661 * job is to wait for an event, freeing the source fibril for
662 * wakeups. There is always one for each running thread.
663 */
664
665 dstf = _ready_list_pop_nonblocking(true);
666 if (!dstf) {
667 // XXX: It is possible for the _ready_list_pop_nonblocking() to
668 // check for IPC, find a pending message, and trigger the
669 // event on which we are currently trying to sleep.
670 if (event->fibril == _EVENT_TRIGGERED) {
671 event->fibril = _EVENT_INITIAL;
672 futex_unlock(&fibril_futex);
673 return EOK;
674 }
675
676 dstf = srcf->thread_ctx;
677 assert(dstf);
678 }
679
680 _timeout_t timeout = { 0 };
681 if (expires) {
682 timeout.expires = *expires;
683 timeout.event = event;
684 _insert_timeout(&timeout);
685 }
686
687 assert(srcf);
688
689 event->fibril = srcf;
690 srcf->sleep_event = event;
691
692 assert(event->fibril != _EVENT_INITIAL);
693
694 _fibril_switch_to(SWITCH_FROM_BLOCKED, dstf, true);
695
696 assert(event->fibril != srcf);
697 assert(event->fibril != _EVENT_INITIAL);
698 assert(event->fibril == _EVENT_TIMED_OUT || event->fibril == _EVENT_TRIGGERED);
699
700 list_remove(&timeout.link);
701 errno_t rc = (event->fibril == _EVENT_TIMED_OUT) ? ETIMEOUT : EOK;
702 event->fibril = _EVENT_INITIAL;
703
[df7cbc6]704 futex_unlock(&fibril_futex);
[514d561]705 _fibril_cleanup_dead();
706 return rc;
[bc1f1c2]707}
708
[514d561]709void fibril_wait_for(fibril_event_t *event)
[bc1f1c2]710{
[2965d18]711 assert(fibril_self()->rmutex_locks == 0);
712
[514d561]713 (void) fibril_wait_timeout(event, NULL);
714}
[a35b458]715
[1de92fb0]716/**
717 * Wake up the fibril waiting for the given event.
718 * Up to one wakeup is remembered if the fibril is not currently waiting.
719 *
720 * This function is safe for use under restricted mutex lock.
721 */
[514d561]722void fibril_notify(fibril_event_t *event)
723{
[df7cbc6]724 futex_lock(&fibril_futex);
[514d561]725 _ready_list_push(_fibril_trigger_internal(event, _EVENT_TRIGGERED));
[df7cbc6]726 futex_unlock(&fibril_futex);
[bc1f1c2]727}
728
[514d561]729/** Start a fibril that has not been running yet. */
730void fibril_start(fibril_t *fibril)
[bc1f1c2]731{
[df7cbc6]732 futex_lock(&fibril_futex);
[514d561]733 assert(!fibril->is_running);
734 fibril->is_running = true;
735
736 if (!link_in_use(&fibril->all_link))
737 list_append(&fibril->all_link, &fibril_list);
738
739 _ready_list_push(fibril);
740
[df7cbc6]741 futex_unlock(&fibril_futex);
[bc1f1c2]742}
743
[514d561]744/** Start a fibril that has not been running yet. (obsolete) */
745void fibril_add_ready(fibril_t *fibril)
746{
747 fibril_start(fibril);
748}
749
750/** @return the currently running fibril. */
[d73d992]751fibril_t *fibril_self(void)
752{
[40abf56]753 assert(__tcb_is_set());
754 tcb_t *tcb = __tcb_get();
755 assert(tcb->fibril_data);
756 return tcb->fibril_data;
[d73d992]757}
758
[514d561]759/**
760 * Obsolete, use fibril_self().
[3562ec82]761 *
[514d561]762 * @return ID of the currently running fibril.
[bc1f1c2]763 */
764fid_t fibril_get_id(void)
765{
[d73d992]766 return (fid_t) fibril_self();
767}
768
[514d561]769/**
770 * Switch to another fibril, if one is ready to run.
771 * Has no effect on a heavy fibril.
772 */
[d73d992]773void fibril_yield(void)
774{
[2965d18]775 if (fibril_self()->rmutex_locks > 0)
776 return;
777
[514d561]778 fibril_t *f = _ready_list_pop_nonblocking(false);
779 if (f)
780 _fibril_switch_to(SWITCH_FROM_YIELD, f, false);
[bc1f1c2]781}
782
[c124c985]783static void _runner_fn(void *arg)
784{
[514d561]785 _helper_fibril_fn(arg);
[c124c985]786}
787
788/**
789 * Spawn a given number of runners (i.e. OS threads) immediately, and
790 * unconditionally. This is meant to be used for tests and debugging.
791 * Regular programs should just use `fibril_enable_multithreaded()`.
792 *
793 * @param n Number of runners to spawn.
794 * @return Number of runners successfully spawned.
795 */
796int fibril_test_spawn_runners(int n)
797{
[2965d18]798 assert(fibril_self()->rmutex_locks == 0);
799
[05208d9]800 if (!multithreaded) {
801 _ready_debug_check();
[45c8eea]802 if (futex_initialize(&ready_semaphore, ready_st_count) != EOK)
803 abort();
[514d561]804 multithreaded = true;
[05208d9]805 }
[514d561]806
[c124c985]807 errno_t rc;
808
809 for (int i = 0; i < n; i++) {
810 thread_id_t tid;
811 rc = thread_create(_runner_fn, NULL, "fibril runner", &tid);
812 if (rc != EOK)
813 return i;
814 thread_detach(tid);
815 }
816
817 return n;
818}
819
820/**
821 * Opt-in to have more than one runner thread.
822 *
823 * Currently, a task only ever runs in one thread because multithreading
824 * might break some existing code.
825 *
826 * Eventually, the number of runner threads for a given task should become
827 * configurable in the environment and this function becomes no-op.
828 */
829void fibril_enable_multithreaded(void)
830{
831 // TODO: Implement better.
832 // For now, 4 total runners is a sensible default.
[514d561]833 if (!multithreaded) {
834 fibril_test_spawn_runners(3);
835 }
[c124c985]836}
837
838/**
839 * Detach a fibril.
840 */
841void fibril_detach(fid_t f)
842{
843 // TODO: Currently all fibrils are detached by default, but they
844 // won't always be. Code that explicitly spawns fibrils with
845 // limited lifetime should call this function.
846}
847
[514d561]848/**
849 * Exit a fibril. Never returns.
850 *
851 * @param retval Value to return from fibril_join() called on this fibril.
852 */
853_Noreturn void fibril_exit(long retval)
854{
855 // TODO: implement fibril_join() and remember retval
856 (void) retval;
857
858 fibril_t *f = _ready_list_pop_nonblocking(false);
859 if (!f)
860 f = fibril_self()->thread_ctx;
861
862 _fibril_switch_to(SWITCH_FROM_DEAD, f, false);
863 __builtin_unreachable();
864}
865
866void __fibrils_init(void)
867{
[45c8eea]868 if (futex_initialize(&fibril_futex, 1) != EOK)
869 abort();
870 if (futex_initialize(&ipc_lists_futex, 1) != EOK)
871 abort();
872
[514d561]873 /*
874 * We allow a fixed, small amount of parallelism for IPC reads, but
875 * since IPC is currently serialized in kernel, there's not much
876 * we can get from more threads reading messages.
877 */
878
879#define IPC_BUFFER_COUNT 1024
880 static _ipc_buffer_t buffers[IPC_BUFFER_COUNT];
881
882 for (int i = 0; i < IPC_BUFFER_COUNT; i++) {
883 list_append(&buffers[i].link, &ipc_buffer_free_list);
[05208d9]884 _ready_up();
[514d561]885 }
886}
887
[25f6bddb]888void __fibrils_fini(void)
889{
890 futex_destroy(&fibril_futex);
891 futex_destroy(&ipc_lists_futex);
892}
893
[bd41ac52]894void fibril_usleep(usec_t timeout)
[514d561]895{
[bd41ac52]896 struct timespec expires;
[514d561]897 getuptime(&expires);
[bd41ac52]898 ts_add_diff(&expires, USEC2NSEC(timeout));
[514d561]899
900 fibril_event_t event = FIBRIL_EVENT_INIT;
901 fibril_wait_timeout(&event, &expires);
902}
903
[bd41ac52]904void fibril_sleep(sec_t sec)
[514d561]905{
[bd41ac52]906 struct timespec expires;
[514d561]907 getuptime(&expires);
908 expires.tv_sec += sec;
909
910 fibril_event_t event = FIBRIL_EVENT_INIT;
911 fibril_wait_timeout(&event, &expires);
912}
913
914void fibril_ipc_poke(void)
915{
916 DPRINTF("Poking.\n");
917 /* Wakeup one thread sleeping in SYS_IPC_WAIT. */
918 ipc_poke();
919}
920
[bd41ac52]921errno_t fibril_ipc_wait(ipc_call_t *call, const struct timespec *expires)
[514d561]922{
923 return _wait_ipc(call, expires);
924}
925
[bc1f1c2]926/** @}
927 */
Note: See TracBrowser for help on using the repository browser.