Mistake on this page?
Report an issue in GitHub or email us
equeue_platform.h
1 
2 /** \addtogroup events */
3 /** @{*/
4 /*
5  * System specific implementation
6  *
7  * Copyright (c) 2016 Christopher Haster
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 #ifndef EQUEUE_PLATFORM_H
22 #define EQUEUE_PLATFORM_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <stdbool.h>
29 
30 // Currently supported platforms
31 //
32 // Uncomment to select a supported platform or reimplement this file
33 // for a specific target.
34 //#define EQUEUE_PLATFORM_POSIX
35 //#define EQUEUE_PLATFORM_MBED
36 
37 // Try to infer a platform if none was manually selected
38 #if !defined(EQUEUE_PLATFORM_POSIX) \
39  && !defined(EQUEUE_PLATFORM_MBED)
40 #if defined(__unix__)
41 #define EQUEUE_PLATFORM_POSIX
42 #elif defined(__MBED__)
43 #define EQUEUE_PLATFORM_MBED
44 #else
45 #warning "Unknown platform! Please update equeue_platform.h"
46 #endif
47 #endif
48 
49 // Platform includes
50 #if defined(EQUEUE_PLATFORM_POSIX)
51 #include <pthread.h>
52 #elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT)
53 #include "cmsis_os2.h"
54 #include "mbed_rtos_storage.h"
55 #endif
56 
57 
58 // Platform millisecond counter
59 //
60 // Return a tick that represents the number of milliseconds that have passed
61 // since an arbitrary point in time. The granularity does not need to be at
62 // the millisecond level, however the accuracy of the equeue library is
63 // limited by the accuracy of this tick.
64 //
65 // Must intentionally overflow to 0 after 2^32-1
66 unsigned equeue_tick(void);
67 
68 
69 // Platform mutex type
70 //
71 // The equeue library requires at minimum a non-recursive mutex that is
72 // safe in interrupt contexts. The mutex section is help for a bounded
73 // amount of time, so simply disabling interrupts is acceptable
74 //
75 // If irq safety is not required, a regular blocking mutex can be used.
76 #if defined(EQUEUE_PLATFORM_POSIX)
77 typedef pthread_mutex_t equeue_mutex_t;
78 #elif defined(EQUEUE_PLATFORM_WINDOWS)
79 typedef CRITICAL_SECTION equeue_mutex_t;
80 #elif defined(EQUEUE_PLATFORM_MBED)
81 typedef unsigned equeue_mutex_t;
82 #elif defined(EQUEUE_PLATFORM_FREERTOS)
83 typedef UBaseType_t equeue_mutex_t;
84 #endif
85 
86 // Platform mutex operations
87 //
88 // The equeue_mutex_create and equeue_mutex_destroy manage the lifetime
89 // of the mutex. On error, equeue_mutex_create should return a negative
90 // error code.
91 //
92 // The equeue_mutex_lock and equeue_mutex_unlock lock and unlock the
93 // underlying mutex.
94 int equeue_mutex_create(equeue_mutex_t *mutex);
95 void equeue_mutex_destroy(equeue_mutex_t *mutex);
96 void equeue_mutex_lock(equeue_mutex_t *mutex);
97 void equeue_mutex_unlock(equeue_mutex_t *mutex);
98 
99 
100 // Platform semaphore type
101 //
102 // The equeue library requires a binary semaphore type that can be safely
103 // signaled from interrupt contexts and from inside a equeue_mutex section.
104 //
105 // The equeue_signal_wait is relied upon by the equeue library to sleep the
106 // processor between events. Spurious wakeups have no negative-effects.
107 //
108 // A counting semaphore will also work, however may cause the event queue
109 // dispatch loop to run unnecessarily. For that matter, equeue_signal_wait
110 // may even be implemented as a single return statement.
111 #if defined(EQUEUE_PLATFORM_POSIX)
112 typedef struct equeue_sema {
113  pthread_mutex_t mutex;
114  pthread_cond_t cond;
115  bool signal;
116 } equeue_sema_t;
117 #elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT)
118 typedef struct equeue_sema {
119  osEventFlagsId_t id;
120  mbed_rtos_storage_event_flags_t mem;
121 } equeue_sema_t;
122 #elif defined(EQUEUE_PLATFORM_MBED)
123 typedef volatile int equeue_sema_t;
124 #endif
125 
126 // Platform semaphore operations
127 //
128 // The equeue_sema_create and equeue_sema_destroy manage the lifetime
129 // of the semaphore. On error, equeue_sema_create should return a negative
130 // error code.
131 //
132 // The equeue_sema_signal marks a semaphore as signalled such that the next
133 // equeue_sema_wait will return true.
134 //
135 // The equeue_sema_wait waits for a semaphore to be signalled or returns
136 // immediately if equeue_sema_signal had been called since the last
137 // equeue_sema_wait. The equeue_sema_wait returns true if it detected that
138 // equeue_sema_signal had been called. If ms is negative, equeue_sema_wait
139 // will wait for a signal indefinitely.
140 int equeue_sema_create(equeue_sema_t *sema);
141 void equeue_sema_destroy(equeue_sema_t *sema);
142 void equeue_sema_signal(equeue_sema_t *sema);
143 bool equeue_sema_wait(equeue_sema_t *sema, int ms);
144 
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif
151 
152 /** @}*/
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.