2 Copyright (c) 2012, Broadcom Europe Ltd
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.
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.
28 /*=============================================================================
29 VideoCore OS Abstraction Layer - public header file
30 =============================================================================*/
32 #ifndef VCOS_SEMAPHORE_H
33 #define VCOS_SEMAPHORE_H
39 #include "interface/vcos/vcos_types.h"
40 #ifndef VCOS_PLATFORM_H
45 * \file vcos_semaphore.h
47 * \section sem Semaphores
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.
54 * \subsection timeout Timeout
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).
62 * \subsection sem_nucleus Changes from Nucleus:
64 * Semaphores are always "FIFO" - i.e. sleeping threads are woken in FIFO order. That's
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.
73 * \brief Create a semaphore.
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.
81 * @return VCOS_SUCCESS if the semaphore was created.
85 VCOS_STATUS_T vcos_semaphore_create(VCOS_SEMAPHORE_T *sem, const char *name, VCOS_UNSIGNED count);
88 * \brief Wait on a semaphore.
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
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.
98 * @param sem Semaphore to wait on
99 * @return VCOS_SUCCESS - semaphore was taken.
100 * VCOS_EAGAIN - could not take semaphore
104 VCOS_STATUS_T vcos_semaphore_wait(VCOS_SEMAPHORE_T *sem);
107 * \brief Wait on a semaphore with a timeout.
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)
113 * Try to obtain the semaphore. If it is already taken, return
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
121 * VCOS_EINVAL - Some other error (most likely bad
125 VCOS_STATUS_T vcos_semaphore_wait_timeout(VCOS_SEMAPHORE_T *sem, VCOS_UNSIGNED timeout);
128 * \brief Try to wait for a semaphore.
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
136 VCOS_STATUS_T vcos_semaphore_trywait(VCOS_SEMAPHORE_T *sem);
139 * \brief Post a semaphore.
141 * @param sem Semaphore to wait on
144 VCOS_STATUS_T vcos_semaphore_post(VCOS_SEMAPHORE_T *sem);
147 * \brief Delete a semaphore, releasing any resources consumed by it.
149 * @param sem Semaphore to wait on
152 void vcos_semaphore_delete(VCOS_SEMAPHORE_T *sem);