[7e2988c] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[7e2988c] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[bc1f1c2] | 35 | #ifndef LIBC_FIBRIL_H_
|
---|
| 36 | #define LIBC_FIBRIL_H_
|
---|
[7e2988c] | 37 |
|
---|
[bc1f1c2] | 38 | #include <libarch/fibril.h>
|
---|
[cf13b17] | 39 | #include <types/common.h>
|
---|
[d9c8c81] | 40 | #include <adt/list.h>
|
---|
[fa23560] | 41 | #include <libarch/tls.h>
|
---|
[7e2988c] | 42 |
|
---|
[b1d3c36] | 43 | #define context_set_generic(c, _pc, stack, size, ptls) \
|
---|
[9dae3e97] | 44 | do { \
|
---|
| 45 | (c)->pc = (sysarg_t) (_pc); \
|
---|
| 46 | (c)->sp = ((sysarg_t) (stack)) + (size) - SP_DELTA; \
|
---|
| 47 | (c)->tls = (sysarg_t) (ptls); \
|
---|
| 48 | } while (0)
|
---|
[7e2988c] | 49 |
|
---|
[1b20da0] | 50 | #define FIBRIL_WRITER 1
|
---|
[085bd54] | 51 |
|
---|
[7f122e3] | 52 | struct fibril;
|
---|
| 53 |
|
---|
| 54 | typedef struct {
|
---|
| 55 | struct fibril *owned_by;
|
---|
| 56 | } fibril_owner_info_t;
|
---|
| 57 |
|
---|
[80649a91] | 58 | typedef enum {
|
---|
[bc1f1c2] | 59 | FIBRIL_PREEMPT,
|
---|
| 60 | FIBRIL_TO_MANAGER,
|
---|
| 61 | FIBRIL_FROM_MANAGER,
|
---|
| 62 | FIBRIL_FROM_DEAD
|
---|
| 63 | } fibril_switch_type_t;
|
---|
[80649a91] | 64 |
|
---|
[bc1f1c2] | 65 | typedef sysarg_t fid_t;
|
---|
[7e2988c] | 66 |
|
---|
[b1d3c36] | 67 | typedef struct fibril {
|
---|
[520492a] | 68 | link_t link;
|
---|
[c1b979a] | 69 | link_t all_link;
|
---|
[29a9f62] | 70 | context_t ctx;
|
---|
| 71 | void *stack;
|
---|
| 72 | void *arg;
|
---|
[b7fd2a0] | 73 | errno_t (*func)(void *);
|
---|
[c4c5de5] | 74 | tcb_t *tcb;
|
---|
[a35b458] | 75 |
|
---|
[bc1f1c2] | 76 | struct fibril *clean_after_me;
|
---|
[b7fd2a0] | 77 | errno_t retval;
|
---|
[29a9f62] | 78 | int flags;
|
---|
[a35b458] | 79 |
|
---|
[7f122e3] | 80 | fibril_owner_info_t *waits_for;
|
---|
[c170438] | 81 |
|
---|
[58563585] | 82 | unsigned int switches;
|
---|
[b1d3c36] | 83 | } fibril_t;
|
---|
[7e2988c] | 84 |
|
---|
[26360f7] | 85 | /** Fibril-local variable specifier */
|
---|
| 86 | #define fibril_local __thread
|
---|
| 87 |
|
---|
[b1d3c36] | 88 | extern int context_save(context_t *ctx) __attribute__((returns_twice));
|
---|
| 89 | extern void context_restore(context_t *ctx) __attribute__((noreturn));
|
---|
[29a9f62] | 90 |
|
---|
[eceff5f] | 91 | #define FIBRIL_DFLT_STK_SIZE 0
|
---|
| 92 |
|
---|
[b4df8db] | 93 | #define fibril_create(func, arg) \
|
---|
[eceff5f] | 94 | fibril_create_generic((func), (arg), FIBRIL_DFLT_STK_SIZE)
|
---|
[b7fd2a0] | 95 | extern fid_t fibril_create_generic(errno_t (*func)(void *), void *arg, size_t);
|
---|
[32d19f7] | 96 | extern void fibril_destroy(fid_t fid);
|
---|
[bc1f1c2] | 97 | extern fibril_t *fibril_setup(void);
|
---|
[4d11204] | 98 | extern void fibril_teardown(fibril_t *f, bool locked);
|
---|
[116d3f6f] | 99 | extern int fibril_switch(fibril_switch_type_t stype);
|
---|
[bc1f1c2] | 100 | extern void fibril_add_ready(fid_t fid);
|
---|
| 101 | extern void fibril_add_manager(fid_t fid);
|
---|
| 102 | extern void fibril_remove_manager(void);
|
---|
| 103 | extern fid_t fibril_get_id(void);
|
---|
[80649a91] | 104 |
|
---|
[b1d3c36] | 105 | static inline int fibril_yield(void)
|
---|
| 106 | {
|
---|
[116d3f6f] | 107 | return fibril_switch(FIBRIL_PREEMPT);
|
---|
[80649a91] | 108 | }
|
---|
[c4c5de5] | 109 |
|
---|
[7e2988c] | 110 | #endif
|
---|
[b2951e2] | 111 |
|
---|
[fadd381] | 112 | /** @}
|
---|
[b2951e2] | 113 | */
|
---|