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