support posix

1. posix pthread.h: pthread, pthread_barrier, pthread_cond, pthread_mutex, , pthread_rwlock, pthread_spin, etc
2. posix semaphore.h: sem_*
3. posix mqueue.h: mq_*
4. posix time.h: timer_*
5. to support posix, add tos_barrier, tos_bitmap, tos_rwlock, tos_stopwatch, change name of k_task_t from a char * pointer to a char array.
6. sample, see examples\posix
7. project, see board\TencentOS_tiny_EVB_MX_Plus\KEIL\posix
This commit is contained in:
daishengdong
2020-02-28 00:11:28 +08:00
parent 7bfc998494
commit 40f55ec57b
84 changed files with 11704 additions and 158 deletions

View File

@@ -0,0 +1,89 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_BARRIER_H_
#define _TOS_BARRIER_H_
__CDECLS_BEGIN
typedef struct k_barrier_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_t knl_obj;
#endif
pend_obj_t pend_obj;
k_barrier_cnt_t count;
} k_barrier_t;
/**
* @brief Create a thread barrier.
*
* @attention the count must be greater then zero.
*
* @param[in] barrier the barrier.
* @param[in] count the number of threads(task) must call tos_barrier_pend before any of them successfully return from the call.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BARRIER_COUNT_INVALID the count is equals to zero.
*/
__API__ k_err_t tos_barrier_create(k_barrier_t *barrier, k_barrier_cnt_t count);
/**
* @brief Create a thread barrier.
*
* @attention the count must be greater then zero.
*
* @param[in] barrier the barrier.
* @param[in] count the number of threads(task) must call tos_barrier_pend before any of them successfully return from the call.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BARRIER_COUNT_INVALID the count is equals to zero.
*/
__API__ k_err_t tos_barrier_destroy(k_barrier_t *barrier);
/**
* @brief Pend on a barrier.
*
* @attention until (countdownlatch->count) of tasks have called the pend, the pender would wake up.
*
* @param[in] barrier the barrier.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BARRIER_OVERFLOW the barrier is pended too many times.
*/
__API__ k_err_t tos_barrier_pend(k_barrier_t *barrier);
/**
* @brief Reset a barrier.
*
* @attention
*
* @param[in] barrier the barrier.
* @param[in] count the count of the barrier to be reset.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_barrier_reset(k_barrier_t *barrier, k_barrier_cnt_t count);
__CDECLS_END
#endif /* _TOS_BARRIER_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_BINARY_HEAP_H_
#define _TOS_BINARY_HEAP_H_
__CDECLS_BEGIN
typedef int (*k_bin_heap_cmp)(void *first, void *second);
typedef struct k_binary_heap_st {
@@ -176,5 +178,7 @@ __API__ int tos_bin_heap_is_empty(k_bin_heap_t *bin_heap);
*/
__API__ int tos_bin_heap_is_full(k_bin_heap_t *bin_heap);
__CDECLS_END
#endif /* _TOS_BINARY_HEAP_H_ */

View File

@@ -0,0 +1,155 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_BITMAP_H_
#define _TOS_BITMAP_H_
typedef uint32_t k_bmtbl_t;
typedef struct k_bitmap_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_t knl_obj;
#endif
uint32_t bit_ndx_max;
uint32_t bit_max;
k_bmtbl_t *bitmap_tbl;
} k_bitmap_t;
#define K_BITMAP_SLOT_SIZE (sizeof(k_bmtbl_t) * 8) /* in bits */
#define K_BITMAP_TBL_SIZE(bit_max) ((bit_max + K_BITMAP_SLOT_SIZE - 1) / K_BITMAP_SLOT_SIZE)
#define K_BITMAP_NDX(bit) ((bit) >> 5u) /* bit / K_BITMAP_SLOT_SIZE */
#define K_BITMAP_BIT(bit) ((uint32_t)1u << (K_BITMAP_SLOT_SIZE - 1u - ((bit) & (K_BITMAP_SLOT_SIZE - 1u))))
#define TOS_BITMAP_SIZE(bit_max) (K_BITMAP_TBL_SIZE(bit_max))
/**
* @brief Create a bitmap with all bit are set to 0.
*
* @attention the size of bitmap_tabl can be caculated by the macro TOS_BITMAP_SIZE
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bitmap_tbl bitmap table buffer.
* @param[in] bit_max maximal bit.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OBJ_PTR_NULL bitmap is NULL.
*/
__API__ k_err_t tos_bitmap_create_empty(k_bitmap_t *bitmap, k_bmtbl_t *bitmap_tbl, uint32_t bit_max);
/**
* @brief Create a bitmap with all bit are set to 1.
*
* @attention the size of bitmap_tabl can be caculated by the macro TOS_BITMAP_SIZE
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bitmap_tbl bitmap table buffer.
* @param[in] bit_max maximal bit.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OBJ_PTR_NULL bitmap is NULL.
*/
__API__ k_err_t tos_bitmap_create_full(k_bitmap_t *bitmap, k_bmtbl_t *bitmap_tbl, uint32_t bit_max);
/**
* @brief Destroy the bitmap.
*
* @attention
*
* @param[in] bitmap pointer to the handler of the bitmap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OBJ_PTR_NULL bitmap is NULL.
*/
__API__ k_err_t tos_bitmap_destroy(k_bitmap_t *bitmap);
/**
* @brief Set a certain bit of the bitmap to 1.
*
* @attention
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bit the bit to set.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BITMAP_EXCEED bit is larger than the bit_max passed to tos_bitmap_create_*.
*/
__API__ k_err_t tos_bitmap_set(k_bitmap_t *bitmap, uint32_t bit);
/**
* @brief Set a certain bit of the bitmap to 0.
*
* @attention
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bit the bit to set.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BITMAP_EXCEED bit is larger than the bit_max passed to tos_bitmap_create_*.
*/
__API__ k_err_t tos_bitmap_reset(k_bitmap_t *bitmap, uint32_t bit);
/**
* @brief Test whether a certain bit of the bitmap is 1.
*
* @attention
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bit the bit to set.
*
* @return whether the bit is 1
* @retval #K_TRUE the certain bit is 1.
* @retval #K_FALSE the certain bit is not 1(that means is 0).
*/
__API__ int tos_bitmap_is_set(k_bitmap_t *bitmap, uint32_t bit);
/**
* @brief Test whether a certain bit of the bitmap is 0.
*
* @attention
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bit the bit to set.
*
* @return whether the bit is 0
* @retval #K_TRUE the certain bit is 0.
* @retval #K_FALSE the certain bit is not 0(that means is 1).
*/
__API__ int tos_bitmap_is_reset(k_bitmap_t *bitmap, uint32_t bit);
/**
* @brief Get the lowest significant bit of the bitmap.
*
* @attention The very first bit which is set to 1.
*
* @param[in] bitmap pointer to the handler of the bitmap.
* @param[in] bit the bit to set.
*
* @return the lowest significant bit of the bitmap.
*/
__API__ int tos_bitmap_lsb(k_bitmap_t *bitmap);
#endif /* _TOS_BITMAP_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_CHAR_FIFO_H_
#define _TOS_CHAR_FIFO_H_
__CDECLS_BEGIN
typedef struct k_char_fifo_st {
knl_obj_t knl_obj;
@@ -187,5 +189,7 @@ __API__ int tos_chr_fifo_is_empty(k_chr_fifo_t *chr_fifo);
*/
__API__ int tos_chr_fifo_is_full(k_chr_fifo_t *chr_fifo);
#endif // _TOS_CHAR_FIFO_H_
__CDECLS_END
#endif /* _TOS_CHAR_FIFO_H_ */

View File

@@ -18,21 +18,30 @@
#ifndef _TOS_COMPILER_H_
#define _TOS_COMPILER_H_
// function with __API__ prefix, api for user
/* function with __API__ prefix, api for user */
#define __API__
// function with __KNL__ prefix, only for kernel
/* function with __KNL__ prefix, only for kernel */
#define __KNL__
// function with __HOOK__ prefix, should be implemented by user
/* function with __HOOK__ prefix, should be implemented by user */
#define __HOOK__
// function with __DEBUG__ prefix, only for debug
/* function with __DEBUG__ prefix, only for debug */
#define __DEBUG__
// function with __PORT__ is architecture depended
/* function with __PORT__ is architecture depended */
#define __PORT__
/* CPP header guards */
#ifdef __cplusplus
#define __CDECLS_BEGIN extern "C" {
#define __CDECLS_END }
#else
#define __CDECLS_BEGIN
#define __CDECLS_END
#endif
/*------------------ RealView Compiler -----------------*/
#if defined(__CC_ARM)

View File

@@ -18,7 +18,7 @@
#ifndef _TOS_COMPLETION_H_
#define _TOS_COMPLETION_H_
#if TOS_CFG_COMPLETION_EN > 0u
__CDECLS_BEGIN
typedef uint16_t completion_done_t;
@@ -147,7 +147,7 @@ __API__ k_err_t tos_completion_reset(k_completion_t *completion);
*/
__API__ int tos_completion_is_done(k_completion_t *completion);
#endif
__CDECLS_END
#endif
#endif /* _TOS_COMPLETION_H_ */

View File

@@ -69,24 +69,6 @@
/////////////////////////////////////////
/////////////////////////////////////////
// disable countdownlatch
#ifdef TOS_CFG_COUNTDOWNLATCH_EN
#undef TOS_CFG_COUNTDOWNLATCH_EN
#endif
#define TOS_CFG_COUNTDOWNLATCH_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable completion
#ifdef TOS_CFG_COMPLETION_EN
#undef TOS_CFG_COMPLETION_EN
#endif
#define TOS_CFG_COMPLETION_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable the "traditional" timer
#ifdef TOS_CFG_TIMER_EN
@@ -206,14 +188,6 @@
#define TOS_CFG_SEM_EN 0u
#endif
#ifndef TOS_CFG_COUNTDOWNLATCH_EN
#define TOS_CFG_COUNTDOWNLATCH_EN 0u
#endif
#ifndef TOS_CFG_COMPLETION_EN
#define TOS_CFG_COMPLETION_EN 0u
#endif
#ifndef TOS_CFG_TIMER_EN
#define TOS_CFG_TIMER_EN 0u
#endif

View File

@@ -18,7 +18,7 @@
#ifndef _TOS_COUNTDOWNLATCH_H_
#define _TOS_COUNTDOWNLATCH_H_
#if TOS_CFG_COUNTDOWNLATCH_EN > 0u
__CDECLS_BEGIN
typedef struct k_countdownlatch_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
@@ -114,7 +114,7 @@ __API__ k_err_t tos_countdownlatch_post(k_countdownlatch_t *countdownlatch);
*/
__API__ k_err_t tos_countdownlatch_reset(k_countdownlatch_t *countdownlatch, k_countdownlatch_cnt_t count);
#endif
__CDECLS_END
#endif
#endif /* _TOS_COUNTDOWNLATCH_H_ */

View File

@@ -21,7 +21,12 @@
typedef enum k_err_en {
K_ERR_NONE = 0u,
K_ERR_BIN_HEAP_FULL = 10u,
K_ERR_BARRIER_COUNT_INVALID = 5u,
K_ERR_BARRIER_OVERFLOW,
K_ERR_BITMAP_EXCEED = 10u,
K_ERR_BIN_HEAP_FULL = 15u,
K_ERR_BIN_HEAP_EMPTY,
K_ERR_BIN_HEAP_ITEM_SIZE_NOT_MATCH,
@@ -64,7 +69,6 @@ typedef enum k_err_en {
K_ERR_PEND_NOWAIT = 1200u,
K_ERR_PEND_SCHED_LOCKED,
K_ERR_PEND_IN_IRQ,
K_ERR_PEND_ABNORMAL,
K_ERR_PEND_TIMEOUT,
K_ERR_PEND_DESTROY,
@@ -79,16 +83,25 @@ typedef enum k_err_en {
K_ERR_PRIO_Q_SLOT_NOT_TAKEN,
K_ERR_PRIO_Q_ITEM_SIZE_NOT_MATCH,
K_ERR_RING_Q_FULL = 1600u,
K_ERR_RING_Q_FULL = 1500u,
K_ERR_RING_Q_EMPTY,
K_ERR_RING_Q_ITEM_SIZE_NOT_MATCH,
K_ERR_RWLOCK_READERS_TO_MANY = 1600u,
K_ERR_RWLOCK_IS_READING,
K_ERR_RWLOCK_IS_WRITTING,
K_ERR_RWLOCK_NOT_READING,
K_ERR_RWLOCK_NOT_WRITTING,
K_ERR_RWLOCK_NOT_TAKEN,
K_ERR_RWLOCK_WAITING_WRITERS_TO_MANY,
K_ERR_SCHED_LOCKED = 1700u,
K_ERR_SCHED_NOT_LOCKED,
K_ERR_SEM_OVERFLOW = 1800u,
K_ERR_TASK_DESTROY_IDLE = 1900u,
K_ERR_TASK_ALREADY_CREATED = 1900u,
K_ERR_TASK_DESTROY_IDLE,
K_ERR_TASK_NOT_DELAY,
K_ERR_TASK_PRIO_INVALID,
K_ERR_TASK_RESUME_SELF,

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_EVENT_H_
#define _TOS_EVENT_H_
__CDECLS_BEGIN
#if TOS_CFG_EVENT_EN > 0
// if we are pending an event, for any flag we expect is set is ok, this flag should be passed to tos_event_pend
@@ -133,5 +135,7 @@ __API__ k_err_t tos_event_post_keep(k_event_t *event, k_event_flag_t flag);
#endif
__CDECLS_END
#endif /* _TOS_EVENT_H_ */

View File

@@ -35,6 +35,7 @@
#include <tos_slist.h>
#include <tos_pend.h>
#include <tos_sys.h>
#include <tos_bitmap.h>
#include <tos_ring_queue.h>
#include <tos_char_fifo.h>
#include <tos_mail_queue.h>
@@ -48,10 +49,13 @@
#include <tos_mutex.h>
#include <tos_sem.h>
#include <tos_event.h>
#include <tos_countdownlatch.h>
#include <tos_barrier.h>
#include <tos_completion.h>
#include <tos_countdownlatch.h>
#include <tos_rwlock.h>
#include <tos_timer.h>
#include <tos_time.h>
#include <tos_stopwatch.h>
#include <tos_mmblk.h>
#include <tos_mmheap.h>
#include <tos_tick.h>

View File

@@ -23,51 +23,57 @@
#include <stdlib.h>
#include <string.h>
#define TOS_MACRO_BEGIN do {
#define TOS_MACRO_END } while (0)
#define TOS_OFFSET_OF_FIELD(type, field) \
((uint32_t)&(((type *)0)->field))
#define TOS_CONTAINER_OF_FIELD(ptr, type, field) \
((type *)((uint8_t *)(ptr) - TOS_OFFSET_OF_FIELD(type, field)))
#define TOS_PTR_SANITY_CHECK(ptr) \
do { \
if (unlikely(!(ptr))) { \
return K_ERR_OBJ_PTR_NULL; \
} \
} while(0)
#define TOS_COUNT_OF(array) (sizeof(array) / sizeof(array[0]))
#define TOS_PTR_SANITY_CHECK_RC(ptr, return_code) \
do { \
if (unlikely(!(ptr))) { \
return return_code; \
} \
} while(0)
#define TOS_PTR_SANITY_CHECK(ptr) \
TOS_MACRO_BEGIN \
if (unlikely(!(ptr))) { \
return K_ERR_OBJ_PTR_NULL; \
} \
TOS_MACRO_END
#define TOS_IN_IRQ_CHECK() \
do { \
if (unlikely(knl_is_inirq())) { \
return K_ERR_IN_IRQ; \
} \
} while(0)
#define TOS_PTR_SANITY_CHECK_RC(ptr, return_code) \
TOS_MACRO_BEGIN \
if (unlikely(!(ptr))) { \
return return_code; \
} \
TOS_MACRO_END
#define TOS_IN_IRQ_CHECK() \
TOS_MACRO_BEGIN \
if (unlikely(knl_is_inirq())) { \
return K_ERR_IN_IRQ; \
} \
TOS_MACRO_END
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
#define TOS_OBJ_INIT(obj, obj_type) knl_object_init(&obj->knl_obj, obj_type)
#define TOS_OBJ_DEINIT(obj) knl_object_deinit(&obj->knl_obj)
#define TOS_OBJ_VERIFY(obj, obj_type) \
do { \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return K_ERR_OBJ_INVALID; \
} \
} while (0)
#define TOS_OBJ_VERIFY(obj, obj_type) \
TOS_MACRO_BEGIN \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return K_ERR_OBJ_INVALID; \
} \
TOS_MACRO_END
#define TOS_OBJ_VERIFY_RC(obj, obj_type, return_code) \
do { \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return return_code; \
} \
} while (0)
#define TOS_OBJ_VERIFY_RC(obj, obj_type, return_code) \
TOS_MACRO_BEGIN \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return return_code; \
} \
TOS_MACRO_END
#else

View File

@@ -30,6 +30,7 @@ typedef uint8_t k_nesting_t;
typedef uint16_t k_opt_t;
typedef uint16_t k_sem_cnt_t;
typedef uint32_t k_event_flag_t;
typedef uint16_t k_barrier_cnt_t;
typedef uint16_t k_countdownlatch_cnt_t;
typedef uint32_t k_time_t;

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_LIST_H_
#define _TOS_LIST_H_
__CDECLS_BEGIN
typedef struct k_list_node_st {
struct k_list_node_st *next;
struct k_list_node_st *prev;
@@ -138,5 +140,7 @@ __API__ __STATIC_INLINE__ int tos_list_empty(const k_list_t *list)
return list->next == list;
}
__CDECLS_END
#endif /* _TOS_LIST_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_MAIL_QUEUE_H_
#define _TOS_MAIL_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_MAIL_QUEUE_EN > 0u
typedef struct k_mail_queue_st {
@@ -155,5 +157,7 @@ __API__ k_err_t tos_mail_q_post_all(k_mail_q_t *mail_q, void *mail_buf, size_t m
#endif
__CDECLS_END
#endif /* _TOS_MAIL_QUEUE_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_MESSAGE_QUEUE_H_
#define _TOS_MESSAGE_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_MESSAGE_QUEUE_EN > 0u
typedef struct k_message_queue_st {
@@ -150,5 +152,7 @@ __API__ k_err_t tos_msg_q_post_all(k_msg_q_t *msg_q, void *msg_ptr);
#endif
__CDECLS_END
#endif /* _TOS_MESSAGE_QUEUE_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_MMBLK_H_
#define _TOS_MMBLK_H_
__CDECLS_BEGIN
#if TOS_CFG_MMBLK_EN > 0u
#define K_MMBLK_NEXT_BLK(blk_curr, blk_size) ((void *)((cpu_addr_t)blk_curr + blk_size))
@@ -103,5 +105,7 @@ __API__ k_err_t tos_mmblk_free(k_mmblk_pool_t *mbp, void *blk);
#endif
__CDECLS_END
#endif /* _TOS_MMBLK_H_ */

View File

@@ -55,6 +55,8 @@
#ifndef _TOS_MMHEAP_H_
#define _TOS_MMHEAP_H_
__CDECLS_BEGIN
#if TOS_CFG_MMHEAP_EN > 0u
/**
@@ -262,5 +264,7 @@ __KNL__ k_err_t mmheap_init_with_pool(void *pool_start, size_t pool_size);
#endif
__CDECLS_END
#endif /* _TOS_MMHEAP_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_MUTEX_H_
#define _TOS_MUTEX_H_
__CDECLS_BEGIN
#if TOS_CFG_MUTEX_EN > 0u
typedef struct k_mutex_st {
@@ -114,5 +116,7 @@ __KNL__ void mutex_release(k_mutex_t *mutex);
#endif
__CDECLS_END
#endif /* _TOS_MUTEX_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_PEND_H_
#define _TOS_PEND_H_
__CDECLS_BEGIN
typedef struct k_task_st k_task_t;
/**
@@ -67,5 +69,7 @@ __KNL__ void pend_wakeup_all(pend_obj_t *object, pend_state_t state);
__KNL__ void pend_wakeup(pend_obj_t *object, pend_state_t state, opt_post_t opt);
__CDECLS_END
#endif /* _TOS_PEND_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_PRIORITY_MAIL_QUEUE_H_
#define _TOS_PRIORITY_MAIL_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_PRIORITY_MAIL_QUEUE_EN > 0u
typedef struct k_priority_mail_queue_st {
@@ -160,5 +162,7 @@ __API__ k_err_t tos_prio_mail_q_post_all(k_prio_mail_q_t *prio_mail_q, void *mai
#endif
__CDECLS_END
#endif /* TOS_CFG_PRIORITY_MAIL_QUEUE_EN */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_PRIORITY_MESSAGE_QUEUE_H_
#define _TOS_PRIORITY_MESSAGE_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN > 0u
typedef struct k_priority_message_queue_st {
@@ -155,5 +157,7 @@ __API__ k_err_t tos_prio_msg_q_post_all(k_prio_msg_q_t *prio_msg_q, void *msg_pt
#endif
__CDECLS_END
#endif /* _TOS_PRIORITY_MESSAGE_QUEUE_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_PRIORITY_QUEUE_H_
#define _TOS_PRIORITY_QUEUE_H_
__CDECLS_BEGIN
typedef uint16_t prio_q_slot_t;
typedef struct prio_q_pool_manager_entry_st {
@@ -207,5 +209,7 @@ __API__ int tos_prio_q_is_empty(k_prio_q_t *prio_q);
*/
__API__ int tos_prio_q_is_full(k_prio_q_t *prio_q);
__CDECLS_END
#endif /* _TOS_PRIORITY_QUEUE_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_ROBIN_H_
#define _TOS_ROBIN_H_
__CDECLS_BEGIN
#if TOS_CFG_ROUND_ROBIN_EN > 0u
/**
@@ -50,5 +52,7 @@ __KNL__ void robin_sched(k_prio_t prio);
#endif
__CDECLS_END
#endif /* _TOS_ROBIN_H_ */

View File

@@ -0,0 +1,192 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_RWLOCK_H_
#define _TOS_RWLOCK_H_
__CDECLS_BEGIN
#if (TOS_CFG_SEM_EN > 0u) && (TOS_CFG_MUTEX_EN > 0u)
typedef uint16_t rw_cnt_t;
typedef struct k_rwlock_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_t knl_obj;
#endif
k_mutex_t lock;
k_sem_t signal;
rw_cnt_t n_readers; /* how many readers are reading? */
rw_cnt_t n_writers; /* how many writers are waiting to obtain the wlock? */
int is_writting;
} k_rwlock_t;
/**
* @brief Create a read-write lock.
*
* @attention a read-write lock can be hold by multi-readers, that means simultaneously reading is allowed;
* but a read-write lock can only be hold by one writes, that means simultaneously writting or read while writting is not allowed.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_rwlock_create(k_rwlock_t *rwlock);
/**
* @brief Destroy a read-write lock.
*
* @attention
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_rwlock_destroy(k_rwlock_t *rwlock);
/**
* @brief Pend on the read-lock of a read-write lock.
*
* @attention if one reader already hold the read-lock, other reader can hold the read-lock simultaneously.
* and no writers can hold the write-lock.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_READERS_TO_MANY too many reader are holding the read-lock
*/
__API__ k_err_t tos_rwlock_rpend_timed(k_rwlock_t *rwlock, k_tick_t timeout);
/**
* @brief Pend on the read-lock of a read-write lock.
*
* @attention if one reader already hold the read-lock, other reader can hold the read-lock simultaneously.
* and no writers can hold the write-lock.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_READERS_TO_MANY too many reader are holding the read-lock
*/
__API__ k_err_t tos_rwlock_rpend(k_rwlock_t *rwlock);
/**
* @brief Try pend on the read-lock of a read-write lock.
*
* @attention Try means just take a look, if can obtain the read-lock, then we obtain it; otherwise, just return with no-waiting.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_IS_WRITTING the read-write lock is hold by a writter(is writting).
*/
__API__ k_err_t tos_rwlock_rpend_try(k_rwlock_t *rwlock);
/**
* @brief Pend on the write-lock of a read-write lock.
*
* @attention if one writer already hold the write-lock, other writer CANNOT hold the write-lock any more.
* and no readers can hold the read-lock.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_WAITING_WRITERS_TO_MANY too many writers are waiting for the write-lock
*/
__API__ k_err_t tos_rwlock_wpend_timed(k_rwlock_t *rwlock, k_tick_t timeout);
/**
* @brief Pend on the write-lock of a read-write lock.
*
* @attention if one writer already hold the write-lock, other writer CANNOT hold the write-lock any more.
* and no readers can hold the read-lock.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_WAITING_WRITERS_TO_MANY too many writers are waiting for the write-lock
*/
__API__ k_err_t tos_rwlock_wpend(k_rwlock_t *rwlock);
/**
* @brief Pend on the write-lock of a read-write lock.
*
* @attention Try means just take a look, if can obtain the write-lock, then we obtain it; otherwise, just return with no-waiting.
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_IS_READING the read-write lock is hold by other reader[s](is reading).
* @retval #K_ERR_RWLOCK_IS_WRITTING the read-write lock is hold by another writter(is writting).
*/
__API__ k_err_t tos_rwlock_wpend_try(k_rwlock_t *rwlock);
/**
* @brief Post the read-lock of a read-write lock.
*
* @attention
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_NOT_READING the read-lock is not held by reader[s].
*/
__API__ k_err_t tos_rwlock_rpost(k_rwlock_t *rwlock);
/**
* @brief Post the write-lock of a read-write lock.
*
* @attention
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_NOT_WRITTING the write-lock is not held by a writter.
*/
__API__ k_err_t tos_rwlock_wpost(k_rwlock_t *rwlock);
/**
* @brief Post the read&write-lock of a read-write lock.
*
* @attention
*
* @param[in] rwlock the read-write lock.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RWLOCK_NOT_TAKEN the read-write lock is neither held by reader[s] nor held by a writter.
*/
__API__ k_err_t tos_rwlock_post(k_rwlock_t *rwlock);
#endif
__CDECLS_END
#endif /* _TOS_RWLOCK_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_SCHED_H_
#define _TOS_SCHED_H_
__CDECLS_BEGIN
#define K_PRIO_TBL_SIZE ((TOS_CFG_TASK_PRIO_MAX + 31) / 32)
#define K_PRIO_TBL_SLOT_SIZE (32u)
@@ -48,5 +50,7 @@ __KNL__ void readyqueue_remove(k_task_t *task);
__KNL__ void readyqueue_move_head_to_tail(k_prio_t prio);
__CDECLS_END
#endif /* _TOS_SCHED_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_SEM_H_
#define _TOS_SEM_H_
__CDECLS_BEGIN
#if TOS_CFG_SEM_EN > 0u
typedef struct k_sem_st {
@@ -120,5 +122,7 @@ __API__ k_err_t tos_sem_post_all(k_sem_t *sem);
#endif
__CDECLS_END
#endif /* _TOS_SEM_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_SLIST_H_
#define _TOS_SLIST_H_
__CDECLS_BEGIN
typedef struct k_slist_node_st {
struct k_slist_node_st *next;
} k_slist_t;
@@ -181,5 +183,7 @@ __API__ __STATIC_INLINE__ int tos_slist_empty(k_slist_t *slist)
return !slist->next;
}
__CDECLS_END
#endif /* _TOS_SLIST_H_ */

View File

@@ -0,0 +1,141 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_STOPWATCH_H_
#define _TOS_STOPWATCH_H_
__CDECLS_BEGIN
typedef struct k_stopwatch_st {
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_t knl_obj;
#endif
k_tick_t until;
} k_stopwatch_t;
/**
* @brief Create a stopwatch.
*
* @attention
*
* @param[in] stopwatch the stopwatch.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_stopwatch_create(k_stopwatch_t *stopwatch);
/**
* @brief Destroy a stopwatch.
*
* @attention
*
* @param[in] stopwatch the stopwatch.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_stopwatch_destroy(k_stopwatch_t *stopwatch);
/**
* @brief Count down for a certain tick.
*
* @attention
*
* @param[in] stopwatch the stopwatch.
* @param[in] tick tick to count down.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_stopwatch_countdown(k_stopwatch_t *stopwatch, k_tick_t tick);
/**
* @brief Count down for a certain time(in millisecond).
*
* @attention
*
* @param[in] stopwatch the stopwatch.
* @param[in] millisec time(in millisecond) to count down.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_stopwatch_countdown_ms(k_stopwatch_t *stopwatch, k_time_t millisec);
/**
* @brief Delay for a certain tick.
*
* @attention the stopwatch delay is a "busy" delay without give up of CPU(compared to tos_task_delay)
*
* @param[in] tick tick to delay.
*
* @return None
*/
__API__ void tos_stopwatch_delay(k_tick_t tick);
/**
* @brief Delay for a certain time(in millisecond).
*
* @attention the stopwatch delay is a "busy" delay without give up of CPU(compared to tos_task_delay)
*
* @param[in] millisec time(in millisecond) to delay.
*
* @return None
*/
__API__ void tos_stopwatch_delay_ms(k_time_t millisec);
/**
* @brief How much time remain of the stopwatch(in tick).
*
* @attention
*
* @param[in] stopwatch ticks remain.
*
* @return ticks remain
*/
__API__ k_tick_t tos_stopwatch_remain(k_stopwatch_t *stopwatch);
/**
* @brief How much time remain of the stopwatch(in millisecond).
*
* @attention
*
* @param[in] stopwatch milliseconds remain.
*
* @return milliseconds remain
*/
__API__ k_time_t tos_stopwatch_remain_ms(k_stopwatch_t *stopwatch);
/**
* @brief Whether the stopwatch is expired.
*
* @attention
*
* @param[in] stopwatch milliseconds remain.
*
* @return whether the stopwatch is expired
* @retval #K_TRUE the stopwatch is expired.
* @retval #K_FALSE the stopwatch is no expired.
*/
__API__ int tos_stopwatch_is_expired(k_stopwatch_t *stopwatch);
__CDECLS_END
#endif /* _TOS_STOPWATCH_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_SYS_H_
#define _TOS_SYS_H_
__CDECLS_BEGIN
#define K_NESTING_LIMIT_IRQ (k_nesting_t)250u
#define K_NESTING_LIMIT_SCHED_LOCK (k_nesting_t)250u
@@ -28,26 +30,31 @@ typedef enum knl_state_en {
// some kind of magic number, mainly for identifing whether the object is initialized, or whether user pass the correct parameter.
typedef enum knl_obj_type_en {
KNL_OBJ_TYPE_NONE = 0x0000,
KNL_OBJ_TYPE_TASK = 0xDAD1,
KNL_OBJ_TYPE_TIMER = 0xDAD2,
KNL_OBJ_TYPE_MSG_QUEUE = 0xDAD3,
KNL_OBJ_TYPE_MMBLK_POOL = 0xDAD4,
KNL_OBJ_TYPE_RING_QUEUE = 0xDAD5,
KNL_OBJ_TYPE_BINARY_HEAP = 0xDAD6,
KNL_OBJ_TYPE_PRIORITY_QUEUE = 0xDAD7,
KNL_OBJ_TYPE_CHAR_FIFO = 0xDAD8,
KNL_OBJ_TYPE_NONE = 0x0000,
KNL_OBJ_TYPE_BINARY_HEAP = 0xDAD0,
KNL_OBJ_TYPE_BITMAP = 0xDAD1,
KNL_OBJ_TYPE_CHAR_FIFO = 0xDAD2,
KNL_OBJ_TYPE_MMBLK_POOL = 0xDAD3,
KNL_OBJ_TYPE_MSG_QUEUE = 0xDAD4,
KNL_OBJ_TYPE_PRIORITY_QUEUE = 0xDAD5,
KNL_OBJ_TYPE_RING_QUEUE = 0xDAD6,
KNL_OBJ_TYPE_STOPWATCH = 0xDAD7,
KNL_OBJ_TYPE_TASK = 0xDAD8,
KNL_OBJ_TYPE_TIMER = 0xDAD9,
// ipc object
KNL_OBJ_TYPE_SEMAPHORE = 0x1BEE,
KNL_OBJ_TYPE_MUTEX = 0x2BEE,
KNL_OBJ_TYPE_EVENT = 0x3BEE,
KNL_OBJ_TYPE_MAIL_QUEUE = 0x4BEE,
KNL_OBJ_TYPE_MESSAGE_QUEUE = 0x5BEE,
KNL_OBJ_TYPE_PRIORITY_MAIL_QUEUE = 0x6BEE,
KNL_OBJ_TYPE_PRIORITY_MESSAGE_QUEUE = 0x7BEE,
KNL_OBJ_TYPE_COUNTDOWNLATCH = 0x8BEE,
KNL_OBJ_TYPE_COMPLETION = 0x9BEE,
KNL_OBJ_TYPE_BARRIER = 0x0BEE,
KNL_OBJ_TYPE_COMPLETION = 0x1BEE,
KNL_OBJ_TYPE_COUNTDOWNLATCH = 0x2BEE,
KNL_OBJ_TYPE_EVENT = 0x3BEE,
KNL_OBJ_TYPE_MAIL_QUEUE = 0x4BEE,
KNL_OBJ_TYPE_MESSAGE_QUEUE = 0x5BEE,
KNL_OBJ_TYPE_MUTEX = 0x6BEE,
KNL_OBJ_TYPE_PRIORITY_MAIL_QUEUE = 0x7BEE,
KNL_OBJ_TYPE_PRIORITY_MESSAGE_QUEUE = 0x8BEE,
KNL_OBJ_TYPE_RWLOCK = 0x9BEE,
KNL_OBJ_TYPE_SEMAPHORE = 0xABEE,
} knl_obj_type_t;
typedef enum knl_obj_alloc_type_en {
@@ -218,5 +225,7 @@ __KNL__ __STATIC_INLINE__ int knl_object_alloc_is_static(knl_obj_t *knl_obj)
#endif
__CDECLS_END
#endif /* _TOS_SYS_H_ */

View File

@@ -18,6 +18,11 @@
#ifndef _TOS_TASK_H_
#define _TOS_TASK_H_
__CDECLS_BEGIN
#define K_TASK_NAME_MAX (16u)
#define K_TASK_STK_SIZE_MIN (sizeof(cpu_context_t))
// task state is just a flag, indicating which manager list we are in.
// ready to schedule
@@ -67,43 +72,43 @@ typedef void (*k_task_walker_t)(k_task_t *task);
* task control block
*/
typedef struct k_task_st {
k_stack_t *sp; /**< task stack pointer. This lady always comes first, we count on her in port_s.S for context switch. */
k_stack_t *sp; /**< task stack pointer. This lady always comes first, we count on her in port_s.S for context switch. */
knl_obj_t knl_obj; /**< just for verification, test whether current object is really a task. */
knl_obj_t knl_obj; /**< just for verification, test whether current object is really a task. */
char *name; /**< task name */
k_task_entry_t entry; /**< task entry */
void *arg; /**< argument for task entry */
k_task_state_t state; /**< just state */
k_prio_t prio; /**< just priority */
char name[K_TASK_NAME_MAX]; /**< task name */
k_task_entry_t entry; /**< task entry */
void *arg; /**< argument for task entry */
k_task_state_t state; /**< just state */
k_prio_t prio; /**< just priority */
k_stack_t *stk_base; /**< task stack base address */
size_t stk_size; /**< stack size of the task */
k_stack_t *stk_base; /**< task stack base address */
size_t stk_size; /**< stack size of the task */
#if TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u
k_list_t dead_list; /**< when a dynamic allocated task destroyed, we hook the task's dead_list to the k_dead_task_list */
k_list_t dead_list; /**< when a dynamic allocated task destroyed, we hook the task's dead_list to the k_dead_task_list */
#endif
k_list_t stat_list; /**< list for hooking us to the k_stat_list */
k_list_t stat_list; /**< list for hooking us to the k_stat_list */
k_tick_t tick_expires; /**< if we are in k_tick_list, how much time will we wait for? */
k_tick_t tick_expires; /**< if we are in k_tick_list, how much time will we wait for? */
k_list_t tick_list; /**< list for hooking us to the k_tick_list */
k_list_t pend_list; /**< when we are ready, our pend_list is in readyqueue; when pend, in a certain pend object's list. */
k_list_t tick_list; /**< list for hooking us to the k_tick_list */
k_list_t pend_list; /**< when we are ready, our pend_list is in readyqueue; when pend, in a certain pend object's list. */
#if TOS_CFG_MUTEX_EN > 0u
k_list_t mutex_own_list; /**< the list hold all the mutex we own.
When we die(tos_task_destroy), we have an obligation to wakeup all the task pending for those mutexs we own;
if not, those pending tasks may never get a chance to wakeup. */
k_prio_t prio_pending; /*< when tos_task_prio_change called, we may be just the owner of a mutex.
to avoid PRIORITY INVERSION, must make sure our priority is higher than any one who is pending for
the mutex we hold. So, if the prio_new of tos_task_prio_change is not appropriate
(may against the principle of PRIORITY INVERSION), we just mark the prio_new here, do the real priority
change in the right time(mutex_old_owner_release) later. */
k_list_t mutex_own_list; /**< the list hold all the mutex we own.
When we die(tos_task_destroy), we have an obligation to wakeup all the task pending for those mutexs we own;
if not, those pending tasks may never get a chance to wakeup. */
k_prio_t prio_pending; /*< when tos_task_prio_change called, we may be just the owner of a mutex.
to avoid PRIORITY INVERSION, must make sure our priority is higher than any one who is pending for
the mutex we hold. So, if the prio_new of tos_task_prio_change is not appropriate
(may against the principle of PRIORITY INVERSION), we just mark the prio_new here, do the real priority
change in the right time(mutex_old_owner_release) later. */
#endif
pend_obj_t *pending_obj; /**< if we are pending, which pend object's list we are in? */
pend_state_t pend_state; /**< why we wakeup from a pend */
pend_obj_t *pending_obj; /**< if we are pending, which pend object's list we are in? */
pend_state_t pend_state; /**< why we wakeup from a pend */
#if TOS_CFG_ROUND_ROBIN_EN > 0u
k_timeslice_t timeslice_reload; /**< if current time slice is used up, use time_slice_reload to reload our time slice */
@@ -406,7 +411,9 @@ __DEBUG__ __STATIC_INLINE__ void task_default_walker(k_task_t *task)
state_str = state_str;
tos_kprintln("tsk name: %s", task->name);
if (task->state == K_TASK_STATE_PENDTIMEOUT_SUSPENDED) {
if (tos_task_curr_task_get() == task) {
state_str = "RUNNING";
} else if (task->state == K_TASK_STATE_PENDTIMEOUT_SUSPENDED) {
state_str = "PENDTIMEOUT_SUSPENDED";
} else if (task->state == K_TASK_STATE_PEND_SUSPENDED) {
state_str = "PEND_SUSPENDED";
@@ -431,5 +438,7 @@ __DEBUG__ __STATIC_INLINE__ void task_default_walker(k_task_t *task)
tos_kprintf("\n");
}
__CDECLS_END
#endif /* _TOS_TASK_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_TICK_H_
#define _TOS_TICK_H_
__CDECLS_BEGIN
/**
* @brief Systick interrupt handler.
* systick interrupt handler.
@@ -40,5 +42,7 @@ __KNL__ void tick_list_remove(k_task_t *task);
__KNL__ k_tick_t tick_next_expires_get(void);
#endif
__CDECLS_END
#endif /* _TOS_TICK_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_TIME_H_
#define _TOS_TIME_H_
__CDECLS_BEGIN
// if you wanna pend for something forever, use TOS_TIME_FOREVER
#define TOS_TIME_FOREVER (k_tick_t)(-1)
// if you don't wanna wait when you pend nothing, use TOS_TIME_NOWAIT
@@ -106,5 +108,7 @@ __API__ k_err_t tos_sleep_ms(k_time_t millisec);
*/
__API__ k_err_t tos_sleep_hmsm(k_time_t hour, k_time_t minute, k_time_t second, k_time_t millisec);
__CDECLS_END
#endif /* _TOS_TIME_H_ */

View File

@@ -18,6 +18,8 @@
#ifndef _TOS_TIMER_H_
#define _TOS_TIMER_H_
__CDECLS_BEGIN
#if TOS_CFG_TIMER_EN > 0u
// if we just want the timer to run only once, this option should be passed to tos_timer_create.
@@ -185,5 +187,7 @@ __KNL__ k_tick_t timer_next_expires_get(void);
#endif
__CDECLS_END
#endif /* _TOS_TIMER_H_ */