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