source: mainline/uspace/lib/c/generic/fibril.c@ 5892ec1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5892ec1 was 5892ec1, checked in by Jakub Jermar <jakub@…>, 13 years ago

Rename AS_AREA_NORESERVE to AS_AREA_LATE_RESERVE.

This reflects the fact that we still reserve memory for these areas,
but the reservation is done late during the page fault.

  • Property mode set to 100644
File size: 9.6 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 <tls.h>
40#include <malloc.h>
41#include <abi/mm/as.h>
42#include <as.h>
43#include <unistd.h>
44#include <stdio.h>
45#include <libarch/barrier.h>
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
52 #define FIBRIL_INITIAL_STACK_PAGES_NO 1
53#endif
54
55/**
56 * This futex serializes access to ready_list,
57 * serialized_list and manager_list.
58 */
59static atomic_t fibril_futex = FUTEX_INITIALIZER;
60
61static LIST_INITIALIZE(ready_list);
62static LIST_INITIALIZE(serialized_list);
63static LIST_INITIALIZE(manager_list);
64
65/** Number of threads that are executing a manager fibril. */
66static int threads_in_manager;
67
68/**
69 * Number of threads that are executing a manager fibril
70 * and are serialized. Protected by async_futex.
71 */
72static int serialized_threads;
73
74/** Fibril-local count of serialization. If > 0, we must not preempt */
75static fibril_local int serialization_count;
76
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 */
84static void fibril_main(void)
85{
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}
94
95/** Setup fibril information into TCB structure
96 *
97 */
98fibril_t *fibril_setup(void)
99{
100 tcb_t *tcb = __make_tls();
101 if (!tcb)
102 return NULL;
103
104 fibril_t *fibril = malloc(sizeof(fibril_t));
105 if (!fibril) {
106 __free_tls(tcb);
107 return NULL;
108 }
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
120 fibril->waits_for = NULL;
121
122 return fibril;
123}
124
125void fibril_teardown(fibril_t *fibril)
126{
127 __free_tls(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 *
262 * @return 0 on failure or TLS of the new fibril.
263 *
264 */
265fid_t fibril_create(int (*func)(void *), void *arg)
266{
267 fibril_t *fibril;
268
269 fibril = fibril_setup();
270 if (fibril == NULL)
271 return 0;
272
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 |
276 AS_AREA_LATE_RESERVE);
277 if (fibril->stack == (void *) -1) {
278 fibril_teardown(fibril);
279 return 0;
280 }
281
282 fibril->func = func;
283 fibril->arg = arg;
284
285 context_save(&fibril->ctx);
286 context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
287 FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), fibril->tcb);
288
289 return (fid_t) fibril;
290}
291
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 */
300void fibril_destroy(fid_t fid)
301{
302 fibril_t *fibril = (fibril_t *) fid;
303
304 as_area_destroy(fibril->stack);
305 fibril_teardown(fibril);
306}
307
308/** Add a fibril to the ready list.
309 *
310 * @param fid Pointer to the fibril structure of the fibril to be
311 * added.
312 *
313 */
314void fibril_add_ready(fid_t fid)
315{
316 fibril_t *fibril = (fibril_t *) fid;
317
318 futex_down(&fibril_futex);
319
320 if ((fibril->flags & FIBRIL_SERIALIZED))
321 list_append(&fibril->link, &serialized_list);
322 else
323 list_append(&fibril->link, &ready_list);
324
325 futex_up(&fibril_futex);
326}
327
328/** Add a fibril to the manager list.
329 *
330 * @param fid Pointer to the fibril structure of the fibril to be
331 * added.
332 *
333 */
334void fibril_add_manager(fid_t fid)
335{
336 fibril_t *fibril = (fibril_t *) fid;
337
338 futex_down(&fibril_futex);
339 list_append(&fibril->link, &manager_list);
340 futex_up(&fibril_futex);
341}
342
343/** Remove one manager from the manager list. */
344void fibril_remove_manager(void)
345{
346 futex_down(&fibril_futex);
347
348 if (!list_empty(&manager_list))
349 list_remove(list_first(&manager_list));
350
351 futex_up(&fibril_futex);
352}
353
354/** Return fibril id of the currently running fibril.
355 *
356 * @return fibril ID of the currently running fibril.
357 *
358 */
359fid_t fibril_get_id(void)
360{
361 return (fid_t) __tcb_get()->fibril_data;
362}
363
364/** Disable preemption
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
370 * futex as well.
371 *
372 */
373void fibril_inc_sercount(void)
374{
375 serialization_count++;
376}
377
378/** Restore the preemption counter to the previous state. */
379void fibril_dec_sercount(void)
380{
381 serialization_count--;
382}
383
384int fibril_get_sercount(void)
385{
386 return serialization_count;
387}
388
389/** @}
390 */
Note: See TracBrowser for help on using the repository browser.