|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Particle Industries, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation, either |
| 7 | + * version 3 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <cxxabi.h> |
| 19 | +#include <stdint.h> |
| 20 | +#include "service_debug.h" |
| 21 | +#include "static_recursive_mutex.h" |
| 22 | +#include "interrupts_hal.h" |
| 23 | +#include "static_event_group.h" |
| 24 | +#include "concurrent_hal.h" |
| 25 | +#include <mutex> |
| 26 | + |
| 27 | +/* __guard type is already defined in cxxabi_tweaks.h for each architecture */ |
| 28 | +using __cxxabiv1::__guard; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +/* Using a global recursive mutex, should be enough for our use-cases */ |
| 33 | +StaticRecursiveMutex s_mutex; |
| 34 | +StaticEventGroup s_event_group; |
| 35 | + |
| 36 | +struct __attribute__((packed)) guard_t { |
| 37 | + /* Normally this is: |
| 38 | + * Guard Object Layout: |
| 39 | + * --------------------------------------------------------------------------- |
| 40 | + * | a+0: guard byte | a+1: init byte | a+2: unused ... | a+4: thread-id ... | |
| 41 | + * --------------------------------------------------------------------------- |
| 42 | + * On ARM this is just 4 bytes |
| 43 | + * */ |
| 44 | + uint8_t done; |
| 45 | + uint8_t init; |
| 46 | + uint8_t wait_count; |
| 47 | + |
| 48 | + enum GuardFlags : uint8_t { |
| 49 | + NONE = 0, |
| 50 | + COMPLETE = 0x01, |
| 51 | + PENDING = 0x02, |
| 52 | + WAITING = 0x04 |
| 53 | + }; |
| 54 | +}; |
| 55 | + |
| 56 | +static_assert(sizeof(guard_t) <= sizeof(__guard), "guard is too large"); |
| 57 | + |
| 58 | +} /* anonymous */ |
| 59 | + |
| 60 | +/* http://refspecs.linuxbase.org/cxxabi-1.86.html#once-ctor */ |
| 61 | + |
| 62 | +extern "C" { |
| 63 | + |
| 64 | +int __cxa_guard_acquire(__guard* g) { |
| 65 | + guard_t* guard = reinterpret_cast<guard_t*>(g); |
| 66 | + |
| 67 | + SPARK_ASSERT(!hal_interrupt_is_isr()); |
| 68 | + |
| 69 | + /* Acquire mutex */ |
| 70 | + std::unique_lock<StaticRecursiveMutex> lk(s_mutex); |
| 71 | + |
| 72 | + while (true) { |
| 73 | + // Nothing to do here, already initialized |
| 74 | + if (guard->done) { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + if (guard->init & guard_t::PENDING) { |
| 79 | + // Pending initialization |
| 80 | + // We need to wait for initialization to complete |
| 81 | + // Scheduler MUST be running |
| 82 | + auto scheduler = os_scheduler_get_state(nullptr); |
| 83 | + SPARK_ASSERT(scheduler == OS_SCHEDULER_STATE_RUNNING); |
| 84 | + |
| 85 | + guard->init |= guard_t::WAITING; |
| 86 | + guard->wait_count++; |
| 87 | + lk.unlock(); |
| 88 | + s_event_group.wait(guard_t::COMPLETE, StaticEventGroup::Flag::CLEAR_ON_EXIT); |
| 89 | + lk.lock(); |
| 90 | + guard->wait_count--; |
| 91 | + if (guard->wait_count == 0) { |
| 92 | + s_event_group.set(guard_t::WAITING); |
| 93 | + } |
| 94 | + } else { |
| 95 | + // Set pending flag continue with initialization |
| 96 | + guard->init |= guard_t::PENDING; |
| 97 | + return 1; |
| 98 | + } |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +void __cxa_guard_release(__guard* g) { |
| 104 | + guard_t* guard = reinterpret_cast<guard_t*>(g); |
| 105 | + SPARK_ASSERT(!hal_interrupt_is_isr()); |
| 106 | + |
| 107 | + std::unique_lock<StaticRecursiveMutex> lk(s_mutex); |
| 108 | + guard->init &= ~(guard_t::PENDING); |
| 109 | + guard->done = guard_t::COMPLETE; |
| 110 | + guard->init |= guard_t::COMPLETE; |
| 111 | + if ((guard->init & guard_t::WAITING) && guard->wait_count > 0) { |
| 112 | + // This will wake all threads waiting on initialization completion |
| 113 | + s_event_group.set(guard_t::COMPLETE); |
| 114 | + } else { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + while (guard->wait_count > 0) { |
| 119 | + lk.unlock(); |
| 120 | + // FIXME: not ideal, but we'll spin with 10ms delay |
| 121 | + auto ev = s_event_group.sync(guard_t::COMPLETE, guard_t::WAITING, 10); |
| 122 | + lk.lock(); |
| 123 | + if (ev & guard_t::WAITING) { |
| 124 | + s_event_group.clear(guard_t::WAITING); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +void __cxa_guard_abort (__guard*) { |
| 130 | + /* We do not have exceptions enabled. This should not have happened */ |
| 131 | + SPARK_ASSERT(false); |
| 132 | +} |
| 133 | + |
| 134 | +void __cxa_pure_virtual() { |
| 135 | + PANIC(PureVirtualCall, "Call on pure virtual"); |
| 136 | + while (1); |
| 137 | +} |
| 138 | + |
| 139 | +} // extern "C" |
0 commit comments