From 3c05a4e7193ee553bf502560e7de4be6489c7629 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Mon, 8 Mar 2021 18:19:04 +0800 Subject: [compoenets] auto & manual formatted --- components/cplusplus/cxx_Thread.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'components/cplusplus/cxx_Thread.cpp') diff --git a/components/cplusplus/cxx_Thread.cpp b/components/cplusplus/cxx_Thread.cpp index b21fbdce1f..9689f6691b 100644 --- a/components/cplusplus/cxx_Thread.cpp +++ b/components/cplusplus/cxx_Thread.cpp @@ -1,11 +1,12 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes */ + #include "Thread.h" using namespace rtthread; -- cgit v1.3.1 From 9f2616e32313bd3c642ed46e72707f80f8272de3 Mon Sep 17 00:00:00 2001 From: liuxianliang Date: Wed, 13 Oct 2021 15:20:29 +0800 Subject: [modify] those file name is risky if you use both "c++" and "POSIX pthread". --- components/cplusplus/Lock.h | 28 ------------ components/cplusplus/Mail.h | 81 ---------------------------------- components/cplusplus/Mutex.h | 46 ------------------- components/cplusplus/Queue.h | 74 ------------------------------- components/cplusplus/Semaphore.h | 42 ------------------ components/cplusplus/Thread.h | 67 ---------------------------- components/cplusplus/crt.h | 26 ----------- components/cplusplus/crt_init.c | 68 ---------------------------- components/cplusplus/cxx_Mutex.cpp | 2 +- components/cplusplus/cxx_Semaphore.cpp | 2 +- components/cplusplus/cxx_Thread.cpp | 2 +- components/cplusplus/cxx_crt.cpp | 2 +- components/cplusplus/cxx_crt.h | 26 +++++++++++ components/cplusplus/cxx_crt_init.c | 68 ++++++++++++++++++++++++++++ components/cplusplus/cxx_lock.h | 28 ++++++++++++ components/cplusplus/cxx_mail.h | 81 ++++++++++++++++++++++++++++++++++ components/cplusplus/cxx_mutex.h | 46 +++++++++++++++++++ components/cplusplus/cxx_queue.h | 74 +++++++++++++++++++++++++++++++ components/cplusplus/cxx_semaphore.h | 42 ++++++++++++++++++ components/cplusplus/cxx_thread.h | 67 ++++++++++++++++++++++++++++ 20 files changed, 436 insertions(+), 436 deletions(-) delete mode 100644 components/cplusplus/Lock.h delete mode 100644 components/cplusplus/Mail.h delete mode 100644 components/cplusplus/Mutex.h delete mode 100644 components/cplusplus/Queue.h delete mode 100644 components/cplusplus/Semaphore.h delete mode 100644 components/cplusplus/Thread.h delete mode 100644 components/cplusplus/crt.h delete mode 100644 components/cplusplus/crt_init.c create mode 100644 components/cplusplus/cxx_crt.h create mode 100644 components/cplusplus/cxx_crt_init.c create mode 100644 components/cplusplus/cxx_lock.h create mode 100644 components/cplusplus/cxx_mail.h create mode 100644 components/cplusplus/cxx_mutex.h create mode 100644 components/cplusplus/cxx_queue.h create mode 100644 components/cplusplus/cxx_semaphore.h create mode 100644 components/cplusplus/cxx_thread.h (limited to 'components/cplusplus/cxx_Thread.cpp') diff --git a/components/cplusplus/Lock.h b/components/cplusplus/Lock.h deleted file mode 100644 index c3cc1d2dae..0000000000 --- a/components/cplusplus/Lock.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -namespace rtthread { - -class Lock -{ -public: - Lock(Mutex& mutex) : m(mutex) {m.lock();} - ~Lock() {m.unlock();} - -protected: - Mutex &m; -}; - -} diff --git a/components/cplusplus/Mail.h b/components/cplusplus/Mail.h deleted file mode 100644 index ae3bfb5c41..0000000000 --- a/components/cplusplus/Mail.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -#include - -namespace rtthread { - -/** - * The Mail class allow to control, send, receive, or wait for mail. - * A mail is a memory block that is send to a thread or interrupt service routine. - * @param T data type of a single message element. - * @param queue_sz maximum number of messages in queue. - */ - -template -class Mail { -public: - /** Create and Initialise Mail queue. */ - Mail(const char *name = "") - { - rt_mb_init(&mID, name, mPool, queue_sz, RT_IPC_FLAG_FIFO); - } - - ~Mail() - { - rt_mb_detach(&mID); - } - - /** Put a mail in the queue. - @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. - @return status code that indicates the execution status of the function. - */ - bool put(T *mptr, int32_t millisec = 0) - { - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - return rt_mb_send_wait(&mID, (rt_uint32_t)mptr, tick) == RT_EOK; - } - - /** Get a mail from a queue. - @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). - @return event that contains mail information or error code. - */ - T* get(int32_t millisec = -1) - { - T *t = NULL; - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - rt_mb_recv(&mID, &t, tick); - - return t; - } - -private: - struct rt_mailbox mID; - T* mPool[queue_sz]; -}; - -} diff --git a/components/cplusplus/Mutex.h b/components/cplusplus/Mutex.h deleted file mode 100644 index 63efb4a6ea..0000000000 --- a/components/cplusplus/Mutex.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -namespace rtthread { - -/** The Mutex class is used to synchronise the execution of threads. - This is for example used to protect access to a shared resource. -*/ -class Mutex { -public: - /** Create and Initialize a Mutex object */ - Mutex(const char *name = "mutex"); - ~Mutex(); - - /** Wait until a Mutex becomes available. - @param millisec timeout value or 0 in case of no time-out. (default: WaitForever) - @return true if the mutex was acquired, false otherwise. - */ - bool lock(int32_t millisec = -1); - - /** Try to lock the mutex, and return immediately - @return true if the mutex was acquired, false otherwise. - */ - bool trylock(); - - /** Unlock the mutex that has previously been locked by the same thread - */ - void unlock(); - -private: - struct rt_mutex mID; -}; - -} diff --git a/components/cplusplus/Queue.h b/components/cplusplus/Queue.h deleted file mode 100644 index f6b91e1a21..0000000000 --- a/components/cplusplus/Queue.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -#include - -namespace rtthread { - -/** - * The Queue class allow to control, send, receive, or wait for messages. - * A message can be a integer or pointer value to a certain type T that is send - * to a thread or interrupt service routine. - * @param T data type of a single message element. - * @param queue_sz maximum number of messages in queue. - */ -template -class Queue -{ -public: - /** Create and initialise a message Queue. */ - Queue() - { - rt_mq_init(&mID, "mq", mPool, sizeof(T), sizeof(mPool), RT_IPC_FLAG_FIFO); - }; - - ~Queue() - { - rt_mq_detach(&mID); - }; - - /** Put a message in a Queue. - @param data message pointer. - @param millisec timeout value or 0 in case of no time-out. (default: 0) - @return status code that indicates the execution status of the function. - */ - rt_err_t put(T& data, int32_t millisec = 0) - { - return rt_mq_send(&mID, &data, sizeof(data)); - } - - /** Get a message or Wait for a message from a Queue. - @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). - @return bool . - */ - bool get(T& data, int32_t millisec = WAIT_FOREVER) - { - rt_int32_t tick; - - if (millisec < 0) - tick = -1; - else - tick = rt_tick_from_millisecond(millisec); - - return rt_mq_recv(&mID, &data, sizeof(data), tick) == RT_EOK; - } - -private: - struct rt_messagequeue mID; - - char mPool[(sizeof(void *) + RT_ALIGN(sizeof(T), RT_ALIGN_SIZE)) * queue_sz]; -}; - -} diff --git a/components/cplusplus/Semaphore.h b/components/cplusplus/Semaphore.h deleted file mode 100644 index 45c916e169..0000000000 --- a/components/cplusplus/Semaphore.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -namespace rtthread { - -/** The Semaphore class is used to manage and protect access to a set of shared resources. */ -class Semaphore -{ -public: - /** Create and Initialize a Semaphore object used for managing resources. - @param number of available resources; maximum index value is (count-1). - */ - Semaphore(const char *name = "sem", int32_t count = 0); - ~Semaphore(); - - /** Wait until a Semaphore resource becomes available. - @param millisec timeout value or 0 in case of no time-out. - @return true on success. - */ - bool wait(int32_t millisec = -1); - - /** Release a Semaphore resource that was obtain with Semaphore::wait. - */ - void release(void); - -private: - struct rt_semaphore mID; -}; - -} diff --git a/components/cplusplus/Thread.h b/components/cplusplus/Thread.h deleted file mode 100644 index 0e681e5c2a..0000000000 --- a/components/cplusplus/Thread.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2016/10/1 Bernard The first version - */ - -#pragma once - -#include -#include - -namespace rtthread -{ - -/** The Thread class allow defining, creating, and controlling thread functions in the system. */ -class Thread -{ -public: - typedef void (*thread_func_t)(void *param); - - /** Allocate a new thread without starting execution - @param priority initial priority of the thread function. (default: osPriorityNormal). - @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). - @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). - */ - Thread(rt_uint32_t stack_size = 2048, - rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3, - rt_uint32_t tick = 20, - const char *name = "th"); - - Thread(void (*entry)(void *p), - void *p = RT_NULL, - rt_uint32_t stack_size = 2048, - rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3, - rt_uint32_t tick = 20, - const char *name = "th"); - - virtual ~Thread(); - - bool start(); - - static void sleep(int32_t millisec); - - rt_err_t wait(int32_t millisec); - rt_err_t join(int32_t millisec = -1); - -protected: - virtual void run(void *parameter); - -private: - static void func(Thread *pThis); - - rt_thread_t _thread; - - thread_func_t _entry; - void *_param; - - /* event for thread join */ - struct rt_event _event; - bool started; -}; - -} diff --git a/components/cplusplus/crt.h b/components/cplusplus/crt.h deleted file mode 100644 index 903d4c86cd..0000000000 --- a/components/cplusplus/crt.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * -* Change Logs: -* Date Author Notes -* 2015-03-07 Bernard Add copyright header. -*/ - -#ifndef CRT_H_ -#define CRT_H_ - -#include -#include - -void *operator new(size_t size); -void *operator new[](size_t size); - -void operator delete(void * ptr); -void operator delete[](void *ptr); - -extern "C" void __cxa_pure_virtual(void); -extern "C" int cplusplus_system_init(void); - -#endif diff --git a/components/cplusplus/crt_init.c b/components/cplusplus/crt_init.c deleted file mode 100644 index 977dee6ee9..0000000000 --- a/components/cplusplus/crt_init.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2014-12-03 Bernard Add copyright header. - * 2014-12-29 Bernard Add cplusplus initialization for ARMCC. - * 2016-06-28 Bernard Add _init/_fini routines for GCC. - * 2016-10-02 Bernard Add WEAK for cplusplus_system_init routine. - */ - -#include - -#if defined(__CC_ARM) || defined(__CLANG_ARM) -extern void $Super$$__cpp_initialize__aeabi_(void); -/* we need to change the cpp_initialize order */ -RT_WEAK void $Sub$$__cpp_initialize__aeabi_(void) -{ - /* empty */ -} -#elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__) -/* The _init()/_fini() routines has been defined in codesourcery g++ lite */ -RT_WEAK void _init() -{ -} - -RT_WEAK void _fini() -{ -} - -RT_WEAK void *__dso_handle = 0; - -#endif - -RT_WEAK int cplusplus_system_init(void) -{ -#if defined(__CC_ARM) || defined(__CLANG_ARM) - /* If there is no SHT$$INIT_ARRAY, calling - * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12 - * the problem still exists. So we have to initialize the C++ runtime by ourself. - */ - typedef void PROC(); - extern const unsigned long SHT$$INIT_ARRAY$$Base[]; - extern const unsigned long SHT$$INIT_ARRAY$$Limit[]; - - const unsigned long *base = SHT$$INIT_ARRAY$$Base; - const unsigned long *lim = SHT$$INIT_ARRAY$$Limit; - - for (; base != lim; base++) - { - PROC *proc = (PROC *)((const char *)base + *base); - (*proc)(); - } -#elif defined(__GNUC__) - typedef void(*pfunc)(); - extern pfunc __ctors_start__[]; - extern pfunc __ctors_end__[]; - pfunc *p; - - for (p = __ctors_start__; p < __ctors_end__; p++) - (*p)(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(cplusplus_system_init); diff --git a/components/cplusplus/cxx_Mutex.cpp b/components/cplusplus/cxx_Mutex.cpp index ad20e805e9..97d8fa87e1 100644 --- a/components/cplusplus/cxx_Mutex.cpp +++ b/components/cplusplus/cxx_Mutex.cpp @@ -7,7 +7,7 @@ * Date Author Notes */ -#include "Mutex.h" +#include "cxx_mutex.h" using namespace rtthread; diff --git a/components/cplusplus/cxx_Semaphore.cpp b/components/cplusplus/cxx_Semaphore.cpp index eda71f24a3..672eeb04af 100644 --- a/components/cplusplus/cxx_Semaphore.cpp +++ b/components/cplusplus/cxx_Semaphore.cpp @@ -7,7 +7,7 @@ * Date Author Notes */ -#include "Semaphore.h" +#include "cxx_semaphore.h" using namespace rtthread; diff --git a/components/cplusplus/cxx_Thread.cpp b/components/cplusplus/cxx_Thread.cpp index 9689f6691b..f3484bb1f4 100644 --- a/components/cplusplus/cxx_Thread.cpp +++ b/components/cplusplus/cxx_Thread.cpp @@ -7,7 +7,7 @@ * Date Author Notes */ -#include "Thread.h" +#include "cxx_thread.h" using namespace rtthread; diff --git a/components/cplusplus/cxx_crt.cpp b/components/cplusplus/cxx_crt.cpp index 750e01e0cb..de2fe3cd74 100644 --- a/components/cplusplus/cxx_crt.cpp +++ b/components/cplusplus/cxx_crt.cpp @@ -9,7 +9,7 @@ */ #include -#include "crt.h" +#include "cxx_crt.h" void *operator new(size_t size) { diff --git a/components/cplusplus/cxx_crt.h b/components/cplusplus/cxx_crt.h new file mode 100644 index 0000000000..903d4c86cd --- /dev/null +++ b/components/cplusplus/cxx_crt.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * +* Change Logs: +* Date Author Notes +* 2015-03-07 Bernard Add copyright header. +*/ + +#ifndef CRT_H_ +#define CRT_H_ + +#include +#include + +void *operator new(size_t size); +void *operator new[](size_t size); + +void operator delete(void * ptr); +void operator delete[](void *ptr); + +extern "C" void __cxa_pure_virtual(void); +extern "C" int cplusplus_system_init(void); + +#endif diff --git a/components/cplusplus/cxx_crt_init.c b/components/cplusplus/cxx_crt_init.c new file mode 100644 index 0000000000..977dee6ee9 --- /dev/null +++ b/components/cplusplus/cxx_crt_init.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2014-12-03 Bernard Add copyright header. + * 2014-12-29 Bernard Add cplusplus initialization for ARMCC. + * 2016-06-28 Bernard Add _init/_fini routines for GCC. + * 2016-10-02 Bernard Add WEAK for cplusplus_system_init routine. + */ + +#include + +#if defined(__CC_ARM) || defined(__CLANG_ARM) +extern void $Super$$__cpp_initialize__aeabi_(void); +/* we need to change the cpp_initialize order */ +RT_WEAK void $Sub$$__cpp_initialize__aeabi_(void) +{ + /* empty */ +} +#elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__) +/* The _init()/_fini() routines has been defined in codesourcery g++ lite */ +RT_WEAK void _init() +{ +} + +RT_WEAK void _fini() +{ +} + +RT_WEAK void *__dso_handle = 0; + +#endif + +RT_WEAK int cplusplus_system_init(void) +{ +#if defined(__CC_ARM) || defined(__CLANG_ARM) + /* If there is no SHT$$INIT_ARRAY, calling + * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12 + * the problem still exists. So we have to initialize the C++ runtime by ourself. + */ + typedef void PROC(); + extern const unsigned long SHT$$INIT_ARRAY$$Base[]; + extern const unsigned long SHT$$INIT_ARRAY$$Limit[]; + + const unsigned long *base = SHT$$INIT_ARRAY$$Base; + const unsigned long *lim = SHT$$INIT_ARRAY$$Limit; + + for (; base != lim; base++) + { + PROC *proc = (PROC *)((const char *)base + *base); + (*proc)(); + } +#elif defined(__GNUC__) + typedef void(*pfunc)(); + extern pfunc __ctors_start__[]; + extern pfunc __ctors_end__[]; + pfunc *p; + + for (p = __ctors_start__; p < __ctors_end__; p++) + (*p)(); +#endif + + return 0; +} +INIT_COMPONENT_EXPORT(cplusplus_system_init); diff --git a/components/cplusplus/cxx_lock.h b/components/cplusplus/cxx_lock.h new file mode 100644 index 0000000000..c3cc1d2dae --- /dev/null +++ b/components/cplusplus/cxx_lock.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +namespace rtthread { + +class Lock +{ +public: + Lock(Mutex& mutex) : m(mutex) {m.lock();} + ~Lock() {m.unlock();} + +protected: + Mutex &m; +}; + +} diff --git a/components/cplusplus/cxx_mail.h b/components/cplusplus/cxx_mail.h new file mode 100644 index 0000000000..ae3bfb5c41 --- /dev/null +++ b/components/cplusplus/cxx_mail.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +#include + +namespace rtthread { + +/** + * The Mail class allow to control, send, receive, or wait for mail. + * A mail is a memory block that is send to a thread or interrupt service routine. + * @param T data type of a single message element. + * @param queue_sz maximum number of messages in queue. + */ + +template +class Mail { +public: + /** Create and Initialise Mail queue. */ + Mail(const char *name = "") + { + rt_mb_init(&mID, name, mPool, queue_sz, RT_IPC_FLAG_FIFO); + } + + ~Mail() + { + rt_mb_detach(&mID); + } + + /** Put a mail in the queue. + @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. + @return status code that indicates the execution status of the function. + */ + bool put(T *mptr, int32_t millisec = 0) + { + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_mb_send_wait(&mID, (rt_uint32_t)mptr, tick) == RT_EOK; + } + + /** Get a mail from a queue. + @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). + @return event that contains mail information or error code. + */ + T* get(int32_t millisec = -1) + { + T *t = NULL; + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + rt_mb_recv(&mID, &t, tick); + + return t; + } + +private: + struct rt_mailbox mID; + T* mPool[queue_sz]; +}; + +} diff --git a/components/cplusplus/cxx_mutex.h b/components/cplusplus/cxx_mutex.h new file mode 100644 index 0000000000..63efb4a6ea --- /dev/null +++ b/components/cplusplus/cxx_mutex.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +namespace rtthread { + +/** The Mutex class is used to synchronise the execution of threads. + This is for example used to protect access to a shared resource. +*/ +class Mutex { +public: + /** Create and Initialize a Mutex object */ + Mutex(const char *name = "mutex"); + ~Mutex(); + + /** Wait until a Mutex becomes available. + @param millisec timeout value or 0 in case of no time-out. (default: WaitForever) + @return true if the mutex was acquired, false otherwise. + */ + bool lock(int32_t millisec = -1); + + /** Try to lock the mutex, and return immediately + @return true if the mutex was acquired, false otherwise. + */ + bool trylock(); + + /** Unlock the mutex that has previously been locked by the same thread + */ + void unlock(); + +private: + struct rt_mutex mID; +}; + +} diff --git a/components/cplusplus/cxx_queue.h b/components/cplusplus/cxx_queue.h new file mode 100644 index 0000000000..f6b91e1a21 --- /dev/null +++ b/components/cplusplus/cxx_queue.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +#include + +namespace rtthread { + +/** + * The Queue class allow to control, send, receive, or wait for messages. + * A message can be a integer or pointer value to a certain type T that is send + * to a thread or interrupt service routine. + * @param T data type of a single message element. + * @param queue_sz maximum number of messages in queue. + */ +template +class Queue +{ +public: + /** Create and initialise a message Queue. */ + Queue() + { + rt_mq_init(&mID, "mq", mPool, sizeof(T), sizeof(mPool), RT_IPC_FLAG_FIFO); + }; + + ~Queue() + { + rt_mq_detach(&mID); + }; + + /** Put a message in a Queue. + @param data message pointer. + @param millisec timeout value or 0 in case of no time-out. (default: 0) + @return status code that indicates the execution status of the function. + */ + rt_err_t put(T& data, int32_t millisec = 0) + { + return rt_mq_send(&mID, &data, sizeof(data)); + } + + /** Get a message or Wait for a message from a Queue. + @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). + @return bool . + */ + bool get(T& data, int32_t millisec = WAIT_FOREVER) + { + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_mq_recv(&mID, &data, sizeof(data), tick) == RT_EOK; + } + +private: + struct rt_messagequeue mID; + + char mPool[(sizeof(void *) + RT_ALIGN(sizeof(T), RT_ALIGN_SIZE)) * queue_sz]; +}; + +} diff --git a/components/cplusplus/cxx_semaphore.h b/components/cplusplus/cxx_semaphore.h new file mode 100644 index 0000000000..45c916e169 --- /dev/null +++ b/components/cplusplus/cxx_semaphore.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +namespace rtthread { + +/** The Semaphore class is used to manage and protect access to a set of shared resources. */ +class Semaphore +{ +public: + /** Create and Initialize a Semaphore object used for managing resources. + @param number of available resources; maximum index value is (count-1). + */ + Semaphore(const char *name = "sem", int32_t count = 0); + ~Semaphore(); + + /** Wait until a Semaphore resource becomes available. + @param millisec timeout value or 0 in case of no time-out. + @return true on success. + */ + bool wait(int32_t millisec = -1); + + /** Release a Semaphore resource that was obtain with Semaphore::wait. + */ + void release(void); + +private: + struct rt_semaphore mID; +}; + +} diff --git a/components/cplusplus/cxx_thread.h b/components/cplusplus/cxx_thread.h new file mode 100644 index 0000000000..0e681e5c2a --- /dev/null +++ b/components/cplusplus/cxx_thread.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2016/10/1 Bernard The first version + */ + +#pragma once + +#include +#include + +namespace rtthread +{ + +/** The Thread class allow defining, creating, and controlling thread functions in the system. */ +class Thread +{ +public: + typedef void (*thread_func_t)(void *param); + + /** Allocate a new thread without starting execution + @param priority initial priority of the thread function. (default: osPriorityNormal). + @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE). + @param stack_pointer pointer to the stack area to be used by this thread (default: NULL). + */ + Thread(rt_uint32_t stack_size = 2048, + rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3, + rt_uint32_t tick = 20, + const char *name = "th"); + + Thread(void (*entry)(void *p), + void *p = RT_NULL, + rt_uint32_t stack_size = 2048, + rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3, + rt_uint32_t tick = 20, + const char *name = "th"); + + virtual ~Thread(); + + bool start(); + + static void sleep(int32_t millisec); + + rt_err_t wait(int32_t millisec); + rt_err_t join(int32_t millisec = -1); + +protected: + virtual void run(void *parameter); + +private: + static void func(Thread *pThis); + + rt_thread_t _thread; + + thread_func_t _entry; + void *_param; + + /* event for thread join */ + struct rt_event _event; + bool started; +}; + +} -- cgit v1.3.1