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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a55d76b1 was 899342e, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 8 years ago

fibril_futex must be locked during context_swap().

  • Property mode set to 100644
File size: 8.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 <stack.h>
40#include <tls.h>
41#include <stdlib.h>
42#include <abi/mm/as.h>
43#include <as.h>
44#include <stdio.h>
45#include <libarch/barrier.h>
46#include <context.h>
47#include <futex.h>
48#include <assert.h>
49#include <async.h>
50
51#ifdef FUTEX_UPGRADABLE
52#include <rcu.h>
53#endif
54
55/**
56 * This futex serializes access to ready_list,
57 * manager_list and fibril_list.
58 */
59static futex_t fibril_futex = FUTEX_INITIALIZER;
60
61static LIST_INITIALIZE(ready_list);
62static LIST_INITIALIZE(manager_list);
63static LIST_INITIALIZE(fibril_list);
64
65/** Function that spans the whole life-cycle of a fibril.
66 *
67 * Each fibril begins execution in this function. Then the function implementing
68 * the fibril logic is called. After its return, the return value is saved.
69 * The fibril then switches to another fibril, which cleans up after it.
70 *
71 */
72static void fibril_main(void)
73{
74 /* fibril_futex is locked when a fibril is first started. */
75 futex_unlock(&fibril_futex);
76
77 fibril_t *fibril = __tcb_get()->fibril_data;
78
79#ifdef FUTEX_UPGRADABLE
80 rcu_register_fibril();
81#endif
82
83 /* Call the implementing function. */
84 fibril->retval = fibril->func(fibril->arg);
85
86 futex_down(&async_futex);
87 fibril_switch(FIBRIL_FROM_DEAD);
88 /* Not reached */
89}
90
91/** Setup fibril information into TCB structure
92 *
93 */
94fibril_t *fibril_setup(void)
95{
96 tcb_t *tcb = tls_make();
97 if (!tcb)
98 return NULL;
99
100 fibril_t *fibril = malloc(sizeof(fibril_t));
101 if (!fibril) {
102 tls_free(tcb);
103 return NULL;
104 }
105
106 tcb->fibril_data = fibril;
107 fibril->tcb = tcb;
108
109 fibril->func = NULL;
110 fibril->arg = NULL;
111 fibril->stack = NULL;
112 fibril->clean_after_me = NULL;
113 fibril->retval = 0;
114 fibril->flags = 0;
115
116 fibril->waits_for = NULL;
117
118 fibril->switches = 0;
119
120 /*
121 * We are called before __tcb_set(), so we need to use
122 * futex_down/up() instead of futex_lock/unlock() that
123 * may attempt to access TLS.
124 */
125 futex_down(&fibril_futex);
126 list_append(&fibril->all_link, &fibril_list);
127 futex_up(&fibril_futex);
128
129 return fibril;
130}
131
132void fibril_teardown(fibril_t *fibril, bool locked)
133{
134 if (!locked)
135 futex_lock(&fibril_futex);
136 list_remove(&fibril->all_link);
137 if (!locked)
138 futex_unlock(&fibril_futex);
139 tls_free(fibril->tcb);
140 free(fibril);
141}
142
143/** Switch from the current fibril.
144 *
145 * If stype is FIBRIL_TO_MANAGER or FIBRIL_FROM_DEAD, the async_futex must
146 * be held.
147 *
148 * @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
149 * FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
150 * describes the circumstances of the switch.
151 *
152 * @return 0 if there is no ready fibril,
153 * @return 1 otherwise.
154 *
155 */
156int fibril_switch(fibril_switch_type_t stype)
157{
158 futex_lock(&fibril_futex);
159
160 fibril_t *srcf = __tcb_get()->fibril_data;
161 fibril_t *dstf = NULL;
162
163 /* Choose a new fibril to run */
164 switch (stype) {
165 case FIBRIL_TO_MANAGER:
166 case FIBRIL_FROM_DEAD:
167 /* Make sure the async_futex is held. */
168 assert((atomic_signed_t) async_futex.val.count <= 0);
169
170 /* If we are going to manager and none exists, create it */
171 while (list_empty(&manager_list)) {
172 futex_unlock(&fibril_futex);
173 async_create_manager();
174 futex_lock(&fibril_futex);
175 }
176
177 dstf = list_get_instance(list_first(&manager_list),
178 fibril_t, link);
179
180 if (stype == FIBRIL_FROM_DEAD)
181 dstf->clean_after_me = srcf;
182 break;
183 case FIBRIL_PREEMPT:
184 case FIBRIL_FROM_MANAGER:
185 if (list_empty(&ready_list)) {
186 futex_unlock(&fibril_futex);
187 return 0;
188 }
189
190 dstf = list_get_instance(list_first(&ready_list), fibril_t,
191 link);
192 break;
193 }
194 list_remove(&dstf->link);
195
196 /* Put the current fibril into the correct run list */
197 switch (stype) {
198 case FIBRIL_PREEMPT:
199 list_append(&srcf->link, &ready_list);
200 break;
201 case FIBRIL_FROM_MANAGER:
202 list_append(&srcf->link, &manager_list);
203 break;
204 case FIBRIL_FROM_DEAD:
205 // Nothing.
206 break;
207 case FIBRIL_TO_MANAGER:
208 srcf->switches++;
209 /*
210 * Don't put the current fibril into any list, it should
211 * already be somewhere, or it will be lost.
212 */
213 break;
214 }
215
216#ifdef FUTEX_UPGRADABLE
217 if (stype == FIBRIL_FROM_DEAD) {
218 rcu_deregister_fibril();
219 }
220#endif
221
222 /* Swap to the next fibril. */
223 context_swap(&srcf->ctx, &dstf->ctx);
224
225 /* Restored by another fibril! */
226
227 /* Must be after context_swap()! */
228 futex_unlock(&fibril_futex);
229
230 if (srcf->clean_after_me) {
231 /*
232 * Cleanup after the dead fibril from which we
233 * restored context here.
234 */
235 void *stack = srcf->clean_after_me->stack;
236 if (stack) {
237 /*
238 * This check is necessary because a
239 * thread could have exited like a
240 * normal fibril using the
241 * FIBRIL_FROM_DEAD switch type. In that
242 * case, its fibril will not have the
243 * stack member filled.
244 */
245 as_area_destroy(stack);
246 }
247 fibril_teardown(srcf->clean_after_me, true);
248 srcf->clean_after_me = NULL;
249 }
250
251 return 1;
252}
253
254/** Create a new fibril.
255 *
256 * @param func Implementing function of the new fibril.
257 * @param arg Argument to pass to func.
258 * @param stksz Stack size in bytes.
259 *
260 * @return 0 on failure or TLS of the new fibril.
261 *
262 */
263fid_t fibril_create_generic(errno_t (*func)(void *), void *arg, size_t stksz)
264{
265 fibril_t *fibril;
266
267 fibril = fibril_setup();
268 if (fibril == NULL)
269 return 0;
270
271 size_t stack_size = (stksz == FIBRIL_DFLT_STK_SIZE) ?
272 stack_size_get() : stksz;
273 fibril->stack = as_area_create(AS_AREA_ANY, stack_size,
274 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE | AS_AREA_GUARD |
275 AS_AREA_LATE_RESERVE, AS_AREA_UNPAGED);
276 if (fibril->stack == (void *) -1) {
277 fibril_teardown(fibril, false);
278 return 0;
279 }
280
281 fibril->func = func;
282 fibril->arg = arg;
283
284 context_create_t sctx = {
285 .fn = fibril_main,
286 .stack_base = fibril->stack,
287 .stack_size = stack_size,
288 .tls = fibril->tcb,
289 };
290
291 context_create(&fibril->ctx, &sctx);
292 return (fid_t) fibril;
293}
294
295/** Delete a fibril that has never run.
296 *
297 * Free resources of a fibril that has been created with fibril_create()
298 * but never readied using fibril_add_ready().
299 *
300 * @param fid Pointer to the fibril structure of the fibril to be
301 * added.
302 */
303void fibril_destroy(fid_t fid)
304{
305 fibril_t *fibril = (fibril_t *) fid;
306
307 as_area_destroy(fibril->stack);
308 fibril_teardown(fibril, false);
309}
310
311/** Add a fibril to the ready list.
312 *
313 * @param fid Pointer to the fibril structure of the fibril to be
314 * added.
315 *
316 */
317void fibril_add_ready(fid_t fid)
318{
319 fibril_t *fibril = (fibril_t *) fid;
320
321 futex_lock(&fibril_futex);
322 list_append(&fibril->link, &ready_list);
323 futex_unlock(&fibril_futex);
324}
325
326/** Add a fibril to the manager list.
327 *
328 * @param fid Pointer to the fibril structure of the fibril to be
329 * added.
330 *
331 */
332void fibril_add_manager(fid_t fid)
333{
334 fibril_t *fibril = (fibril_t *) fid;
335
336 futex_lock(&fibril_futex);
337 list_append(&fibril->link, &manager_list);
338 futex_unlock(&fibril_futex);
339}
340
341/** Remove one manager from the manager list. */
342void fibril_remove_manager(void)
343{
344 futex_lock(&fibril_futex);
345 if (!list_empty(&manager_list))
346 list_remove(list_first(&manager_list));
347 futex_unlock(&fibril_futex);
348}
349
350/** Return fibril id of the currently running fibril.
351 *
352 * @return fibril ID of the currently running fibril.
353 *
354 */
355fid_t fibril_get_id(void)
356{
357 return (fid_t) __tcb_get()->fibril_data;
358}
359
360/** @}
361 */
Note: See TracBrowser for help on using the repository browser.