source: mainline/uspace/lib/c/generic/fibril.c@ b1c57a8

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

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

  • Property mode set to 100644
File size: 10.0 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#include <futex.h>
52
53#ifdef FUTEX_UPGRADABLE
54#include <rcu.h>
55#endif
56
57/**
58 * This futex serializes access to ready_list,
59 * serialized_list and manager_list.
60 */
61static futex_t fibril_futex = FUTEX_INITIALIZER;
62
63static LIST_INITIALIZE(ready_list);
64static LIST_INITIALIZE(serialized_list);
65static LIST_INITIALIZE(manager_list);
66static LIST_INITIALIZE(fibril_list);
67
68/** Number of threads that are executing a manager fibril. */
69static int threads_in_manager;
70
71/**
72 * Number of threads that are executing a manager fibril
73 * and are serialized. Protected by async_futex.
74 */
75static int serialized_threads;
76
77/** Fibril-local count of serialization. If > 0, we must not preempt */
78static fibril_local int serialization_count;
79
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 */
87static void fibril_main(void)
88{
89 fibril_t *fibril = __tcb_get()->fibril_data;
90
91#ifdef FUTEX_UPGRADABLE
92 rcu_register_fibril();
93#endif
94
95 /* Call the implementing function. */
96 fibril->retval = fibril->func(fibril->arg);
97
98 fibril_switch(FIBRIL_FROM_DEAD);
99 /* Not reached */
100}
101
102/** Setup fibril information into TCB structure
103 *
104 */
105fibril_t *fibril_setup(void)
106{
107 tcb_t *tcb = tls_make();
108 if (!tcb)
109 return NULL;
110
111 fibril_t *fibril = malloc(sizeof(fibril_t));
112 if (!fibril) {
113 tls_free(tcb);
114 return NULL;
115 }
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
127 fibril->waits_for = NULL;
128 list_append(&fibril->all_link, &fibril_list);
129
130 return fibril;
131}
132
133void fibril_teardown(fibril_t *fibril)
134{
135 list_remove(&fibril->all_link);
136 tls_free(fibril->tcb);
137 free(fibril);
138}
139
140/** Switch from the current fibril.
141 *
142 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
143 * held.
144 *
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 *
152 */
153int fibril_switch(fibril_switch_type_t stype)
154{
155 int retval = 0;
156
157 futex_lock(&fibril_futex);
158
159 if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
160 goto ret_0;
161
162 if (stype == FIBRIL_FROM_MANAGER) {
163 if ((list_empty(&ready_list)) && (list_empty(&serialized_list)))
164 goto ret_0;
165
166 /*
167 * Do not preempt if there is not enough threads to run the
168 * ready fibrils which are not serialized.
169 */
170 if ((list_empty(&serialized_list)) &&
171 (threads_in_manager <= serialized_threads)) {
172 goto ret_0;
173 }
174 }
175
176 /* If we are going to manager and none exists, create it */
177 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
178 while (list_empty(&manager_list)) {
179 futex_unlock(&fibril_futex);
180 async_create_manager();
181 futex_lock(&fibril_futex);
182 }
183 }
184
185 fibril_t *srcf = __tcb_get()->fibril_data;
186 if (stype != FIBRIL_FROM_DEAD) {
187
188 /* Save current state */
189 if (!context_save(&srcf->ctx)) {
190 if (serialization_count)
191 srcf->flags &= ~FIBRIL_SERIALIZED;
192
193 if (srcf->clean_after_me) {
194 /*
195 * Cleanup after the dead fibril from which we
196 * restored context here.
197 */
198 void *stack = srcf->clean_after_me->stack;
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 */
208 as_area_destroy(stack);
209 }
210 fibril_teardown(srcf->clean_after_me);
211 srcf->clean_after_me = NULL;
212 }
213
214 return 1; /* futex_unlock already done here */
215 }
216
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);
222 threads_in_manager--;
223 } else {
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 }
231
232 /* Choose a new fibril to run */
233 fibril_t *dstf;
234 if ((stype == FIBRIL_TO_MANAGER) || (stype == FIBRIL_FROM_DEAD)) {
235 dstf = list_get_instance(list_first(&manager_list), fibril_t,
236 link);
237 if (serialization_count && stype == FIBRIL_TO_MANAGER) {
238 serialized_threads++;
239 srcf->flags |= FIBRIL_SERIALIZED;
240 }
241 threads_in_manager++;
242
243 if (stype == FIBRIL_FROM_DEAD)
244 dstf->clean_after_me = srcf;
245 } else {
246 if (!list_empty(&serialized_list)) {
247 dstf = list_get_instance(list_first(&serialized_list),
248 fibril_t, link);
249 serialized_threads--;
250 } else {
251 dstf = list_get_instance(list_first(&ready_list),
252 fibril_t, link);
253 }
254 }
255 list_remove(&dstf->link);
256
257 futex_unlock(&fibril_futex);
258
259#ifdef FUTEX_UPGRADABLE
260 if (stype == FIBRIL_FROM_DEAD) {
261 rcu_deregister_fibril();
262 }
263#endif
264
265 context_restore(&dstf->ctx);
266 /* not reached */
267
268ret_0:
269 futex_unlock(&fibril_futex);
270 return retval;
271}
272
273/** Create a new fibril.
274 *
275 * @param func Implementing function of the new fibril.
276 * @param arg Argument to pass to func.
277 * @param stksz Stack size in bytes.
278 *
279 * @return 0 on failure or TLS of the new fibril.
280 *
281 */
282fid_t fibril_create_generic(int (*func)(void *), void *arg, size_t stksz)
283{
284 fibril_t *fibril;
285
286 fibril = fibril_setup();
287 if (fibril == NULL)
288 return 0;
289
290 size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
291 stack_size_get() : stksz;
292 fibril->stack = as_area_create((void *) -1, stack_size,
293 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
294 AS_AREA_LATE_RESERVE);
295 if (fibril->stack == (void *) -1) {
296 fibril_teardown(fibril);
297 return 0;
298 }
299
300 fibril->func = func;
301 fibril->arg = arg;
302
303 context_save(&fibril->ctx);
304 context_set(&fibril->ctx, FADDR(fibril_main), fibril->stack,
305 stack_size, fibril->tcb);
306
307 return (fid_t) fibril;
308}
309
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 */
318void fibril_destroy(fid_t fid)
319{
320 fibril_t *fibril = (fibril_t *) fid;
321
322 as_area_destroy(fibril->stack);
323 fibril_teardown(fibril);
324}
325
326/** Add a fibril to the ready list.
327 *
328 * @param fid Pointer to the fibril structure of the fibril to be
329 * added.
330 *
331 */
332void fibril_add_ready(fid_t fid)
333{
334 fibril_t *fibril = (fibril_t *) fid;
335
336 futex_lock(&fibril_futex);
337
338 if ((fibril->flags & FIBRIL_SERIALIZED))
339 list_append(&fibril->link, &serialized_list);
340 else
341 list_append(&fibril->link, &ready_list);
342
343 futex_unlock(&fibril_futex);
344}
345
346/** Add a fibril to the manager list.
347 *
348 * @param fid Pointer to the fibril structure of the fibril to be
349 * added.
350 *
351 */
352void fibril_add_manager(fid_t fid)
353{
354 fibril_t *fibril = (fibril_t *) fid;
355
356 futex_lock(&fibril_futex);
357 list_append(&fibril->link, &manager_list);
358 futex_unlock(&fibril_futex);
359}
360
361/** Remove one manager from the manager list. */
362void fibril_remove_manager(void)
363{
364 futex_lock(&fibril_futex);
365
366 if (!list_empty(&manager_list))
367 list_remove(list_first(&manager_list));
368
369 futex_unlock(&fibril_futex);
370}
371
372/** Return fibril id of the currently running fibril.
373 *
374 * @return fibril ID of the currently running fibril.
375 *
376 */
377fid_t fibril_get_id(void)
378{
379 return (fid_t) __tcb_get()->fibril_data;
380}
381
382/** Disable preemption
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
388 * futex as well.
389 *
390 */
391void fibril_inc_sercount(void)
392{
393 serialization_count++;
394}
395
396/** Restore the preemption counter to the previous state. */
397void fibril_dec_sercount(void)
398{
399 serialization_count--;
400}
401
402int fibril_get_sercount(void)
403{
404 return serialization_count;
405}
406
407/** @}
408 */
Note: See TracBrowser for help on using the repository browser.