1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON 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
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*=============================================================================
29VideoCore OS Abstraction Layer - public header file
30=============================================================================*/
31
32#ifndef VCOS_SEMAPHORE_H
33#define VCOS_SEMAPHORE_H
34
35#ifdef __cplusplus
36extern "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 */
84VCOS_INLINE_DECL
85VCOS_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 */
103VCOS_INLINE_DECL
104VCOS_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 */
124VCOS_INLINE_DECL
125VCOS_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 */
135VCOS_INLINE_DECL
136VCOS_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 */
143VCOS_INLINE_DECL
144VCOS_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 */
151VCOS_INLINE_DECL
152void vcos_semaphore_delete(VCOS_SEMAPHORE_T *sem);
153
154#ifdef __cplusplus
155}
156#endif
157#endif
158
159