This is a fork of the `events` subdirectory of https://github.com/ARMmbed/mbed-os

Dependents:   HelloWorld_CCA01M1 HelloWorld_CCA02M1 CI-data-logger-server HelloWorld_CCA02M1 ... more

This is a fork of the events subdirectory of https://github.com/ARMmbed/mbed-os.

Note, you must import this library with import name: events!!!

Committer:
Bogdan Marinescu
Date:
Thu Sep 29 12:38:02 2016 +0300
Revision:
0:a792d4bf36c2
Child:
5:705843a08e16
Added mbed-events library

Added mbed-events from https://github.com/ARMMbed/mbed-events. Changes
from upstream:

- the whole code is licensed under the Apache license. Sources and
headers were updates with this information.
- removed the porting layers for Windows and FreeRTOS and the references
to these porting layers in equeue_platform.h.
- moved the TESTS directory in mbed-events to the TESTS directory of
mbed-os.

Who changed what in which revision?

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