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