rttypes.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-01-18 Shell Separate the basic types from rtdef.h
  9. */
  10. #ifndef __RT_TYPES_H__
  11. #define __RT_TYPES_H__
  12. #include <rtconfig.h>
  13. #include <stdint.h>
  14. #include <stddef.h>
  15. #include <stdarg.h>
  16. #ifndef RT_USING_NANO
  17. #include <sys/types.h>
  18. #include <sys/errno.h>
  19. #if defined(RT_USING_SIGNALS) || defined(RT_USING_SMART)
  20. #include <sys/signal.h>
  21. #endif /* defined(RT_USING_SIGNALS) || defined(RT_USING_SMART) */
  22. #endif /* RT_USING_NANO */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * RT-Thread basic data types definition
  28. */
  29. #if defined(_WIN64) || defined(__x86_64__)
  30. #ifndef ARCH_CPU_64BIT
  31. #define ARCH_CPU_64BIT
  32. #endif // ARCH_CPU_64BIT
  33. #endif // defined(_WIN64) || defined(__x86_64__)
  34. typedef int rt_bool_t; /**< boolean type */
  35. #ifndef RT_USING_ARCH_DATA_TYPE
  36. #ifdef RT_USING_LIBC
  37. typedef int8_t rt_int8_t; /**< 8bit integer type */
  38. typedef int16_t rt_int16_t; /**< 16bit integer type */
  39. typedef int32_t rt_int32_t; /**< 32bit integer type */
  40. typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */
  41. typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
  42. typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
  43. typedef int64_t rt_int64_t; /**< 64bit integer type */
  44. typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
  45. #else
  46. typedef signed char rt_int8_t; /**< 8bit integer type */
  47. typedef signed short rt_int16_t; /**< 16bit integer type */
  48. typedef signed int rt_int32_t; /**< 32bit integer type */
  49. typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
  50. typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
  51. typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */
  52. #ifdef ARCH_CPU_64BIT
  53. typedef signed long rt_int64_t; /**< 64bit integer type */
  54. typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
  55. #else
  56. typedef signed long long rt_int64_t; /**< 64bit integer type */
  57. typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */
  58. #endif /* ARCH_CPU_64BIT */
  59. #endif /* RT_USING_LIBC */
  60. #endif /* RT_USING_ARCH_DATA_TYPE */
  61. #ifdef ARCH_CPU_64BIT
  62. typedef rt_int64_t rt_base_t; /**< Nbit CPU related data type */
  63. typedef rt_uint64_t rt_ubase_t; /**< Nbit unsigned CPU related data type */
  64. #else
  65. typedef rt_int32_t rt_base_t; /**< Nbit CPU related data type */
  66. typedef rt_uint32_t rt_ubase_t; /**< Nbit unsigned CPU related data type */
  67. #endif
  68. #if defined(RT_USING_LIBC) && !defined(RT_USING_NANO)
  69. typedef size_t rt_size_t; /**< Type for size number */
  70. typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  71. typedef intptr_t rt_intptr_t; /**< Type for signed pointer length integer */
  72. typedef uintptr_t rt_uintptr_t; /**< Type for unsigned pointer length integer */
  73. #else
  74. typedef rt_ubase_t rt_size_t; /**< Type for size number */
  75. typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  76. typedef rt_base_t rt_intptr_t; /**< Type for signed pointer length integer */
  77. typedef rt_ubase_t rt_uintptr_t; /**< Type for unsigned pointer length integer */
  78. #endif /* defined(RT_USING_LIBC) && !defined(RT_USING_NANO) */
  79. typedef rt_base_t rt_err_t; /**< Type for error number */
  80. typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
  81. typedef rt_base_t rt_flag_t; /**< Type for flags */
  82. typedef rt_ubase_t rt_dev_t; /**< Type for device */
  83. typedef rt_base_t rt_off_t; /**< Type for offset */
  84. #if defined(RT_USING_STDC_ATOMIC) && __STDC_VERSION__ < 201112L
  85. #undef RT_USING_STDC_ATOMIC
  86. #warning Not using C11 or beyond! Maybe you should change the -std option on your compiler
  87. #endif
  88. #ifdef __cplusplus
  89. typedef rt_uint8_t rt_atomic8_t;
  90. typedef rt_uint16_t rt_atomic16_t;
  91. typedef rt_base_t rt_atomic_t;
  92. #else
  93. #if defined(RT_USING_STDC_ATOMIC)
  94. #include <stdatomic.h>
  95. typedef _Atomic(rt_uint8_t) rt_atomic8_t;
  96. typedef _Atomic(rt_uint16_t) rt_atomic16_t;
  97. typedef _Atomic(rt_base_t) rt_atomic_t;
  98. #elif defined(RT_USING_HW_ATOMIC)
  99. typedef rt_uint8_t rt_atomic8_t;
  100. typedef rt_uint16_t rt_atomic16_t;
  101. typedef rt_base_t rt_atomic_t;
  102. #else
  103. typedef rt_uint8_t rt_atomic8_t;
  104. typedef rt_uint16_t rt_atomic16_t;
  105. typedef rt_base_t rt_atomic_t;
  106. #endif /* RT_USING_STDC_ATOMIC */
  107. #endif /* __cplusplus */
  108. /* boolean type definitions */
  109. #define RT_TRUE 1 /**< boolean true */
  110. #define RT_FALSE 0 /**< boolean fails */
  111. /* null pointer definition */
  112. #define RT_NULL 0
  113. /**
  114. * Double List structure
  115. */
  116. struct rt_list_node
  117. {
  118. struct rt_list_node *next; /**< point to next node. */
  119. struct rt_list_node *prev; /**< point to prev node. */
  120. };
  121. typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  122. /**
  123. * Single List structure
  124. */
  125. struct rt_slist_node
  126. {
  127. struct rt_slist_node *next; /**< point to next node. */
  128. };
  129. typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
  130. /**
  131. * Lock-less Single List structure
  132. */
  133. struct rt_lockless_slist_node
  134. {
  135. rt_atomic_t next; /**< point to next node. */
  136. };
  137. typedef struct rt_lockless_slist_node rt_ll_slist_t; /**< Type for lock-les single list. */
  138. /**
  139. * Spinlock
  140. */
  141. #ifdef RT_USING_SMP
  142. #include <cpuport.h> /* for spinlock from arch */
  143. struct rt_spinlock
  144. {
  145. rt_hw_spinlock_t lock;
  146. #ifdef RT_USING_DEBUG
  147. rt_uint32_t critical_level;
  148. #endif /* RT_USING_DEBUG */
  149. #if defined(RT_DEBUGING_SPINLOCK)
  150. void *owner;
  151. void *pc;
  152. #endif /* RT_DEBUGING_SPINLOCK */
  153. };
  154. #ifndef RT_SPINLOCK_INIT
  155. #define RT_SPINLOCK_INIT {{0}} /* can be overridden by cpuport.h */
  156. #endif /* RT_SPINLOCK_INIT */
  157. #else /* !RT_USING_SMP */
  158. struct rt_spinlock
  159. {
  160. #ifdef RT_USING_DEBUG
  161. rt_uint32_t critical_level;
  162. #endif /* RT_USING_DEBUG */
  163. rt_ubase_t lock;
  164. };
  165. #define RT_SPINLOCK_INIT {0}
  166. #endif /* RT_USING_SMP */
  167. #if defined(RT_DEBUGING_SPINLOCK) && defined(RT_USING_SMP)
  168. #define __OWNER_MAGIC ((void *)0xdeadbeaf)
  169. #if defined(__GNUC__)
  170. #define __GET_RETURN_ADDRESS __builtin_return_address(0)
  171. #else /* !__GNUC__ */
  172. #define __GET_RETURN_ADDRESS RT_NULL
  173. #endif /* __GNUC__ */
  174. #define _SPIN_LOCK_DEBUG_OWNER(lock) \
  175. do \
  176. { \
  177. struct rt_thread *_curthr = rt_thread_self(); \
  178. if (_curthr != RT_NULL) \
  179. { \
  180. (lock)->owner = _curthr; \
  181. (lock)->pc = __GET_RETURN_ADDRESS; \
  182. } \
  183. } while (0)
  184. #define _SPIN_UNLOCK_DEBUG_OWNER(lock) \
  185. do \
  186. { \
  187. (lock)->owner = __OWNER_MAGIC; \
  188. (lock)->pc = RT_NULL; \
  189. } while (0)
  190. #else /* !RT_DEBUGING_SPINLOCK */
  191. #define _SPIN_LOCK_DEBUG_OWNER(lock) RT_UNUSED(lock)
  192. #define _SPIN_UNLOCK_DEBUG_OWNER(lock) RT_UNUSED(lock)
  193. #endif /* RT_DEBUGING_SPINLOCK */
  194. #ifdef RT_DEBUGING_CRITICAL
  195. #define _SPIN_LOCK_DEBUG_CRITICAL(lock) \
  196. do \
  197. { \
  198. (lock)->critical_level = rt_critical_level(); \
  199. } while (0)
  200. #define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) \
  201. do \
  202. { \
  203. (critical) = (lock)->critical_level; \
  204. } while (0)
  205. #else /* !RT_DEBUGING_CRITICAL */
  206. #define _SPIN_LOCK_DEBUG_CRITICAL(lock) RT_UNUSED(lock)
  207. #define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) do {critical = 0; RT_UNUSED(lock);} while (0)
  208. #endif /* RT_DEBUGING_CRITICAL */
  209. #define RT_SPIN_LOCK_DEBUG(lock) \
  210. do \
  211. { \
  212. _SPIN_LOCK_DEBUG_OWNER(lock); \
  213. _SPIN_LOCK_DEBUG_CRITICAL(lock); \
  214. } while (0)
  215. #define RT_SPIN_UNLOCK_DEBUG(lock, critical) \
  216. do \
  217. { \
  218. _SPIN_UNLOCK_DEBUG_OWNER(lock); \
  219. _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical); \
  220. } while (0)
  221. typedef struct rt_spinlock rt_spinlock_t;
  222. #define RT_DEFINE_SPINLOCK(x) struct rt_spinlock x = RT_SPINLOCK_INIT
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /* __RT_TYPES_H__ */