Merge branch 'feature/cplusplus' into 'master'

C++ support

This change adds necessary support for compiling C++ programs:
- linking against libstdc++
- implementation of static initialization guards using FreeRTOS primitives: since we don't have condition variables at our disposal, and we don't want to allocate a synchronization primitive for every guard variable generated by the compiler, we imitate condition variables using a combination of a mutex, counting semaphore, and a counter (based on [Microsoft Research paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2004/12/ImplementingCVs.pdf), albeit because we don't need *arbitrary* code to use these CVs, implementation gets simpler).

Note that libstdc++ also contains an implementation of `__cxa_guard_{acquire,release,abort}` functions. These implementations come from an `#ifndef GXX_THREADS` branch, i.e. are not aware of multthreading. There are three ways of replacing these libstdc++ functions with our implementation:

1. Move our code into gcc. Pros: cleanest solution. Cons: Such changes are unlikely to be merged by any upstream, so we end up maintaining our own forks of {gcc,crosstool-ng}.
2. Use library as it is built by crosstool, use `ar` to delete one object file (`guards.o`), add this library to ESP-IDF. Pros: easy to implement. Cons: libstdc++ is a 15MB binary 😯 
3. Keep using libstdc++ from crosstool, force our implementation to be linked using a `-u` linker flag. Pros: no impact on repo size, easy to implement. Cons: somewhat less clean than 1 (and about as hacky as 2).

For the reasons mentioned, option (3) looks like the best tradeoff.

Ref. TW6702

See merge request !364
This commit is contained in:
Ivan Grokhotkov 2017-01-07 18:50:12 +08:00
commit b1c754bbb5
7 changed files with 434 additions and 115 deletions

View file

@ -0,0 +1,3 @@
# Mark __cxa_guard_dummy as undefined so that implementation of static guards
# is taken from cxx_guards.o instead of libstdc++.a
COMPONENT_ADD_LDFLAGS := -l$(COMPONENT_NAME) -u __cxa_guard_dummy

View file

@ -0,0 +1,216 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <assert.h>
#include <cxxabi.h>
#include <stdint.h>
#include <limits.h>
#include <algorithm>
#include <sys/lock.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
using __cxxabiv1::__guard;
static SemaphoreHandle_t s_static_init_mutex = NULL; //!< lock used for the critical section
static SemaphoreHandle_t s_static_init_wait_sem = NULL; //!< counting semaphore used by the waiting tasks
static portMUX_TYPE s_init_spinlock = portMUX_INITIALIZER_UNLOCKED; //!< spinlock used to guard initialization of the above two primitives
static size_t s_static_init_waiting_count = 0; //!< number of tasks which are waiting for static init guards
#ifndef _NDEBUG
static size_t s_static_init_max_waiting_count = 0; //!< maximum ever value of the above; can be inspected using GDB for debugging purposes
#endif
/**
* Layout of the guard object (defined by the ABI).
*
* Compiler will check lower byte before calling guard functions.
*/
typedef struct {
uint8_t ready; //!< nonzero if initialization is done
uint8_t pending; //!< nonzero if initialization is in progress
} guard_t;
static void static_init_prepare()
{
portENTER_CRITICAL(&s_init_spinlock);
if (s_static_init_mutex == NULL) {
s_static_init_mutex = xSemaphoreCreateMutex();
s_static_init_wait_sem = xSemaphoreCreateCounting(INT_MAX, 0);
if (s_static_init_mutex == NULL || s_static_init_wait_sem == NULL) {
// no way to bail out of static initialization without these
abort();
}
}
portEXIT_CRITICAL(&s_init_spinlock);
}
/**
* Use s_static_init_wait_sem to wait until guard->pending == 0.
* Preconditions:
* - s_static_init_mutex taken
* - guard.pending == 1
* Postconditions:
* - s_static_init_mutex taken
* - guard.pending == 0
*/
static void wait_for_guard_obj(guard_t* g)
{
s_static_init_waiting_count++;
#ifndef _NDEBUG
s_static_init_max_waiting_count = std::max(s_static_init_waiting_count,
s_static_init_max_waiting_count);
#endif
do {
auto result = xSemaphoreGive(s_static_init_mutex);
assert(result);
/* Task may be preempted here, but this isn't a problem,
* as the semaphore will be given exactly the s_static_init_waiting_count
* number of times; eventually the current task will execute next statement,
* which will immediately succeed.
*/
result = xSemaphoreTake(s_static_init_wait_sem, portMAX_DELAY);
assert(result);
/* At this point the semaphore was given, so all waiting tasks have woken up.
* We take s_static_init_mutex before accessing the state of the guard
* object again.
*/
result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
assert(result);
/* Semaphore may have been given because some other guard object became ready.
* Check the guard object we need and wait again if it is still pending.
*/
} while(g->pending);
s_static_init_waiting_count--;
}
/**
* Unblock tasks waiting for static initialization to complete.
* Preconditions:
* - s_static_init_mutex taken
* Postconditions:
* - s_static_init_mutex taken
*/
static void signal_waiting_tasks()
{
auto count = s_static_init_waiting_count;
while (count--) {
xSemaphoreGive(s_static_init_wait_sem);
}
}
extern "C" int __cxa_guard_acquire(__guard* pg)
{
guard_t* g = reinterpret_cast<guard_t*>(pg);
const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
if (!scheduler_started) {
if (g->pending) {
/* Before the scheduler has started, there we don't support simultaneous
* static initialization. This may be implemented using a spinlock and a
* s32c1i instruction, though.
*/
abort();
}
} else {
if (s_static_init_mutex == NULL) {
static_init_prepare();
}
/* We don't need to use double-checked locking pattern here, as the compiler
* must generate code to check if the first byte of *pg is non-zero, before
* calling __cxa_guard_acquire.
*/
auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
assert(result);
if (g->pending) {
/* Another task is doing initialization at the moment; wait until it calls
* __cxa_guard_release or __cxa_guard_abort
*/
wait_for_guard_obj(g);
/* At this point there are two scenarios:
* - the task which was doing static initialization has called __cxa_guard_release,
* which means that g->ready is set. We need to return 0.
* - the task which was doing static initialization has called __cxa_guard_abort,
* which means that g->ready is not set; we should acquire the guard and return 1,
* same as for the case if we didn't have to wait.
* Note: actually the second scenario is unlikely to occur in the current
* configuration because exception support is disabled.
*/
}
}
int ret;
if (g->ready) {
/* Static initialization has been done by another task; nothing to do here */
ret = 0;
} else {
/* Current task can start doing static initialization */
g->pending = 1;
ret = 1;
}
if (scheduler_started) {
auto result = xSemaphoreGive(s_static_init_mutex);
assert(result);
}
return ret;
}
extern "C" void __cxa_guard_release(__guard* pg)
{
guard_t* g = reinterpret_cast<guard_t*>(pg);
const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
if (scheduler_started) {
auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
assert(result);
}
assert(g->pending && "tried to release a guard which wasn't acquired");
g->pending = 0;
/* Initialization was successful */
g->ready = 1;
if (scheduler_started) {
/* Unblock the tasks waiting for static initialization to complete */
signal_waiting_tasks();
auto result = xSemaphoreGive(s_static_init_mutex);
assert(result);
}
}
extern "C" void __cxa_guard_abort(__guard* pg)
{
guard_t* g = reinterpret_cast<guard_t*>(pg);
const auto scheduler_started = xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
if (scheduler_started) {
auto result = xSemaphoreTake(s_static_init_mutex, portMAX_DELAY);
assert(result);
}
assert(!g->ready && "tried to abort a guard which is ready");
assert(g->pending && "tried to release a guard which is not acquired");
g->pending = 0;
if (scheduler_started) {
/* Unblock the tasks waiting for static initialization to complete */
signal_waiting_tasks();
auto result = xSemaphoreGive(s_static_init_mutex);
assert(result);
}
}
/**
* Dummy function used to force linking this file instead of the same one in libstdc++.
* This works via -u __cxa_guard_dummy flag in component.mk
*/
extern "C" void __cxa_guard_dummy()
{
}

View file

@ -0,0 +1 @@
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View file

@ -0,0 +1,203 @@
#include <functional>
#include <vector>
#include <algorithm>
#include "unity.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
static const char* TAG = "cxx";
TEST_CASE("can use new and delete", "[cxx]")
{
int* int_p = new int(10);
delete int_p;
int* int_array = new int[10];
delete[] int_array;
}
class Base
{
public:
virtual ~Base() {}
virtual void foo() = 0;
};
class Derived : public Base
{
public:
virtual void foo() { }
};
TEST_CASE("can call virtual functions", "[cxx]")
{
Derived d;
Base& b = static_cast<Base&>(d);
b.foo();
}
class NonPOD
{
public:
NonPOD(int a_) : a(a_) { }
int a;
};
static int non_pod_test_helper(int new_val)
{
static NonPOD non_pod(42);
int ret = non_pod.a;
non_pod.a = new_val;
return ret;
}
TEST_CASE("can use static initializers for non-POD types", "[cxx]")
{
TEST_ASSERT_EQUAL(42, non_pod_test_helper(1));
TEST_ASSERT_EQUAL(1, non_pod_test_helper(0));
}
TEST_CASE("can call std::function and bind", "[cxx]")
{
int outer = 1;
std::function<int(int)> fn = [&outer](int x) -> int {
return x + outer;
};
outer = 5;
TEST_ASSERT_EQUAL(6, fn(1));
auto bound = std::bind(fn, outer);
outer = 10;
TEST_ASSERT_EQUAL(15, bound());
}
TEST_CASE("can use std::vector", "[cxx]")
{
std::vector<int> v(10, 1);
v[0] = 42;
TEST_ASSERT_EQUAL(51, std::accumulate(std::begin(v), std::end(v), 0));
}
/*
* This test exercises static initialization guards for two objects.
* For each object, 4 tasks are created which attempt to perform static initialization.
* We check that constructor runs only once for each object.
*/
static SemaphoreHandle_t s_slow_init_sem = NULL;
template<int obj>
class SlowInit
{
public:
SlowInit(int arg) {
ESP_LOGD(TAG, "init obj=%d start, arg=%d\n", obj, arg);
vTaskDelay(300/portTICK_PERIOD_MS);
TEST_ASSERT_EQUAL(-1, mInitBy);
TEST_ASSERT_EQUAL(0, mInitCount);
mInitBy = arg;
++mInitCount;
ESP_LOGD(TAG, "init obj=%d done\n", obj);
}
static void task(void* arg) {
int taskId = reinterpret_cast<int>(arg);
ESP_LOGD(TAG, "obj=%d before static init, task=%d\n", obj, taskId);
static SlowInit slowinit(taskId);
ESP_LOGD(TAG, "obj=%d after static init, task=%d\n", obj, taskId);
xSemaphoreGive(s_slow_init_sem);
vTaskDelay(10);
vTaskDelete(NULL);
}
private:
static int mInitBy;
static int mInitCount;
};
template<> int SlowInit<1>::mInitBy = -1;
template<> int SlowInit<1>::mInitCount = 0;
template<> int SlowInit<2>::mInitBy = -1;
template<> int SlowInit<2>::mInitCount = 0;
template<int obj>
static void start_slow_init_task(int id, int affinity)
{
xTaskCreatePinnedToCore(&SlowInit<obj>::task, "slow_init", 2048,
reinterpret_cast<void*>(id), 3, NULL, affinity);
}
TEST_CASE("static initialization guards work as expected", "[cxx]")
{
s_slow_init_sem = xSemaphoreCreateCounting(10, 0);
TEST_ASSERT_NOT_NULL(s_slow_init_sem);
// four tasks competing for static initialization of one object
start_slow_init_task<1>(0, PRO_CPU_NUM);
start_slow_init_task<1>(1, APP_CPU_NUM);
start_slow_init_task<1>(2, PRO_CPU_NUM);
start_slow_init_task<1>(3, tskNO_AFFINITY);
// four tasks competing for static initialization of another object
start_slow_init_task<2>(0, PRO_CPU_NUM);
start_slow_init_task<2>(1, APP_CPU_NUM);
start_slow_init_task<2>(2, PRO_CPU_NUM);
start_slow_init_task<2>(3, tskNO_AFFINITY);
// All tasks should
for (int i = 0; i < 8; ++i) {
TEST_ASSERT_TRUE(xSemaphoreTake(s_slow_init_sem, 500/portTICK_PERIOD_MS));
}
vSemaphoreDelete(s_slow_init_sem);
}
struct GlobalInitTest
{
GlobalInitTest() : index(order++) {
}
int index;
static int order;
};
int GlobalInitTest::order = 0;
GlobalInitTest g_init_test1;
GlobalInitTest g_init_test2;
GlobalInitTest g_init_test3;
TEST_CASE("global initializers run in the correct order", "[cxx]")
{
TEST_ASSERT_EQUAL(0, g_init_test1.index);
TEST_ASSERT_EQUAL(1, g_init_test2.index);
TEST_ASSERT_EQUAL(2, g_init_test3.index);
}
struct StaticInitTestBeforeScheduler
{
StaticInitTestBeforeScheduler()
{
static int first_init_order = getOrder();
index = first_init_order;
}
int getOrder()
{
return order++;
}
int index;
static int order;
};
int StaticInitTestBeforeScheduler::order = 1;
StaticInitTestBeforeScheduler g_static_init_test1;
StaticInitTestBeforeScheduler g_static_init_test2;
StaticInitTestBeforeScheduler g_static_init_test3;
TEST_CASE("before scheduler has started, static initializers work correctly", "[cxx]")
{
TEST_ASSERT_EQUAL(1, g_static_init_test1.index);
TEST_ASSERT_EQUAL(1, g_static_init_test2.index);
TEST_ASSERT_EQUAL(1, g_static_init_test3.index);
TEST_ASSERT_EQUAL(2, StaticInitTestBeforeScheduler::order);
}

View file

@ -1,114 +0,0 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <assert.h>
#include <cxxabi.h>
#include <stdint.h>
// using __cxxabiv1::__guard;
void *operator new(size_t size)
{
return malloc(size);
}
void *operator new[](size_t size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
void operator delete[](void * ptr)
{
free(ptr);
}
extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
void __cxa_pure_virtual(void)
{
abort();
}
void __cxa_deleted_virtual(void)
{
abort();
}
#if 0
typedef struct {
uint8_t guard;
uint8_t ps;
} guard_t;
extern "C" int __cxa_guard_acquire(__guard* pg)
{
uint8_t ps = xt_rsil(15);
if (reinterpret_cast<guard_t*>(pg)->guard) {
xt_wsr_ps(ps);
return 0;
}
reinterpret_cast<guard_t*>(pg)->ps = ps;
return 1;
}
extern "C" void __cxa_guard_release(__guard* pg)
{
reinterpret_cast<guard_t*>(pg)->guard = 1;
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
}
extern "C" void __cxa_guard_abort(__guard* pg)
{
xt_wsr_ps(reinterpret_cast<guard_t*>(pg)->ps);
}
#endif
extern "C" void __cxa_throw_bad_array_new_length()
{
abort();
}
namespace std
{
void __throw_bad_function_call()
{
abort();
}
void __throw_length_error(char const*)
{
abort();
}
void __throw_bad_alloc()
{
abort();
}
void __throw_logic_error(const char* str)
{
abort();
}
void __throw_out_of_range(const char* str)
{
abort();
}
}

View file

@ -187,6 +187,7 @@ LDFLAGS ?= -nostdlib \
-Wl,--start-group \
$(COMPONENT_LDFLAGS) \
-lgcc \
-lstdc++ \
-Wl,--end-group \
-Wl,-EL

View file

@ -61,11 +61,20 @@ void unity_run_all_tests();
.desc = desc_, \
.fn = &UNITY_TEST_UID(test_func_), \
.file = __FILE__, \
.line = __LINE__ \
.line = __LINE__, \
.next = NULL \
}; \
unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
}\
static void UNITY_TEST_UID(test_func_) (void)
/**
* Note: initialization of test_desc_t fields above has to be done exactly
* in the same order as the fields are declared in the structure.
* Otherwise the initializer will not be valid in C++ (which doesn't
* support designated initializers). G++ can parse the syntax, but
* field names are treated as annotations and don't affect initialization
* order. Also make sure all the fields are initialized.
*/
// shorthand to check esp_err_t return code
#define TEST_ESP_OK(rc) TEST_ASSERT_EQUAL_INT32(ESP_OK, rc)