| 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 <tls.h>
|
|---|
| 40 | #include <malloc.h>
|
|---|
| 41 | #include <unistd.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <arch/barrier.h>
|
|---|
| 44 | #include <libarch/faddr.h>
|
|---|
| 45 | #include <futex.h>
|
|---|
| 46 | #include <assert.h>
|
|---|
| 47 | #include <async.h>
|
|---|
| 48 |
|
|---|
| 49 | #ifndef FIBRIL_INITIAL_STACK_PAGES_NO
|
|---|
| 50 | #define FIBRIL_INITIAL_STACK_PAGES_NO 1
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * This futex serializes access to ready_list,
|
|---|
| 55 | * serialized_list and manager_list.
|
|---|
| 56 | */
|
|---|
| 57 | static atomic_t fibril_futex = FUTEX_INITIALIZER;
|
|---|
| 58 |
|
|---|
| 59 | static LIST_INITIALIZE(ready_list);
|
|---|
| 60 | static LIST_INITIALIZE(serialized_list);
|
|---|
| 61 | static LIST_INITIALIZE(manager_list);
|
|---|
| 62 |
|
|---|
| 63 | /** Number of threads that are executing a manager fibril. */
|
|---|
| 64 | static int threads_in_manager;
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Number of threads that are executing a manager fibril
|
|---|
| 68 | * and are serialized. Protected by async_futex.
|
|---|
| 69 | */
|
|---|
| 70 | static int serialized_threads;
|
|---|
| 71 |
|
|---|
| 72 | /** Fibril-local count of serialization. If > 0, we must not preempt */
|
|---|
| 73 | static fibril_local int serialization_count;
|
|---|
| 74 |
|
|---|
| 75 | /** Function that spans the whole life-cycle of a fibril.
|
|---|
| 76 | *
|
|---|
| 77 | * Each fibril begins execution in this function. Then the function implementing
|
|---|
| 78 | * the fibril logic is called. After its return, the return value is saved.
|
|---|
| 79 | * The fibril then switches to another fibril, which cleans up after it.
|
|---|
| 80 | *
|
|---|
| 81 | */
|
|---|
| 82 | static void fibril_main(void)
|
|---|
| 83 | {
|
|---|
| 84 | fibril_t *fibril = __tcb_get()->fibril_data;
|
|---|
| 85 |
|
|---|
| 86 | /* Call the implementing function. */
|
|---|
| 87 | fibril->retval = fibril->func(fibril->arg);
|
|---|
| 88 |
|
|---|
| 89 | fibril_switch(FIBRIL_FROM_DEAD);
|
|---|
| 90 | /* Not reached */
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /** Setup fibril information into TCB structure
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 | fibril_t *fibril_setup(void)
|
|---|
| 97 | {
|
|---|
| 98 | tcb_t *tcb = __make_tls();
|
|---|
| 99 | if (!tcb)
|
|---|
| 100 | return NULL;
|
|---|
| 101 |
|
|---|
| 102 | fibril_t *fibril = malloc(sizeof(fibril_t));
|
|---|
| 103 | if (!fibril) {
|
|---|
| 104 | __free_tls(tcb);
|
|---|
| 105 | return NULL;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | tcb->fibril_data = fibril;
|
|---|
| 109 | fibril->tcb = tcb;
|
|---|
| 110 |
|
|---|
| 111 | fibril->func = NULL;
|
|---|
| 112 | fibril->arg = NULL;
|
|---|
| 113 | fibril->stack = NULL;
|
|---|
| 114 | fibril->clean_after_me = NULL;
|
|---|
| 115 | fibril->retval = 0;
|
|---|
| 116 | fibril->flags = 0;
|
|---|
| 117 |
|
|---|
| 118 | fibril->waits_for = NULL;
|
|---|
| 119 |
|
|---|
| 120 | return fibril;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void fibril_teardown(fibril_t *fibril)
|
|---|
| 124 | {
|
|---|
| 125 | __free_tls(fibril->tcb);
|
|---|
| 126 | free(fibril);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Switch from the current fibril.
|
|---|
| 130 | *
|
|---|
| 131 | * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
|
|---|
| 132 | * held.
|
|---|
| 133 | *
|
|---|
| 134 | * @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
|
|---|
| 135 | * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
|
|---|
| 136 | * describes the circumstances of the switch.
|
|---|
| 137 | *
|
|---|
| 138 | * @return 0 if there is no ready fibril,
|
|---|
| 139 | * @return 1 otherwise.
|
|---|
| 140 | *
|
|---|
| 141 | */
|
|---|
| 142 | int fibril_switch(fibril_switch_type_t stype)
|
|---|
| 143 | {
|
|---|
| 144 | int retval = 0;
|
|---|
| 145 |
|
|---|
| 146 | futex_down(&fibril_futex);
|
|---|
| 147 |
|
|---|
| 148 | if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
|
|---|
| 149 | goto ret_0;
|
|---|
| 150 |
|
|---|
| 151 | if (stype == FIBRIL_FROM_MANAGER) {
|
|---|
| 152 | if ((list_empty(&ready_list)) && (list_empty(&serialized_list)))
|
|---|
| 153 | goto ret_0;
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * Do not preempt if there is not enough threads to run the
|
|---|
| 157 | * ready fibrils which are not serialized.
|
|---|
| 158 | */
|
|---|
| 159 | if ((list_empty(&serialized_list)) &&
|
|---|
| 160 | (threads_in_manager <= serialized_threads)) {
|
|---|
| 161 | goto ret_0;
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /* If we are going to manager and none exists, create it */
|
|---|
| 166 | if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
|
|---|
| 167 | while (list_empty(&manager_list)) {
|
|---|
| 168 | futex_up(&fibril_futex);
|
|---|
| 169 | async_create_manager();
|
|---|
| 170 | futex_down(&fibril_futex);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | fibril_t *srcf = __tcb_get()->fibril_data;
|
|---|
| 175 | if (stype != FIBRIL_FROM_DEAD) {
|
|---|
| 176 |
|
|---|
| 177 | /* Save current state */
|
|---|
| 178 | if (!context_save(&srcf->ctx)) {
|
|---|
| 179 | if (serialization_count)
|
|---|
| 180 | srcf->flags &= ~FIBRIL_SERIALIZED;
|
|---|
| 181 |
|
|---|
| 182 | if (srcf->clean_after_me) {
|
|---|
| 183 | /*
|
|---|
| 184 | * Cleanup after the dead fibril from which we
|
|---|
| 185 | * restored context here.
|
|---|
| 186 | */
|
|---|
| 187 | void *stack = srcf->clean_after_me->stack;
|
|---|
| 188 | if (stack) {
|
|---|
| 189 | /*
|
|---|
| 190 | * This check is necessary because a
|
|---|
| 191 | * thread could have exited like a
|
|---|
| 192 | * normal fibril using the
|
|---|
| 193 | * FIBRIL_FROM_DEAD switch type. In that
|
|---|
| 194 | * case, its fibril will not have the
|
|---|
| 195 | * stack member filled.
|
|---|
| 196 | */
|
|---|
| 197 | free(stack);
|
|---|
| 198 | }
|
|---|
| 199 | fibril_teardown(srcf->clean_after_me);
|
|---|
| 200 | srcf->clean_after_me = NULL;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | return 1; /* futex_up already done here */
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /* Save myself to the correct run list */
|
|---|
| 207 | if (stype == FIBRIL_PREEMPT)
|
|---|
| 208 | list_append(&srcf->link, &ready_list);
|
|---|
| 209 | else if (stype == FIBRIL_FROM_MANAGER) {
|
|---|
| 210 | list_append(&srcf->link, &manager_list);
|
|---|
| 211 | threads_in_manager--;
|
|---|
| 212 | } else {
|
|---|
| 213 | /*
|
|---|
| 214 | * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
|
|---|
| 215 | * any list, we should already be somewhere, or we will
|
|---|
| 216 | * be lost.
|
|---|
| 217 | */
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /* Choose a new fibril to run */
|
|---|
| 222 | fibril_t *dstf;
|
|---|
| 223 | if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
|
|---|
| 224 | dstf = list_get_instance(manager_list.next, fibril_t, link);
|
|---|
| 225 | if (serialization_count && stype == FIBRIL_TO_MANAGER) {
|
|---|
| 226 | serialized_threads++;
|
|---|
| 227 | srcf->flags |= FIBRIL_SERIALIZED;
|
|---|
| 228 | }
|
|---|
| 229 | threads_in_manager++;
|
|---|
| 230 |
|
|---|
| 231 | if (stype == FIBRIL_FROM_DEAD)
|
|---|
| 232 | dstf->clean_after_me = srcf;
|
|---|
| 233 | } else {
|
|---|
| 234 | if (!list_empty(&serialized_list)) {
|
|---|
| 235 | dstf = list_get_instance(serialized_list.next, fibril_t,
|
|---|
| 236 | link);
|
|---|
| 237 | serialized_threads--;
|
|---|
| 238 | } else {
|
|---|
| 239 | dstf = list_get_instance(ready_list.next, fibril_t,
|
|---|
| 240 | link);
|
|---|
| 241 | }
|
|---|
| 242 | }
|
|---|
| 243 | list_remove(&dstf->link);
|
|---|
| 244 |
|
|---|
| 245 | futex_up(&fibril_futex);
|
|---|
| 246 | context_restore(&dstf->ctx);
|
|---|
| 247 | /* not reached */
|
|---|
| 248 |
|
|---|
| 249 | ret_0:
|
|---|
| 250 | futex_up(&fibril_futex);
|
|---|
| 251 | return retval;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** Create a new fibril.
|
|---|
| 255 | *
|
|---|
| 256 | * @param func Implementing function of the new fibril.
|
|---|
| 257 | * @param arg Argument to pass to func.
|
|---|
| 258 | *
|
|---|
| 259 | * @return 0 on failure or TLS of the new fibril.
|
|---|
| 260 | *
|
|---|
| 261 | */
|
|---|
| 262 | fid_t fibril_create(int (*func)(void *), void *arg)
|
|---|
| 263 | {
|
|---|
| 264 | fibril_t *fibril;
|
|---|
| 265 |
|
|---|
| 266 | fibril = fibril_setup();
|
|---|
| 267 | if (fibril == NULL)
|
|---|
| 268 | return 0;
|
|---|
| 269 |
|
|---|
| 270 | fibril->stack =
|
|---|
| 271 | (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize());
|
|---|
| 272 | if (!fibril->stack) {
|
|---|
| 273 | fibril_teardown(fibril);
|
|---|
| 274 | return 0;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | fibril->func = func;
|
|---|
| 278 | fibril->arg = arg;
|
|---|
| 279 |
|
|---|
| 280 | context_save(&fibril->ctx);
|
|---|
| 281 | context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
|
|---|
| 282 | FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), fibril->tcb);
|
|---|
| 283 |
|
|---|
| 284 | return (fid_t) fibril;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Add a fibril to the ready list.
|
|---|
| 288 | *
|
|---|
| 289 | * @param fid Pointer to the fibril structure of the fibril to be
|
|---|
| 290 | * added.
|
|---|
| 291 | *
|
|---|
| 292 | */
|
|---|
| 293 | void fibril_add_ready(fid_t fid)
|
|---|
| 294 | {
|
|---|
| 295 | fibril_t *fibril = (fibril_t *) fid;
|
|---|
| 296 |
|
|---|
| 297 | futex_down(&fibril_futex);
|
|---|
| 298 |
|
|---|
| 299 | if ((fibril->flags & FIBRIL_SERIALIZED))
|
|---|
| 300 | list_append(&fibril->link, &serialized_list);
|
|---|
| 301 | else
|
|---|
| 302 | list_append(&fibril->link, &ready_list);
|
|---|
| 303 |
|
|---|
| 304 | futex_up(&fibril_futex);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /** Add a fibril to the manager list.
|
|---|
| 308 | *
|
|---|
| 309 | * @param fid Pointer to the fibril structure of the fibril to be
|
|---|
| 310 | * added.
|
|---|
| 311 | *
|
|---|
| 312 | */
|
|---|
| 313 | void fibril_add_manager(fid_t fid)
|
|---|
| 314 | {
|
|---|
| 315 | fibril_t *fibril = (fibril_t *) fid;
|
|---|
| 316 |
|
|---|
| 317 | futex_down(&fibril_futex);
|
|---|
| 318 | list_append(&fibril->link, &manager_list);
|
|---|
| 319 | futex_up(&fibril_futex);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /** Remove one manager from the manager list. */
|
|---|
| 323 | void fibril_remove_manager(void)
|
|---|
| 324 | {
|
|---|
| 325 | futex_down(&fibril_futex);
|
|---|
| 326 |
|
|---|
| 327 | if (!list_empty(&manager_list))
|
|---|
| 328 | list_remove(manager_list.next);
|
|---|
| 329 |
|
|---|
| 330 | futex_up(&fibril_futex);
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /** Return fibril id of the currently running fibril.
|
|---|
| 334 | *
|
|---|
| 335 | * @return fibril ID of the currently running fibril.
|
|---|
| 336 | *
|
|---|
| 337 | */
|
|---|
| 338 | fid_t fibril_get_id(void)
|
|---|
| 339 | {
|
|---|
| 340 | return (fid_t) __tcb_get()->fibril_data;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /** Disable preemption
|
|---|
| 344 | *
|
|---|
| 345 | * If the fibril wants to send several message in a row and does not want to be
|
|---|
| 346 | * preempted, it should start async_serialize_start() in the beginning of
|
|---|
| 347 | * communication and async_serialize_end() in the end. If it is a true
|
|---|
| 348 | * multithreaded application, it should protect the communication channel by a
|
|---|
| 349 | * futex as well.
|
|---|
| 350 | *
|
|---|
| 351 | */
|
|---|
| 352 | void fibril_inc_sercount(void)
|
|---|
| 353 | {
|
|---|
| 354 | serialization_count++;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /** Restore the preemption counter to the previous state. */
|
|---|
| 358 | void fibril_dec_sercount(void)
|
|---|
| 359 | {
|
|---|
| 360 | serialization_count--;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | int fibril_get_sercount(void)
|
|---|
| 364 | {
|
|---|
| 365 | return serialization_count;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | /** @}
|
|---|
| 369 | */
|
|---|