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