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 - event flags implemented via a semaphore |
30 | =============================================================================*/ |
31 | |
32 | #ifndef VCOS_GENERIC_EVENT_FLAGS_H |
33 | #define VCOS_GENERIC_EVENT_FLAGS_H |
34 | |
35 | #ifdef __cplusplus |
36 | extern "C" { |
37 | #endif |
38 | |
39 | #include "interface/vcos/vcos_types.h" |
40 | |
41 | /** |
42 | * \file |
43 | * |
44 | * This provides event flags (as per Nucleus Event Groups) based on a |
45 | * mutex, a semaphore (per waiting thread) and a timer (per waiting |
46 | * thread). |
47 | * |
48 | * The data structure is a 32 bit unsigned int (the current set of |
49 | * flags) and a linked list of clients waiting to be 'satisfied'. |
50 | * |
51 | * The mutex merely locks access to the data structure. If a client |
52 | * calls vcos_event_flags_get() and the requested bits are not already |
53 | * present, it then sleeps on its per-thread semaphore after adding |
54 | * this semaphore to the queue waiting. It also sets up a timer. |
55 | * |
56 | * The per-thread semaphore and timer are actually stored in the |
57 | * thread context (joinable thread). In future it may become necessary |
58 | * to support non-VCOS threads by using thread local storage to |
59 | * create these objects and associate them with the thread. |
60 | */ |
61 | |
62 | struct VCOS_EVENT_WAITER_T; |
63 | |
64 | typedef struct VCOS_EVENT_FLAGS_T |
65 | { |
66 | VCOS_UNSIGNED events; /**< Events currently set */ |
67 | VCOS_MUTEX_T lock; /**< Serialize access */ |
68 | struct |
69 | { |
70 | struct VCOS_EVENT_WAITER_T *head; /**< List of threads waiting */ |
71 | struct VCOS_EVENT_WAITER_T *tail; /**< List of threads waiting */ |
72 | } waiters; |
73 | } VCOS_EVENT_FLAGS_T; |
74 | |
75 | #define VCOS_OR 1 |
76 | #define VCOS_AND 2 |
77 | #define VCOS_CONSUME 4 |
78 | #define VCOS_OR_CONSUME (VCOS_OR | VCOS_CONSUME) |
79 | #define VCOS_AND_CONSUME (VCOS_AND | VCOS_CONSUME) |
80 | #define VCOS_EVENT_FLAG_OP_MASK (VCOS_OR|VCOS_AND) |
81 | |
82 | VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_generic_event_flags_create(VCOS_EVENT_FLAGS_T *flags, const char *name); |
83 | VCOSPRE_ void VCOSPOST_ vcos_generic_event_flags_set(VCOS_EVENT_FLAGS_T *flags, |
84 | VCOS_UNSIGNED events, |
85 | VCOS_OPTION op); |
86 | VCOSPRE_ void VCOSPOST_ vcos_generic_event_flags_delete(VCOS_EVENT_FLAGS_T *); |
87 | VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_generic_event_flags_get(VCOS_EVENT_FLAGS_T *flags, |
88 | VCOS_UNSIGNED requested_events, |
89 | VCOS_OPTION op, |
90 | VCOS_UNSIGNED suspend, |
91 | VCOS_UNSIGNED *retrieved_events); |
92 | |
93 | #ifdef VCOS_INLINE_BODIES |
94 | |
95 | VCOS_INLINE_IMPL |
96 | VCOS_STATUS_T vcos_event_flags_create(VCOS_EVENT_FLAGS_T *flags, const char *name) { |
97 | return vcos_generic_event_flags_create(flags, name); |
98 | } |
99 | |
100 | VCOS_INLINE_IMPL |
101 | void vcos_event_flags_set(VCOS_EVENT_FLAGS_T *flags, |
102 | VCOS_UNSIGNED events, |
103 | VCOS_OPTION op) { |
104 | vcos_generic_event_flags_set(flags, events, op); |
105 | } |
106 | |
107 | VCOS_INLINE_IMPL |
108 | void vcos_event_flags_delete(VCOS_EVENT_FLAGS_T *f) { |
109 | vcos_generic_event_flags_delete(f); |
110 | } |
111 | |
112 | VCOS_INLINE_IMPL |
113 | VCOS_STATUS_T vcos_event_flags_get(VCOS_EVENT_FLAGS_T *flags, |
114 | VCOS_UNSIGNED requested_events, |
115 | VCOS_OPTION op, |
116 | VCOS_UNSIGNED suspend, |
117 | VCOS_UNSIGNED *retrieved_events) { |
118 | return vcos_generic_event_flags_get(flags, requested_events, op, suspend, retrieved_events); |
119 | } |
120 | |
121 | #endif /* VCOS_INLINE_BODIES */ |
122 | |
123 | #ifdef __cplusplus |
124 | } |
125 | #endif |
126 | #endif |
127 | |
128 | |