Ticket #2274: lock.h

File lock.h, 5.0 KB (added by Sebastian Huber, on 07/23/15 at 08:01:05)
Line 
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
34struct _Thread_Control;
35
36struct _Thread_queue_Heads;
37
38struct _Ticket_lock_Control {
39        unsigned int _next_ticket;
40        unsigned int _now_serving;
41};
42
43struct _Thread_queue_Queue {
44        struct _Thread_queue_Heads *_heads;
45        struct _Ticket_lock_Control _Lock;
46};
47
48struct _Mutex_Control {
49        struct _Thread_queue_Queue _Queue;
50        struct _Thread_Control *_owner;
51};
52
53struct _Mutex_recursive_Control {
54        struct _Mutex_Control _Mutex;
55        unsigned int _nest_level;
56};
57
58struct _Semaphore_Control {
59        struct _Thread_queue_Queue _Queue;
60        unsigned int _count;
61};
62
63struct _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
77static inline void
78_Mutex_Initialize(struct _Mutex_Control *_mutex)
79{
80        struct _Mutex_Control _init = _MUTEX_INITIALIZER;
81
82        *_mutex = _init;
83}
84
85void _Mutex_Acquire(struct _Mutex_Control *);
86
87int _Mutex_Try_acquire(struct _Mutex_Control *);
88
89void _Mutex_Release(struct _Mutex_Control *);
90
91static inline void
92_Mutex_Destroy(struct _Mutex_Control *_mutex)
93{
94
95        (void)_mutex;
96}
97
98static 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
106void _Mutex_recursive_Acquire(struct _Mutex_recursive_Control *);
107
108int _Mutex_recursive_Try_acquire(struct _Mutex_recursive_Control *);
109
110void _Mutex_recursive_Release(struct _Mutex_recursive_Control *);
111
112static inline void
113_Mutex_recursive_Destroy(struct _Mutex_recursive_Control *_mutex)
114{
115
116        (void)_mutex;
117}
118
119static 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
128void _Semaphore_Wait(struct _Semaphore_Control *);
129
130void _Semaphore_Post(struct _Semaphore_Control *);
131
132static inline void
133_Semaphore_Destroy(struct _Semaphore_Control *_semaphore)
134{
135
136        (void)_semaphore;
137}
138
139static inline void
140_Futex_Initialize(struct _Futex_Control *_futex)
141{
142        struct _Futex_Control _init = _FUTEX_INITIALIZER;
143
144        *_futex = _init;
145}
146
147int _Futex_Wait(struct _Futex_Control *, int *, int);
148
149int _Futex_Wake(struct _Futex_Control *, int);
150
151static inline void
152_Futex_Destroy(struct _Futex_Control *_futex)
153{
154
155        (void)_futex;
156}
157
158int _Sched_Count(void);
159
160int _Sched_Index(void);
161
162int _Sched_Name_to_index(const char *, size_t);
163
164int _Sched_Processor_count(int);
165
166/* Newlib internal locks */
167
168typedef struct _Mutex_Control _LOCK_T;
169
170typedef 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_ */