1 | /* |
---|
2 | * Copyright (c) 2015 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
14 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
15 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
16 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
17 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
18 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
19 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #ifndef _SYS_LOCK_H_ |
---|
27 | #define _SYS_LOCK_H_ |
---|
28 | |
---|
29 | #include <sys/cdefs.h> |
---|
30 | #include <stddef.h> |
---|
31 | |
---|
32 | __BEGIN_DECLS |
---|
33 | |
---|
34 | struct _Thread_Control; |
---|
35 | |
---|
36 | struct _Thread_queue_Heads; |
---|
37 | |
---|
38 | struct _Ticket_lock_Control { |
---|
39 | unsigned int _next_ticket; |
---|
40 | unsigned int _now_serving; |
---|
41 | }; |
---|
42 | |
---|
43 | struct _Thread_queue_Queue { |
---|
44 | struct _Thread_queue_Heads *_heads; |
---|
45 | struct _Ticket_lock_Control _Lock; |
---|
46 | }; |
---|
47 | |
---|
48 | struct _Mutex_Control { |
---|
49 | struct _Thread_queue_Queue _Queue; |
---|
50 | struct _Thread_Control *_owner; |
---|
51 | }; |
---|
52 | |
---|
53 | struct _Mutex_recursive_Control { |
---|
54 | struct _Mutex_Control _Mutex; |
---|
55 | unsigned int _nest_level; |
---|
56 | }; |
---|
57 | |
---|
58 | struct _Semaphore_Control { |
---|
59 | struct _Thread_queue_Queue _Queue; |
---|
60 | unsigned int _count; |
---|
61 | }; |
---|
62 | |
---|
63 | struct _Futex_Control { |
---|
64 | struct _Thread_queue_Queue _Queue; |
---|
65 | }; |
---|
66 | |
---|
67 | #define _THREAD_QUEUE_INITIALIZER { 0, { 0, 0 } } |
---|
68 | |
---|
69 | #define _MUTEX_INITIALIZER { _THREAD_QUEUE_INITIALIZER, 0 } |
---|
70 | |
---|
71 | #define _MUTEX_RECURSIVE_INITIALIZER { _MUTEX_INITIALIZER, 0 } |
---|
72 | |
---|
73 | #define _SEMAPHORE_INITIALIZER(_count) { _THREAD_QUEUE_INITIALIZER, _count } |
---|
74 | |
---|
75 | #define _FUTEX_INITIALIZER { _THREAD_QUEUE_INITIALIZER } |
---|
76 | |
---|
77 | static inline void |
---|
78 | _Mutex_Initialize(struct _Mutex_Control *_mutex) |
---|
79 | { |
---|
80 | struct _Mutex_Control _init = _MUTEX_INITIALIZER; |
---|
81 | |
---|
82 | *_mutex = _init; |
---|
83 | } |
---|
84 | |
---|
85 | void _Mutex_Acquire(struct _Mutex_Control *); |
---|
86 | |
---|
87 | int _Mutex_Try_acquire(struct _Mutex_Control *); |
---|
88 | |
---|
89 | void _Mutex_Release(struct _Mutex_Control *); |
---|
90 | |
---|
91 | static inline void |
---|
92 | _Mutex_Destroy(struct _Mutex_Control *_mutex) |
---|
93 | { |
---|
94 | |
---|
95 | (void)_mutex; |
---|
96 | } |
---|
97 | |
---|
98 | static inline void |
---|
99 | _Mutex_recursive_Initialize(struct _Mutex_recursive_Control *_mutex) |
---|
100 | { |
---|
101 | struct _Mutex_recursive_Control _init = _MUTEX_RECURSIVE_INITIALIZER; |
---|
102 | |
---|
103 | *_mutex = _init; |
---|
104 | } |
---|
105 | |
---|
106 | void _Mutex_recursive_Acquire(struct _Mutex_recursive_Control *); |
---|
107 | |
---|
108 | int _Mutex_recursive_Try_acquire(struct _Mutex_recursive_Control *); |
---|
109 | |
---|
110 | void _Mutex_recursive_Release(struct _Mutex_recursive_Control *); |
---|
111 | |
---|
112 | static inline void |
---|
113 | _Mutex_recursive_Destroy(struct _Mutex_recursive_Control *_mutex) |
---|
114 | { |
---|
115 | |
---|
116 | (void)_mutex; |
---|
117 | } |
---|
118 | |
---|
119 | static inline void |
---|
120 | _Semaphore_Initialize(struct _Semaphore_Control *_semaphore, |
---|
121 | unsigned int _count) |
---|
122 | { |
---|
123 | struct _Semaphore_Control _init = _SEMAPHORE_INITIALIZER(_count); |
---|
124 | |
---|
125 | *_semaphore = _init; |
---|
126 | } |
---|
127 | |
---|
128 | void _Semaphore_Wait(struct _Semaphore_Control *); |
---|
129 | |
---|
130 | void _Semaphore_Post(struct _Semaphore_Control *); |
---|
131 | |
---|
132 | static inline void |
---|
133 | _Semaphore_Destroy(struct _Semaphore_Control *_semaphore) |
---|
134 | { |
---|
135 | |
---|
136 | (void)_semaphore; |
---|
137 | } |
---|
138 | |
---|
139 | static inline void |
---|
140 | _Futex_Initialize(struct _Futex_Control *_futex) |
---|
141 | { |
---|
142 | struct _Futex_Control _init = _FUTEX_INITIALIZER; |
---|
143 | |
---|
144 | *_futex = _init; |
---|
145 | } |
---|
146 | |
---|
147 | int _Futex_Wait(struct _Futex_Control *, int *, int); |
---|
148 | |
---|
149 | int _Futex_Wake(struct _Futex_Control *, int); |
---|
150 | |
---|
151 | static inline void |
---|
152 | _Futex_Destroy(struct _Futex_Control *_futex) |
---|
153 | { |
---|
154 | |
---|
155 | (void)_futex; |
---|
156 | } |
---|
157 | |
---|
158 | int _Sched_Count(void); |
---|
159 | |
---|
160 | int _Sched_Index(void); |
---|
161 | |
---|
162 | int _Sched_Name_to_index(const char *, size_t); |
---|
163 | |
---|
164 | int _Sched_Processor_count(int); |
---|
165 | |
---|
166 | /* Newlib internal locks */ |
---|
167 | |
---|
168 | typedef struct _Mutex_Control _LOCK_T; |
---|
169 | |
---|
170 | typedef struct _Mutex_recursive_Control _LOCK_RECURSIVE_T; |
---|
171 | |
---|
172 | #define __LOCK_INIT(_qualifier, _designator) \ |
---|
173 | _qualifier _LOCK_T _designator = _MUTEX_INITIALIZER |
---|
174 | |
---|
175 | #define __LOCK_INIT_RECURSIVE(_qualifier, _designator) \ |
---|
176 | _qualifier _LOCK_T _designator = _MUTEX_RECURSIVE_INITIALIZER |
---|
177 | |
---|
178 | #define __lock_init(_lock) _Mutex_Initialize(&_lock) |
---|
179 | #define __lock_acquire(_lock) _Mutex_Acquire(&_lock) |
---|
180 | #define __lock_try_acquire(lock) _Mutex_Try_acquire(&_lock) |
---|
181 | #define __lock_release(_lock) _Mutex_Release(&_lock) |
---|
182 | #define __lock_close(_lock) _Mutex_Destroy(&_lock) |
---|
183 | |
---|
184 | #define __lock_init_recursive(_lock) _Mutex_recursive_Initialize(&_lock) |
---|
185 | #define __lock_acquire_recursive(_lock) _Mutex_recursive_Acquire(&_lock) |
---|
186 | #define __lock_try_acquire_recursive(lock) _Mutex_recursive_Try_acquire(&_lock) |
---|
187 | #define __lock_release_recursive(_lock) _Mutex_recursive_Release(&_lock) |
---|
188 | #define __lock_close_recursive(_lock) _Mutex_recursive_Destroy(&_lock) |
---|
189 | |
---|
190 | __END_DECLS |
---|
191 | |
---|
192 | #endif /* _SYS_LOCK_H_ */ |
---|