source: mainline/uspace/lib/c/generic/fibril.c@ d73d992

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d73d992 was d73d992, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Hide libc-internal details of the fibril implementation.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[bc1f1c2]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
[d9c8c81]36#include <adt/list.h>
[bc1f1c2]37#include <fibril.h>
[fa23560]38#include <thread.h>
[0aae87a6]39#include <stack.h>
[fa23560]40#include <tls.h>
[38d150e]41#include <stdlib.h>
[1107050]42#include <abi/mm/as.h>
43#include <as.h>
[bc1f1c2]44#include <stdio.h>
[c0699467]45#include <libarch/barrier.h>
[e0a4686]46#include <context.h>
[bc1f1c2]47#include <futex.h>
48#include <assert.h>
49#include <async.h>
50
[d73d992]51#include "private/fibril.h"
52
[6c1bb0d]53#ifdef FUTEX_UPGRADABLE
[d54b303]54#include <rcu.h>
55#endif
[bc1f1c2]56
[cc27c8c5]57/**
[596d65c]58 * This futex serializes access to ready_list,
[2654afb]59 * manager_list and fibril_list.
[596d65c]60 */
[927a181e]61static futex_t fibril_futex = FUTEX_INITIALIZER;
[12f91130]62
[bc1f1c2]63static LIST_INITIALIZE(ready_list);
64static LIST_INITIALIZE(manager_list);
[c1b979a]65static LIST_INITIALIZE(fibril_list);
[bc1f1c2]66
[596d65c]67/** Function that spans the whole life-cycle of a fibril.
68 *
69 * Each fibril begins execution in this function. Then the function implementing
70 * the fibril logic is called. After its return, the return value is saved.
71 * The fibril then switches to another fibril, which cleans up after it.
72 *
73 */
74static void fibril_main(void)
[bc1f1c2]75{
[899342e]76 /* fibril_futex is locked when a fibril is first started. */
77 futex_unlock(&fibril_futex);
78
[d73d992]79 fibril_t *fibril = fibril_self();
[d54b303]80
[6c1bb0d]81#ifdef FUTEX_UPGRADABLE
[d54b303]82 rcu_register_fibril();
83#endif
[a35b458]84
[596d65c]85 /* Call the implementing function. */
86 fibril->retval = fibril->func(fibril->arg);
[a35b458]87
[c721d26]88 futex_down(&async_futex);
[596d65c]89 fibril_switch(FIBRIL_FROM_DEAD);
90 /* Not reached */
91}
[bc1f1c2]92
[596d65c]93/** Setup fibril information into TCB structure
94 *
95 */
96fibril_t *fibril_setup(void)
97{
[31399f3]98 tcb_t *tcb = tls_make();
[bc1f1c2]99 if (!tcb)
100 return NULL;
[a35b458]101
[d73d992]102 fibril_t *fibril = calloc(1, sizeof(fibril_t));
[596d65c]103 if (!fibril) {
[31399f3]104 tls_free(tcb);
[bc1f1c2]105 return NULL;
106 }
[a35b458]107
[596d65c]108 tcb->fibril_data = fibril;
109 fibril->tcb = tcb;
[a35b458]110
[6d87dce]111 /*
112 * We are called before __tcb_set(), so we need to use
113 * futex_down/up() instead of futex_lock/unlock() that
114 * may attempt to access TLS.
115 */
116 futex_down(&fibril_futex);
[c1b979a]117 list_append(&fibril->all_link, &fibril_list);
[6d87dce]118 futex_up(&fibril_futex);
[a35b458]119
[596d65c]120 return fibril;
[bc1f1c2]121}
122
[4d11204]123void fibril_teardown(fibril_t *fibril, bool locked)
[1b20da0]124{
[4d11204]125 if (!locked)
126 futex_lock(&fibril_futex);
[c1b979a]127 list_remove(&fibril->all_link);
[4d11204]128 if (!locked)
129 futex_unlock(&fibril_futex);
[31399f3]130 tls_free(fibril->tcb);
[596d65c]131 free(fibril);
[bc1f1c2]132}
133
[116d3f6f]134/** Switch from the current fibril.
[bc1f1c2]135 *
[c721d26]136 * If stype is FIBRIL_TO_MANAGER or FIBRIL_FROM_DEAD, the async_futex must
137 * be held.
[bc1f1c2]138 *
[596d65c]139 * @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
140 * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
141 * describes the circumstances of the switch.
142 *
143 * @return 0 if there is no ready fibril,
144 * @return 1 otherwise.
145 *
[bc1f1c2]146 */
[116d3f6f]147int fibril_switch(fibril_switch_type_t stype)
[bc1f1c2]148{
[df7cbc6]149 futex_lock(&fibril_futex);
[c721d26]150
[d73d992]151 fibril_t *srcf = fibril_self();
[e0a4686]152 fibril_t *dstf = NULL;
153
154 /* Choose a new fibril to run */
[2654afb]155 switch (stype) {
156 case FIBRIL_TO_MANAGER:
157 case FIBRIL_FROM_DEAD:
[c721d26]158 /* Make sure the async_futex is held. */
159 assert((atomic_signed_t) async_futex.val.count <= 0);
160
[2654afb]161 /* If we are going to manager and none exists, create it */
[bc1f1c2]162 while (list_empty(&manager_list)) {
[df7cbc6]163 futex_unlock(&fibril_futex);
[bc1f1c2]164 async_create_manager();
[df7cbc6]165 futex_lock(&fibril_futex);
[bc1f1c2]166 }
[a35b458]167
[e0a4686]168 dstf = list_get_instance(list_first(&manager_list),
169 fibril_t, link);
[a35b458]170
[1b20da0]171 if (stype == FIBRIL_FROM_DEAD)
[bc1f1c2]172 dstf->clean_after_me = srcf;
[2654afb]173 break;
[e0a4686]174 case FIBRIL_PREEMPT:
175 case FIBRIL_FROM_MANAGER:
176 if (list_empty(&ready_list)) {
177 futex_unlock(&fibril_futex);
178 return 0;
179 }
180
[2654afb]181 dstf = list_get_instance(list_first(&ready_list), fibril_t,
182 link);
183 break;
[bc1f1c2]184 }
185 list_remove(&dstf->link);
[a35b458]186
[e0a4686]187 /* Put the current fibril into the correct run list */
188 switch (stype) {
189 case FIBRIL_PREEMPT:
190 list_append(&srcf->link, &ready_list);
191 break;
192 case FIBRIL_FROM_MANAGER:
193 list_append(&srcf->link, &manager_list);
194 break;
195 case FIBRIL_FROM_DEAD:
196 // Nothing.
197 break;
198 case FIBRIL_TO_MANAGER:
199 /*
200 * Don't put the current fibril into any list, it should
201 * already be somewhere, or it will be lost.
202 */
203 break;
204 }
205
[6c1bb0d]206#ifdef FUTEX_UPGRADABLE
[d54b303]207 if (stype == FIBRIL_FROM_DEAD) {
208 rcu_deregister_fibril();
209 }
210#endif
[a35b458]211
[e0a4686]212 /* Swap to the next fibril. */
213 context_swap(&srcf->ctx, &dstf->ctx);
214
215 /* Restored by another fibril! */
216
[899342e]217 /* Must be after context_swap()! */
218 futex_unlock(&fibril_futex);
219
[e0a4686]220 if (srcf->clean_after_me) {
221 /*
222 * Cleanup after the dead fibril from which we
223 * restored context here.
224 */
225 void *stack = srcf->clean_after_me->stack;
226 if (stack) {
227 /*
228 * This check is necessary because a
229 * thread could have exited like a
230 * normal fibril using the
231 * FIBRIL_FROM_DEAD switch type. In that
232 * case, its fibril will not have the
233 * stack member filled.
234 */
235 as_area_destroy(stack);
236 }
237 fibril_teardown(srcf->clean_after_me, true);
238 srcf->clean_after_me = NULL;
239 }
240
241 return 1;
[bc1f1c2]242}
243
244/** Create a new fibril.
245 *
[596d65c]246 * @param func Implementing function of the new fibril.
247 * @param arg Argument to pass to func.
[eceff5f]248 * @param stksz Stack size in bytes.
[596d65c]249 *
250 * @return 0 on failure or TLS of the new fibril.
[bc1f1c2]251 *
252 */
[b7fd2a0]253fid_t fibril_create_generic(errno_t (*func)(void *), void *arg, size_t stksz)
[bc1f1c2]254{
[596d65c]255 fibril_t *fibril;
[a35b458]256
[596d65c]257 fibril = fibril_setup();
258 if (fibril == NULL)
[bc1f1c2]259 return 0;
[a35b458]260
[eceff5f]261 size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
262 stack_size_get() : stksz;
[6aeca0d]263 fibril->stack = as_area_create(AS_AREA_ANY, stack_size,
[1107050]264 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
[6aeca0d]265 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
[1107050]266 if (fibril->stack == (void *) -1) {
[4d11204]267 fibril_teardown(fibril, false);
[bc1f1c2]268 return 0;
269 }
[a35b458]270
[596d65c]271 fibril->func = func;
272 fibril->arg = arg;
[7f122e3]273
[e0a4686]274 context_create_t sctx = {
275 .fn = fibril_main,
276 .stack_base = fibril->stack,
277 .stack_size = stack_size,
278 .tls = fibril->tcb,
279 };
[bc1f1c2]280
[e0a4686]281 context_create(&fibril->ctx, &sctx);
[596d65c]282 return (fid_t) fibril;
[bc1f1c2]283}
284
[32d19f7]285/** Delete a fibril that has never run.
286 *
287 * Free resources of a fibril that has been created with fibril_create()
288 * but never readied using fibril_add_ready().
289 *
290 * @param fid Pointer to the fibril structure of the fibril to be
291 * added.
292 */
293void fibril_destroy(fid_t fid)
294{
295 fibril_t *fibril = (fibril_t *) fid;
[a35b458]296
[1107050]297 as_area_destroy(fibril->stack);
[4d11204]298 fibril_teardown(fibril, false);
[32d19f7]299}
300
[bc1f1c2]301/** Add a fibril to the ready list.
302 *
[596d65c]303 * @param fid Pointer to the fibril structure of the fibril to be
304 * added.
305 *
[bc1f1c2]306 */
307void fibril_add_ready(fid_t fid)
308{
[596d65c]309 fibril_t *fibril = (fibril_t *) fid;
[a35b458]310
[df7cbc6]311 futex_lock(&fibril_futex);
[2654afb]312 list_append(&fibril->link, &ready_list);
[df7cbc6]313 futex_unlock(&fibril_futex);
[bc1f1c2]314}
315
316/** Add a fibril to the manager list.
317 *
[596d65c]318 * @param fid Pointer to the fibril structure of the fibril to be
319 * added.
320 *
[bc1f1c2]321 */
322void fibril_add_manager(fid_t fid)
323{
[596d65c]324 fibril_t *fibril = (fibril_t *) fid;
[a35b458]325
[df7cbc6]326 futex_lock(&fibril_futex);
[596d65c]327 list_append(&fibril->link, &manager_list);
[df7cbc6]328 futex_unlock(&fibril_futex);
[bc1f1c2]329}
330
331/** Remove one manager from the manager list. */
332void fibril_remove_manager(void)
333{
[df7cbc6]334 futex_lock(&fibril_futex);
[596d65c]335 if (!list_empty(&manager_list))
[b72efe8]336 list_remove(list_first(&manager_list));
[df7cbc6]337 futex_unlock(&fibril_futex);
[bc1f1c2]338}
339
[d73d992]340fibril_t *fibril_self(void)
341{
342 return __tcb_get()->fibril_data;
343}
344
[bc1f1c2]345/** Return fibril id of the currently running fibril.
346 *
[3562ec82]347 * @return fibril ID of the currently running fibril.
348 *
[bc1f1c2]349 */
350fid_t fibril_get_id(void)
351{
[d73d992]352 return (fid_t) fibril_self();
353}
354
355void fibril_yield(void)
356{
357 fibril_switch(FIBRIL_PREEMPT);
[bc1f1c2]358}
359
360/** @}
361 */
Note: See TracBrowser for help on using the repository browser.