2024-02-23 17:49:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2024, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2024-01-18 Shell Separate the basic types from rtdef.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __RT_TYPES_H__
|
|
|
|
#define __RT_TYPES_H__
|
|
|
|
|
|
|
|
#include <rtconfig.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#ifndef RT_USING_NANO
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#if defined(RT_USING_SIGNALS) || defined(RT_USING_SMART)
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif /* defined(RT_USING_SIGNALS) || defined(RT_USING_SMART) */
|
|
|
|
#endif /* RT_USING_NANO */
|
|
|
|
|
2024-03-11 01:39:20 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
/**
|
|
|
|
* RT-Thread basic data types definition
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef int rt_bool_t; /**< boolean type */
|
|
|
|
|
|
|
|
#ifndef RT_USING_ARCH_DATA_TYPE
|
|
|
|
#ifdef RT_USING_LIBC
|
|
|
|
typedef int8_t rt_int8_t; /**< 8bit integer type */
|
|
|
|
typedef int16_t rt_int16_t; /**< 16bit integer type */
|
|
|
|
typedef int32_t rt_int32_t; /**< 32bit integer type */
|
|
|
|
typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */
|
|
|
|
typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
|
|
|
|
typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
|
|
|
|
typedef int64_t rt_int64_t; /**< 64bit integer type */
|
|
|
|
typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
|
|
|
|
#else
|
|
|
|
typedef signed char rt_int8_t; /**< 8bit integer type */
|
|
|
|
typedef signed short rt_int16_t; /**< 16bit integer type */
|
|
|
|
typedef signed int rt_int32_t; /**< 32bit integer type */
|
|
|
|
typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
|
|
|
|
typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
|
|
|
|
typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */
|
|
|
|
#ifdef ARCH_CPU_64BIT
|
|
|
|
typedef signed long rt_int64_t; /**< 64bit integer type */
|
|
|
|
typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
|
|
|
|
#else
|
|
|
|
typedef signed long long rt_int64_t; /**< 64bit integer type */
|
|
|
|
typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */
|
|
|
|
#endif /* ARCH_CPU_64BIT */
|
|
|
|
#endif /* RT_USING_LIBC */
|
|
|
|
#endif /* RT_USING_ARCH_DATA_TYPE */
|
|
|
|
|
2024-08-28 20:54:55 +08:00
|
|
|
#ifdef ARCH_CPU_64BIT
|
|
|
|
typedef rt_int64_t rt_base_t; /**< Nbit CPU related data type */
|
|
|
|
typedef rt_uint64_t rt_ubase_t; /**< Nbit unsigned CPU related data type */
|
|
|
|
#else
|
|
|
|
typedef rt_int32_t rt_base_t; /**< Nbit CPU related data type */
|
|
|
|
typedef rt_uint32_t rt_ubase_t; /**< Nbit unsigned CPU related data type */
|
|
|
|
#endif
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
#if defined(RT_USING_LIBC) && !defined(RT_USING_NANO)
|
|
|
|
typedef size_t rt_size_t; /**< Type for size number */
|
|
|
|
typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
|
2024-08-28 20:54:55 +08:00
|
|
|
typedef intptr_t rt_intptr_t; /**< Type for signed pointer length integer */
|
|
|
|
typedef uintptr_t rt_uintptr_t; /**< Type for unsigned pointer length integer */
|
2024-02-23 17:49:15 +08:00
|
|
|
#else
|
|
|
|
typedef rt_ubase_t rt_size_t; /**< Type for size number */
|
|
|
|
typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
|
2024-08-28 20:54:55 +08:00
|
|
|
typedef rt_ubase_t rt_intptr_t; /**< Type for signed pointer length integer */
|
|
|
|
typedef rt_base_t rt_uintptr_t; /**< Type for unsigned pointer length integer */
|
2024-02-23 17:49:15 +08:00
|
|
|
#endif /* defined(RT_USING_LIBC) && !defined(RT_USING_NANO) */
|
|
|
|
|
|
|
|
typedef rt_base_t rt_err_t; /**< Type for error number */
|
|
|
|
typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
|
|
|
|
typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
|
|
|
|
typedef rt_base_t rt_flag_t; /**< Type for flags */
|
|
|
|
typedef rt_ubase_t rt_dev_t; /**< Type for device */
|
|
|
|
typedef rt_base_t rt_off_t; /**< Type for offset */
|
|
|
|
|
2024-08-06 11:20:38 +08:00
|
|
|
#if defined(RT_USING_STDC_ATOMIC) && __STDC_VERSION__ < 201112L
|
|
|
|
#undef RT_USING_STDC_ATOMIC
|
|
|
|
#warning Not using C11 or beyond! Maybe you should change the -std option on your compiler
|
|
|
|
#endif
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
#ifdef __cplusplus
|
2024-04-12 06:48:54 +08:00
|
|
|
typedef rt_base_t rt_atomic_t;
|
2024-02-23 17:49:15 +08:00
|
|
|
#else
|
2024-08-06 11:20:38 +08:00
|
|
|
#if defined(RT_USING_STDC_ATOMIC)
|
2024-04-12 06:48:54 +08:00
|
|
|
#include <stdatomic.h>
|
2024-08-06 11:20:38 +08:00
|
|
|
typedef _Atomic(rt_base_t) rt_atomic_t;
|
|
|
|
#elif defined(RT_USING_HW_ATOMIC)
|
|
|
|
typedef rt_base_t rt_atomic_t;
|
2024-04-12 06:48:54 +08:00
|
|
|
#else
|
|
|
|
typedef rt_base_t rt_atomic_t;
|
|
|
|
#endif /* RT_USING_STDC_ATOMIC */
|
2024-02-23 17:49:15 +08:00
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
|
|
|
/* boolean type definitions */
|
|
|
|
#define RT_TRUE 1 /**< boolean true */
|
|
|
|
#define RT_FALSE 0 /**< boolean fails */
|
|
|
|
|
|
|
|
/* null pointer definition */
|
|
|
|
#define RT_NULL 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Double List structure
|
|
|
|
*/
|
|
|
|
struct rt_list_node
|
|
|
|
{
|
|
|
|
struct rt_list_node *next; /**< point to next node. */
|
|
|
|
struct rt_list_node *prev; /**< point to prev node. */
|
|
|
|
};
|
|
|
|
typedef struct rt_list_node rt_list_t; /**< Type for lists. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Single List structure
|
|
|
|
*/
|
|
|
|
struct rt_slist_node
|
|
|
|
{
|
|
|
|
struct rt_slist_node *next; /**< point to next node. */
|
|
|
|
};
|
|
|
|
typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spinlock
|
|
|
|
*/
|
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
#include <cpuport.h> /* for spinlock from arch */
|
|
|
|
|
|
|
|
struct rt_spinlock
|
|
|
|
{
|
|
|
|
rt_hw_spinlock_t lock;
|
|
|
|
#ifdef RT_USING_DEBUG
|
|
|
|
rt_uint32_t critical_level;
|
|
|
|
#endif /* RT_USING_DEBUG */
|
|
|
|
#if defined(RT_DEBUGING_SPINLOCK)
|
|
|
|
void *owner;
|
|
|
|
void *pc;
|
|
|
|
#endif /* RT_DEBUGING_SPINLOCK */
|
|
|
|
};
|
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
#ifndef RT_SPINLOCK_INIT
|
|
|
|
#define RT_SPINLOCK_INIT {{0}} /* can be overridden by cpuport.h */
|
|
|
|
#endif /* RT_SPINLOCK_INIT */
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
#else /* !RT_USING_SMP */
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
struct rt_spinlock
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_DEBUG
|
|
|
|
rt_uint32_t critical_level;
|
|
|
|
#endif /* RT_USING_DEBUG */
|
|
|
|
rt_ubase_t lock;
|
|
|
|
};
|
|
|
|
#define RT_SPINLOCK_INIT {0}
|
|
|
|
#endif /* RT_USING_SMP */
|
|
|
|
#if defined(RT_DEBUGING_SPINLOCK) && defined(RT_USING_SMP)
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
#define __OWNER_MAGIC ((void *)0xdeadbeaf)
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define __GET_RETURN_ADDRESS __builtin_return_address(0)
|
|
|
|
#else /* !__GNUC__ */
|
|
|
|
#define __GET_RETURN_ADDRESS RT_NULL
|
|
|
|
#endif /* __GNUC__ */
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 10:45:09 +08:00
|
|
|
#define _SPIN_LOCK_DEBUG_OWNER(lock) \
|
|
|
|
do \
|
2024-02-23 17:49:15 +08:00
|
|
|
{ \
|
2024-04-19 10:45:09 +08:00
|
|
|
struct rt_thread *_curthr = rt_thread_self(); \
|
|
|
|
if (_curthr != RT_NULL) \
|
|
|
|
{ \
|
|
|
|
(lock)->owner = _curthr; \
|
|
|
|
(lock)->pc = __GET_RETURN_ADDRESS; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define _SPIN_UNLOCK_DEBUG_OWNER(lock) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
(lock)->owner = __OWNER_MAGIC; \
|
|
|
|
(lock)->pc = RT_NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#else /* !RT_DEBUGING_SPINLOCK */
|
|
|
|
|
2024-04-19 11:27:30 +08:00
|
|
|
#define _SPIN_LOCK_DEBUG_OWNER(lock) RT_UNUSED(lock)
|
|
|
|
#define _SPIN_UNLOCK_DEBUG_OWNER(lock) RT_UNUSED(lock)
|
2024-04-19 10:45:09 +08:00
|
|
|
#endif /* RT_DEBUGING_SPINLOCK */
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 13:13:31 +08:00
|
|
|
#ifdef RT_DEBUGING_CRITICAL
|
2024-04-19 11:27:30 +08:00
|
|
|
#define _SPIN_LOCK_DEBUG_CRITICAL(lock) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
(lock)->critical_level = rt_critical_level(); \
|
2024-04-19 10:45:09 +08:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
(critical) = (lock)->critical_level; \
|
|
|
|
} while (0)
|
|
|
|
|
2024-04-19 13:13:31 +08:00
|
|
|
#else /* !RT_DEBUGING_CRITICAL */
|
2024-04-19 11:27:30 +08:00
|
|
|
#define _SPIN_LOCK_DEBUG_CRITICAL(lock) RT_UNUSED(lock)
|
|
|
|
#define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) do {critical = 0; RT_UNUSED(lock);} while (0)
|
2024-02-23 17:49:15 +08:00
|
|
|
|
2024-04-19 13:13:31 +08:00
|
|
|
#endif /* RT_DEBUGING_CRITICAL */
|
2024-02-23 17:49:15 +08:00
|
|
|
|
|
|
|
#define RT_SPIN_LOCK_DEBUG(lock) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
_SPIN_LOCK_DEBUG_OWNER(lock); \
|
|
|
|
_SPIN_LOCK_DEBUG_CRITICAL(lock); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define RT_SPIN_UNLOCK_DEBUG(lock, critical) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
_SPIN_UNLOCK_DEBUG_OWNER(lock); \
|
|
|
|
_SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
typedef struct rt_spinlock rt_spinlock_t;
|
|
|
|
|
|
|
|
#define RT_DEFINE_SPINLOCK(x) struct rt_spinlock x = RT_SPINLOCK_INIT
|
|
|
|
|
2024-03-11 01:39:20 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
#endif /* __RT_TYPES_H__ */
|