source: mainline/uspace/lib/c/generic/fibril.c@ 9696b01

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9696b01 was c1b979a, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Taskdump printing of fibril stacktraces.

  • Property mode set to 100644
File size: 9.7 KB
Line 
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 <stack.h>
40#include <tls.h>
41#include <malloc.h>
42#include <abi/mm/as.h>
43#include <as.h>
44#include <unistd.h>
45#include <stdio.h>
46#include <libarch/barrier.h>
47#include <libarch/faddr.h>
48#include <futex.h>
49#include <assert.h>
50#include <async.h>
51
52/**
53 * This futex serializes access to ready_list,
54 * serialized_list and manager_list.
55 */
56static atomic_t fibril_futex = FUTEX_INITIALIZER;
57
58static LIST_INITIALIZE(ready_list);
59static LIST_INITIALIZE(serialized_list);
60static LIST_INITIALIZE(manager_list);
61static LIST_INITIALIZE(fibril_list);
62
63/** Number of threads that are executing a manager fibril. */
64static 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 */
70static int serialized_threads;
71
72/** Fibril-local count of serialization. If > 0, we must not preempt */
73static 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 */
82static 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 */
96fibril_t *fibril_setup(void)
97{
98 tcb_t *tcb = tls_make();
99 if (!tcb)
100 return NULL;
101
102 fibril_t *fibril = malloc(sizeof(fibril_t));
103 if (!fibril) {
104 tls_free(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 list_append(&fibril->all_link, &fibril_list);
120
121 return fibril;
122}
123
124void fibril_teardown(fibril_t *fibril)
125{
126 list_remove(&fibril->all_link);
127 tls_free(fibril->tcb);
128 free(fibril);
129}
130
131/** Switch from the current fibril.
132 *
133 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
134 * held.
135 *
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 *
143 */
144int fibril_switch(fibril_switch_type_t stype)
145{
146 int retval = 0;
147
148 futex_down(&fibril_futex);
149
150 if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
151 goto ret_0;
152
153 if (stype == FIBRIL_FROM_MANAGER) {
154 if ((list_empty(&ready_list)) && (list_empty(&serialized_list)))
155 goto ret_0;
156
157 /*
158 * Do not preempt if there is not enough threads to run the
159 * ready fibrils which are not serialized.
160 */
161 if ((list_empty(&serialized_list)) &&
162 (threads_in_manager <= serialized_threads)) {
163 goto ret_0;
164 }
165 }
166
167 /* If we are going to manager and none exists, create it */
168 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
169 while (list_empty(&manager_list)) {
170 futex_up(&fibril_futex);
171 async_create_manager();
172 futex_down(&fibril_futex);
173 }
174 }
175
176 fibril_t *srcf = __tcb_get()->fibril_data;
177 if (stype != FIBRIL_FROM_DEAD) {
178
179 /* Save current state */
180 if (!context_save(&srcf->ctx)) {
181 if (serialization_count)
182 srcf->flags &= ~FIBRIL_SERIALIZED;
183
184 if (srcf->clean_after_me) {
185 /*
186 * Cleanup after the dead fibril from which we
187 * restored context here.
188 */
189 void *stack = srcf->clean_after_me->stack;
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 */
199 as_area_destroy(stack);
200 }
201 fibril_teardown(srcf->clean_after_me);
202 srcf->clean_after_me = NULL;
203 }
204
205 return 1; /* futex_up already done here */
206 }
207
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);
213 threads_in_manager--;
214 } else {
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 }
222
223 /* Choose a new fibril to run */
224 fibril_t *dstf;
225 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
226 dstf = list_get_instance(list_first(&manager_list), fibril_t,
227 link);
228 if (serialization_count && stype == FIBRIL_TO_MANAGER) {
229 serialized_threads++;
230 srcf->flags |= FIBRIL_SERIALIZED;
231 }
232 threads_in_manager++;
233
234 if (stype == FIBRIL_FROM_DEAD)
235 dstf->clean_after_me = srcf;
236 } else {
237 if (!list_empty(&serialized_list)) {
238 dstf = list_get_instance(list_first(&serialized_list),
239 fibril_t, link);
240 serialized_threads--;
241 } else {
242 dstf = list_get_instance(list_first(&ready_list),
243 fibril_t, link);
244 }
245 }
246 list_remove(&dstf->link);
247
248 futex_up(&fibril_futex);
249 context_restore(&dstf->ctx);
250 /* not reached */
251
252ret_0:
253 futex_up(&fibril_futex);
254 return retval;
255}
256
257/** Create a new fibril.
258 *
259 * @param func Implementing function of the new fibril.
260 * @param arg Argument to pass to func.
261 * @param stksz Stack size in bytes.
262 *
263 * @return 0 on failure or TLS of the new fibril.
264 *
265 */
266fid_t fibril_create_generic(int (*func)(void *), void *arg, size_t stksz)
267{
268 fibril_t *fibril;
269
270 fibril = fibril_setup();
271 if (fibril == NULL)
272 return 0;
273
274 size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
275 stack_size_get() : stksz;
276 fibril->stack = as_area_create((void *) -1, stack_size,
277 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
278 AS_AREA_LATE_RESERVE);
279 if (fibril->stack == (void *) -1) {
280 fibril_teardown(fibril);
281 return 0;
282 }
283
284 fibril->func = func;
285 fibril->arg = arg;
286
287 context_save(&fibril->ctx);
288 context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
289 stack_size, fibril->tcb);
290
291 return (fid_t) fibril;
292}
293
294/** Delete a fibril that has never run.
295 *
296 * Free resources of a fibril that has been created with fibril_create()
297 * but never readied using fibril_add_ready().
298 *
299 * @param fid Pointer to the fibril structure of the fibril to be
300 * added.
301 */
302void fibril_destroy(fid_t fid)
303{
304 fibril_t *fibril = (fibril_t *) fid;
305
306 as_area_destroy(fibril->stack);
307 fibril_teardown(fibril);
308}
309
310/** Add a fibril to the ready list.
311 *
312 * @param fid Pointer to the fibril structure of the fibril to be
313 * added.
314 *
315 */
316void fibril_add_ready(fid_t fid)
317{
318 fibril_t *fibril = (fibril_t *) fid;
319
320 futex_down(&fibril_futex);
321
322 if ((fibril->flags & FIBRIL_SERIALIZED))
323 list_append(&fibril->link, &serialized_list);
324 else
325 list_append(&fibril->link, &ready_list);
326
327 futex_up(&fibril_futex);
328}
329
330/** Add a fibril to the manager list.
331 *
332 * @param fid Pointer to the fibril structure of the fibril to be
333 * added.
334 *
335 */
336void fibril_add_manager(fid_t fid)
337{
338 fibril_t *fibril = (fibril_t *) fid;
339
340 futex_down(&fibril_futex);
341 list_append(&fibril->link, &manager_list);
342 futex_up(&fibril_futex);
343}
344
345/** Remove one manager from the manager list. */
346void fibril_remove_manager(void)
347{
348 futex_down(&fibril_futex);
349
350 if (!list_empty(&manager_list))
351 list_remove(list_first(&manager_list));
352
353 futex_up(&fibril_futex);
354}
355
356/** Return fibril id of the currently running fibril.
357 *
358 * @return fibril ID of the currently running fibril.
359 *
360 */
361fid_t fibril_get_id(void)
362{
363 return (fid_t) __tcb_get()->fibril_data;
364}
365
366/** Disable preemption
367 *
368 * If the fibril wants to send several message in a row and does not want to be
369 * preempted, it should start async_serialize_start() in the beginning of
370 * communication and async_serialize_end() in the end. If it is a true
371 * multithreaded application, it should protect the communication channel by a
372 * futex as well.
373 *
374 */
375void fibril_inc_sercount(void)
376{
377 serialization_count++;
378}
379
380/** Restore the preemption counter to the previous state. */
381void fibril_dec_sercount(void)
382{
383 serialization_count--;
384}
385
386int fibril_get_sercount(void)
387{
388 return serialization_count;
389}
390
391/** @}
392 */
Note: See TracBrowser for help on using the repository browser.