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