rttypes.h 7.8 KB

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