1 | /* |
2 | Copyright (c) 2012, Broadcom Europe Ltd |
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 are met: |
7 | * Redistributions of source code must retain the above copyright |
8 | notice, this list of conditions and the following disclaimer. |
9 | * 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 | * Neither the name of the copyright holder nor the |
13 | names of its contributors may be used to endorse or promote products |
14 | derived from this software without specific prior written permission. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | /*============================================================================= |
29 | VideoCore OS Abstraction Layer - public header file |
30 | =============================================================================*/ |
31 | |
32 | #ifndef VCOS_SEMAPHORE_H |
33 | #define VCOS_SEMAPHORE_H |
34 | |
35 | #ifdef __cplusplus |
36 | extern "C" { |
37 | #endif |
38 | |
39 | #include "interface/vcos/vcos_types.h" |
40 | #ifndef VCOS_PLATFORM_H |
41 | #include "vcos.h" |
42 | #endif |
43 | |
44 | /** |
45 | * \file vcos_semaphore.h |
46 | * |
47 | * \section sem Semaphores |
48 | * |
49 | * This provides counting semaphores. Semaphores are not re-entrant. On sensible |
50 | * operating systems a semaphore can always be posted but can only be taken in |
51 | * thread (not interrupt) context. Under Nucleus, a LISR cannot post a semaphore, |
52 | * although it would not be hard to lift this restriction. |
53 | * |
54 | * \subsection timeout Timeout |
55 | * |
56 | * On both Nucleus and ThreadX a semaphore can be taken with a timeout. This is |
57 | * not supported by VCOS because it makes the non-timeout code considerably more |
58 | * complicated (and hence slower). In the unlikely event that you need a timeout |
59 | * with a semaphore, and you cannot simply redesign your code to avoid it, use |
60 | * an event flag (vcos_event_flags.h). |
61 | * |
62 | * \subsection sem_nucleus Changes from Nucleus: |
63 | * |
64 | * Semaphores are always "FIFO" - i.e. sleeping threads are woken in FIFO order. That's |
65 | * because: |
66 | * \arg there's no support for NU_PRIORITY in threadx (though it can be emulated, slowly) |
67 | * \arg we don't appear to actually consciously use it - for example, Dispmanx uses |
68 | * it, but all threads waiting are the same priority. |
69 | * |
70 | */ |
71 | |
72 | /** |
73 | * \brief Create a semaphore. |
74 | * |
75 | * Create a semaphore. |
76 | * |
77 | * @param sem Pointer to memory to be initialized |
78 | * @param name A name for this semaphore. The name may be truncated internally. |
79 | * @param count The initial count for the semaphore. |
80 | * |
81 | * @return VCOS_SUCCESS if the semaphore was created. |
82 | * |
83 | */ |
84 | VCOS_INLINE_DECL |
85 | VCOS_STATUS_T vcos_semaphore_create(VCOS_SEMAPHORE_T *sem, const char *name, VCOS_UNSIGNED count); |
86 | |
87 | /** |
88 | * \brief Wait on a semaphore. |
89 | * |
90 | * There is no timeout option on a semaphore, as adding this will slow down |
91 | * implementations on some platforms. If you need that kind of behaviour, use |
92 | * an event group. |
93 | * |
94 | * On most platforms this always returns VCOS_SUCCESS, and so would ideally be |
95 | * a void function, however some platforms allow a wait to be interrupted so |
96 | * it remains non-void. |
97 | * |
98 | * @param sem Semaphore to wait on |
99 | * @return VCOS_SUCCESS - semaphore was taken. |
100 | * VCOS_EAGAIN - could not take semaphore |
101 | * |
102 | */ |
103 | VCOS_INLINE_DECL |
104 | VCOS_STATUS_T vcos_semaphore_wait(VCOS_SEMAPHORE_T *sem); |
105 | |
106 | /** |
107 | * \brief Wait on a semaphore with a timeout. |
108 | * |
109 | * Note that this function may not be implemented on all |
110 | * platforms, and may not be efficient on all platforms |
111 | * (see comment in vcos_semaphore_wait) |
112 | * |
113 | * Try to obtain the semaphore. If it is already taken, return |
114 | * VCOS_EAGAIN. |
115 | * @param sem Semaphore to wait on |
116 | * @param timeout Number of milliseconds to wait before |
117 | * returning if the semaphore can't be acquired. |
118 | * @return VCOS_SUCCESS - semaphore was taken. |
119 | * VCOS_EAGAIN - could not take semaphore (i.e. timeout |
120 | * expired) |
121 | * VCOS_EINVAL - Some other error (most likely bad |
122 | * parameters). |
123 | */ |
124 | VCOS_INLINE_DECL |
125 | VCOS_STATUS_T vcos_semaphore_wait_timeout(VCOS_SEMAPHORE_T *sem, VCOS_UNSIGNED timeout); |
126 | |
127 | /** |
128 | * \brief Try to wait for a semaphore. |
129 | * |
130 | * Try to obtain the semaphore. If it is already taken, return VCOS_TIMEOUT. |
131 | * @param sem Semaphore to wait on |
132 | * @return VCOS_SUCCESS - semaphore was taken. |
133 | * VCOS_EAGAIN - could not take semaphore |
134 | */ |
135 | VCOS_INLINE_DECL |
136 | VCOS_STATUS_T vcos_semaphore_trywait(VCOS_SEMAPHORE_T *sem); |
137 | |
138 | /** |
139 | * \brief Post a semaphore. |
140 | * |
141 | * @param sem Semaphore to wait on |
142 | */ |
143 | VCOS_INLINE_DECL |
144 | VCOS_STATUS_T vcos_semaphore_post(VCOS_SEMAPHORE_T *sem); |
145 | |
146 | /** |
147 | * \brief Delete a semaphore, releasing any resources consumed by it. |
148 | * |
149 | * @param sem Semaphore to wait on |
150 | */ |
151 | VCOS_INLINE_DECL |
152 | void vcos_semaphore_delete(VCOS_SEMAPHORE_T *sem); |
153 | |
154 | #ifdef __cplusplus |
155 | } |
156 | #endif |
157 | #endif |
158 | |
159 | |