]>
Commit | Line | Data |
---|---|---|
5fd1486c | 1 | /* fibers.h -- an extremely simple lightweight thread (fiber) implementation |
99dee823 | 2 | Copyright (C) 2015-2021 Free Software Foundation, Inc. |
5fd1486c PJ |
3 | Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com> |
4 | for General Processor Tech. | |
5 | ||
6 | Permission is hereby granted, free of charge, to any person obtaining a | |
7 | copy of this software and associated documentation files | |
8 | (the "Software"), to deal in the Software without restriction, including | |
9 | without limitation the rights to use, copy, modify, merge, publish, | |
10 | distribute, sublicense, and/or sell copies of the Software, and to | |
11 | permit persons to whom the Software is furnished to do so, subject to | |
12 | the following conditions: | |
13 | ||
14 | The above copyright notice and this permission notice shall be included | |
15 | in all copies or substantial portions of the Software. | |
16 | ||
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
18 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
21 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
22 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
23 | USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #ifndef PHSA_RT_FIBERS_H | |
27 | #define PHSA_RT_FIBERS_H | |
28 | ||
29 | #include <ucontext.h> | |
30 | ||
31 | typedef enum | |
32 | { | |
33 | /* Ready to run. */ | |
34 | FIBER_STATUS_READY, | |
35 | /* Exited by calling fiber_thread_exit. */ | |
36 | FIBER_STATUS_EXITED, | |
37 | /* Joined by the main thread. */ | |
38 | FIBER_STATUS_JOINED | |
39 | } fiber_status_t; | |
40 | ||
41 | /* A light weight thread (fiber). */ | |
42 | struct fiber_s | |
43 | { | |
44 | ucontext_t context; | |
45 | volatile fiber_status_t status; | |
46 | struct fiber_s *next; | |
47 | struct fiber_s *prev; | |
48 | }; | |
49 | ||
50 | typedef struct fiber_s fiber_t; | |
51 | ||
52 | typedef void (*fiber_function_t)(int, int); | |
53 | ||
54 | /* Initializes the fiber with the start function given as the first | |
55 | argument, and the argument to pass to the start function, | |
56 | as the second. The allocated stack size is given as the last argument. */ | |
57 | void | |
58 | fiber_init (fiber_t *fiber, fiber_function_t start_function, void *arg, | |
59 | size_t stack_size, size_t stack_align); | |
60 | ||
61 | /* Terminates the fiber execution from within the fiber itself. */ | |
62 | void | |
63 | fiber_exit (); | |
64 | ||
65 | /* Blocks until the given fiber returns. Frees the resources allocated | |
66 | for the fiber. After join returns, the fiber itself can be deleted. */ | |
67 | void | |
68 | fiber_join (fiber_t *fiber); | |
69 | ||
70 | /* Co-operatively transfer execution turn to other fibers. */ | |
71 | void | |
72 | fiber_yield (); | |
73 | ||
74 | /* A multi-entry barrier. After the last fiber has reached the | |
75 | barrier, it is automatically re-initialized to the threshold. */ | |
76 | typedef struct | |
77 | { | |
78 | /* The barrier participant count. */ | |
79 | volatile size_t threshold; | |
80 | /* Number of fibers that have reached the barrier. */ | |
81 | volatile size_t reached; | |
82 | /* Number of fibers that are waiting at the barrier. */ | |
83 | volatile size_t waiting_count; | |
84 | } fiber_barrier_t; | |
85 | ||
86 | /* Reach the given barrier. Blocks (co-operatively switches the execution | |
87 | fibers) until all other parties have reached it. Returns 0 only in case | |
88 | the calling fiber was the first one to return from the barrier. */ | |
89 | size_t | |
90 | fiber_barrier_reach (fiber_barrier_t *barrier); | |
91 | ||
92 | /* Initializes the given barrier. */ | |
93 | void | |
94 | fiber_barrier_init (fiber_barrier_t *barrier, size_t threshold); | |
95 | ||
96 | void * | |
97 | fiber_int_args_to_ptr (int arg0, int arg1); | |
98 | ||
99 | #endif |