Mbed OS Reference
Loading...
Searching...
No Matches
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
23extern "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)
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
65void equeue_tick_init(void);
66unsigned 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)
77typedef pthread_mutex_t equeue_mutex_t;
78#elif defined(EQUEUE_PLATFORM_WINDOWS)
79typedef CRITICAL_SECTION equeue_mutex_t;
80#elif defined(EQUEUE_PLATFORM_MBED)
81typedef unsigned equeue_mutex_t;
82#elif defined(EQUEUE_PLATFORM_FREERTOS)
83typedef 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.
94int equeue_mutex_create(equeue_mutex_t *mutex);
95void equeue_mutex_destroy(equeue_mutex_t *mutex);
96void equeue_mutex_lock(equeue_mutex_t *mutex);
97void 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)
112typedef 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
118typedef 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 osEventFlagsId_t _id;
122 mbed_rtos_storage_event_flags_t _obj_mem;
123 uint32_t _flags;
124} equeue_sema_t;
125#elif defined(EQUEUE_PLATFORM_MBED)
126typedef int equeue_sema_t;
127#endif
128
129// Platform semaphore operations
130//
131// The equeue_sema_create and equeue_sema_destroy manage the lifetime
132// of the semaphore. On error, equeue_sema_create should return a negative
133// error code.
134//
135// The equeue_sema_signal marks a semaphore as signalled such that the next
136// equeue_sema_wait will return true.
137//
138// The equeue_sema_wait waits for a semaphore to be signalled or returns
139// immediately if equeue_sema_signal had been called since the last
140// equeue_sema_wait. The equeue_sema_wait returns true if it detected that
141// equeue_sema_signal had been called. If ms is negative, equeue_sema_wait
142// will wait for a signal indefinitely.
143int equeue_sema_create(equeue_sema_t *sema);
144void equeue_sema_destroy(equeue_sema_t *sema);
145void equeue_sema_signal(equeue_sema_t *sema);
146bool equeue_sema_wait(equeue_sema_t *sema, int ms);
147
148#ifdef __cplusplus
149}
150#endif
151
152#endif