Simple Collections
Loading...
Searching...
No Matches
SCThreadingSupport.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry)
3 * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4 */
5
6#ifndef SIMPLECOLLECTIONS_SCTHREADINGSUPPORT_H
7#define SIMPLECOLLECTIONS_SCTHREADINGSUPPORT_H
8
13
14#if defined(BUILD_FOR_PICO_CMAKE)
15#include <pico/stdlib.h>
16#elif !defined(__MBED__)
17// when not on mbed, we need to load Arduino.h to get the right defines for some boards.
18#include <Arduino.h>
19#endif
20
29
30// You can add your own local definitions header file here, this enables you to adjust build flags in environments
31// where there is no easy way to do so with compiler options. Just create an include file "io_local_definitions.h"
32// at the top level of your project source tree. This file will be honoured by all our libraries.
33#if defined __has_include
34# if __has_include ("zio_local_definitions.h")
35# include "zio_local_definitions.h"
36# endif
37#endif // has include "io_local_definitions"
38
39#include <inttypes.h>
40
41// START PROCESSOR/BOARD SELECTION BLOCK
42#if defined(ARDUINO_PICO_REVISION) || defined(BUILD_FOR_PICO_CMAKE)
43typedef volatile uint32_t* position_ptr_t;
44typedef volatile uint32_t position_t;
45bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal);
46inline position_t readAtomic(position_ptr_t ptr) { return *(ptr); }
47void atomicInitialisationSupport();
48#define SIMPLECOLLECTIONS_PICO_PHT_SUPPORT
49#elif defined(__MBED__) || defined(TMIOA_FORCE_ARDUINO_MBED)
50#include <mbed_atomic.h>
51typedef volatile uint32_t* position_ptr_t;
52typedef volatile uint32_t position_t;
53#define atomicInitialisationSupport()
54inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
55 uint32_t exp = expected;
56 return core_util_atomic_cas_u32(ptr, &exp, newVal);
57}
58inline uint32_t readAtomic(position_ptr_t ptr) { return *(ptr); }
59#elif (defined(SC_USE_ARM_ASM_CAS) || defined(ARDUINO_ARCH_STM32)) && !defined(SC_NO_ARM_ASM_CAS)
60#define atomicInitialisationSupport()
61#include <Arduino.h>
62#if __CORTEX_M > 0x03U
63#define SIMPLE_COLLECTIONS_ARM_SUPPORT
64typedef volatile uint32_t* position_ptr_t;
65typedef volatile uint32_t position_t;
66bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal);
67inline uint32_t readAtomic(position_ptr_t ptr) { return *(ptr); }
68#else
69typedef volatile uint32_t* position_ptr_t;
70typedef volatile uint32_t position_t;
71#define NEEDS_CAS_EMULATION
72#endif // __CORTEX_M > 3
73#elif defined(ESP8266)
74#include <Arduino.h>
75typedef volatile uint32_t* position_ptr_t;
76typedef volatile uint32_t position_t;
77#define atomicInitialisationSupport()
78#define NEEDS_CAS_EMULATION
79#elif defined(ESP32)
80#include <Arduino.h>
81typedef volatile uint32_t* position_ptr_t;
82typedef volatile uint32_t position_t;
83#define atomicInitialisationSupport()
84#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
85inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
86 uint32_t exp32 = expected;
87 uint32_t new32 = newVal;
88 uxPortCompareSet(ptr, exp32, &new32);
89 return new32 == expected;
90}
91#else
92inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
93 // function added in ESP-IDF v5.0
94 return esp_cpu_compare_and_set(ptr, expected, newVal);
95}
96#endif // ESP_IDF_VERSION < 5
97inline uint16_t readAtomic(position_ptr_t ptr) { return *(ptr); }
98#else
99#include <Arduino.h>
100#define atomicInitialisationSupport()
101typedef volatile uint16_t* position_ptr_t;
102typedef volatile uint16_t position_t;
103#define NEEDS_CAS_EMULATION
104#endif // PROCESSOR/BOARD SELECTION BLOCK
105
106#ifdef NEEDS_CAS_EMULATION
107bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal);
108inline uint16_t readAtomic(position_ptr_t ptr) { return *ptr; }
109#endif // NEEDS_CAS_EMULATION
110
111#endif //SIMPLECOLLECTIONS_SCTHREADINGSUPPORT_H