annotate libhsail-rt/include/internal/fibers.h @ 144:8f4e72ab4e11

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