rttypes.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. typedef int rt_bool_t; /**< boolean type */
  30. typedef signed long rt_base_t; /**< Nbit CPU related date type */
  31. typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */
  32. #ifndef RT_USING_ARCH_DATA_TYPE
  33. #ifdef RT_USING_LIBC
  34. typedef int8_t rt_int8_t; /**< 8bit integer type */
  35. typedef int16_t rt_int16_t; /**< 16bit integer type */
  36. typedef int32_t rt_int32_t; /**< 32bit integer type */
  37. typedef uint8_t rt_uint8_t; /**< 8bit unsigned integer type */
  38. typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
  39. typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
  40. typedef int64_t rt_int64_t; /**< 64bit integer type */
  41. typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
  42. #else
  43. typedef signed char rt_int8_t; /**< 8bit integer type */
  44. typedef signed short rt_int16_t; /**< 16bit integer type */
  45. typedef signed int rt_int32_t; /**< 32bit integer type */
  46. typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */
  47. typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */
  48. typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */
  49. #ifdef ARCH_CPU_64BIT
  50. typedef signed long rt_int64_t; /**< 64bit integer type */
  51. typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
  52. #else
  53. typedef signed long long rt_int64_t; /**< 64bit integer type */
  54. typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */
  55. #endif /* ARCH_CPU_64BIT */
  56. #endif /* RT_USING_LIBC */
  57. #endif /* RT_USING_ARCH_DATA_TYPE */
  58. #if defined(RT_USING_LIBC) && !defined(RT_USING_NANO)
  59. typedef size_t rt_size_t; /**< Type for size number */
  60. typedef ssize_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  61. #else
  62. typedef rt_ubase_t rt_size_t; /**< Type for size number */
  63. typedef rt_base_t rt_ssize_t; /**< Used for a count of bytes or an error indication */
  64. #endif /* defined(RT_USING_LIBC) && !defined(RT_USING_NANO) */
  65. typedef rt_base_t rt_err_t; /**< Type for error number */
  66. typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
  67. typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
  68. typedef rt_base_t rt_flag_t; /**< Type for flags */
  69. typedef rt_ubase_t rt_dev_t; /**< Type for device */
  70. typedef rt_base_t rt_off_t; /**< Type for offset */
  71. #ifdef __cplusplus
  72. typedef rt_base_t rt_atomic_t;
  73. #else
  74. #if defined(RT_USING_HW_ATOMIC)
  75. typedef rt_base_t rt_atomic_t;
  76. #elif defined(RT_USING_STDC_ATOMIC)
  77. #include <stdatomic.h>
  78. typedef atomic_size_t rt_atomic_t;
  79. #else
  80. typedef rt_base_t rt_atomic_t;
  81. #endif /* RT_USING_STDC_ATOMIC */
  82. #endif /* __cplusplus */
  83. /* boolean type definitions */
  84. #define RT_TRUE 1 /**< boolean true */
  85. #define RT_FALSE 0 /**< boolean fails */
  86. /* null pointer definition */
  87. #define RT_NULL 0
  88. /**
  89. * Double List structure
  90. */
  91. struct rt_list_node
  92. {
  93. struct rt_list_node *next; /**< point to next node. */
  94. struct rt_list_node *prev; /**< point to prev node. */
  95. };
  96. typedef struct rt_list_node rt_list_t; /**< Type for lists. */
  97. /**
  98. * Single List structure
  99. */
  100. struct rt_slist_node
  101. {
  102. struct rt_slist_node *next; /**< point to next node. */
  103. };
  104. typedef struct rt_slist_node rt_slist_t; /**< Type for single list. */
  105. /**
  106. * Spinlock
  107. */
  108. #ifdef RT_USING_SMP
  109. #include <cpuport.h> /* for spinlock from arch */
  110. struct rt_spinlock
  111. {
  112. rt_hw_spinlock_t lock;
  113. #ifdef RT_USING_DEBUG
  114. rt_uint32_t critical_level;
  115. #endif /* RT_USING_DEBUG */
  116. #if defined(RT_DEBUGING_SPINLOCK)
  117. void *owner;
  118. void *pc;
  119. #endif /* RT_DEBUGING_SPINLOCK */
  120. };
  121. #ifdef RT_DEBUGING_SPINLOCK
  122. #define __OWNER_MAGIC ((void *)0xdeadbeaf)
  123. #if defined(__GNUC__)
  124. #define __GET_RETURN_ADDRESS __builtin_return_address(0)
  125. #else
  126. #define __GET_RETURN_ADDRESS RT_NULL
  127. #endif
  128. #define _SPIN_LOCK_DEBUG_OWNER(lock) \
  129. do \
  130. { \
  131. struct rt_thread *_curthr = rt_thread_self(); \
  132. if (_curthr != RT_NULL) \
  133. { \
  134. (lock)->owner = _curthr; \
  135. (lock)->pc = __GET_RETURN_ADDRESS; \
  136. } \
  137. } while (0)
  138. #define _SPIN_UNLOCK_DEBUG_OWNER(lock) \
  139. do \
  140. { \
  141. (lock)->owner = __OWNER_MAGIC; \
  142. (lock)->pc = RT_NULL; \
  143. } while (0)
  144. #else
  145. #define _SPIN_LOCK_DEBUG_OWNER(lock)
  146. #define _SPIN_UNLOCK_DEBUG_OWNER(lock)
  147. #endif
  148. #ifdef RT_USING_DEBUG
  149. #define _SPIN_LOCK_DEBUG_CRITICAL(lock) \
  150. do \
  151. { \
  152. struct rt_thread *_curthr = rt_thread_self(); \
  153. if (_curthr != RT_NULL) \
  154. { \
  155. (lock)->critical_level = rt_critical_level(); \
  156. } \
  157. } while (0)
  158. #define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) \
  159. do \
  160. { \
  161. (critical) = (lock)->critical_level; \
  162. } while (0)
  163. #else
  164. #define _SPIN_LOCK_DEBUG_CRITICAL(lock)
  165. #define _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical) (critical = 0)
  166. #endif /* RT_USING_DEBUG */
  167. #define RT_SPIN_LOCK_DEBUG(lock) \
  168. do \
  169. { \
  170. _SPIN_LOCK_DEBUG_OWNER(lock); \
  171. _SPIN_LOCK_DEBUG_CRITICAL(lock); \
  172. } while (0)
  173. #define RT_SPIN_UNLOCK_DEBUG(lock, critical) \
  174. do \
  175. { \
  176. _SPIN_UNLOCK_DEBUG_OWNER(lock); \
  177. _SPIN_UNLOCK_DEBUG_CRITICAL(lock, critical); \
  178. } while (0)
  179. #ifndef RT_SPINLOCK_INIT
  180. #define RT_SPINLOCK_INIT {{0}} /* can be overridden by cpuport.h */
  181. #endif /* RT_SPINLOCK_INIT */
  182. #else
  183. struct rt_spinlock
  184. {
  185. rt_ubase_t lock;
  186. };
  187. #define RT_SPINLOCK_INIT {0}
  188. #endif /* RT_USING_SMP */
  189. typedef struct rt_spinlock rt_spinlock_t;
  190. #define RT_DEFINE_SPINLOCK(x) struct rt_spinlock x = RT_SPINLOCK_INIT
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /* __RT_TYPES_H__ */