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  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #ifndef EQUEUE_PLATFORM_H
20 #define EQUEUE_PLATFORM_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 // Currently supported platforms
30 //
31 // Uncomment to select a supported platform or reimplement this file
32 // for a specific target.
33 //#define EQUEUE_PLATFORM_POSIX
34 //#define EQUEUE_PLATFORM_MBED
35 
36 // Try to infer a platform if none was manually selected
37 #if !defined(EQUEUE_PLATFORM_POSIX) \
38  && !defined(EQUEUE_PLATFORM_MBED)
39 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__MINGW32__) || defined(__MINGW64__)
40 #define EQUEUE_PLATFORM_POSIX
41 #elif defined(__MBED__)
42 #define EQUEUE_PLATFORM_MBED
43 #else
44 #warning "Unknown platform! Please update equeue_platform.h"
45 #endif
46 #endif
47 
48 // Platform includes
49 #if defined(EQUEUE_PLATFORM_POSIX)
50 #include <pthread.h>
51 #elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT)
52 #include "cmsis_os2.h"
53 #include "mbed_rtos_storage.h"
54 #endif
55 
56 
57 // Platform millisecond counter
58 //
59 // Return a tick that represents the number of milliseconds that have passed
60 // since an arbitrary point in time. The granularity does not need to be at
61 // the millisecond level, however the accuracy of the equeue library is
62 // limited by the accuracy of this tick.
63 //
64 // Must intentionally overflow to 0 after 2^32-1
65 void equeue_tick_init(void);
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) && MBED_CONF_RTOS_API_PRESENT
118 typedef struct equeue_sema {
119  // We will actually store a C++ rtos:EventQueue in here;
120  // attempt to match layout for storage, and assert size in equeue_mbed.cpp
121 #if MBED_CONF_RTOS_PRESENT
122  osEventFlagsId_t _id;
123  mbed_rtos_storage_event_flags_t _obj_mem;
124 #else
125  uint32_t _flags;
126 #endif
127 } equeue_sema_t;
128 #elif defined(EQUEUE_PLATFORM_MBED)
129 typedef int equeue_sema_t;
130 #endif
131 
132 // Platform semaphore operations
133 //
134 // The equeue_sema_create and equeue_sema_destroy manage the lifetime
135 // of the semaphore. On error, equeue_sema_create should return a negative
136 // error code.
137 //
138 // The equeue_sema_signal marks a semaphore as signalled such that the next
139 // equeue_sema_wait will return true.
140 //
141 // The equeue_sema_wait waits for a semaphore to be signalled or returns
142 // immediately if equeue_sema_signal had been called since the last
143 // equeue_sema_wait. The equeue_sema_wait returns true if it detected that
144 // equeue_sema_signal had been called. If ms is negative, equeue_sema_wait
145 // will wait for a signal indefinitely.
146 int equeue_sema_create(equeue_sema_t *sema);
147 void equeue_sema_destroy(equeue_sema_t *sema);
148 void equeue_sema_signal(equeue_sema_t *sema);
149 bool equeue_sema_wait(equeue_sema_t *sema, int ms);
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif
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.