[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>
|
---|
[bc1f1c2] | 41 | #include <malloc.h>
|
---|
[1107050] | 42 | #include <abi/mm/as.h>
|
---|
| 43 | #include <as.h>
|
---|
[bc1f1c2] | 44 | #include <unistd.h>
|
---|
| 45 | #include <stdio.h>
|
---|
[c0699467] | 46 | #include <libarch/barrier.h>
|
---|
[bc1f1c2] | 47 | #include <libarch/faddr.h>
|
---|
| 48 | #include <futex.h>
|
---|
| 49 | #include <assert.h>
|
---|
| 50 | #include <async.h>
|
---|
[927a181e] | 51 | #include <futex.h>
|
---|
[bc1f1c2] | 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,
|
---|
| 59 | * serialized_list and manager_list.
|
---|
| 60 | */
|
---|
[927a181e] | 61 | static futex_t fibril_futex = FUTEX_INITIALIZER;
|
---|
[12f91130] | 62 |
|
---|
[bc1f1c2] | 63 | static LIST_INITIALIZE(ready_list);
|
---|
| 64 | static LIST_INITIALIZE(serialized_list);
|
---|
| 65 | static LIST_INITIALIZE(manager_list);
|
---|
[c1b979a] | 66 | static LIST_INITIALIZE(fibril_list);
|
---|
[bc1f1c2] | 67 |
|
---|
[cc27c8c5] | 68 | /** Number of threads that are executing a manager fibril. */
|
---|
| 69 | static int threads_in_manager;
|
---|
[596d65c] | 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Number of threads that are executing a manager fibril
|
---|
| 73 | * and are serialized. Protected by async_futex.
|
---|
| 74 | */
|
---|
| 75 | static int serialized_threads;
|
---|
| 76 |
|
---|
[26360f7] | 77 | /** Fibril-local count of serialization. If > 0, we must not preempt */
|
---|
| 78 | static fibril_local int serialization_count;
|
---|
[bc1f1c2] | 79 |
|
---|
[596d65c] | 80 | /** Function that spans the whole life-cycle of a fibril.
|
---|
| 81 | *
|
---|
| 82 | * Each fibril begins execution in this function. Then the function implementing
|
---|
| 83 | * the fibril logic is called. After its return, the return value is saved.
|
---|
| 84 | * The fibril then switches to another fibril, which cleans up after it.
|
---|
| 85 | *
|
---|
| 86 | */
|
---|
| 87 | static void fibril_main(void)
|
---|
[bc1f1c2] | 88 | {
|
---|
[596d65c] | 89 | fibril_t *fibril = __tcb_get()->fibril_data;
|
---|
[d54b303] | 90 |
|
---|
[6c1bb0d] | 91 | #ifdef FUTEX_UPGRADABLE
|
---|
[d54b303] | 92 | rcu_register_fibril();
|
---|
| 93 | #endif
|
---|
[596d65c] | 94 |
|
---|
| 95 | /* Call the implementing function. */
|
---|
| 96 | fibril->retval = fibril->func(fibril->arg);
|
---|
| 97 |
|
---|
| 98 | fibril_switch(FIBRIL_FROM_DEAD);
|
---|
| 99 | /* Not reached */
|
---|
| 100 | }
|
---|
[bc1f1c2] | 101 |
|
---|
[596d65c] | 102 | /** Setup fibril information into TCB structure
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
| 105 | fibril_t *fibril_setup(void)
|
---|
| 106 | {
|
---|
[31399f3] | 107 | tcb_t *tcb = tls_make();
|
---|
[bc1f1c2] | 108 | if (!tcb)
|
---|
| 109 | return NULL;
|
---|
[596d65c] | 110 |
|
---|
| 111 | fibril_t *fibril = malloc(sizeof(fibril_t));
|
---|
| 112 | if (!fibril) {
|
---|
[31399f3] | 113 | tls_free(tcb);
|
---|
[bc1f1c2] | 114 | return NULL;
|
---|
| 115 | }
|
---|
[596d65c] | 116 |
|
---|
| 117 | tcb->fibril_data = fibril;
|
---|
| 118 | fibril->tcb = tcb;
|
---|
| 119 |
|
---|
| 120 | fibril->func = NULL;
|
---|
| 121 | fibril->arg = NULL;
|
---|
| 122 | fibril->stack = NULL;
|
---|
| 123 | fibril->clean_after_me = NULL;
|
---|
| 124 | fibril->retval = 0;
|
---|
| 125 | fibril->flags = 0;
|
---|
| 126 |
|
---|
[8cf6709] | 127 | fibril->waits_for = NULL;
|
---|
[c1b979a] | 128 | list_append(&fibril->all_link, &fibril_list);
|
---|
[8cf6709] | 129 |
|
---|
[596d65c] | 130 | return fibril;
|
---|
[bc1f1c2] | 131 | }
|
---|
| 132 |
|
---|
[596d65c] | 133 | void fibril_teardown(fibril_t *fibril)
|
---|
[bc1f1c2] | 134 | {
|
---|
[c1b979a] | 135 | list_remove(&fibril->all_link);
|
---|
[31399f3] | 136 | tls_free(fibril->tcb);
|
---|
[596d65c] | 137 | free(fibril);
|
---|
[bc1f1c2] | 138 | }
|
---|
| 139 |
|
---|
[116d3f6f] | 140 | /** Switch from the current fibril.
|
---|
[bc1f1c2] | 141 | *
|
---|
| 142 | * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
|
---|
| 143 | * held.
|
---|
| 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 | {
|
---|
| 155 | int retval = 0;
|
---|
| 156 |
|
---|
[df7cbc6] | 157 | futex_lock(&fibril_futex);
|
---|
[36e9cd1] | 158 |
|
---|
[bc1f1c2] | 159 | if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
|
---|
| 160 | goto ret_0;
|
---|
[36e9cd1] | 161 |
|
---|
[bc1f1c2] | 162 | if (stype == FIBRIL_FROM_MANAGER) {
|
---|
[36e9cd1] | 163 | if ((list_empty(&ready_list)) && (list_empty(&serialized_list)))
|
---|
[bc1f1c2] | 164 | goto ret_0;
|
---|
[36e9cd1] | 165 |
|
---|
[bc1f1c2] | 166 | /*
|
---|
[cc27c8c5] | 167 | * Do not preempt if there is not enough threads to run the
|
---|
[bd8bfcbd] | 168 | * ready fibrils which are not serialized.
|
---|
[bc1f1c2] | 169 | */
|
---|
[36e9cd1] | 170 | if ((list_empty(&serialized_list)) &&
|
---|
| 171 | (threads_in_manager <= serialized_threads)) {
|
---|
[bc1f1c2] | 172 | goto ret_0;
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[36e9cd1] | 175 |
|
---|
[bc1f1c2] | 176 | /* If we are going to manager and none exists, create it */
|
---|
[36e9cd1] | 177 | if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
|
---|
[bc1f1c2] | 178 | while (list_empty(&manager_list)) {
|
---|
[df7cbc6] | 179 | futex_unlock(&fibril_futex);
|
---|
[bc1f1c2] | 180 | async_create_manager();
|
---|
[df7cbc6] | 181 | futex_lock(&fibril_futex);
|
---|
[bc1f1c2] | 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[36e9cd1] | 185 | fibril_t *srcf = __tcb_get()->fibril_data;
|
---|
[bc1f1c2] | 186 | if (stype != FIBRIL_FROM_DEAD) {
|
---|
[36e9cd1] | 187 |
|
---|
[bc1f1c2] | 188 | /* Save current state */
|
---|
| 189 | if (!context_save(&srcf->ctx)) {
|
---|
| 190 | if (serialization_count)
|
---|
| 191 | srcf->flags &= ~FIBRIL_SERIALIZED;
|
---|
[36e9cd1] | 192 |
|
---|
[bc1f1c2] | 193 | if (srcf->clean_after_me) {
|
---|
| 194 | /*
|
---|
| 195 | * Cleanup after the dead fibril from which we
|
---|
| 196 | * restored context here.
|
---|
| 197 | */
|
---|
[36e9cd1] | 198 | void *stack = srcf->clean_after_me->stack;
|
---|
[116d3f6f] | 199 | if (stack) {
|
---|
| 200 | /*
|
---|
| 201 | * This check is necessary because a
|
---|
| 202 | * thread could have exited like a
|
---|
| 203 | * normal fibril using the
|
---|
| 204 | * FIBRIL_FROM_DEAD switch type. In that
|
---|
| 205 | * case, its fibril will not have the
|
---|
| 206 | * stack member filled.
|
---|
| 207 | */
|
---|
[1107050] | 208 | as_area_destroy(stack);
|
---|
[116d3f6f] | 209 | }
|
---|
[bc1f1c2] | 210 | fibril_teardown(srcf->clean_after_me);
|
---|
| 211 | srcf->clean_after_me = NULL;
|
---|
| 212 | }
|
---|
[36e9cd1] | 213 |
|
---|
[df7cbc6] | 214 | return 1; /* futex_unlock already done here */
|
---|
[bc1f1c2] | 215 | }
|
---|
[36e9cd1] | 216 |
|
---|
[bc1f1c2] | 217 | /* Save myself to the correct run list */
|
---|
| 218 | if (stype == FIBRIL_PREEMPT)
|
---|
| 219 | list_append(&srcf->link, &ready_list);
|
---|
| 220 | else if (stype == FIBRIL_FROM_MANAGER) {
|
---|
| 221 | list_append(&srcf->link, &manager_list);
|
---|
[cc27c8c5] | 222 | threads_in_manager--;
|
---|
[36e9cd1] | 223 | } else {
|
---|
[bc1f1c2] | 224 | /*
|
---|
| 225 | * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
|
---|
| 226 | * any list, we should already be somewhere, or we will
|
---|
| 227 | * be lost.
|
---|
| 228 | */
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[116d3f6f] | 231 |
|
---|
[bc1f1c2] | 232 | /* Choose a new fibril to run */
|
---|
[36e9cd1] | 233 | fibril_t *dstf;
|
---|
| 234 | if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
|
---|
[b72efe8] | 235 | dstf = list_get_instance(list_first(&manager_list), fibril_t,
|
---|
| 236 | link);
|
---|
[bc1f1c2] | 237 | if (serialization_count && stype == FIBRIL_TO_MANAGER) {
|
---|
[cc27c8c5] | 238 | serialized_threads++;
|
---|
[bc1f1c2] | 239 | srcf->flags |= FIBRIL_SERIALIZED;
|
---|
| 240 | }
|
---|
[cc27c8c5] | 241 | threads_in_manager++;
|
---|
[36e9cd1] | 242 |
|
---|
[116d3f6f] | 243 | if (stype == FIBRIL_FROM_DEAD)
|
---|
[bc1f1c2] | 244 | dstf->clean_after_me = srcf;
|
---|
| 245 | } else {
|
---|
| 246 | if (!list_empty(&serialized_list)) {
|
---|
[b72efe8] | 247 | dstf = list_get_instance(list_first(&serialized_list),
|
---|
| 248 | fibril_t, link);
|
---|
[cc27c8c5] | 249 | serialized_threads--;
|
---|
[bc1f1c2] | 250 | } else {
|
---|
[b72efe8] | 251 | dstf = list_get_instance(list_first(&ready_list),
|
---|
| 252 | fibril_t, link);
|
---|
[bc1f1c2] | 253 | }
|
---|
| 254 | }
|
---|
| 255 | list_remove(&dstf->link);
|
---|
[36e9cd1] | 256 |
|
---|
[df7cbc6] | 257 | futex_unlock(&fibril_futex);
|
---|
[d54b303] | 258 |
|
---|
[6c1bb0d] | 259 | #ifdef FUTEX_UPGRADABLE
|
---|
[d54b303] | 260 | if (stype == FIBRIL_FROM_DEAD) {
|
---|
| 261 | rcu_deregister_fibril();
|
---|
| 262 | }
|
---|
| 263 | #endif
|
---|
| 264 |
|
---|
[bc1f1c2] | 265 | context_restore(&dstf->ctx);
|
---|
| 266 | /* not reached */
|
---|
[36e9cd1] | 267 |
|
---|
[bc1f1c2] | 268 | ret_0:
|
---|
[df7cbc6] | 269 | futex_unlock(&fibril_futex);
|
---|
[bc1f1c2] | 270 | return retval;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /** Create a new fibril.
|
---|
| 274 | *
|
---|
[596d65c] | 275 | * @param func Implementing function of the new fibril.
|
---|
| 276 | * @param arg Argument to pass to func.
|
---|
[eceff5f] | 277 | * @param stksz Stack size in bytes.
|
---|
[596d65c] | 278 | *
|
---|
| 279 | * @return 0 on failure or TLS of the new fibril.
|
---|
[bc1f1c2] | 280 | *
|
---|
| 281 | */
|
---|
[b4df8db] | 282 | fid_t fibril_create_generic(int (*func)(void *), void *arg, size_t stksz)
|
---|
[bc1f1c2] | 283 | {
|
---|
[596d65c] | 284 | fibril_t *fibril;
|
---|
| 285 |
|
---|
| 286 | fibril = fibril_setup();
|
---|
| 287 | if (fibril == NULL)
|
---|
[bc1f1c2] | 288 | return 0;
|
---|
[596d65c] | 289 |
|
---|
[eceff5f] | 290 | size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
|
---|
| 291 | stack_size_get() : stksz;
|
---|
[0aae87a6] | 292 | fibril->stack = as_area_create((void *) -1, stack_size,
|
---|
[1107050] | 293 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
|
---|
[5892ec1] | 294 | AS_AREA_LATE_RESERVE);
|
---|
[1107050] | 295 | if (fibril->stack == (void *) -1) {
|
---|
[596d65c] | 296 | fibril_teardown(fibril);
|
---|
[bc1f1c2] | 297 | return 0;
|
---|
| 298 | }
|
---|
[116d3f6f] | 299 |
|
---|
[596d65c] | 300 | fibril->func = func;
|
---|
| 301 | fibril->arg = arg;
|
---|
[7f122e3] | 302 |
|
---|
[596d65c] | 303 | context_save(&fibril->ctx);
|
---|
| 304 | context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
|
---|
[0aae87a6] | 305 | stack_size, fibril->tcb);
|
---|
[bc1f1c2] | 306 |
|
---|
[596d65c] | 307 | return (fid_t) fibril;
|
---|
[bc1f1c2] | 308 | }
|
---|
| 309 |
|
---|
[32d19f7] | 310 | /** Delete a fibril that has never run.
|
---|
| 311 | *
|
---|
| 312 | * Free resources of a fibril that has been created with fibril_create()
|
---|
| 313 | * but never readied using fibril_add_ready().
|
---|
| 314 | *
|
---|
| 315 | * @param fid Pointer to the fibril structure of the fibril to be
|
---|
| 316 | * added.
|
---|
| 317 | */
|
---|
| 318 | void fibril_destroy(fid_t fid)
|
---|
| 319 | {
|
---|
| 320 | fibril_t *fibril = (fibril_t *) fid;
|
---|
| 321 |
|
---|
[1107050] | 322 | as_area_destroy(fibril->stack);
|
---|
[32d19f7] | 323 | fibril_teardown(fibril);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[bc1f1c2] | 326 | /** Add a fibril to the ready list.
|
---|
| 327 | *
|
---|
[596d65c] | 328 | * @param fid Pointer to the fibril structure of the fibril to be
|
---|
| 329 | * added.
|
---|
| 330 | *
|
---|
[bc1f1c2] | 331 | */
|
---|
| 332 | void fibril_add_ready(fid_t fid)
|
---|
| 333 | {
|
---|
[596d65c] | 334 | fibril_t *fibril = (fibril_t *) fid;
|
---|
| 335 |
|
---|
[df7cbc6] | 336 | futex_lock(&fibril_futex);
|
---|
[596d65c] | 337 |
|
---|
| 338 | if ((fibril->flags & FIBRIL_SERIALIZED))
|
---|
| 339 | list_append(&fibril->link, &serialized_list);
|
---|
[bc1f1c2] | 340 | else
|
---|
[596d65c] | 341 | list_append(&fibril->link, &ready_list);
|
---|
| 342 |
|
---|
[df7cbc6] | 343 | futex_unlock(&fibril_futex);
|
---|
[bc1f1c2] | 344 | }
|
---|
| 345 |
|
---|
| 346 | /** Add a fibril to the manager list.
|
---|
| 347 | *
|
---|
[596d65c] | 348 | * @param fid Pointer to the fibril structure of the fibril to be
|
---|
| 349 | * added.
|
---|
| 350 | *
|
---|
[bc1f1c2] | 351 | */
|
---|
| 352 | void fibril_add_manager(fid_t fid)
|
---|
| 353 | {
|
---|
[596d65c] | 354 | fibril_t *fibril = (fibril_t *) fid;
|
---|
| 355 |
|
---|
[df7cbc6] | 356 | futex_lock(&fibril_futex);
|
---|
[596d65c] | 357 | list_append(&fibril->link, &manager_list);
|
---|
[df7cbc6] | 358 | futex_unlock(&fibril_futex);
|
---|
[bc1f1c2] | 359 | }
|
---|
| 360 |
|
---|
| 361 | /** Remove one manager from the manager list. */
|
---|
| 362 | void fibril_remove_manager(void)
|
---|
| 363 | {
|
---|
[df7cbc6] | 364 | futex_lock(&fibril_futex);
|
---|
[596d65c] | 365 |
|
---|
| 366 | if (!list_empty(&manager_list))
|
---|
[b72efe8] | 367 | list_remove(list_first(&manager_list));
|
---|
[596d65c] | 368 |
|
---|
[df7cbc6] | 369 | futex_unlock(&fibril_futex);
|
---|
[bc1f1c2] | 370 | }
|
---|
| 371 |
|
---|
| 372 | /** Return fibril id of the currently running fibril.
|
---|
| 373 | *
|
---|
[3562ec82] | 374 | * @return fibril ID of the currently running fibril.
|
---|
| 375 | *
|
---|
[bc1f1c2] | 376 | */
|
---|
| 377 | fid_t fibril_get_id(void)
|
---|
| 378 | {
|
---|
| 379 | return (fid_t) __tcb_get()->fibril_data;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[3562ec82] | 382 | /** Disable preemption
|
---|
[bc1f1c2] | 383 | *
|
---|
| 384 | * If the fibril wants to send several message in a row and does not want to be
|
---|
| 385 | * preempted, it should start async_serialize_start() in the beginning of
|
---|
| 386 | * communication and async_serialize_end() in the end. If it is a true
|
---|
| 387 | * multithreaded application, it should protect the communication channel by a
|
---|
[3562ec82] | 388 | * futex as well.
|
---|
| 389 | *
|
---|
[bc1f1c2] | 390 | */
|
---|
| 391 | void fibril_inc_sercount(void)
|
---|
| 392 | {
|
---|
| 393 | serialization_count++;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /** Restore the preemption counter to the previous state. */
|
---|
| 397 | void fibril_dec_sercount(void)
|
---|
| 398 | {
|
---|
| 399 | serialization_count--;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[2e7291a] | 402 | int fibril_get_sercount(void)
|
---|
| 403 | {
|
---|
| 404 | return serialization_count;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[bc1f1c2] | 407 | /** @}
|
---|
| 408 | */
|
---|