From 055c3d3e1d75fedac44ffb41943350d9844f5b7e Mon Sep 17 00:00:00 2001 From: chenjh Date: Fri, 26 Feb 2021 18:23:52 +0800 Subject: [Update] Sync from v4.0.3 --- components/cplusplus/Mutex.cpp | 43 ------------ components/cplusplus/Semaphore.cpp | 38 ----------- components/cplusplus/Thread.cpp | 116 --------------------------------- components/cplusplus/crt.cpp | 37 ----------- components/cplusplus/cxx_Mutex.cpp | 43 ++++++++++++ components/cplusplus/cxx_Semaphore.cpp | 38 +++++++++++ components/cplusplus/cxx_Thread.cpp | 116 +++++++++++++++++++++++++++++++++ components/cplusplus/cxx_crt.cpp | 37 +++++++++++ 8 files changed, 234 insertions(+), 234 deletions(-) delete mode 100644 components/cplusplus/Mutex.cpp delete mode 100644 components/cplusplus/Semaphore.cpp delete mode 100644 components/cplusplus/Thread.cpp delete mode 100644 components/cplusplus/crt.cpp create mode 100644 components/cplusplus/cxx_Mutex.cpp create mode 100644 components/cplusplus/cxx_Semaphore.cpp create mode 100644 components/cplusplus/cxx_Thread.cpp create mode 100644 components/cplusplus/cxx_crt.cpp (limited to 'components/cplusplus') diff --git a/components/cplusplus/Mutex.cpp b/components/cplusplus/Mutex.cpp deleted file mode 100644 index 229e842446..0000000000 --- a/components/cplusplus/Mutex.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -#include "Mutex.h" - -using namespace rtthread; - -Mutex::Mutex(const char *name) -{ - rt_mutex_init(&mID, name, RT_IPC_FLAG_FIFO); -} - -bool Mutex::lock(int32_t millisec) -{ - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - return rt_mutex_take(&mID, tick) == RT_EOK; -} - -bool Mutex::trylock() -{ - return lock(0); -} - -void Mutex::unlock() -{ - rt_mutex_release(&mID); -} - -Mutex::~Mutex() -{ - rt_mutex_detach(&mID); -} diff --git a/components/cplusplus/Semaphore.cpp b/components/cplusplus/Semaphore.cpp deleted file mode 100644 index c977a27c1a..0000000000 --- a/components/cplusplus/Semaphore.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -#include "Semaphore.h" - -using namespace rtthread; - -Semaphore::Semaphore(const char *name, int32_t count) -{ - rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO); -} - -bool Semaphore::wait(int32_t millisec) -{ - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - return rt_sem_take(&mID, tick) == RT_EOK; -} - -void Semaphore::release(void) -{ - rt_sem_release(&mID); -} - -Semaphore::~Semaphore() -{ - rt_sem_detach(&mID); -} diff --git a/components/cplusplus/Thread.cpp b/components/cplusplus/Thread.cpp deleted file mode 100644 index b21fbdce1f..0000000000 --- a/components/cplusplus/Thread.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -#include "Thread.h" - -using namespace rtthread; - -Thread::Thread(rt_uint32_t stack_size, - rt_uint8_t priority, - rt_uint32_t tick, - const char *name) - : _entry(RT_NULL), _param(RT_NULL), started(false) -{ - rt_event_init(&_event, name, 0); - - _thread = rt_thread_create(name, - (thread_func_t)func, - this, - stack_size, - priority, - tick); -} - -Thread::Thread(void (*entry)(void *p), - void *p, - rt_uint32_t stack_size, - rt_uint8_t priority, - rt_uint32_t tick, - const char *name) - : _entry(entry), _param(p), started(false) -{ - rt_event_init(&_event, name, 0); - - _thread = rt_thread_create(name, - (thread_func_t)func, - this, - stack_size, - priority, - tick); -} - -Thread::~Thread() -{ - rt_event_detach(&_event); - rt_thread_delete(_thread); -} - -bool Thread::start() -{ - if (rt_thread_startup(_thread) == RT_EOK) - { - started = true; - } - - return started; -} - -void Thread::sleep(int32_t millisec) -{ - rt_int32_t tick; - - if (millisec < 0) - tick = 1; - else - tick = rt_tick_from_millisecond(millisec); - - rt_thread_delay(tick); -} - -void Thread::func(Thread *pThis) -{ - if (pThis->_entry != RT_NULL) - { - pThis->_entry(pThis->_param); - } - else - { - pThis->run(pThis->_param); - } - - rt_event_send(&pThis->_event, 1); -} - -void Thread::run(void *parameter) -{ - /* please overload this method */ -} - -rt_err_t Thread::wait(int32_t millisec) -{ - return join(millisec); -} - -rt_err_t Thread::join(int32_t millisec) -{ - if (started) - { - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL); - } - else - { - return -RT_ENOSYS; - } -} diff --git a/components/cplusplus/crt.cpp b/components/cplusplus/crt.cpp deleted file mode 100644 index d4cb84f77c..0000000000 --- a/components/cplusplus/crt.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * -* Change Logs: -* Date Author Notes -* 2015-03-07 Bernard Add copyright header. -*/ - -#include -#include "crt.h" - -void *operator new(size_t size) -{ - return rt_malloc(size); -} - -void *operator new[](size_t size) -{ - return rt_malloc(size); -} - -void operator delete(void *ptr) -{ - rt_free(ptr); -} - -void operator delete[](void *ptr) -{ - return rt_free(ptr); -} - -void __cxa_pure_virtual(void) -{ - rt_kprintf("Illegal to call a pure virtual function.\n"); -} diff --git a/components/cplusplus/cxx_Mutex.cpp b/components/cplusplus/cxx_Mutex.cpp new file mode 100644 index 0000000000..229e842446 --- /dev/null +++ b/components/cplusplus/cxx_Mutex.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ +#include "Mutex.h" + +using namespace rtthread; + +Mutex::Mutex(const char *name) +{ + rt_mutex_init(&mID, name, RT_IPC_FLAG_FIFO); +} + +bool Mutex::lock(int32_t millisec) +{ + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_mutex_take(&mID, tick) == RT_EOK; +} + +bool Mutex::trylock() +{ + return lock(0); +} + +void Mutex::unlock() +{ + rt_mutex_release(&mID); +} + +Mutex::~Mutex() +{ + rt_mutex_detach(&mID); +} diff --git a/components/cplusplus/cxx_Semaphore.cpp b/components/cplusplus/cxx_Semaphore.cpp new file mode 100644 index 0000000000..c977a27c1a --- /dev/null +++ b/components/cplusplus/cxx_Semaphore.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ +#include "Semaphore.h" + +using namespace rtthread; + +Semaphore::Semaphore(const char *name, int32_t count) +{ + rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO); +} + +bool Semaphore::wait(int32_t millisec) +{ + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_sem_take(&mID, tick) == RT_EOK; +} + +void Semaphore::release(void) +{ + rt_sem_release(&mID); +} + +Semaphore::~Semaphore() +{ + rt_sem_detach(&mID); +} diff --git a/components/cplusplus/cxx_Thread.cpp b/components/cplusplus/cxx_Thread.cpp new file mode 100644 index 0000000000..b21fbdce1f --- /dev/null +++ b/components/cplusplus/cxx_Thread.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ +#include "Thread.h" + +using namespace rtthread; + +Thread::Thread(rt_uint32_t stack_size, + rt_uint8_t priority, + rt_uint32_t tick, + const char *name) + : _entry(RT_NULL), _param(RT_NULL), started(false) +{ + rt_event_init(&_event, name, 0); + + _thread = rt_thread_create(name, + (thread_func_t)func, + this, + stack_size, + priority, + tick); +} + +Thread::Thread(void (*entry)(void *p), + void *p, + rt_uint32_t stack_size, + rt_uint8_t priority, + rt_uint32_t tick, + const char *name) + : _entry(entry), _param(p), started(false) +{ + rt_event_init(&_event, name, 0); + + _thread = rt_thread_create(name, + (thread_func_t)func, + this, + stack_size, + priority, + tick); +} + +Thread::~Thread() +{ + rt_event_detach(&_event); + rt_thread_delete(_thread); +} + +bool Thread::start() +{ + if (rt_thread_startup(_thread) == RT_EOK) + { + started = true; + } + + return started; +} + +void Thread::sleep(int32_t millisec) +{ + rt_int32_t tick; + + if (millisec < 0) + tick = 1; + else + tick = rt_tick_from_millisecond(millisec); + + rt_thread_delay(tick); +} + +void Thread::func(Thread *pThis) +{ + if (pThis->_entry != RT_NULL) + { + pThis->_entry(pThis->_param); + } + else + { + pThis->run(pThis->_param); + } + + rt_event_send(&pThis->_event, 1); +} + +void Thread::run(void *parameter) +{ + /* please overload this method */ +} + +rt_err_t Thread::wait(int32_t millisec) +{ + return join(millisec); +} + +rt_err_t Thread::join(int32_t millisec) +{ + if (started) + { + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} diff --git a/components/cplusplus/cxx_crt.cpp b/components/cplusplus/cxx_crt.cpp new file mode 100644 index 0000000000..d4cb84f77c --- /dev/null +++ b/components/cplusplus/cxx_crt.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * +* Change Logs: +* Date Author Notes +* 2015-03-07 Bernard Add copyright header. +*/ + +#include +#include "crt.h" + +void *operator new(size_t size) +{ + return rt_malloc(size); +} + +void *operator new[](size_t size) +{ + return rt_malloc(size); +} + +void operator delete(void *ptr) +{ + rt_free(ptr); +} + +void operator delete[](void *ptr) +{ + return rt_free(ptr); +} + +void __cxa_pure_virtual(void) +{ + rt_kprintf("Illegal to call a pure virtual function.\n"); +} -- cgit v1.3.1