first init

This commit is contained in:
supowang
2024-01-03 11:19:04 +08:00
parent 837003ea32
commit 40be5eb96d
341 changed files with 93068 additions and 32 deletions

View File

@@ -0,0 +1,90 @@
/*----------------------------------------------------------------------------
* 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
#if TOS_CFG_BARRIER_EN > 0
typedef struct k_barrier_st {
knl_obj_t knl_obj;
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 Destroy a thread barrier.
*
* @attention
*
* @param[in] barrier the barrier.
*
* @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);
#endif /* TOS_CFG_BARRIER_EN */
__CDECLS_END
#endif /* _TOS_BARRIER_H_ */

View File

@@ -0,0 +1,180 @@
/*----------------------------------------------------------------------------
* 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_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 {
knl_obj_t knl_obj;
size_t total;
k_bin_heap_cmp cmp;
size_t item_size;
size_t item_cnt;
uint8_t *pool;
} k_bin_heap_t;
#define BIN_HEAP_FIRST_ITEM(bin_heap) (void *)(&bin_heap->pool[0])
#define BIN_HEAP_LAST_ITEM(bin_heap) (void *)(&bin_heap->pool[bin_heap->total * bin_heap->item_size])
#define BIN_HEAP_THE_ITEM(bin_heap, index) (void *)(&bin_heap->pool[index * bin_heap->item_size])
#define BIN_HEAP_PARENT(index) ((index - 1) / 2)
#define BIN_HEAP_RCHILD(index) (2 * (index + 1))
#define BIN_HEAP_LSIBLING(index) (index - 1)
/**
* @brief Create a binary heap.
* create a binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[in] pool pool buffer of the binary heap.
* @param[in] item_cnt item count of the binary heap.
* @param[in] item_size size of each item of the binary heap.
* @param[in] cmp compare function to determine two items which is bigger or smaller.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_create(k_bin_heap_t *bin_heap, void *pool, size_t item_cnt, size_t item_size, k_bin_heap_cmp cmp);
/**
* @brief Destroy a binary heap.
* destroy a binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_destroy(k_bin_heap_t *bin_heap);
/**
* @brief Create a binary heap with a dynamic allocated pool.
* create a binary heap with a dynamic allocated pool.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[in] item_cnt item count of the binary heap.
* @param[in] item_size size of each item of the binary heap.
* @param[in] cmp compare function to determine two items which is bigger or smaller.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_create_dyn(k_bin_heap_t *bin_heap, size_t item_cnt, size_t item_size, k_bin_heap_cmp cmp);
/**
* @brief Destroy a binary heap with a dynamic allocated pool.
* destroy a binary heap with a dynamic allocated pool.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_destroy_dyn(k_bin_heap_t *bin_heap);
/**
* @brief Push an item.
* push an item into the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[in] item the item to be pushed.
* @param[in] item_size size of the item(should be consistent with the item_size passed to tos_bin_heap_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BIN_HEAP_ITEM_SIZE_NOT_MATCH the item_size is not consistent with the item_size passed to tos_bin_heap_create.
* @retval #K_ERR_BIN_HEAP_FULL the binary heap is full.
*/
__API__ k_err_t tos_bin_heap_push(k_bin_heap_t *bin_heap, void *item, size_t item_size);
/**
* @brief Pop an item.
* pop an item from the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
* @param[out] item buffer to hold the item poped.
* @param[out] item_size size of the item poped(should be consistent with the item_size passed to tos_bin_heap_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_BIN_HEAP_EMPTY the ring queue is empty.
*/
__API__ k_err_t tos_bin_heap_pop(k_bin_heap_t *bin_heap, void *item, size_t *item_size);
/**
* @brief Flush the binary heap.
* flush the binary heap.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_bin_heap_flush(k_bin_heap_t *bin_heap);
/**
* @brief Whether the binary heap is empty.
* Whether the binary heap is empty.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return whether the binary heap is emtpy.
* @retval #0 the binary heap is not empty.
* @retval #Not 0 the binary heap is empty.
*/
__API__ int tos_bin_heap_is_empty(k_bin_heap_t *bin_heap);
/**
* @brief Whether the binary heap is full.
* Whether the binary heap is full.
*
* @attention None
*
* @param[in] bin_heap pointer to the handler of the binary heap.
*
* @return whether the binary hea is full.
* @retval #0 the binary hea is not full.
* @retval #Not 0 the binary hea is full.
*/
__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,152 @@
/*----------------------------------------------------------------------------
* 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 {
knl_obj_t knl_obj;
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.
*
* @return the lowest significant bit of the bitmap.
*/
__API__ int tos_bitmap_lsb(k_bitmap_t *bitmap);
#endif /* _TOS_BITMAP_H_ */

View File

@@ -0,0 +1,191 @@
/*----------------------------------------------------------------------------
* 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_CHAR_FIFO_H_
#define _TOS_CHAR_FIFO_H_
__CDECLS_BEGIN
typedef struct k_char_fifo_st {
knl_obj_t knl_obj;
k_ring_q_t ring_q;
} k_chr_fifo_t;
/**
* @brief Create a character fifo.
* Create a character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the fifo.
* @param[in] buffer memory buffer provided to be as the inner buffer.
* @param[in] size size of the memory buffer.
*
* @return errno
* @retval #K_ERR_OBJ_PTR_NULL fifo is a NULL pointer.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_create(k_chr_fifo_t *chr_fifo, void *buffer, size_t size);
/**
* @brief Destroy a character fifo.
* Destroy a character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the fifo.
*
* @return errno
* @retval #K_ERR_OBJ_PTR_NULL fifo is a NULL pointer.
* @retval #K_ERR_OBJ_INVALID not a valid pointer to a fifo.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_destroy(k_chr_fifo_t *chr_fifo);
/**
* @brief Create a character fifo with a dynamic allocated buffer.
* Create a character fifo with a dynamic allocated buffer.
*
* @attention the buffer is dynamic allocated(tos_mmheap_alloc)
*
* @param[in] fifo pointer to the handler of the fifo.
* @param[in] fifo_size size of the fifo.
*
* @return errno
* @retval #K_ERR_OBJ_PTR_NULL fifo is a NULL pointer.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_create_dyn(k_chr_fifo_t *chr_fifo, size_t fifo_size);
/**
* @brief Destroy a character fifo with a dynamic allocated buffer.
* Destroy a character fifo with a dynamic allocated buffer.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the fifo.
*
* @return errno
* @retval #K_ERR_OBJ_PTR_NULL fifo is a NULL pointer.
* @retval #K_ERR_OBJ_INVALID not a valid pointer to a fifo.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_destroy_dyn(k_chr_fifo_t *chr_fifo);
/**
* @brief Push data into character fifo.
* Push one single data into the character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
* @param[in] data data to push into the fifo.
*
* @return errno
* @retval #K_ERR_RING_Q_FULL the character fifo is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_push(k_chr_fifo_t *chr_fifo, uint8_t data);
/**
* @brief Push stream into character fifo.
* Push a stream data into the character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
* @param[IN] stream stream to be pushed into the character fifo.
* @param[OUT] size size of the stream.
*
* @return the actual number of the data pushed into the character fifo.
*/
__API__ int tos_chr_fifo_push_stream(k_chr_fifo_t *chr_fifo, uint8_t *stream, size_t size);
/**
* @brief Pop data from character fifo.
* Pop one single data from the character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
* @param[OUT] out one signle buffer to hold the data poped from the character fifo.
*
* @return errno
* @retval #K_ERR_FIFO_EMPTY the character fifo is empty.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_chr_fifo_pop(k_chr_fifo_t *chr_fifo, uint8_t *out);
/**
* @brief Pop stream from character fifo.
* Pop a stream data from the character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
* @param[OUT] buffer pointer to the buffer to receive the stream poped.
* @param[OUT] size size of the buffer.
*
* @return the actual number of the data poped from the character fifo.
*/
__API__ int tos_chr_fifo_pop_stream(k_chr_fifo_t *chr_fifo, uint8_t *buffer, size_t size);
/**
* @brief Flush character fifo.
* Flush/reset the character fifo.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
*
* @return None.
*/
__API__ k_err_t tos_chr_fifo_flush(k_chr_fifo_t *chr_fifo);
/**
* @brief Whether the character fifo is empty.
* Whether the character fifo is empty.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
*
* @return whether the character fifo is emtpy.
* @retval #0 the character fifo is not empty.
* @retval #Not 0 the character fifo is empty.
*/
__API__ int tos_chr_fifo_is_empty(k_chr_fifo_t *chr_fifo);
/**
* @brief Whether the character fifo is full.
* Whether the character fifo is full.
*
* @attention None
*
* @param[in] fifo pointer to the handler of the character fifo.
*
* @return whether the character fifo is full.
* @retval #0 the character fifo is not full.
* @retval #Not 0 the character fifo is full.
*/
__API__ int tos_chr_fifo_is_full(k_chr_fifo_t *chr_fifo);
__CDECLS_END
#endif /* _TOS_CHAR_FIFO_H_ */

View File

@@ -0,0 +1,166 @@
/*----------------------------------------------------------------------------
* 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_COMPILER_H_
#define _TOS_COMPILER_H_
/* function with __API__ prefix, api for user */
#define __API__
/* function with __KNL__ prefix, only for kernel */
#define __KNL__
/* function with __HOOK__ prefix, should be implemented by user */
#define __HOOK__
/* function with __DEBUG__ prefix, only for debug */
#define __DEBUG__
/* 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)
#define ARMCC_V5
#define __ASM__ __asm
#define __VOLATILE__ volatile
#define __INLINE__ inline
#define __STATIC__ static
#if (__ARMCC_VERSION < 5060750) // how to know the exact number?
#define __STATIC_INLINE__ static
#else
#define __STATIC_INLINE__ static inline
#endif
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define __UNUSED__ __attribute__((__unused__))
#define __USED__ __attribute__((__used__))
#define __PACKED__ __attribute__((packed))
#define __ALIGNED__(x) __attribute__((aligned(x)))
#define __PURE__ __attribute__((__pure__))
#define __CONST__ __attribute__((__const__))
#define __NO_RETURN__ __attribute__((__noreturn__))
#define __WEAK__ __attribute__((weak))
/*------------------ ARM Compiler V6 -------------------*/
#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#define ARMCC_V6
#define __ASM__ __asm
#define __VOLATILE__ volatile
#define __INLINE__ inline
#define __STATIC__ static
#define __STATIC_INLINE__ static inline
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define __UNUSED__ __attribute__((__unused__))
#define __USED__ __attribute__((__used__))
#define __PACKED__ __attribute__((packed))
#define __ALIGNED__(x) __attribute__((aligned(x)))
#define __PURE__ __attribute__((__pure__))
#define __CONST__ __attribute__((__const__))
#define __NO_RETURN__ __attribute__((__noreturn__))
#define __NAKED__ __attribute__((naked))
#define __WEAK__ __attribute__((weak))
/*------------------ ICC Compiler ----------------------*/
#elif defined(__ICCARM__) || defined(__ICC430__) // __IAR_SYSTEMS_ICC__
#define __ASM__ __asm
#define __VOLATILE__ volatile
#define __INLINE__ inline
#define __STATIC__ static
#define __STATIC_INLINE__ static inline
#define likely(x) (x)
#define unlikely(x) (x)
#define __UNUSED__
#define __USED__
#define __PACKED__
#define __ALIGNED__(x)
#define __PURE__
#define __CONST__
#define __NO_RETURN__
#define __NAKED__
#define __WEAK__ __weak
/*------------------ ICC Compiler for STM8/AVR ----------------------*/
#elif defined(__IAR_SYSTEMS_ICC__)
#define __ASM__ __asm
#define __VOLATILE__ volatile
#define __INLINE__ inline
#define __STATIC__ static
#define __STATIC_INLINE__ static inline
#define likely(x) (x)
#define unlikely(x) (x)
#define __UNUSED__
#define __USED__
#define __PACKED__
#define __ALIGNED__(x)
#define __PURE__
#define __CONST__
#define __NO_RETURN__
#define __NAKED__
#define __WEAK__ __weak
/*------------------ GNU Compiler ----------------------*/
#elif defined(__GNUC__)
#define __ASM__ __asm
#define __VOLATILE__ volatile
#define __INLINE__ inline
#define __STATIC__ static
#define __STATIC_INLINE__ static inline
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define __UNUSED__ __attribute__((__unused__))
#define __USED__ __attribute__((__used__))
#define __PACKED__ __attribute__((packed))
#define __ALIGNED__(x) __attribute__((aligned(x)))
#define __PURE__ __attribute__((__pure__))
#define __CONST__ __attribute__((__const__))
#define __NO_RETURN__ __attribute__((__noreturn__))
#define __NAKED__ __attribute__((naked))
#define __WEAK__ __attribute__((weak))
#endif
#endif /* _TOS_COMPILER_H_ */

View File

@@ -0,0 +1,154 @@
/*----------------------------------------------------------------------------
* 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_COMPLETION_H_
#define _TOS_COMPLETION_H_
__CDECLS_BEGIN
#if TOS_CFG_COMPLETION_EN > 0
typedef uint16_t completion_done_t;
typedef struct k_completion_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
completion_done_t done;
} k_completion_t;
/**
* @brief Create a completion.
* create a completion.
*
* @attention None
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_create(k_completion_t *completion);
/**
* @brief Destroy a completion.
* destroy a completion.
*
* @attention None
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_OBJ_INVALID completion is not a valid pointer to completion
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_destroy(k_completion_t *completion);
/**
* @brief Pend a completion.
* pend a completion.
*
* @attention None
*
* @param[in] completion pointer to the handler of the completion.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the completion we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_pend_timed(k_completion_t *completion, k_tick_t timeout);
/**
* @brief Pend a completion.
* pend a completion.
*
* @attention None
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_DESTROY the completion we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_pend(k_completion_t *completion);
/**
* @brief Post a completion.
* post a completion and wakeup one pending task.
*
* @attention when tos_completion_post return successfully, only one task who are waitting for the completion will be woken up.
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_COMPLETION_OVERFLOW we are nesting post a completion too much.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_post(k_completion_t *completion);
/**
* @brief Post a completion.
* post a completion and wakeup all the pending task.
*
* @attention when tos_completion_post_all return successfully, all of the tasks who are waitting for the completion will be woken up.
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_COMPLETION_OVERFLOW we are nesting post a completion too much.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_post_all(k_completion_t *completion);
/**
* @brief Reset a completion.
* reset a completion to un-done.
*
* @attention None.
*
* @param[in] completion pointer to the handler of the completion.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_completion_reset(k_completion_t *completion);
/**
* @brief Test whether a completion is done.
* test whether a completion is done.
*
* @attention None
*
* @param[in] completion pointer to the handler of the completion.
*
* @return whether a completion is done
* @retval K_TRUE the completion is done.
* @retval K_FALSE the completion is not done.
*/
__API__ int tos_completion_is_done(k_completion_t *completion);
#endif /* TOS_CFG_COMPLETION_EN */
__CDECLS_END
#endif /* _TOS_COMPLETION_H_ */

View File

@@ -0,0 +1,96 @@
/*----------------------------------------------------------------------------
* 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_CONFIG_CHECK_H_
#define _TOS_CONFIG_CHECK_H_
#if TOS_CFG_EVENT_DRIVEN_EN > 0u
#if TOS_CFG_TICKLESS_EN == 1u
#error "INVALID config, tickless not supported in event-driven yet"
#endif
#if (TOS_CFG_MMHEAP_EN > 0u) && (TOS_CFG_MMHEAP_DEFAULT_POOL_EN > 0u)
#if !defined(TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE) || (TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE == 0u)
#error "INVALID config, must define a valid TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE"
#endif
#endif
#else /* TOS_CFG_EVENT_DRIVEN_EN */
#if TOS_CFG_TASK_PRIO_MAX < 8u
#error "INVALID config, TOS_CFG_TASK_PRIO_MAX must be >= 8"
#endif
#if ((TOS_CFG_TIMER_EN > 0u) && !defined(TOS_CFG_TIMER_AS_PROC))
#error "UNDECLARED config, TOS_CFG_TIMER_AS_PROC"
#endif
#if (TOS_CFG_MMHEAP_EN > 0u) && (TOS_CFG_MMHEAP_DEFAULT_POOL_EN > 0u)
#if !defined(TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE) || (TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE == 0u)
#error "INVALID config, must define a valid TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE"
#endif
#endif
#ifndef TOS_CFG_CPU_HRTIMER_EN
#error "UNDECLARED config, TOS_CFG_CPU_HRTIMER_EN should be declared in 'port_config.h'"
#elif (TOS_CFG_CPU_HRTIMER_EN > 0u) && !defined(TOS_CFG_CPU_HRTIMER_SIZE)
#error "UNDECLARED config, TOS_CFG_CPU_HRTIMER_SIZE should be declared in 'port_config.h'"
#elif (TOS_CFG_CPU_HRTIMER_EN > 0u) && ((TOS_CFG_CPU_HRTIMER_SIZE != CPU_WORD_SIZE_08) && \
(TOS_CFG_CPU_HRTIMER_SIZE != CPU_WORD_SIZE_16) && \
(TOS_CFG_CPU_HRTIMER_SIZE != CPU_WORD_SIZE_32) && \
(TOS_CFG_CPU_HRTIMER_SIZE != CPU_WORD_SIZE_64))
#error "INVALID config, TOS_CFG_CPU_HRTIMER_SIZE"
#endif
#ifndef TOS_CFG_CPU_LEAD_ZEROS_ASM_PRESENT
#error "UNDECLARED config, TOS_CFG_CPU_LEAD_ZEROS_ASM_PRESENT, should be declared in 'port_config.h'"
#endif
#ifndef TOS_CFG_CPU_BYTE_ORDER
#error "UNDECLARED config, TOS_CFG_CPU_BYTE_ORDER, should be declared in 'port_config.h'"
#endif
#ifndef TOS_CFG_CPU_STK_GROWTH
#error "UNDECLARED config, TOS_CFG_CPU_STK_GROWTH, should be declared in 'port_config.h'"
#elif ((TOS_CFG_CPU_STK_GROWTH != CPU_STK_GROWTH_ASCENDING) && \
(TOS_CFG_CPU_STK_GROWTH != CPU_STK_GROWTH_DESCENDING))
#error "INVALID config, TOS_CFG_CPU_STK_GROWTH"
#endif
#ifndef TOS_CFG_CPU_ADDR_SIZE
#error "UNDECLARED config, TOS_CFG_CPU_ADDR_SIZE, should be declared in 'port_config.h'"
#elif ((TOS_CFG_CPU_ADDR_SIZE != CPU_WORD_SIZE_08) && \
(TOS_CFG_CPU_ADDR_SIZE != CPU_WORD_SIZE_16) && \
(TOS_CFG_CPU_ADDR_SIZE != CPU_WORD_SIZE_32) && \
(TOS_CFG_CPU_ADDR_SIZE != CPU_WORD_SIZE_64))
#error "INVALID config, TOS_CFG_CPU_ADDR_SIZE"
#endif
#ifndef TOS_CFG_CPU_DATA_SIZE
#error "UNDECLARED config, TOS_CFG_CPU_DATA_SIZE, should be declared in 'port_config.h'"
#elif ((TOS_CFG_CPU_DATA_SIZE != CPU_WORD_SIZE_08) && \
(TOS_CFG_CPU_DATA_SIZE != CPU_WORD_SIZE_16) && \
(TOS_CFG_CPU_DATA_SIZE != CPU_WORD_SIZE_32) && \
(TOS_CFG_CPU_DATA_SIZE != CPU_WORD_SIZE_64))
#error "INVALID config, TOS_CFG_CPU_DATA_SIZE"
#endif
#endif /* TOS_CFG_EVENT_DRIVEN_EN */
#endif /* _TOS_CONFIG_CHECK_H_ */

View File

@@ -0,0 +1,264 @@
/*----------------------------------------------------------------------------
* 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_CONFIG_DEFAULT_H_
#define _TOS_CONFIG_DEFAULT_H_
#ifndef TOS_CFG_EVENT_DRIVEN_EN
#define TOS_CFG_EVENT_DRIVEN_EN 0u
#endif
#if TOS_CFG_EVENT_DRIVEN_EN > 0u
/////////////////////////////////////////
// disable round robin
#ifdef TOS_CFG_ROUND_ROBIN_EN
#undef TOS_CFG_ROUND_ROBIN_EN
#endif
#define TOS_CFG_ROUND_ROBIN_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable dynamic task create
#ifdef TOS_CFG_TASK_DYNAMIC_CREATE_EN
#undef TOS_CFG_TASK_DYNAMIC_CREATE_EN
#endif
#define TOS_CFG_TASK_DYNAMIC_CREATE_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable event
#ifdef TOS_CFG_EVENT_EN
#undef TOS_CFG_EVENT_EN
#endif
#define TOS_CFG_EVENT_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable mutex
#ifdef TOS_CFG_MUTEX_EN
#undef TOS_CFG_MUTEX_EN
#endif
#define TOS_CFG_MUTEX_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable semaphore
#ifdef TOS_CFG_SEM_EN
#undef TOS_CFG_SEM_EN
#endif
#define TOS_CFG_SEM_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable the "traditional" timer
#ifdef TOS_CFG_TIMER_EN
#undef TOS_CFG_TIMER_EN
#endif
#define TOS_CFG_TIMER_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// disable stack draught depth detact
#ifdef TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN
#undef TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN
#endif
#define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN 0u
/////////////////////////////////////////
/////////////////////////////////////////
// enable mmheap
#ifndef TOS_CFG_MMHEAP_EN
#define TOS_CFG_MMHEAP_EN 0u
#endif
#ifndef TOS_CFG_MMHEAP_DEFAULT_POOL_EN
#define TOS_CFG_MMHEAP_DEFAULT_POOL_EN 1u
#endif
/////////////////////////////////////////
/////////////////////////////////////////
// disable default
#ifndef TOS_CFG_FAULT_BACKTRACE_EN
#define TOS_CFG_FAULT_BACKTRACE_EN 0u
#endif
/////////////////////////////////////////
/////////////////////////////////////////
#ifndef TOS_CFG_CPU_SYSTICK_PRIO
#define TOS_CFG_CPU_SYSTICK_PRIO 0u
#endif
/////////////////////////////////////////
/////////////////////////////////////////
// disable default
#ifndef TOS_CFG_PWR_MGR_EN
#define TOS_CFG_PWR_MGR_EN 0u
#endif
#ifndef TOS_CFG_TICKLESS_EN
#define TOS_CFG_TICKLESS_EN 0u
#endif
/////////////////////////////////////////
// we donot really need these, it's a compromise to the compiler.
#ifndef TOS_CFG_TASK_PRIO_MAX
#define TOS_CFG_TASK_PRIO_MAX 8u
#endif
#ifndef TOS_CFG_IDLE_TASK_STK_SIZE
#define TOS_CFG_IDLE_TASK_STK_SIZE 128u
#endif
/////////////////////////////////////////
/////////////////////////////////////////
#else /* TOS_CFG_EVENT_DRIVEN_EN */
#ifndef TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN
#define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN 0u
#endif
#ifndef TOS_CFG_TASK_DYNAMIC_CREATE_EN
#define TOS_CFG_TASK_DYNAMIC_CREATE_EN 0u
#endif
#ifndef TOS_CFG_ROUND_ROBIN_EN
#define TOS_CFG_ROUND_ROBIN_EN 0u
#endif
#ifndef TOS_CFG_EVENT_EN
#define TOS_CFG_EVENT_EN 0u
#endif
#ifndef TOS_CFG_MUTEX_EN
#define TOS_CFG_MUTEX_EN 0u
#endif
#ifndef TOS_CFG_MESSAGE_QUEUE_EN
#define TOS_CFG_MESSAGE_QUEUE_EN 0u
#endif
#ifndef TOS_CFG_MAIL_QUEUE_EN
#define TOS_CFG_MAIL_QUEUE_EN 0u
#endif
#ifndef TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN
#define TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN 0u
#endif
#ifndef TOS_CFG_PRIORITY_MAIL_QUEUE_EN
#define TOS_CFG_PRIORITY_MAIL_QUEUE_EN 0u
#endif
#ifndef TOS_CFG_SEM_EN
#define TOS_CFG_SEM_EN 0u
#endif
#ifndef TOS_CFG_TIMER_EN
#define TOS_CFG_TIMER_EN 0u
#endif
#if (TOS_CFG_TIMER_EN > 0u) && !defined(TOS_CFG_TIMER_AS_PROC)
#define TOS_CFG_TIMER_AS_PROC 0u
#endif
#ifndef TOS_CFG_IDLE_TASK_STK_SIZE
#define TOS_CFG_IDLE_TASK_STK_SIZE 128u
#endif
#ifndef TOS_CFG_OBJECT_VERIFY_EN
#define TOS_CFG_OBJECT_VERIFY_EN 0u
#endif
#ifndef TOS_CFG_LIBC_PRINTF_EN
#define TOS_CFG_LIBC_PRINTF_EN 1u // we enable this by default
#endif
#if (TOS_CFG_TIMER_AS_PROC == 0u) && !defined(TOS_CFG_TIMER_TASK_PRIO)
#define TOS_CFG_TIMER_TASK_PRIO (k_prio_t)(K_TASK_PRIO_IDLE - (k_prio_t)1u)
#endif
#if (TOS_CFG_TIMER_AS_PROC == 0u) && !defined(TOS_CFG_TIMER_TASK_STK_SIZE)
#define TOS_CFG_TIMER_TASK_STK_SIZE 128u
#endif
#ifndef TOS_CFG_CPU_SYSTICK_PRIO
#define TOS_CFG_CPU_SYSTICK_PRIO 0u
#endif
#ifndef TOS_CFG_CPU_TICK_PER_SECOND
#define TOS_CFG_CPU_TICK_PER_SECOND 1000u
#endif
#ifndef TOS_CFG_CPU_CLOCK
#define TOS_CFG_CPU_CLOCK 16000000u
#endif
#ifndef TOS_CFG_TASK_PRIO_MAX
#define TOS_CFG_TASK_PRIO_MAX 8u
#endif
#ifndef TOS_CFG_MMHEAP_EN
#define TOS_CFG_MMHEAP_EN 0u
#endif
#ifndef TOS_CFG_MMHEAP_DEFAULT_POOL_EN
#define TOS_CFG_MMHEAP_DEFAULT_POOL_EN 1u
#endif
#ifndef TOS_CFG_PWR_MGR_EN
#define TOS_CFG_PWR_MGR_EN 0u
#endif
#ifndef TOS_CFG_TICKLESS_EN
#define TOS_CFG_TICKLESS_EN 0u
#endif
#if (TOS_CFG_PWR_MGR_EN > 0u) || (TOS_CFG_TICKLESS_EN > 0u)
#if TOS_CFG_IDLE_TASK_STK_SIZE < 256
#undef TOS_CFG_IDLE_TASK_STK_SIZE
#define TOS_CFG_IDLE_TASK_STK_SIZE 256u
#endif
#endif
#if (TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u)
#if TOS_CFG_IDLE_TASK_STK_SIZE < 512
#undef TOS_CFG_IDLE_TASK_STK_SIZE
#define TOS_CFG_IDLE_TASK_STK_SIZE 512u
#endif
#endif
#ifndef TOS_CFG_FAULT_BACKTRACE_EN
#define TOS_CFG_FAULT_BACKTRACE_EN 0u
#endif
#endif /* TOS_CFG_EVENT_DRIVEN_EN */
#endif /* _TOS_CONFIG_DEFAULT_H_ */

View File

@@ -0,0 +1,122 @@
/*----------------------------------------------------------------------------
* 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_COUNTDOWNLATCH_H_
#define _TOS_COUNTDOWNLATCH_H_
__CDECLS_BEGIN
#if TOS_CFG_COUNTDOWNLATCH_EN > 0
typedef struct k_countdownlatch_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_countdownlatch_cnt_t count;
} k_countdownlatch_t;
/**
* @brief Create a countdown-latch.
* create a countdown latch.
*
* @attention the count is how many posts have been done the pender would wakeup.
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
* @param[in] count the count to wait of the countdown-latch.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_create(k_countdownlatch_t *countdownlatch, k_countdownlatch_cnt_t count);
/**
* @brief Destroy a countdown-latch.
* destroy a countdown-latch.
*
* @attention None
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_destroy(k_countdownlatch_t *countdownlatch);
/**
* @brief Pend a countdown-latch.
* pend a countdown-latch.
*
* @attention The task will keep blocked until the countdown-latch is obtained or a timeout comes.
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_pend_timed(k_countdownlatch_t *countdownlatch, k_tick_t timeout);
/**
* @brief Pend a countdown-latch.
* pend a countdown latch.
*
* @attention until (countdownlatch->count) of tasks have done the post, the pender would wake up.
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
*
* @return errcode
* @retval #K_ERR_PEND_SCHED_LOCKED the schedule is locked.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_pend(k_countdownlatch_t *countdownlatch);
/**
* @brief Post a countdown-latch.
* post a countdown-latch.
*
* @attention until (countdownlatch->count) of tasks have done the post, the pender would wake up.
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
*
* @return errcode
* @retval #K_ERR_COUNTDOWNLATCH_OVERFLOW we are posting the countdown-latch too much.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_post(k_countdownlatch_t *countdownlatch);
/**
* @brief Reset a countdown-latch.
* reset a countdown-latch's count.
*
* @attention None.
*
* @param[in] countdownlatch pointer to the handler of the countdown-latch.
* @param[in] count the count to wait of the countdown-latch.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_countdownlatch_reset(k_countdownlatch_t *countdownlatch, k_countdownlatch_cnt_t count);
#endif /* TOS_CFG_COUNTDOWNLATCH_EN */
__CDECLS_END
#endif /* _TOS_COUNTDOWNLATCH_H_ */

View File

@@ -0,0 +1,170 @@
/*----------------------------------------------------------------------------
* 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_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
#define TOS_OPT_EVENT_PEND_ANY (k_opt_t)0x0001
// if we are pending an event, must all the flag we expect is set is ok, this flag should be passed to tos_event_pend
#define TOS_OPT_EVENT_PEND_ALL (k_opt_t)0x0002
// if we are pending an event, and we wanna clear the event's flag after we read, this flag should be passed to tos_event_pend
/* ATTENTION:
we can pass both TOS_OPT_EVENT_PEND_CLR and TOS_OPT_EVENT_PEND_ANY, or TOS_OPT_EVENT_PEND_CLR and TOS_OPT_EVENT_PEND_ALL
to tos_event_pend, if we wanna do this, a (TOS_OPT_EVENT_PEND_CLR | TOS_OPT_EVENT_PEND_ANY) or
(TOS_OPT_EVENT_PEND_CLR | TOS_OPT_EVENT_PEND_ALL) should be passed.
but, (TOS_OPT_EVENT_PEND_ANY | TOS_OPT_EVENT_PEND_ALL) is invalid, we cannot both wanna any and all flag is set.
*/
#define TOS_OPT_EVENT_PEND_CLR (k_opt_t)0x0004
typedef enum opt_event_post_en {
OPT_EVENT_POST_KEP,
OPT_EVENT_POST_CLR,
} opt_event_post_t;
typedef struct k_event_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_event_flag_t flag;
} k_event_t;
/**
* @brief Create an event.
* create an event.
*
* @attention None
*
* @param[in] event pointer to the handler of the event.
* @param[in] init_flag initial flag of the event.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_create(k_event_t *event, k_event_flag_t init_flag);
/**
* @brief Destroy an event.
* destroy an event.
*
* @attention None
*
* @param[in] event pointer to the handler of the event.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_destroy(k_event_t *event);
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/**
* @brief Create an dynamic event.
* create an event.
*
* @attention None
*
* @param[in] event pointer to the handler of the event pointer.
* @param[in] init_flag initial flag of the event.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_create_dyn(k_event_t **event, k_event_flag_t init_flag);
/**
* @brief Destroy an dynamic event.
* destroy an event.
*
* @attention None
*
* @param[in] event pointer to the handler of the event.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_destroy_dyn(k_event_t *event);
#endif
/**
* @brief Pend an event.
* pend an event.
*
* @attention if opt is TOS_OPT_EVENT_PEND_ANY, any of the flag_expect is set is ok;
* if opt is TOS_OPT_EVENT_PEND_ALL<4C><4C> must all the flag_expect is set is ok.
*
* @param[in] event pointer to the handler of the event.
* @param[in] flag_expect the flag we expect from the event.
* @param[OUT] flag_match if we get the flag we expect, what exactly they are?
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
* @param[in] opt option for pend.
*
* @return errcode
* @retval #K_ERR_EVENT_PEND_OPT_INVALID opt is invalid
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the event we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_pend(k_event_t *event, k_event_flag_t flag_expect, k_event_flag_t *flag_match, k_tick_t timeout, k_opt_t opt);
/**
* @brief Post an event.
* post an event.
*
* @attention if you are posting an event in tos_event_post, event's own flag will be overwrited by the flag we post.
* eg. if an event's own flag is 0x0001, and we are posting a flag 0x0030, after the post, the event's flag
* will be overwrited to 0x0030.
*
* @param[in] event pointer to the handler of the event.
* @param[in] flag the flag we post.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_event_post(k_event_t *event, k_event_flag_t flag);
/**
* @brief Post an event.
* post an event, and keep the original own flag of the event.
*
* @attention the original own flag of the event will be keeped.
* eg.if an event's own flag is 0x0001, and we are posting a flag 0x0030, after the post, the event's flag
* will be changed to 0x0031(0x0030 | 0x0001), which means the event's original flag is keeped.
*
* @param[in] event pointer to the handler of the event.
* @param[in] flag the flag we post.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__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

@@ -0,0 +1,106 @@
/*----------------------------------------------------------------------------
* 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_GLOBAL_H_
#define _TOS_GLOBAL_H_
/* interrupt nesting count */
extern k_nesting_t k_irq_nest_cnt;
/* schedule lock nesting count */
extern k_nesting_t k_sched_lock_nest_cnt;
/* kernel running state */
extern knl_state_t k_knl_state;
/* ready queue of tasks */
extern readyqueue_t k_rdyq;
/* ticks since boot up */
extern k_tick_t k_tick_count;
/* current task */
extern k_task_t *k_curr_task;
/* next task to run */
extern k_task_t *k_next_task;
/* idle task related stuff */
extern k_task_t k_idle_task;
extern k_stack_t k_idle_task_stk[];
extern k_stack_t *const k_idle_task_stk_addr;
extern size_t const k_idle_task_stk_size;
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/* list to hold all the destroyed dynamic created tasks */
extern k_list_t k_dead_task_list;
#endif
/* list to hold all the tasks for statistics */
extern k_list_t k_stat_list;
/* list to hold all the tasks delayed or pend for timeout */
extern k_list_t k_tick_list;
/* how many ticks will be triggered in a second */
extern k_tick_t k_cpu_tick_per_second;
/* how many cycle per tick */
extern k_cycle_t k_cpu_cycle_per_tick;
#if TOS_CFG_FAULT_BACKTRACE_EN > 0u
extern k_fault_log_writer_t k_fault_log_writer;
#endif
#if TOS_CFG_MMHEAP_EN > 0u
#if TOS_CFG_MMHEAP_DEFAULT_POOL_EN > 0u
extern uint8_t k_mmheap_default_pool[] __ALIGNED__(4);
#endif
extern k_mmheap_ctl_t k_mmheap_ctl;
#endif
#if TOS_CFG_ROUND_ROBIN_EN > 0u
extern k_timeslice_t k_robin_default_timeslice;
#endif
#if TOS_CFG_TIMER_EN > 0u
/* list holding all the timer */
extern timer_ctl_t k_timer_ctl;
#if TOS_CFG_TIMER_AS_PROC == 0u
extern k_task_t k_timer_task;
extern k_stack_t k_timer_task_stk[];
extern k_prio_t const k_timer_task_prio;
extern k_stack_t *const k_timer_task_stk_addr;
extern size_t const k_timer_task_stk_size;
#endif
#endif
#if TOS_CFG_PWR_MGR_EN > 0u
extern pm_device_ctl_t k_pm_device_ctl;
extern idle_pwrmgr_mode_t k_idle_pwr_mgr_mode;
extern k_cpu_lpwr_mode_t k_cpu_lpwr_mode;
#endif
#if TOS_CFG_TICKLESS_EN > 0u
extern k_tickless_wkup_alarm_t *k_tickless_wkup_alarm[__LOW_POWER_MODE_DUMMY];
#endif
#endif /* _TOS_GLOBAL_H_ */

View File

@@ -0,0 +1,73 @@
/*----------------------------------------------------------------------------
* 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_K_H_
#define _TOS_K_H_
#include <tos_compiler.h>
#include <tos_kerr.h>
#include <tos_cpu_def.h>
#include <tos_config.h>
#include <tos_config_default.h>
#include <port_config.h>
#include <tos_config_check.h>
#include <tos_ktypes.h>
#include <tos_cpu_types.h>
#include <port.h>
#include <tos_cpu.h>
#include <tos_fault.h>
#include <tos_klib.h>
#include <tos_list.h>
#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>
#include <tos_message_queue.h>
#include <tos_binary_heap.h>
#include <tos_priority_queue.h>
#include <tos_priority_mail_queue.h>
#include <tos_priority_message_queue.h>
#include <tos_task.h>
#include <tos_robin.h>
#include <tos_mutex.h>
#include <tos_sem.h>
#include <tos_event.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>
#include <tos_sched.h>
#if TOS_CFG_PWR_MGR_EN > 0u
#include <tos_pm.h>
#if TOS_CFG_TICKLESS_EN > 0u
#include <tos_tickless.h>
#endif
#endif
#include <tos_global.h>
#include <tos_version.h>
#endif /* _TOS_K_H_ */

View File

@@ -0,0 +1,131 @@
/*----------------------------------------------------------------------------
* 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_ERR_H_
#define _TOS_ERR_H_
typedef enum k_err_en {
K_ERR_NONE = 0u,
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,
K_ERR_COMPLETION_OVERFLOW = 25u,
K_ERR_COUNTDOWNLATCH_OVERFLOW = 50u,
K_ERR_DELAY_ZERO = 100u,
K_ERR_DELAY_FOREVER,
K_ERR_EVENT_PEND_OPT_INVALID = 200u,
K_ERR_IN_IRQ = 400u,
K_ERR_KNL_NOT_RUNNING = 500u,
K_ERR_KNL_RUNNING,
K_ERR_LOCK_NESTING_OVERFLOW = 600u,
K_ERR_MMBLK_POOL_FULL = 700u,
K_ERR_MMBLK_POOL_EMPTY,
K_ERR_MMBLK_INVALID_BLK_SIZE,
K_ERR_MMBLK_INVALID_POOL_ADDR,
K_ERR_MMBLK_POOL_OUT_OF_MEMORY,
K_ERR_MMBLK_OUT_OF_MEMORY,
K_ERR_MMHEAP_INVALID_POOL_ADDR = 800u,
K_ERR_MMHEAP_INVALID_POOL_SIZE,
K_ERR_MMHEAP_POOL_OVERFLOW,
K_ERR_MMHEAP_POOL_ALREADY_EXIST,
K_ERR_MMHEAP_POOL_NOT_EXIST,
K_ERR_MUTEX_NOT_OWNER = 1000u,
K_ERR_MUTEX_NESTING,
K_ERR_MUTEX_NESTING_OVERFLOW,
K_ERR_OBJ_PTR_NULL = 1100u,
K_ERR_OBJ_INVALID,
K_ERR_OBJ_INVALID_ALLOC_TYPE,
K_ERR_OUT_OF_MEMORY = 1150u,
K_ERR_PEND_NOWAIT = 1200u,
K_ERR_PEND_SCHED_LOCKED,
K_ERR_PEND_ABNORMAL,
K_ERR_PEND_TIMEOUT,
K_ERR_PEND_DESTROY,
K_ERR_PEND_OWNER_DIE,
K_ERR_PM_DEVICE_ALREADY_REG = 1300u,
K_ERR_PM_DEVICE_OVERFLOW,
K_ERR_PM_WKUP_SOURCE_NOT_INSTALL,
K_ERR_PRIO_Q_EMPTY = 1400u,
K_ERR_PRIO_Q_FULL,
K_ERR_PRIO_Q_SLOT_NOT_TAKEN,
K_ERR_PRIO_Q_ITEM_SIZE_NOT_MATCH,
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_ALREADY_CREATED = 1900u,
K_ERR_TASK_DESTROY_IDLE,
K_ERR_TASK_NOT_DELAY,
K_ERR_TASK_PRIO_INVALID,
K_ERR_TASK_RESUME_SELF,
K_ERR_TASK_SUSPENDED,
K_ERR_TASK_SUSPEND_IDLE,
K_ERR_TASK_STK_OVERFLOW,
K_ERR_TASK_STK_SIZE_INVALID,
K_ERR_TICKLESS_WKUP_ALARM_NOT_INSTALLED = 2000u,
K_ERR_TICKLESS_WKUP_ALARM_NO_INIT,
K_ERR_TICKLESS_WKUP_ALARM_INIT_FAILED,
K_ERR_TIMER_INACTIVE = 2100u,
K_ERR_TIMER_DELAY_FOREVER,
K_ERR_TIMER_PERIOD_FOREVER,
K_ERR_TIMER_INVALID_DELAY,
K_ERR_TIMER_INVALID_PERIOD,
K_ERR_TIMER_INVALID_STATE,
K_ERR_TIMER_INVALID_OPT,
K_ERR_TIMER_STOPPED,
K_ERR_TIMER_RUNNING,
} k_err_t;
#endif /* _TOS_ERR_H_ */

View File

@@ -0,0 +1,128 @@
/*----------------------------------------------------------------------------
* 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_KLIB_H_
#define _TOS_KLIB_H_
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __MACRO_BEGIN do {
#define __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_COUNT_OF(array) (sizeof(array) / sizeof(array[0]))
#define TOS_PTR_SANITY_CHECK(ptr) \
__MACRO_BEGIN \
if (unlikely(!(ptr))) { \
return K_ERR_OBJ_PTR_NULL; \
} \
__MACRO_END
#define TOS_PTR_SANITY_CHECK_RC(ptr, return_code) \
__MACRO_BEGIN \
if (unlikely(!(ptr))) { \
return return_code; \
} \
__MACRO_END
#define TOS_IN_IRQ_CHECK() \
__MACRO_BEGIN \
if (unlikely(knl_is_inirq())) { \
return K_ERR_IN_IRQ; \
} \
__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) \
__MACRO_BEGIN \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return K_ERR_OBJ_INVALID; \
} \
__MACRO_END
#define TOS_OBJ_VERIFY_RC(obj, obj_type, return_code) \
__MACRO_BEGIN \
if (!knl_object_verify(&obj->knl_obj, obj_type)) { \
return return_code; \
} \
__MACRO_END
#define TOS_OBJ_TEST(obj, obj_type) \
__MACRO_BEGIN \
if (knl_object_verify(&obj->knl_obj, obj_type)) { \
return K_ERR_OBJ_INVALID; \
} \
__MACRO_END
#define TOS_OBJ_TEST_RC(obj, obj_type, return_code) \
__MACRO_BEGIN \
if (knl_object_verify(&obj->knl_obj, obj_type)) { \
return return_code; \
} \
__MACRO_END
#else
#define TOS_OBJ_INIT(obj, obj_type)
#define TOS_OBJ_DEINIT(obj)
#define TOS_OBJ_VERIFY(obj, obj_type)
#define TOS_OBJ_VERIFY_RC(obj, obj_type, return_code)
#define TOS_OBJ_TEST(obj, obj_type)
#define TOS_OBJ_TEST_RC(obj, obj_type, return_code)
#endif
#if TOS_CFG_LIBC_PRINTF_EN > 0u
#define LIBC_PRINTF printf
#else
#define LIBC_PRINTF(...)
#endif
// currently we use default microlib supplied by mdk
#define tos_kprintf(...) LIBC_PRINTF(__VA_ARGS__);
#define tos_kprintln(...) \
LIBC_PRINTF(__VA_ARGS__); \
LIBC_PRINTF("\n");
#define TOS_ASSERT_AUX(exp, function, line) \
if (!(exp)) { \
tos_kprintln("assert failed: %s %d\n", function, line); \
tos_knl_sched_lock(); \
tos_cpu_int_disable(); \
while (K_TRUE) { \
; \
} \
}
#define TOS_ASSERT(exp) TOS_ASSERT_AUX(exp, __FUNCTION__, __LINE__)
#endif /* _TOS_KLIB_H_ */

View File

@@ -0,0 +1,57 @@
/*----------------------------------------------------------------------------
* 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_KTYPES_H_
#define _TOS_KTYPES_H_
#include <stdint.h>
#include <stddef.h>
typedef uint8_t k_prio_t;
typedef uint8_t k_stack_t;
typedef uint8_t k_task_state_t;
typedef struct k_task_st k_task_t;
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;
typedef uint32_t k_timeslice_t;
typedef uint32_t k_cycle_t;
#if TOS_CFG_CPU_DATA_SIZE == CPU_WORD_SIZE_08
typedef uint32_t k_tick_t;
#else
typedef uint64_t k_tick_t;
#endif
#define K_TRUE (1u)
#define K_FALSE (0u)
#ifndef K_NULL
#define K_NULL 0
#endif
#endif /* _TOS_KTYPES_H_ */

View File

@@ -0,0 +1,146 @@
/*----------------------------------------------------------------------------
* 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_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;
} k_list_t;
#define TOS_LIST_NODE(node) \
{ &(node), &(node) }
#define TOS_LIST_DEFINE(list) \
k_list_t list = { &(list), &(list) }
#define TOS_LIST_ENTRY(node, type, field) \
TOS_CONTAINER_OF_FIELD(node, type, field)
#define TOS_LIST_FIRST_ENTRY(list, type, field) \
TOS_LIST_ENTRY((list)->next, type, field)
#define TOS_LIST_FIRST_ENTRY_OR_NULL(list, type, field) \
(tos_list_empty(list) ? K_NULL : TOS_LIST_FIRST_ENTRY(list, type, field))
#define TOS_LIST_FOR_EACH(curr, list) \
for (curr = (list)->next; curr != (list); curr = curr->next)
#define TOS_LIST_FOR_EACH_PREV(curr, list) \
for (curr = (list)->prev; curr != (list); curr = curr->prev)
#define TOS_LIST_FOR_EACH_SAFE(curr, next, list) \
for (curr = (list)->next, next = curr->next; curr != (list); \
curr = next, next = curr->next)
#define TOS_LIST_FOR_EACH_PREV_SAFE(curr, next, list) \
for (curr = (list)->prev, next = curr->prev; \
curr != (list); \
curr = next, next = curr->prev)
#define TOS_LIST_FOR_EACH_ENTRY(entry, type, field, list) \
for (entry = TOS_LIST_ENTRY((list)->next, type, field); \
&entry->field != (list); \
entry = TOS_LIST_ENTRY(entry->field.next, type, field))
#define TOS_LIST_FOR_EACH_ENTRY_REVERSE(entry, type, field, list) \
for (entry = TOS_LIST_ENTRY((list)->prev, type, field); \
&entry->field != (list); \
entry = TOS_LIST_ENTRY(entry->field.prev, type, field))
#define TOS_LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, type, field, list) \
for (entry = TOS_LIST_ENTRY((list)->next, type, field), \
tmp = TOS_LIST_ENTRY(entry->field.next, type, field); \
&entry->field != (list); \
entry = tmp, tmp = TOS_LIST_ENTRY(entry->field.next, type, field))
#define TOS_LIST_FOR_EACH_ENTRY_SAFE_REVERSE(entry, tmp, type, field, list) \
for (entry = TOS_LIST_ENTRY((list)->prev, type, field), \
tmp = TOS_LIST_ENTRY(entry->field.prev, type, field); \
&entry->field != (list); \
entry = tmp, tmp = TOS_LIST_ENTRY(entry->field.prev, type, field))
__STATIC_INLINE__ void _list_add(k_list_t *node, k_list_t *prev, k_list_t *next)
{
next->prev = node;
node->next = next;
node->prev = prev;
prev->next = node;
}
__STATIC_INLINE__ void _list_del(k_list_t *prev, k_list_t *next)
{
next->prev = prev;
prev->next = next;
}
__STATIC_INLINE__ void _list_del_node(k_list_t *node)
{
_list_del(node->prev, node->next);
}
__API__ __STATIC_INLINE__ void tos_list_init(k_list_t *list)
{
list->next = list;
list->prev = list;
}
__API__ __STATIC_INLINE__ void tos_list_add(k_list_t *node, k_list_t *list)
{
_list_add(node, list, list->next);
}
__API__ __STATIC_INLINE__ void tos_list_add_tail(k_list_t *node, k_list_t *list)
{
_list_add(node, list->prev, list);
}
__API__ __STATIC_INLINE__ void tos_list_del(k_list_t *node)
{
_list_del(node->prev, node->next);
}
__API__ __STATIC_INLINE__ void tos_list_del_init(k_list_t *node)
{
_list_del_node(node);
tos_list_init(node);
}
__API__ __STATIC_INLINE__ void tos_list_move(k_list_t *node, k_list_t *list)
{
_list_del_node(node);
tos_list_add(node, list);
}
__API__ __STATIC_INLINE__ void tos_list_move_tail(k_list_t *node, k_list_t *list)
{
_list_del_node(node);
tos_list_add_tail(node, list);
}
__API__ __STATIC_INLINE__ int tos_list_empty(const k_list_t *list)
{
return list->next == list;
}
__CDECLS_END
#endif /* _TOS_LIST_H_ */

View File

@@ -0,0 +1,159 @@
/*----------------------------------------------------------------------------
* 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_MAIL_QUEUE_H_
#define _TOS_MAIL_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_MAIL_QUEUE_EN > 0u
typedef struct k_mail_queue_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_ring_q_t ring_q;
} k_mail_q_t;
/**
* @brief Create a mail queue.
* create a mail queue.
*
* @attention a MAIL is a buffer with a certain size.
*
* @param[in] mail_q pointer to the handler of the mail queue.
* @param[in] pool pool buffer of the mail queue.
* @param[in] mail_cnt mail count of the mail queue.
* @param[in] mail_size size of each mail in the mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_create(k_mail_q_t *mail_q, void *pool, size_t mail_cnt, size_t mail_size);
/**
* @brief Destroy a mail queue.
* destroy a mail queue.
*
* @attention None
*
* @param[in] mail_q pointer to the handler of the mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_destroy(k_mail_q_t *mail_q);
/**
* @brief Create a mail queue with dynamic allocated pool.
* create a mail queue with dynamic allocated pool.
*
* @attention a MAIL is a buffer with a certain size.
*
* @param[in] mail_q pointer to the handler of the mail queue.
* @param[in] mail_cnt mail count of the mail queue.
* @param[in] mail_size size of each mail in the mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_create_dyn(k_mail_q_t *mail_q, size_t mail_cnt, size_t mail_size);
/**
* @brief Destroy a mail queue with dynamic allocated pool.
* destroy a mail queue with dynamic allocated pool.
*
* @attention None
*
* @param[in] mail_q pointer to the handler of the mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_destroy_dyn(k_mail_q_t *mail_q);
/**
* @brief Flush the mail queue.
* flush the mail queue.
*
* @attention None
*
* @param[in] mail_q pointer to the handler of the mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_flush(k_mail_q_t *mail_q);
/**
* @brief Pend a mail queue.
* pend a mail queue.
*
* @attention we WILL perform a memcpy when mail_buf is received(a MAIL is a buffer with a certain size).
*
* @param[in] mail_q pointer to the handler of the mail queue.
* @param[OUT] mail_buf a pointer to the mail buffer we wanna receive.
* @param[OUT] mail_size size of the mail buffer received(should be consistent with the mail_size passed to tos_mail_q_create).
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the queue we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_pend(k_mail_q_t *mail_q, void *mail_buf, size_t *mail_size, k_tick_t timeout);
/**
* @brief Post a mail queue.
* post a mail queue and wakeup one pending task.
*
* @attention when tos_mail_q_post return successfully, only one task who are waitting for the mail queue will be woken up.
*
* @param[in] mail_q pointer to the handler of the mail queue.
* @param[in] mail_buf the mail to post(a MAIL is a buffer).
* @param[in] mail_size the size of the mail to post(a MAIL is just a buffer).
*
* @return errcode
* @retval #K_ERR_RING_Q_FULL the mail queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_post(k_mail_q_t *mail_q, void *mail_buf, size_t mail_size);
/**
* @brief Post a mail queue.
* post a mail queue and wakeup all the pending task.
*
* @attention when tos_mail_q_post_all return successfully, all of the tasks who are waitting for the message queue will be woken up.
*
* @param[in] mail_q pointer to the handler of the mail queue.
* @param[in] mail_buf the mail to post(a MAIL is a buffer).
* @param[in] mail_size the size of the mail to post(a MAIL is just a buffer).
*
* @return errcode
* @retval #K_ERR_RING_Q_FULL the mail queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mail_q_post_all(k_mail_q_t *mail_q, void *mail_buf, size_t mail_size);
#endif
__CDECLS_END
#endif /* _TOS_MAIL_QUEUE_H_ */

View File

@@ -0,0 +1,154 @@
/*----------------------------------------------------------------------------
* 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_MESSAGE_QUEUE_H_
#define _TOS_MESSAGE_QUEUE_H_
__CDECLS_BEGIN
#if TOS_CFG_MESSAGE_QUEUE_EN > 0u
typedef struct k_message_queue_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_ring_q_t ring_q;
} k_msg_q_t;
/**
* @brief Create a message queue.
* create a message queue.
*
* @attention a MESSAGE is a "void *" pointer.
*
* @param[in] msg_q pointer to the handler of the message queue.
* @param[in] pool pool buffer of the message queue.
* @param[in] msg_cnt message count of the message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_create(k_msg_q_t *msg_q, void *pool, size_t msg_cnt);
/**
* @brief Destroy a message queue.
* destroy a message queue.
*
* @attention None
*
* @param[in] msg_q pointer to the handler of the message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_destroy(k_msg_q_t *msg_q);
/**
* @brief Create a message queue with dynamic allocated pool.
* create a message queue with dynamic allocated pool.
*
* @attention a MESSAGE is a "void *" pointer.
*
* @param[in] msg_q pointer to the handler of the message queue.
* @param[in] msg_cnt message count of the message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_create_dyn(k_msg_q_t *msg_q, size_t msg_cnt);
/**
* @brief Destroy a message queue with dynamic allocated pool.
* destroy a message queue with dynamic allocated pool.
*
* @attention None
*
* @param[in] msg_q pointer to the handler of the message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_destroy_dyn(k_msg_q_t *msg_q);
/**
* @brief Flush the message queue.
* flush the message queue.
*
* @attention None
*
* @param[in] msg_q pointer to the handler of the message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_flush(k_msg_q_t *msg_q);
/**
* @brief Pend a message queue.
* pend a message queue.
*
* @attention we DONNOT perform a memcpy when msg_ptr is received(a MESSAGE is just a pointer).
*
* @param[in] msg_q pointer to the handler of the message queue.
* @param[OUT] msg_ptr a pointer to the message we wanna receive.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the queue we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_pend(k_msg_q_t *msg_q, void **msg_ptr, k_tick_t timeout);
/**
* @brief Post a message queue.
* post a message queue and wakeup one pending task.
*
* @attention when tos_msg_q_post return successfully, only one task who are waitting for the message queue will be woken up.
*
* @param[in] msg_q pointer to the handler of the message queue.
* @param[in] msg_ptr the message to post(a MESSAGE is just a pointer).
*
* @return errcode
* @retval #K_ERR_RING_Q_FULL the message queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_msg_q_post(k_msg_q_t *msg_q, void *msg_ptr);
/**
* @brief Post a message queue.
* post a message queue and wakeup all the pending task.
*
* @attention when tos_msg_q_post_all return successfully, all of the tasks who are waitting for the message queue will be woken up.
*
* @param[in] msg_q pointer to the handler of the message queue.
* @param[in] msg_ptr the message to post(a MESSAGE is just a pointer).
*
* @return errcode
* @retval #K_ERR_RING_Q_FULL the message queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__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

@@ -0,0 +1,132 @@
/*----------------------------------------------------------------------------
* 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_MMBLK_H_
#define _TOS_MMBLK_H_
__CDECLS_BEGIN
#define K_MMBLK_NEXT_BLK(blk_curr, blk_size) ((void *)((cpu_addr_t)blk_curr + blk_size))
#define K_MMBLK_ALIGN_MASK (sizeof(void *) - 1u)
typedef struct k_mmblk_pool_st {
knl_obj_t knl_obj;
void *pool_start;
void *free_list;
size_t blk_size;
size_t blk_max;
size_t blk_free;
} k_mmblk_pool_t;
/**
* @brief Create a memory manage block pool.
* Create a memory manage block pool.
*
* @attention None
*
* @param[in] mbp pointer to the memory block pool handler.
* @param[in] pool_start start address of the pool.
* @param[in] blk_num number of the blocks in the pool.
* @param[in] blk_size size of each block in the pool.
*
* @return errcode
* @retval #K_ERR_MMBLK_INVALID_POOL_ADDR start address of the pool is invalid.
* @retval #K_ERR_MMBLK_INVALID_BLK_SIZE size of the block is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_pool_create(k_mmblk_pool_t *mbp, void *pool_start, size_t blk_num, size_t blk_size);
/**
* @brief Destroy a memory manage block pool.
* Destroy a memory manage block pool.
*
* @attention None
*
* @param[in] mbp pointer to the memory block pool handler.
*
* @return errcode
* @retval #K_ERR_OBJ_INVALID mbp is not a valid memory block pool handler.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_pool_destroy(k_mmblk_pool_t *mbp);
/**
* @brief Create a dynamic memory manage block pool.
* Create a dynamic memory manage block pool.
*
* @attention None
*
* @param[in] mbp pointer to the pointer of memory block pool handler.
* @param[in] blk_num number of the blocks in the pool.
* @param[in] blk_size size of each block in the pool.
*
* @return errcode
* @retval #K_ERR_MMBLK_INVALID_POOL_ADDR start address of the pool is invalid.
* @retval #K_ERR_MMBLK_INVALID_BLK_SIZE size of the block is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_pool_create_dyn(k_mmblk_pool_t **mbp, size_t blk_num, size_t blk_size);
/**
* @brief Destroy a dynamic memory manage block pool.
* Destroy a memory dynamic manage block pool.
*
* @attention None
*
* @param[in] mbp pointer to the memory block pool handler.
*
* @return errcode
* @retval #K_ERR_OBJ_INVALID mbp is not a valid memory block pool handler.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_pool_destroy_dyn(k_mmblk_pool_t *mbp);
/**
* @brief Allocate a memory manage block.
* Allocate a memory manage block.
*
* @attention None
*
* @param[in] mbp pointer to the memory block pool handler.
* @param[in] blk start address of the memory manage block.
*
* @return errcode
* @retval #K_ERR_MMBLK_POOL_EMPTY the pool is empty.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_alloc(k_mmblk_pool_t *mbp, void **blk);
/**
* @brief Free a memory manage block.
* Free a memory manage block.
*
* @attention None
*
* @param[in] mbp pointer to the memory block pool handler.
* @param[in] blk start address of the memory manage block.
*
* @return errcode
* @retval #K_ERR_MMBLK_POOL_FULL the pool is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmblk_free(k_mmblk_pool_t *mbp, void *blk);
#endif
__CDECLS_END

View File

@@ -0,0 +1,277 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
/*
** Two Level Segregated Fit memory allocator, version 3.1.
** Written by Matthew Conte
** http://tlsf.baisoku.org
**
** Based on the original documentation by Miguel Masmano:
** http://www.gii.upv.es/tlsf/main/docs
**
** This implementation was written to the specification
** of the document, therefore no GPL restrictions apply.
**
** Copyright (c) 2006-2016, Matthew Conte
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the copyright holder nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TOS_MMHEAP_H_
#define _TOS_MMHEAP_H_
__CDECLS_BEGIN
#if TOS_CFG_MMHEAP_EN > 0u
/**
* log2 of number of linear subdivisions of block sizes. Larger
* values require more memory in the control structure. Values of
* 4 or 5 are typical.
*/
#define K_MMHEAP_SL_INDEX_COUNT_LOG2 5
/* All allocation sizes and addresses are aligned to 4 bytes. */
#define K_MMHEAP_ALIGN_SIZE_LOG2 2
#define K_MMHEAP_ALIGN_SIZE (1 << K_MMHEAP_ALIGN_SIZE_LOG2)
/*
* We support allocations of sizes up to (1 << K_MMHEAP_FL_INDEX_MAX) bits.
* However, because we linearly subdivide the second-level lists, and
* our minimum size granularity is 4 bytes, it doesn't make sense to
* create first-level lists for sizes smaller than K_MMHEAP_SL_INDEX_COUNT * 4,
* or (1 << (K_MMHEAP_SL_INDEX_COUNT_LOG2 + 2)) bytes, as there we will be
* trying to split size ranges into more slots than we have available.
* Instead, we calculate the minimum threshold size, and place all
* blocks below that size into the 0th first-level list.
*/
#define K_MMHEAP_FL_INDEX_MAX 30
#define K_MMHEAP_SL_INDEX_COUNT (1 << K_MMHEAP_SL_INDEX_COUNT_LOG2)
#define K_MMHEAP_FL_INDEX_SHIFT (K_MMHEAP_SL_INDEX_COUNT_LOG2 + K_MMHEAP_ALIGN_SIZE_LOG2)
#define K_MMHEAP_FL_INDEX_COUNT (K_MMHEAP_FL_INDEX_MAX - K_MMHEAP_FL_INDEX_SHIFT + 1)
#define K_MMHEAP_SMALL_BLOCK_SIZE (1 << K_MMHEAP_FL_INDEX_SHIFT)
#define K_MMHEAP_BLOCK_CURR_FREE (1 << 0)
#define K_MMHEAP_BLOCK_PREV_FREE (1 << 1)
#define K_MMHEAP_BLOCK_SIZE_MASK ~(K_MMHEAP_BLOCK_CURR_FREE | K_MMHEAP_BLOCK_PREV_FREE)
#define K_MMHEAP_BLOCK_STATE_MASK (K_MMHEAP_BLOCK_CURR_FREE | K_MMHEAP_BLOCK_PREV_FREE)
typedef struct k_mmheap_information_st {
uint32_t used; /* space is used */
uint32_t free; /* space is free */
} k_mmheap_info_t;
/**
* Block structure.
*
* There are several implementation subtleties involved:
* - The prev_phys_block field is only valid if the previous block is free.
* - The prev_phys_block field is actually stored at the end of the
* previous block. It appears at the beginning of this structure only to
* simplify the implementation.
* - The next_free / prev_free fields are only valid if the block is free.
*/
typedef struct mmheap_blk_st {
struct mmheap_blk_st *prev_phys_blk;
size_t size;
struct mmheap_blk_st *next_free;
struct mmheap_blk_st *prev_free;
} mmheap_blk_t;
/**
* A free block must be large enough to store its header minus the size of
* the prev_phys_block field, and no larger than the number of addressable
* bits for FL_INDEX.
*/
#define K_MMHEAP_BLK_SIZE_MIN (sizeof(mmheap_blk_t) - sizeof(mmheap_blk_t *))
#define K_MMHEAP_BLK_SIZE_MAX (1 << K_MMHEAP_FL_INDEX_MAX)
#define K_MMHEAP_BLK_HEADER_OVERHEAD (sizeof(size_t))
#define K_MMHEAP_BLK_START_OFFSET (TOS_OFFSET_OF_FIELD(mmheap_blk_t, size) + sizeof(size_t))
#define K_MMHEAP_POOL_MAX 3
/**
* memory heap control
*/
typedef struct k_mmheap_control_st {
int pool_cnt;
void *pool_start[K_MMHEAP_POOL_MAX];
mmheap_blk_t block_null; /**< Empty lists point at this block to indicate they are free. */
uint32_t fl_bitmap; /**< Bitmaps for free lists. */
uint32_t sl_bitmap[K_MMHEAP_FL_INDEX_COUNT];
mmheap_blk_t *blocks[K_MMHEAP_FL_INDEX_COUNT][K_MMHEAP_SL_INDEX_COUNT]; /**< Head of free lists. */
} k_mmheap_ctl_t;
/**
* @brief Add a pool.
* Add addtional pool to the heap.
*
* @attention None
*
* @param[in] pool_start start address of the pool.
* @param[in] pool_size size of the pool.
*
* @return errcode
* @retval #K_ERR_MMHEAP_INVALID_POOL_ADDR start address of the pool is invalid.
* @retval #K_ERR_MMHEAP_INVALID_POOL_SIZE size of the pool is invalid.
* @retval #K_ERR_MMHEAP_POOL_OVERFLOW too many pools are added.
* @retval #K_ERR_MMHEAP_POOL_ALREADY_EXIST the pool is already exist.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmheap_pool_add(void *pool_start, size_t pool_size);
/**
* @brief Remove a pool.
* Remove a pool from the heap.
*
* @attention None
*
* @param[in] pool_start start address of the pool.
*
* @return errcode
* @retval #K_ERR_OBJ_PTR_NULL start address of the pool is NULL
* @retval #K_ERR_MMHEAP_POOL_NOT_EXIST the pool is not exist
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmheap_pool_rmv(void *pool_start);
/**
* @brief Alloc memory.
* Allocate size bytes and returns a pointer to the allocated memory.
*
* @attention size should no bigger than K_MMHEAP_BLK_SIZE_MAX.
*
* @param[in] size size of the memory.
*
* @return the pointer to the allocated memory.
*/
__API__ void *tos_mmheap_alloc(size_t size);
__API__ void *tos_mmheap_calloc(size_t num, size_t size);
/**
* @brief Alloc start address aligned memory from the heap.
* Alloc aligned address and specified size memory from the heap.
*
* @attention
*
* @param[in] size size of the memory.
* @param[in] align address align mask of the memory.
*
* @return the pointer to the allocated memory.
*/
__API__ void *tos_mmheap_aligned_alloc(size_t size, size_t align);
/**
* @brief Realloc memory from the heap.
* Change the size of the memory block pointed to by ptr to size bytes.
*
* @attention
* <ul>
* <li> if ptr is K_NULL, then the call is equivalent to tos_mmheap_alloc(size), for all values of size.
* <li> if ptr is if size is equal to zero, and ptr is not K_NULL, then the call is equivalent to tos_mmheap_free(ptr).
* </ul>
*
* @param[in] ptr old pointer to the memory space.
* @param[in] size new size of the memory space.
*
* @return the new pointer to the allocated memory.
*/
__API__ void *tos_mmheap_realloc(void *ptr, size_t size);
/**
* @brief Free the memory.
* Free the memory space pointed to by ptr, which must have been returned by a previous call to tos_mmheap_alloc(), tos_mmheap_aligned_alloc(), or tos_mmheap_realloc().
*
* @attention
*
* @param[in] ptr pointer to the memory.
*
* @return None.
*/
__API__ void tos_mmheap_free(void *ptr);
/**
* @brief Check the pool.
*
* @attention
*
* @param[in] pool_start start address of the pool.
* @param[out] info pointer to the information struct.
*
* @return errcode.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmheap_pool_check(void *pool_start, k_mmheap_info_t *info);
/**
* @brief Check the heap.
*
* @attention
*
* @param[out] info pointer to the information struct.
*
* @return errcode.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mmheap_check(k_mmheap_info_t *info);
__KNL__ k_err_t mmheap_init(void);
__KNL__ k_err_t mmheap_init_with_pool(void *pool_start, size_t pool_size);
#else /* if mmheap is not enabled, use libc instead */
#define tos_mmheap_alloc malloc
#define tos_mmheap_calloc calloc
#define tos_mmheap_realloc realloc
#define tos_mmheap_free free
#endif
__CDECLS_END
#endif /* _TOS_MMHEAP_H_ */

View File

@@ -0,0 +1,150 @@
/*----------------------------------------------------------------------------
* 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_MUTEX_H_
#define _TOS_MUTEX_H_
__CDECLS_BEGIN
#if TOS_CFG_MUTEX_EN > 0u
typedef struct k_mutex_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_nesting_t pend_nesting;
k_task_t *owner;
k_prio_t owner_orig_prio;
k_list_t owner_anchor;
} k_mutex_t;
/**
* @brief Create a mutex.
* create a mutex.
*
* @attention None
*
* @param[in] mutex pointer to the handler of the mutex.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_create(k_mutex_t *mutex);
/**
* @brief Destroy a mutex.
* destroy a mutex.
*
* @attention None
*
* @param[in] mutex pointer to the handler of the mutex.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_destroy(k_mutex_t *mutex);
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/**
* @brief Create a dynamic mutex.
* create a dynamic mutex.
*
* @attention None
*
* @param[in] mutex pointer to the pointer of the mutex.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_create_dyn(k_mutex_t **mutex);
/**
* @brief Destroy a dynamic mutex.
* destroy a dynamic mutex.
*
* @attention None
*
* @param[in] mutex pointer to the handler of the mutex.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_destroy_dyn(k_mutex_t *mutex);
#endif
/**
* @brief Pend a mutex.
* pend a mutex.
*
* @attention The task will keep blocked until the mutex is obtained or a timeout comes.
*
* @param[in] mutex pointer to the handler of the mutex.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_MUTEX_NESTING_OVERFLOW we are the owner of the mutex, and we are nesting pend too much on this mutex.
* @retval #K_ERR_MUTEX_NESTING we are the owner of the mutex, and we are nesting pend on it.
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the mutex we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_pend_timed(k_mutex_t *mutex, k_tick_t timeout);
/**
* @brief Pend a mutex.
* pend a mutex.
*
* @attention The task will keep blocked until the mutex is obtained.
*
* @param[in] mutex pointer to the handler of the mutex.
*
* @return errcode
* @retval #K_ERR_MUTEX_NESTING_OVERFLOW we are the owner of the mutex, and we are nesting pend too much on this mutex.
* @retval #K_ERR_MUTEX_NESTING we are the owner of the mutex, and we are nesting pend on it.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_DESTROY the mutex we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_pend(k_mutex_t *mutex);
/**
* @brief Post a mutex.
* post a mutex.
*
* @attention None
*
* @param[in] mutex pointer to the handler of the mutex.
*
* @return errcode
* @retval #K_ERR_MUTEX_NOT_OWNER we are posting a mutex of which the owner is not us.
* @retval #K_ERR_MUTEX_NESTING we are posting a mutex owned by us, and we are still in a nesting.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_mutex_post(k_mutex_t *mutex);
__KNL__ void mutex_release(k_mutex_t *mutex);
#endif
__CDECLS_END
#endif /* _TOS_MUTEX_H_ */

View File

@@ -0,0 +1,73 @@
/*----------------------------------------------------------------------------
* 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_PEND_H_
#define _TOS_PEND_H_
__CDECLS_BEGIN
/**
* The reason why we wakeup from a pend.
* when we wakeup, we need to know why.
*/
typedef enum pend_state_en {
PEND_STATE_NONE, /**< nothing. */
PEND_STATE_POST, /**< someone has post, we get what we want. */
PEND_STATE_TIMEOUT, /**< a post has never came until time is out. */
PEND_STATE_DESTROY, /**< someone has destroyed what we are pending for. */
PEND_STATE_OWNER_DIE, /**< the pend object owner task is destroyed. */
} pend_state_t;
typedef enum opt_post_en {
OPT_POST_ONE,
OPT_POST_ALL,
} opt_post_t;
typedef struct pend_object_st {
k_list_t list;
} pend_obj_t;
__KNL__ void pend_object_init(pend_obj_t *object);
__KNL__ void pend_object_deinit(pend_obj_t *object);
__KNL__ int pend_is_nopending(pend_obj_t *object);
__KNL__ k_prio_t pend_highest_pending_prio_get(pend_obj_t *object);
__KNL__ k_task_t *pend_highest_pending_task_get(pend_obj_t *object);
__KNL__ void pend_list_remove(k_task_t *task);
__KNL__ void pend_list_adjust(k_task_t *task);
__KNL__ k_err_t pend_state2errno(pend_state_t state);
__KNL__ void pend_task_wakeup(k_task_t *task, pend_state_t state);
__KNL__ void pend_task_block(k_task_t *task, pend_obj_t *object, k_tick_t timeout);
__KNL__ void pend_wakeup_one(pend_obj_t *object, pend_state_t state);
__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

@@ -0,0 +1,167 @@
/*----------------------------------------------------------------------------
* 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_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 {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
void *prio_q_mgr_array;
k_prio_q_t prio_q;
} k_prio_mail_q_t;
/**
* @brief Create a priority mail queue.
* create a priority mail queue.
*
* @attention a MAIL is a buffer with a certain size.
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
* @param[in] pool pool buffer of the priority mail queue.
* @param[in] mail_cnt mail count of the priority mail queue.
* @param[in] mail_size size of each mail in the priority mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_create(k_prio_mail_q_t *prio_mail_q, void *pool, size_t mail_cnt, size_t mail_size);
/**
* @brief Destroy a priority mail queue.
* destroy a priority mail queue.
*
* @attention None
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_destroy(k_prio_mail_q_t *prio_mail_q);
/**
* @brief Create a priority mail queue with dynamic allocated pool.
* create a priority mail queue with dynamic allocated pool.
*
* @attention a MAIL is a buffer with a certain size.
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
* @param[in] mail_cnt mail count of the priority mail queue.
* @param[in] mail_size size of each mail in the priority mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_create_dyn(k_prio_mail_q_t *prio_mail_q, size_t mail_cnt, size_t mail_size);
/**
* @brief Destroy a priority mail queue with dynamic allocated pool.
* destroy a priority mail queue with dynamic allocated pool.
*
* @attention None
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_destroy_dyn(k_prio_mail_q_t *prio_mail_q);
/**
* @brief Flush the priority mail queue.
* flush the priority mail queue.
*
* @attention None
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_flush(k_prio_mail_q_t *prio_mail_q);
/**
* @brief Pend a priority mail queue.
* pend a priority mail queue.
*
* @attention
* 1. we WILL perform a memcpy when mail_buf is received(a MAIL is a buffer with a certain size).
* 2. With priority mail queue, if the poster has post several mail with different priority, the pender will receive
* the mail in priority order(numerically bigger, actually smaller, means the mail with highest priority(numerically
* smallest) will be received first, then the second highest priority mail, and so on).
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
* @param[OUT] mail_buf a pointer to the mail buffer we wanna receive.
* @param[OUT] mail_size size of the mail buffer received(should be consistent with the mail_size passed to tos_prio_mail_q_create).
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the queue we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_pend(k_prio_mail_q_t *prio_mail_q, void *mail_buf, size_t *mail_size, k_tick_t timeout);
/**
* @brief Post a priority mail queue.
* post a priority mail queue and wakeup one pending task.
*
* @attention when tos_prio_mail_q_post return successfully, only one task who are waitting for the mail queue will be woken up.
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
* @param[in] mail_buf the mail to post(a MAIL is a buffer).
* @param[in] mail_size the size of the mail to post(a MAIL is just a buffer).
* @param[in] prio the priority of the mail.
*
* @return errcode
* @retval #K_ERR_PRIO_Q_FULL the mail queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_post(k_prio_mail_q_t *prio_mail_q, void *mail_buf, size_t mail_size, k_prio_t prio);
/**
* @brief Post a priority mail queue.
* post a priority mail queue and wakeup all the pending task.
*
* @attention when tos_prio_mail_q_post_all return successfully, all of the tasks who are waitting for the message queue will be woken up.
*
* @param[in] prio_mail_q pointer to the handler of the priority mail queue.
* @param[in] mail_buf the mail to post(a MAIL is a buffer).
* @param[in] mail_size the size of the mail to post(a MAIL is just a buffer).
* @param[in] prio the priority of the mail.
*
* @return errcode
* @retval #K_ERR_PRIO_Q_FULL the mail queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_mail_q_post_all(k_prio_mail_q_t *prio_mail_q, void *mail_buf, size_t mail_size, k_prio_t prio);
#endif /* TOS_CFG_PRIORITY_MAIL_QUEUE_EN */
__CDECLS_END
#endif /* _TOS_PRIORITY_MAIL_QUEUE_H_ */

View File

@@ -0,0 +1,162 @@
/*----------------------------------------------------------------------------
* 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_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 {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
void *prio_q_mgr_array;
k_prio_q_t prio_q;
} k_prio_msg_q_t;
/**
* @brief Create a priority message queue.
* create a priority message queue.
*
* @attention a MESSAGE is a "void *" pointer.
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
* @param[in] pool pool buffer of the priority message queue.
* @param[in] msg_cnt message count of the priority message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_create(k_prio_msg_q_t *prio_msg_q, void *pool, size_t msg_cnt);
/**
* @brief Destroy a priority message queue.
* destroy a priority message queue.
*
* @attention None
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_destroy(k_prio_msg_q_t *prio_msg_q);
/**
* @brief Create a priority message queue with dynamic allocated pool.
* create a priority message queue with dynamic allocated pool.
*
* @attention a MESSAGE is a "void *" pointer.
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
* @param[in] msg_cnt message count of the priority message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_create_dyn(k_prio_msg_q_t *prio_msg_q, size_t msg_cnt);
/**
* @brief Destroy a priority message queue with dynamic allocated pool.
* destroy a priority message queue with dynamic allocated pool.
*
* @attention None
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_destroy_dyn(k_prio_msg_q_t *prio_msg_q);
/**
* @brief Flush the priority message queue.
* flush the priority message queue.
*
* @attention None
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_flush(k_prio_msg_q_t *prio_msg_q);
/**
* @brief Pend a priority message queue.
* pend a priority message queue.
*
* @attentio
* 1. we DONNOT perform a memcpy when msg_ptr is received(a MESSAGE is just a pointer).
* 2. With priority message queue, if the poster has post several message with different priority, the pender will receive
* the message in priority order(numerically bigger, actually smaller, means the message with highest priority(numerically
* smallest) will be received first, then the second highest priority message, and so on).
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
* @param[OUT] msg_ptr a pointer to the message we wanna receive.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the queue we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_pend(k_prio_msg_q_t *prio_msg_q, void **msg_ptr, k_tick_t timeout);
/**
* @brief Post a priority message queue.
* post a priority message queue and wakeup one pending task.
*
* @attention when tos_prio_msg_q_post return successfully, only one task who are waitting for the priority message queue will be woken up.
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
* @param[in] msg_ptr the message to post(a MESSAGE is just a pointer).
* @param[in] prio the priority of the message.
*
* @return errcode
* @retval #K_ERR_PRIO_Q_FULL the priority message queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_post(k_prio_msg_q_t *prio_msg_q, void *msg_ptr, k_prio_t prio);
/**
* @brief Post a priority message queue.
* post a priority message queue and wakeup all the pending task.
*
* @attention when tos_prio_msg_q_post_all return successfully, all of the tasks who are waitting for the priority message queue will be woken up.
*
* @param[in] prio_msg_q pointer to the handler of the priority message queue.
* @param[in] msg_ptr the priority message to post(a MESSAGE is just a pointer).
* @param[in] prio the priority of the message.
*
* @return errcode
* @retval #K_ERR_PRIO_Q_FULL the priority message queue is full.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_msg_q_post_all(k_prio_msg_q_t *prio_msg_q, void *msg_ptr, k_prio_t prio);
#endif
__CDECLS_END
#endif /* _TOS_PRIORITY_MESSAGE_QUEUE_H_ */

View File

@@ -0,0 +1,209 @@
/*----------------------------------------------------------------------------
* 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_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 {
prio_q_slot_t next;
} prio_q_pool_mgr_ent_t;
typedef struct prio_q_pool_manager_st {
prio_q_slot_t first_free;
prio_q_pool_mgr_ent_t *pool_mgr_ent_array;
} prio_q_pool_mgr_t;
typedef struct prio_q_priority_manager_entry_st {
k_prio_t priority;
prio_q_slot_t slot;
} prio_q_prio_mgr_ent_t;
typedef struct prio_q_prio_manager_st {
k_bin_heap_t prio_mgr_bin_heap;
prio_q_prio_mgr_ent_t *prio_mgr_ent_pool;
} prio_q_prio_mgr_t;
typedef struct k_priority_queue_st {
knl_obj_t knl_obj;
prio_q_pool_mgr_t pool_mgr;
prio_q_prio_mgr_t prio_mgr;
size_t total;
size_t item_size;
size_t item_cnt;
uint8_t *mgr_pool;
uint8_t *data_pool;
} k_prio_q_t;
#define PRIO_Q_POOL_SLOT_INVALID ((prio_q_slot_t)-1)
#define PRIO_Q_POOL_MGR_ENT_ARRAY_SIZE(item_cnt) \
(item_cnt * sizeof(prio_q_pool_mgr_ent_t))
#define PRIO_Q_PRIO_MGR_ENT_POOL_SIZE(item_cnt) \
(item_cnt * sizeof(prio_q_prio_mgr_ent_t))
#define PRIO_Q_THE_ITEM(prio_q, slot) (void *)(&prio_q->data_pool[slot * prio_q->item_size])
// get the size of mgr_array to create a priority queue
#define TOS_PRIO_Q_MGR_ARRAY_SIZE(item_cnt) \
(PRIO_Q_POOL_MGR_ENT_ARRAY_SIZE(item_cnt) + PRIO_Q_PRIO_MGR_ENT_POOL_SIZE(item_cnt))
/**
* @brief Create a priority queue.
* create a priority queue.
*
* @attention if we wanna create a priority queue, we should offer a manager array buffer of which the size can be calculated by TOS_PRIO_Q_MGR_ARRAY_SIZE.
*
* @param[in] prio_q pointer to the handler of the priority queue.
* @param[in] mgr_array manager array buffer of the priority queue.
* @param[in] pool pool buffer of the priority queue.
* @param[in] item_cnt item count of the priority queue.
* @param[in] item_size size of each item of the priority queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_q_create(k_prio_q_t *prio_q, void *mgr_array, void *pool, size_t item_cnt, size_t item_size);
/**
* @brief Destroy a priority queue.
* destroy a priority queue.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the bpriority queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_q_destroy(k_prio_q_t *prio_q);
/**
* @brief Create a priority queue with dynamic allocated mgr array and data pool.
* create a priority queue with dynamic allocated mgr array and data pool.
*
* @attention if we wanna create a priority queue, we should offer a manager array buffer of which the size can be calculated by TOS_PRIO_Q_MGR_ARRAY_SIZE.
*
* @param[in] prio_q pointer to the handler of the priority queue.
* @param[in] item_cnt item count of the priority queue.
* @param[in] item_size size of each item of the priority queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_q_create_dyn(k_prio_q_t *prio_q, size_t item_cnt, size_t item_size);
/**
* @brief Destroy a priority queue with dynamic allocated mgr array and data pool.
* destroy a priority queue with dynamic allocated mgr array and data pool.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the bpriority queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_q_destroy_dyn(k_prio_q_t *prio_q);
/**
* @brief Enqueue an priority queue.
* enqueue an item into the priority queue.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of priority queue.
* @param[in] item the item to be enqueued.
* @param[in] item_size size of the item(should be consistent with the item_size passed to tos_prio_q_create).
* @param[in] prio priority of the item to be enqueued(should be consistent with the item_size passed to tos_prio_q_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_PRIO_Q_ITEM_SIZE_NOT_MATCH the item_size is not consistent with the item_size passed to tos_prio_q_create.
* @retval #K_ERR_PRIO_Q_FULL the priority queue is full.
*/
__API__ k_err_t tos_prio_q_enqueue(k_prio_q_t *prio_q, void *item, size_t item_size, k_prio_t prio);
/**
* @brief Dequeue an item.
* dequeue an item from the priority queue.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the priority queue.
* @param[out] item buffer to hold the item dequeued.
* @param[out] item_size size of the item dequeued(should be consistent with the item_size passed to tos_prio_q_create).
* @param[out] prio priority of the item dequeued.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_PRIO_Q_EMPTY the priority queue is empty.
*/
__API__ k_err_t tos_prio_q_dequeue(k_prio_q_t *prio_q, void *item, size_t *item_size, k_prio_t *prio);
/**
* @brief Flush the priority queue.
* flush the priority queue.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the priority queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_prio_q_flush(k_prio_q_t *prio_q);
/**
* @brief Whether the priority queue is empty.
* Whether the priority queue is empty.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the priority queue.
*
* @return whether the priority queue is emtpy.
* @retval #0 the priority queue is not empty.
* @retval #Not 0 the priority queue is empty.
*/
__API__ int tos_prio_q_is_empty(k_prio_q_t *prio_q);
/**
* @brief Whether the priority queue is full.
* Whether the priority queue is full.
*
* @attention None
*
* @param[in] prio_q pointer to the handler of the priority queue.
*
* @return whether the priority queue is full.
* @retval #0 the priority queue is not full.
* @retval #Not 0 the priority queue is full.
*/
__API__ int tos_prio_q_is_full(k_prio_q_t *prio_q);
__CDECLS_END
#endif /* _TOS_PRIORITY_QUEUE_H_ */

View File

@@ -0,0 +1,173 @@
/*----------------------------------------------------------------------------
* 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_RING_QUEUE_H_
#define _TOS_RING_QUEUE_H_
typedef struct k_ring_queue_st {
knl_obj_t knl_obj;
uint16_t head;
uint16_t tail;
size_t total;
uint8_t *pool;
size_t item_size;
size_t item_cnt;
} k_ring_q_t;
#define RING_HEAD_ITEM(ring_q) (uint8_t *)(&ring_q->pool[ring_q->head * ring_q->item_size])
#define RING_TAIL_ITEM(ring_q) (uint8_t *)(&ring_q->pool[ring_q->tail * ring_q->item_size])
#define RING_NEXT(ring_q, index) ((index + 1) % ring_q->item_cnt)
/**
* @brief Create a ring queue.
* create a ring queue.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
* @param[in] pool pool buffer of the ring queue.
* @param[in] item_cnt item count of the ring queue.
* @param[in] item_size size of each item of the ring queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_ring_q_create(k_ring_q_t *ring_q, void *pool, size_t item_cnt, size_t item_size);
/**
* @brief Destroy a ring queue.
* destroy a ring queue.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OBJ_INVALID_ALLOC_TYPE invalid alloc type(is dynamic allocated not static)
*/
__API__ k_err_t tos_ring_q_destroy(k_ring_q_t *ring_q);
/**
* @brief Create a ring queue with dynamic allocated pool.
* create a ring queue with dynamic allocated pool.
*
* @attention pool inside is dynamic allocated.
*
* @param[in] ring_q pointer to the handler of the ring queue.
* @param[in] item_cnt item count of the ring queue.
* @param[in] item_size size of each item of the ring queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OUT_OF_MEMORY out of memory
*/
__API__ k_err_t tos_ring_q_create_dyn(k_ring_q_t *ring_q, size_t item_cnt, size_t item_size);
/**
* @brief Destroy a ring queue with a dynamic allocated pool.
* destroy a ring queue with a dynamic allocated pool.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_OBJ_INVALID_ALLOC_TYPE invalid alloc type(is static allocated not dynamic)
*/
__API__ k_err_t tos_ring_q_destroy_dyn(k_ring_q_t *ring_q);
/**
* @brief Enqueue an item.
* enqueue an item into the ring queue.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
* @param[in] item the item to be enqueued.
* @param[in] item_size size of the item(should be consistent with the item_size passed to tos_ring_q_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RING_Q_ITEM_SIZE_NOT_MATCH the item_size is not consistent with the item_size passed to tos_ring_q_create.
* @retval #K_ERR_RING_Q_FULL the ring queue is full.
*/
__API__ k_err_t tos_ring_q_enqueue(k_ring_q_t *ring_q, void *item, size_t item_size);
/**
* @brief Dequeue an item.
* dequeue an item from the ring queue.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
* @param[out] item buffer to hold the item dequeued.
* @param[out] item_size size of the item dequeued(should be consistent with the item_size passed to tos_ring_q_create).
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
* @retval #K_ERR_RING_Q_EMPTY the ring queue is empty.
*/
__API__ k_err_t tos_ring_q_dequeue(k_ring_q_t *ring_q, void *item, size_t *item_size);
/**
* @brief Flush the ring queue.
* flush the ring queue.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_ring_q_flush(k_ring_q_t *ring_q);
/**
* @brief Whether the ring queue is empty.
* Whether the ring queue is empty.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
*
* @return whether the ring queue is emtpy.
* @retval #0 the ring queue is not empty.
* @retval #Not 0 the ring queue is empty.
*/
__API__ int tos_ring_q_is_empty(k_ring_q_t *ring_q);
/**
* @brief Whether the ring queue is full.
* Whether the ring queue is full.
*
* @attention None
*
* @param[in] ring_q pointer to the handler of the ring queue.
*
* @return whether the ring queue is full.
* @retval #0 the ring queue is not full.
* @retval #Not 0 the ring queue is full.
*/
__API__ int tos_ring_q_is_full(k_ring_q_t *ring_q);
#endif

View File

@@ -0,0 +1,57 @@
/*----------------------------------------------------------------------------
* 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_ROBIN_H_
#define _TOS_ROBIN_H_
__CDECLS_BEGIN
#if TOS_CFG_ROUND_ROBIN_EN > 0u
/**
* @brief Set time slice.
* Set time slice of a task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task.
* @param[in] timeslice time slice of the task
*
* @return None
*/
__API__ void tos_robin_timeslice_set(k_task_t *task, k_timeslice_t timeslice);
/**
* @brief Configure round robin.
* Set the default time slice of the task.
*
* @attention None
*
* @param[in] default_timeslice default time slice of the task.
*
* @return None
*/
__API__ void tos_robin_default_timeslice_config(k_timeslice_t default_timeslice);
__KNL__ void robin_sched(k_prio_t prio);
#endif /* TOS_CFG_ROUND_ROBIN_EN */
__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 {
knl_obj_t knl_obj;
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.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @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.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @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

@@ -0,0 +1,56 @@
/*----------------------------------------------------------------------------
* 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_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 (8 * sizeof(uint32_t))
#define K_PRIO_NDX(prio) ((prio) >> 5u) /* prio / 32u */
#define K_PRIO_BIT(prio) ((uint32_t)1u << (K_PRIO_TBL_SLOT_SIZE - 1u - ((prio) & (K_PRIO_TBL_SLOT_SIZE - 1u))))
typedef struct readyqueue_st {
k_list_t task_list_head[TOS_CFG_TASK_PRIO_MAX];
uint32_t prio_mask[K_PRIO_TBL_SIZE];
k_prio_t highest_prio;
} readyqueue_t;
__KNL__ void readyqueue_init(void);
__KNL__ int readyqueue_is_prio_onlyone(k_prio_t prio);
__KNL__ k_task_t *readyqueue_first_task_get(k_prio_t prio);
__KNL__ k_task_t *readyqueue_highest_ready_task_get(void);
__KNL__ void readyqueue_add_head(k_task_t *task);
__KNL__ void readyqueue_add_tail(k_task_t *task);
__KNL__ void readyqueue_add(k_task_t *task);
__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

@@ -0,0 +1,172 @@
/*----------------------------------------------------------------------------
* 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_SEM_H_
#define _TOS_SEM_H_
__CDECLS_BEGIN
#if TOS_CFG_SEM_EN > 0u
typedef struct k_sem_st {
knl_obj_t knl_obj;
pend_obj_t pend_obj;
k_sem_cnt_t count;
k_sem_cnt_t count_max;
} k_sem_t;
/**
* @brief Create a semaphore with a limitation of maximum count.
* create a semaphore with a limitation of maximum count.
*
* @attention None
*
* @param[in] sem pointer to the handler of the semaphore.
* @param[in] init_count initial count of the semaphore.
* @param[in] max_count maximum count of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_create_max(k_sem_t *sem, k_sem_cnt_t init_count, k_sem_cnt_t max_count);
/**
* @brief Create a semaphore.
* create a semaphore.
*
* @attention None
*
* @param[in] sem pointer to the handler of the semaphore.
* @param[in] init_count initial count of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_create(k_sem_t *sem, k_sem_cnt_t init_count);
/**
* @brief Destroy a semaphore.
* destroy a semaphore.
*
* @attention None
*
* @param[in] semaphore pointer to the handler of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_destroy(k_sem_t *sem);
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/**
* @brief Create a dynamic semaphore with a limitation of maximum count.
* create a dynamic semaphore with a limitation of maximum count.
*
* @attention None
*
* @param[in] sem pointer to the pointer of the semaphore.
* @param[in] init_count initial count of the semaphore.
* @param[in] max_count maximum count of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_create_max_dyn(k_sem_t **sem, k_sem_cnt_t init_count, k_sem_cnt_t max_count);
/**
* @brief Create a dynamic semaphore.
* create a dynamic semaphore.
*
* @attention None
*
* @param[in] sem pointer to the pointer of the semaphore.
* @param[in] init_count initial count of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_create_dyn(k_sem_t **sem, k_sem_cnt_t init_count);
/**
* @brief Destroy a dynamic semaphore.
* destroy a dynamic semaphore.
*
* @attention None
*
* @param[in] semaphore pointer to the handler of the semaphore.
*
* @return errcode
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_destroy_dyn(k_sem_t *sem);
#endif
/**
* @brief Pend a semaphore.
* pend a semaphore.
*
* @attention None
*
* @param[in] sem pointer to the handler of the semaphore.
* @param[in] timeout how much time(in k_tick_t) we would like to wait.
*
* @return errcode
* @retval #K_ERR_PEND_NOWAIT we get nothing, and we don't wanna wait.
* @retval #K_ERR_PEND_SCHED_LOCKED we can wait, but scheduler is locked.
* @retval #K_ERR_PEND_TIMEOUT the time we wait is up, we get nothing.
* @retval #K_ERR_PEND_DESTROY the semaphore we are pending is destroyed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_pend(k_sem_t *sem, k_tick_t timeout);
/**
* @brief Post a semaphore.
* post a semaphore and wakeup one pending task.
*
* @attention when tos_sem_post return successfully, only one task who are waitting for the semaphore will be woken up.
*
* @param[in] sem pointer to the handler of the semaphore.
*
* @return errcode
* @retval #K_ERR_SEM_OVERFLOW we are nesting post a semaphore too much.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_post(k_sem_t *sem);
/**
* @brief Post a semaphore.
* post a semaphore and wakeup all the pending task.
*
* @attention when tos_sem_post_all return successfully, all of the tasks who are waitting for the semaphore will be woken up.
*
* @param[in] sem pointer to the handler of the semaphore.
*
* @return errcode
* @retval #K_ERR_SEM_OVERFLOW we are nesting post a semaphore too much.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sem_post_all(k_sem_t *sem);
#endif
__CDECLS_END
#endif /* _TOS_SEM_H_ */

View File

@@ -0,0 +1,189 @@
/*----------------------------------------------------------------------------
* 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_SLIST_H_
#define _TOS_SLIST_H_
__CDECLS_BEGIN
typedef struct k_slist_node_st {
struct k_slist_node_st *next;
} k_slist_t;
#define TOS_SLIST_NODE(dummy) \
{ K_NULL }
#define TOS_SLIST_DEFINE(slist) \
k_slist_t slist = { K_NULL }
#define TOS_SLIST_ENTRY(node, type, field) \
TOS_CONTAINER_OF_FIELD(node, type, field)
#define TOS_SLIST_FIRST_ENTRY(slist, type, field) \
TOS_SLIST_ENTRY((slist)->next, type, field)
#define TOS_SLIST_FIRST_ENTRY_OR_NULL(slist, type, field) \
(tos_slist_empty(slist) ? K_NULL : TOS_SLIST_FIRST_ENTRY(slist, type, field))
#define TOS_SLIST_FOR_EACH(curr, slist) \
for (curr = (slist)->next; curr; curr = curr->next)
#define TOS_SLIST_FOR_EACH_SAFE(curr, next, slist) \
for (curr = (slist)->next, next = curr->next; curr; \
curr = next, next = curr->next)
#define TOS_SLIST_FOR_EACH_ENTRY(entry, type, field, slist) \
for (entry = TOS_SLIST_ENTRY((slist)->next, type, field); \
&entry->field; \
entry = TOS_SLIST_ENTRY(entry->field.next, type, field))
#define TOS_SLIST_FOR_EACH_ENTRY_SAFE(entry, tmp, type, field, slist) \
for (entry = TOS_SLIST_ENTRY((slist)->next, type, field), \
tmp = TOS_SLIST_ENTRY(entry->field.next, type, field); \
&entry->field; \
entry = tmp, tmp = TOS_SLIST_ENTRY(entry->field.next, type, field))
__API__ __STATIC_INLINE__ void tos_slist_init(k_slist_t *slist)
{
slist->next = K_NULL;
}
__API__ __STATIC_INLINE__ k_slist_t *tos_slist_head(k_slist_t *slist)
{
return slist->next;
}
__API__ __STATIC_INLINE__ k_slist_t *tos_slist_tail(k_slist_t *slist)
{
if (!slist->next) {
return K_NULL;
}
while (slist->next) {
slist = slist->next;
}
return slist;
}
__API__ __STATIC_INLINE__ void tos_slist_add_head(k_slist_t *node, k_slist_t *slist)
{
node->next = slist->next;
slist->next = node;
}
__API__ __STATIC_INLINE__ void tos_slist_add_tail(k_slist_t *node, k_slist_t *slist)
{
node->next = K_NULL;
while (slist->next) {
slist = slist->next;
}
tos_slist_add_head(node, slist);
}
__API__ __STATIC_INLINE__ void tos_slist_insert(k_slist_t *next_node, k_slist_t *new_node, k_slist_t *slist)
{
if (!next_node) {
tos_slist_add_tail(new_node, slist);
return;
}
while (slist->next) {
if (slist->next == next_node) {
slist->next = new_node;
new_node->next = next_node;
}
slist = slist->next;
}
}
__API__ __STATIC_INLINE__ k_slist_t *tos_slist_del_head(k_slist_t *slist)
{
k_slist_t *head;
if (!slist->next) {
return K_NULL;
}
head = slist->next;
slist->next = head->next;
head->next = K_NULL;
return head;
}
__API__ __STATIC_INLINE__ k_slist_t *tos_slist_del_tail(k_slist_t *slist)
{
while (slist->next) {
if (!slist->next->next) {
return tos_slist_del_head(slist);
}
slist = slist->next;
}
return K_NULL;
}
__API__ __STATIC_INLINE__ void tos_slist_del(k_slist_t *node, k_slist_t *slist)
{
while (slist->next) {
if (slist->next == node) {
slist->next = node->next;
break;
}
slist = slist->next;
}
}
__API__ __STATIC_INLINE__ int tos_slist_length(k_slist_t *slist)
{
int len = 0;
k_slist_t *iter;
TOS_SLIST_FOR_EACH(iter, slist) {
++len;
}
return len;
}
__API__ __STATIC_INLINE__ int tos_slist_contains(k_slist_t *node, k_slist_t *slist)
{
while (slist->next) {
if (slist->next == node) {
return K_TRUE;
}
slist = slist->next;
}
return K_FALSE;
}
__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,139 @@
/*----------------------------------------------------------------------------
* 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 {
knl_obj_t knl_obj;
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 the stopwatch.
*
* @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 the stopwatch.
*
* @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 the stopwatch.
*
* @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

@@ -0,0 +1,227 @@
/*----------------------------------------------------------------------------
* 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_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
typedef enum knl_state_en {
KNL_STATE_STOPPED,
KNL_STATE_RUNNING,
} knl_state_t;
// 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_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_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 {
KNL_OBJ_ALLOC_TYPE_NONE,
KNL_OBJ_ALLOC_TYPE_STATIC,
KNL_OBJ_ALLOC_TYPE_DYNAMIC,
} knl_obj_alloc_type_t;
typedef struct knl_object_st {
knl_obj_alloc_type_t alloc_type; /* is dynamic allocated(using tos_mmheap) or static memory? */
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
knl_obj_type_t type;
#endif
} knl_obj_t;
/**
* @brief Initialize the kernel.
* initialize the tos tiny kernel.
*
* @attention None
*
* @param None
*
* @return errcode
* @retval Non-#K_ERR_NONE return failed.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_knl_init(void);
/**
* @brief Start the kernel.
* get the kernel start to run, which means start the multitask scheduling.
*
* @attention None
*
* @param None
*
* @return errcode
* @retval #K_ERR_KNL_RUNNING the kernel is already running.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_knl_start(void);
/**
* @brief Get the kernel state.
* whether the kernel is running.
*
* @attention None
*
* @param None
*
* @return whether the kernel is running.
* @retval Non-0 the kernel is running.
* @retval 0 the kernel is not running.
*/
__API__ int tos_knl_is_running(void);
/**
* @brief Kernel enter the interrupt.
* this function should be called in the entrance of a interrupt handler.
*
* @attention None
*
* @param None
*
* @return None
*/
__API__ void tos_knl_irq_enter(void);
/**
* @brief Kernel exit the interrupt.
* this function should be called in the exit of a interrupt handler.
*
* @attention None
*
* @param None
*
* @return None
*/
__API__ void tos_knl_irq_leave(void);
/**
* @brief Lock the scheduler.
* prevent the kernel from performing task schedule.
*
* @attention None
*
* @param None
*
* @return errcode
* @retval K_ERR_KNL_NOT_RUNNING the kernel is not running.
* @retval K_ERR_LOCK_NESTING_OVERFLOW the schedule lock nesting is overflow.
* @retval K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_knl_sched_lock(void);
/**
* @brief Unlock the scheduler.
* re-enable the task schedule.
*
* @attention None
*
* @param None
*
* @return errcode
* @retval K_ERR_KNL_NOT_RUNNING the kernel is not running.
* @retval K_ERR_SCHED_NOT_LOCKED the scheduler is not locked.
* @retval K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_knl_sched_unlock(void);
#if TOS_CFG_TICKLESS_EN > 0u
__KNL__ k_tick_t knl_next_expires_get(void);
#endif
__KNL__ void knl_sched(void);
__KNL__ int knl_is_sched_locked(void);
__KNL__ int knl_is_inirq(void);
__KNL__ int knl_is_idle(k_task_t *task);
__KNL__ int knl_is_self(k_task_t *task);
__KNL__ k_err_t knl_idle_init(void);
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
__KNL__ __STATIC_INLINE__ int knl_object_verify(knl_obj_t *knl_obj, knl_obj_type_t type)
{
return knl_obj->type == type;
}
__KNL__ __STATIC_INLINE__ void knl_object_init(knl_obj_t *knl_obj, knl_obj_type_t type)
{
knl_obj->type = type;
}
__KNL__ __STATIC_INLINE__ void knl_object_deinit(knl_obj_t *knl_obj)
{
knl_obj->type = KNL_OBJ_TYPE_NONE;
}
#endif
__KNL__ __STATIC_INLINE__ void knl_object_alloc_reset(knl_obj_t *knl_obj)
{
knl_obj->alloc_type = KNL_OBJ_ALLOC_TYPE_NONE;
}
__KNL__ __STATIC_INLINE__ void knl_object_alloc_set_dynamic(knl_obj_t *knl_obj)
{
knl_obj->alloc_type = KNL_OBJ_ALLOC_TYPE_DYNAMIC;
}
__KNL__ __STATIC_INLINE__ void knl_object_alloc_set_static(knl_obj_t *knl_obj)
{
knl_obj->alloc_type = KNL_OBJ_ALLOC_TYPE_STATIC;
}
__KNL__ __STATIC_INLINE__ int knl_object_alloc_is_dynamic(knl_obj_t *knl_obj)
{
return knl_obj->alloc_type == KNL_OBJ_ALLOC_TYPE_DYNAMIC;
}
__KNL__ __STATIC_INLINE__ int knl_object_alloc_is_static(knl_obj_t *knl_obj)
{
return knl_obj->alloc_type == KNL_OBJ_ALLOC_TYPE_STATIC;
}
__CDECLS_END
#endif /* _TOS_SYS_H_ */

View File

@@ -0,0 +1,468 @@
/*----------------------------------------------------------------------------
* 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_TASK_H_
#define _TOS_TASK_H_
__CDECLS_BEGIN
#define K_TASK_NAME_LEN_MAX (15u)
#define K_TASK_NAME_MAX (K_TASK_NAME_LEN_MAX + 1)
#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
// a task's pend_list is in readyqueue
#define K_TASK_STATE_READY (k_task_state_t)0x0000
// delayed, or pend for a timeout
// a task's tick_list is in k_tick_list
#define K_TASK_STATE_SLEEP (k_task_state_t)0x0001
// pend for something
// a task's pend_list is in some pend object's list
#define K_TASK_STATE_PEND (k_task_state_t)0x0002
// suspended
#define K_TASK_STATE_SUSPENDED (k_task_state_t)0x0004
// deleted
#define K_TASK_STATE_DELETED (k_task_state_t)0x0008
// actually we don't really need those TASK_STATE below, if you understand the task state deeply, the code can be much more elegant.
// we are pending, also we are waitting for a timeout(eg. tos_sem_pend with a valid timeout, not TOS_TIME_FOREVER)
// both a task's tick_list and pend_list is not empty
#define K_TASK_STATE_PENDTIMEOUT (k_task_state_t)(K_TASK_STATE_PEND | K_TASK_STATE_SLEEP)
// suspended when sleeping
#define K_TASK_STATE_SLEEP_SUSPENDED (k_task_state_t)(K_TASK_STATE_SLEEP | K_TASK_STATE_SUSPENDED)
// suspended when pending
#define K_TASK_STATE_PEND_SUSPENDED (k_task_state_t)(K_TASK_STATE_PEND | K_TASK_STATE_SUSPENDED)
// suspended when pendtimeout
#define K_TASK_STATE_PENDTIMEOUT_SUSPENDED (k_task_state_t)(K_TASK_STATE_PENDTIMEOUT | K_TASK_STATE_SUSPENDED)
// if you configure TOS_CFG_TASK_PRIO_MAX as 10, means the priority for kernel is (0 ... 9]
// the priority 9(TOS_CFG_TASK_PRIO_MAX - 1) is only for idle, so avaliable priority for you is (0 ... 8]
#define K_TASK_PRIO_IDLE (k_prio_t)(TOS_CFG_TASK_PRIO_MAX - (k_prio_t)1u)
#define K_TASK_PRIO_INVALID (k_prio_t)(TOS_CFG_TASK_PRIO_MAX)
typedef void (*k_task_entry_t)(void *arg);
typedef void (*k_task_walker_t)(k_task_t *task);
/**
* task control block
*/
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. */
knl_obj_t knl_obj; /**< just for verification, test whether current object is really a task. */
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 */
#if TOS_CFG_OBJ_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 */
#endif
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_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. */
#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 */
#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 */
k_timeslice_t timeslice; /**< how much time slice left for us? */
#endif
#if (TOS_CFG_MESSAGE_QUEUE_EN > 0u) || (TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN > 0u)
void *msg; /**< if we pend a message queue successfully, our msg will be set by the message queue poster */
#endif
#if (TOS_CFG_MAIL_QUEUE_EN > 0u) || (TOS_CFG_PRIORITY_MAIL_QUEUE_EN > 0u)
void *mail; /**< if we pend a mail queue successfully, our mail and mail_size will be set by the message queue poster */
size_t mail_size;
#endif
#if TOS_CFG_EVENT_EN > 0u
k_opt_t opt_event_pend; /**< if we are pending an event, what's the option for the pending(TOS_OPT_EVENT_PEND_*)? */
k_event_flag_t flag_expect; /**< if we are pending an event, what event flag are we pending for ? */
k_event_flag_t *flag_match; /**< if we pend an event successfully, flag_match will be set by the event poster, and will be returned
by tos_event_pend to the caller */
#endif
};
/**
* @brief Create a task.
* create a task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task.
* @param[in] name name of the task.
* @param[in] entry running entry of the task.
* @param[in] arg argument for the entry of the task.
* @param[in] prio priority of the task.
* @param[in] stk_base stack base address of the task.
* @param[in] stk_size stack size of the task.
* @param[in] timeslice time slice of the task.
*
* @return errcode
* @retval #K_ERR_TASK_STK_SIZE_INVALID stack size is invalid.
* @retval #K_ERR_TASK_PRIO_INVALID priority is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_create(k_task_t *task,
const char *name,
k_task_entry_t entry,
void *arg,
k_prio_t prio,
k_stack_t *stk_base,
size_t stk_size,
k_timeslice_t timeslice);
/**
* @brief Destroy a task.
* delete a task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task to be deleted.
*
* @return errcode
* @retval #K_ERR_TASK_DESTROY_IDLE attempt to destroy idle task.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_destroy(k_task_t *task);
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/**
* @brief Create a task with a dynamic allocated task handler and stack.
* create a task with a dynamic allocated task handler and stack.
*
* @param[out] task dynamic allocated task handler.
* @param[in] name name of the task.
* @param[in] entry running entry of the task.
* @param[in] arg argument for the entry of the task.
* @param[in] prio priority of the task.
* @param[in] stk_size stack size of the task.
* @param[in] timeslice time slice of the task.
*
* @return errcode
* @retval #K_ERR_TASK_STK_SIZE_INVALID stack size is invalid.
* @retval #K_ERR_TASK_PRIO_INVALID priority is invalid.
* @retval #K_ERR_TASK_OUT_OF_MEMORY out of memory(insufficient heap memory).
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_create_dyn(k_task_t **task,
const char *name,
k_task_entry_t entry,
void *arg,
k_prio_t prio,
size_t stk_size,
k_timeslice_t timeslice);
/**
* @brief Destroy a dynamic allocated task.
* delete a dynamic allocated task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task to be deleted.
*
* @return errcode
* @retval #K_ERR_TASK_DESTROY_IDLE attempt to destroy idle task.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_destroy_dyn(k_task_t *task);
#endif
/**
* @brief Delay current task for ticks.
* Delay for a specified amount of ticks.
*
* @attention None
*
* @param[in] delay amount of ticks to delay.
*
* @return errcode
* @retval #K_ERR_DELAY_ZERO delay is zero.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_delay(k_tick_t delay);
/**
* @brief Resume task from delay.
* Resume a delayed task from delay.
*
* @attention None
*
* @param[in] task the pointer to the handler of the task.
*
* @return errcode
* @retval #K_ERR_TASK_NOT_DELAY task is not delayed.
* @retval #K_ERR_TASK_SUSPENDED task is suspended.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_delay_abort(k_task_t *task);
/**
* @brief Suspend a task.
* Bring a task to sleep.
*
* @attention None
*
* @param[in] task pointer to the handler of the task to be resume.
*
* @return errcode
* @retval #K_ERR_TASK_SUSPEND_IDLE attempt to suspend idle task.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_suspend(k_task_t *task);
/**
* @brief Resume a task.
* Bring a task to run.
*
* @attention None
*
* @param[in] task pointer to the handler of the task to be resume.
*
* @return errcode
* @retval #K_ERR_TASK_RESUME_SELF attempt to resume self-task.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_resume(k_task_t *task);
/**
* @brief Change task priority.
* Change a priority of the task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task to be resume.
* @param[in] prio_new new priority.
*
* @return errcode
* @retval #K_ERR_TASK_PRIO_INVALID new priority is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_task_prio_change(k_task_t *task, k_prio_t prio_new);
/**
* @brief Quit schedule this time.
* Quit the cpu this time.
*
* @attention None
*
* @param None
*
* @return None
*/
__API__ void tos_task_yield(void);
/**
* @brief Get current running task.
* Get current running task.
*
* @attention if kernel is not running, you'll get K_NULL
*
* @param None
*
* @return current running task handler
*/
__API__ k_task_t *tos_task_curr_task_get(void);
/**
* @brief Find task by task name.
* Find task by task name.
*
* @param name the name of the task.
*
* @return the matched task handler
*/
__API__ k_task_t *tos_task_find(const char *name);
#if TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN > 0u
/**
* @brief Get the maximum stack draught depth of a task.
*
* @attention None
*
* @param[in] task pointer to the handler of the task.
* @param[out] depth task stack draught depth.
*
* @return errcode
* @retval #K_ERR_NONE get depth successfully.
* @retval #K_ERR_TASK_STK_OVERFLOW task stack is overflow.
*/
__API__ k_err_t tos_task_stack_draught_depth(k_task_t *task, int *depth);
#endif
/**
* @brief Walk through all the tasks in the statistic list.
*
* @attention None
*
* @param[in] walker a function involved when meeting each tasks in the list.
*
* @return None
*/
__API__ void tos_task_walkthru(k_task_walker_t walker);
/**
* @brief A debug API for display all tasks information.
*
* @attention None
*
* @param None
*
* @return None
*/
__DEBUG__ void tos_task_info_display(void);
__KNL__ void task_free_all(void);
__KNL__ __STATIC_INLINE__ int task_state_is_ready(k_task_t *task)
{
return task->state == K_TASK_STATE_READY;
}
__KNL__ __STATIC_INLINE__ int task_state_is_sleeping(k_task_t *task)
{
return task->state & K_TASK_STATE_SLEEP;
}
__KNL__ __STATIC_INLINE__ int task_state_is_pending(k_task_t *task)
{
return task->state & K_TASK_STATE_PEND;
}
__KNL__ __STATIC_INLINE__ int task_state_is_suspended(k_task_t *task)
{
return task->state & K_TASK_STATE_SUSPENDED;
}
__KNL__ __STATIC_INLINE__ void task_state_reset_pending(k_task_t *task)
{
task->state &= ~K_TASK_STATE_PEND;
}
__KNL__ __STATIC_INLINE__ void task_state_reset_sleeping(k_task_t *task)
{
task->state &= ~K_TASK_STATE_SLEEP;
}
__KNL__ __STATIC_INLINE__ void task_state_reset_suspended(k_task_t *task)
{
task->state &= ~K_TASK_STATE_SUSPENDED;
}
__KNL__ __STATIC_INLINE__ void task_state_set_suspended(k_task_t *task)
{
task->state |= K_TASK_STATE_SUSPENDED;
}
__KNL__ __STATIC_INLINE__ void task_state_set_pend(k_task_t *task)
{
task->state |= K_TASK_STATE_PEND;
}
__KNL__ __STATIC_INLINE__ void task_state_set_ready(k_task_t *task)
{
task->state = K_TASK_STATE_READY;
}
__KNL__ __STATIC_INLINE__ void task_state_set_deleted(k_task_t *task)
{
task->state = K_TASK_STATE_DELETED;
}
__KNL__ __STATIC_INLINE__ void task_state_set_sleeping(k_task_t *task)
{
task->state |= K_TASK_STATE_SLEEP;
}
__DEBUG__ __STATIC_INLINE__ void task_default_walker(k_task_t *task)
{
char *state_str = "ABNORMAL";
state_str = state_str;
tos_kprintln("tsk name: %s", task->name);
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";
} else if (task->state == K_TASK_STATE_SLEEP_SUSPENDED) {
state_str = "SLEEP_SUSPENDED";
} else if (task->state == K_TASK_STATE_PENDTIMEOUT) {
state_str = "PENDTIMEOUT";
} else if (task->state == K_TASK_STATE_SUSPENDED) {
state_str = "SUSPENDED";
} else if (task->state == K_TASK_STATE_PEND) {
state_str = "PEND";
} else if (task->state == K_TASK_STATE_SLEEP) {
state_str = "SLEEP";
} else if (task->state == K_TASK_STATE_READY) {
state_str = "READY";
}
tos_kprintln("tsk stat: %s", state_str);
tos_kprintln("stk size: %d", task->stk_size);
tos_kprintln("stk base: 0x%p", task->stk_base);
tos_kprintln("stk top : 0x%p", task->stk_base + task->stk_size);
tos_kprintf("\n");
}
__CDECLS_END
#endif /* _TOS_TASK_H_ */

View File

@@ -0,0 +1,48 @@
/*----------------------------------------------------------------------------
* 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_TICK_H_
#define _TOS_TICK_H_
__CDECLS_BEGIN
/**
* @brief Systick interrupt handler.
* systick interrupt handler.
*
* @attention called from the systick interrupt entrance.
*
* @param None
*
* @return None
*/
__API__ void tos_tick_handler(void);
__KNL__ void tick_update(k_tick_t tick);
__KNL__ void tick_list_add(k_task_t *task, k_tick_t timeout);
__KNL__ void tick_list_remove(k_task_t *task);
#if TOS_CFG_TICKLESS_EN > 0u
__KNL__ k_tick_t tick_next_expires_get(void);
#endif
__CDECLS_END
#endif /* _TOS_TICK_H_ */

View File

@@ -0,0 +1,114 @@
/*----------------------------------------------------------------------------
* 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_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
#define TOS_TIME_NOWAIT (k_tick_t)0u
// those two are not for you, for kernel only.
#define K_TIME_MILLISEC_PER_SEC 1000u
#define K_TIME_MAX (k_tick_t)(TOS_TIME_FOREVER - 1)
/**
* @brief Get system tick.
* Get the number of ticks since boot.
*
* @attention None
*
* @param None
*
* @return tick count since boot
*/
__API__ k_tick_t tos_systick_get(void);
/**
* @brief Set system tick.
* Set the number of ticks.
*
* @attention None
*
* @param tick systick count to set
*
* @return None
*/
__API__ void tos_systick_set(k_tick_t tick);
/**
* @brief Convert ticks to milliseconds.
* Convert tick to millisecond.
*
* @attention None
*
* @param[in] tick tick to convert.
*
* @return milliseconds equals to the ticks.
*/
__API__ k_time_t tos_tick2millisec(k_tick_t tick);
/**
* @brief Convert milliseconds to ticks.
* Convert milliseconds to ticks.
*
* @attention None
*
* @param[in] millisec millisecond to convert.
*
* @return ticks equals to the millisecond.
*/
__API__ k_tick_t tos_millisec2tick(k_time_t millisec);
/**
* @brief Sleep current task.
* Sleep for a specified amount of milliseconds.
*
* @attention None
*
* @param[in] millisec amount of milliseconds to delay.
*
* @return errcode
* @retval #K_ERR_DELAY_ZERO millisec is zero.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_sleep_ms(k_time_t millisec);
/**
* @brief Sleep current task.
* Sleep for a specified amount of time.
*
* @attention None
*
* @param[in] hour amount of hours.
* @param[in] minute amount of minutes.
* @param[in] second amount of seconds.
* @param[in] millisec amount of milliseconds.
*
* @return errcode
* @retval #K_ERR_DELAY_ZERO time is zero.
* @retval #K_ERR_NONE return successfully.
*/
__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

@@ -0,0 +1,232 @@
/*----------------------------------------------------------------------------
* 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_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.
#define TOS_OPT_TIMER_ONESHOT (k_opt_t)(0x0001u)
// if we want the timer run periodically, this option should be passed to tos_timer_create.
#define TOS_OPT_TIMER_PERIODIC (k_opt_t)(0x0002u)
typedef enum timer_change_type_en {
TIMER_CHANGE_TYPE_DELAY,
TIMER_CHANGE_TYPE_PERIOD,
} timer_change_type_t;
/**
* state for timer
*/
typedef enum timer_state_en {
TIMER_STATE_UNUSED, /**< the timer has been destroyed */
TIMER_STATE_STOPPED, /**< the timer has been created but not been started, or just be stopped(tos_timer_stop) */
TIMER_STATE_RUNNING, /**< the timer has been created and been started */
TIMER_STATE_COMPLETED /**< the timer has finished its expires, it can only happen when the timer's opt is TOS_OPT_TIMER_ONESHOT */
} timer_state_t;
// callback function type for a timer
typedef void (*k_timer_callback_t)(void *arg);
/**
* timer control block
*/
typedef struct k_timer_st {
knl_obj_t knl_obj; /**< just for verification, test whether current object is really a timer */
k_timer_callback_t cb; /**< callback when time is up */
void *cb_arg; /**< argument for callback */
k_list_t list; /**< list for hooking us to the k_tick_list */
k_tick_t expires; /**< how much time left until time expires */
k_tick_t delay; /**< how much time from now to begin the first run of the timer */
k_tick_t period; /**< if the time expires, how much time after should we begin the next round */
k_opt_t opt; /**< option for the timer, see TOS_OPT_TIMER_* */
timer_state_t state; /**< state for the timer, see TIMER_STATE_* */
} k_timer_t;
typedef struct timer_control_st {
k_tick_t next_expires;
k_list_t list;
} timer_ctl_t;
/**
* @brief Create a timer.
* Create a timer.
*
* @attention I dont't think a timer need a name. If you do, help yourself.
*
* @param[in] tmr pointer to the handler of the timer.
* @param[in] delay time interval for a timer to run.
* @param[in] period period for a timer to restart to run.
* @param[in] callback callback function called when the timer expires.
* @param[in] cb_arg argument for the callback.
* @param[in] opt option for the function call.
*
* @return errcode
* @retval #K_ERR_TIMER_INVALID_PERIOD period is invalid.
* @retval #K_ERR_TIMER_INVALID_DELAY delay is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_create(k_timer_t *tmr, k_tick_t delay, k_tick_t period,
k_timer_callback_t callback, void *cb_arg, k_opt_t opt);
/**
* @brief Delete a timer.
* Delete the timer.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_destroy(k_timer_t *tmr);
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
/**
* @brief Create a dynamic timer.
* Create a timer.
*
* @attention I dont't think a timer need a name. If you do, help yourself.
*
* @param[in] tmr pointer to the handler of the timer pointer.
* @param[in] delay time interval for a timer to run.
* @param[in] period period for a timer to restart to run.
* @param[in] callback callback function called when the timer expires.
* @param[in] cb_arg argument for the callback.
* @param[in] opt option for the function call.
*
* @return errcode
* @retval #K_ERR_TIMER_INVALID_PERIOD period is invalid.
* @retval #K_ERR_TIMER_INVALID_DELAY delay is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_create_dyn(k_timer_t **tmr, k_tick_t delay, k_tick_t period,
k_timer_callback_t callback, void *cb_arg, k_opt_t opt);
/**
* @brief Delete a dynamic timer.
* Delete the timer.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_destroy_dyn(k_timer_t *tmr);
#endif
/**
* @brief Start a timer.
* Start the timer to run.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_TIMER_INVALID_STATE state of the timer is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_start(k_timer_t *tmr);
/**
* @brief Stop a timer.
* Stop the timer from running.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_TIMER_STOPPED the timer is already stoppped.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_stop(k_timer_t *tmr);
/**
* @brief Change a timer's delay.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
* @param[in] delay new delay of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_TIMER_RUNNING the timer is running.
* @retval #K_ERR_TIMER_INVALID_DELAY the delay is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_delay_change(k_timer_t *tmr, k_tick_t delay);
/**
* @brief Change a timer's period.
*
* @attention None
*
* @param[in] tmr pointer to the handler of the timer.
* @param[in] period new period of the timer.
*
* @return errcode
* @retval #K_ERR_TIMER_INACTIVE the timer is not active yet.
* @retval #K_ERR_TIMER_RUNNING the timer is running.
* @retval #K_ERR_TIMER_INVALID_PERIOD the period is invalid.
* @retval #K_ERR_NONE return successfully.
*/
__API__ k_err_t tos_timer_period_change(k_timer_t *tmr, k_tick_t period);
#if TOS_CFG_TIMER_AS_PROC > 0u
/**
* @brief Timer update function.
* When enable timer as a process function not a task, this will be the function entry.
*
* @attention None
*
* @param None
*
* @return None
*/
__KNL__ void soft_timer_update(void);
#endif
__KNL__ k_err_t soft_timer_init(void);
__KNL__ k_tick_t soft_timer_next_expires_get(void);
#endif
__CDECLS_END
#endif /* _TOS_TIMER_H_ */

View File

@@ -0,0 +1,32 @@
/*----------------------------------------------------------------------------
* 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_VERSION_H_
#define _TOS_VERSION_H_
// the format of the tencentos-tiny version is major.minor.patch
// major is updated for major changes that will need user code changes
// minor is updated for minor changes/bug fixes that may need user code changes
// patch is updated for patch changes/bug fixes that should not need user code changes
#define TOS_VERSION_MAJOR 0x02
#define TOS_VERSION_MINOR 0x05
#define TOS_VERSION_PATCH 0x02
#define TOS_VERSION "2.5.2"
#endif /* _TOS_VERSION_H_ */