pthreadp.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. pthreadp.h
  9. Abstract:
  10. This header contains internal definitions for the POSIX thread library.
  11. Author:
  12. Evan Green 27-Apr-2015
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #include "../libcp.h"
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. //
  22. // ---------------------------------------------------------------- Definitions
  23. //
  24. //
  25. // Define the mask of flags reserved for the mutex type.
  26. //
  27. #define PTHREAD_MUTEX_TYPE_MASK 0x0000000F
  28. //
  29. // This bit is set if the mutex is shared between processes.
  30. //
  31. #define PTHREAD_MUTEX_SHARED 0x00000010
  32. //
  33. // Define the default stack size for a thread.
  34. //
  35. #define PTHREAD_DEFAULT_STACK_SIZE (2 * _1MB)
  36. //
  37. // Define thread flags.
  38. //
  39. //
  40. // This flag is set if the thread has the detached attribute.
  41. //
  42. #define PTHREAD_FLAG_DETACHED 0x00000001
  43. #define PTHREAD_ALLOCATION_TAG 0x72687450
  44. //
  45. // ------------------------------------------------------ Data Type Definitions
  46. //
  47. typedef enum _PTHREAD_THREAD_STATE {
  48. PthreadStateInvalid,
  49. PthreadStateNotJoined,
  50. PthreadStateExited,
  51. PthreadStateJoined,
  52. PthreadStateDetached
  53. } PTHREAD_THREAD_STATE, *PPTHREAD_THREAD_STATE;
  54. typedef
  55. void *
  56. (*PPTHREAD_ENTRY_ROUTINE) (
  57. void *Parameter
  58. );
  59. /*++
  60. Routine Description:
  61. This routine is the entry point prototype for a POSIX thread.
  62. Arguments:
  63. Parameter - Supplies a pointer supplied by the creator of the thread.
  64. Return Value:
  65. Returns a void pointer that can be accessed by calling the join funcion.
  66. --*/
  67. /*++
  68. Structure Description:
  69. This structure stores the internal structure of a mutex.
  70. Members:
  71. State - Stores the state of the mutex.
  72. Owner - Stores the owner of the mutex, used when the recursive
  73. implementation is set.
  74. --*/
  75. typedef struct _PTHREAD_MUTEX {
  76. ULONG State;
  77. UINTN Owner;
  78. } PTHREAD_MUTEX, *PPTHREAD_MUTEX;
  79. /*++
  80. Structure Description:
  81. This structure stores the internal structure of a mutex attribute.
  82. Members:
  83. Flags - Stores the flags for the mutex.
  84. --*/
  85. typedef struct _PTHREAD_MUTEX_ATTRIBUTE {
  86. ULONG Flags;
  87. } PTHREAD_MUTEX_ATTRIBUTE, *PPTHREAD_MUTEX_ATTRIBUTE;
  88. /*++
  89. Structure Description:
  90. This structure stores the internal structure of a condition variable.
  91. Members:
  92. State - Stores the state of the condition variable.
  93. --*/
  94. typedef struct _PTHREAD_CONDITION {
  95. ULONG State;
  96. } PTHREAD_CONDITION, *PPTHREAD_CONDITION;
  97. /*++
  98. Structure Description:
  99. This structure stores the internal structure of a condition variable
  100. attribute.
  101. Members:
  102. Flags - Stores the flags for the condition variable.
  103. --*/
  104. typedef struct _PTHREAD_CONDITION_ATTRIBUTE {
  105. ULONG Flags;
  106. } PTHREAD_CONDITION_ATTRIBUTE, *PPTHREAD_CONDITION_ATTRIBUTE;
  107. /*++
  108. Structure Description:
  109. This structure stores the internal structure of a read/write lock.
  110. Members:
  111. Lock - Stores the OS library RW lock.
  112. --*/
  113. typedef struct _PTHREAD_RWLOCK {
  114. OS_RWLOCK Lock;
  115. } PTHREAD_RWLOCK, *PPTHREAD_RWLOCK;
  116. /*++
  117. Structure Description:
  118. This structure stores the internal structure of a read/write lock
  119. attribute.
  120. Members:
  121. Flags - Stores the flags for the read/write lock.
  122. --*/
  123. typedef struct _PTHREAD_RWLOCK_ATTRIBUTE {
  124. ULONG Flags;
  125. } PTHREAD_RWLOCK_ATTRIBUTE, *PPTHREAD_RWLOCK_ATTRIBUTE;
  126. /*++
  127. Structure Description:
  128. This structure stores the internal structure of a POSIX semaphore.
  129. Members:
  130. State - Stores the current state of the semaphore.
  131. --*/
  132. typedef struct _PTHREAD_SEMAPHORE {
  133. ULONG State;
  134. } PTHREAD_SEMAPHORE, *PPTHREAD_SEMAPHORE;
  135. /*++
  136. Structure Description:
  137. This structure stores the internal structure of a thread attribute.
  138. Members:
  139. Flags - Stores the flags for the thread.
  140. StackBase - Stores a pointer to the stack base.
  141. StackSize - Stores the size of the stack.
  142. GuardSize - Stores the size of the stack guard.
  143. SchedulingPolicy - Stores the thread scheduling policy.
  144. SchedulingPriority - Stores the thread scheduling priority.
  145. --*/
  146. typedef struct _PTHREAD_ATTRIBUTE {
  147. ULONG Flags;
  148. PVOID StackBase;
  149. UINTN StackSize;
  150. UINTN GuardSize;
  151. LONG SchedulingPolicy;
  152. LONG SchedulingPriority;
  153. } PTHREAD_ATTRIBUTE, *PPTHREAD_ATTRIBUTE;
  154. /*++
  155. Structure Description:
  156. This structure stores the internal structure of a thread-specific key
  157. value.
  158. Members:
  159. Sequence - Stores the sequence number associated with this value.
  160. Value - Stores the key value as set by the user.
  161. --*/
  162. typedef struct _PTHREAD_KEY_DATA {
  163. UINTN Sequence;
  164. PVOID Value;
  165. } PTHREAD_KEY_DATA, *PPTHREAD_KEY_DATA;
  166. /*++
  167. Structure Description:
  168. This structure stores the internal structure of a POSIX thread barrier.
  169. Members:
  170. State - Stores the current state of the barrier.
  171. ThreadCount - Stores the thread count that must be reached before waits on
  172. this barrier are satisified. This is set when the barrier is
  173. initialized.
  174. WaitingThreadCount - Stores the number of threads that are currently
  175. waiting on the barrier. This is reset once a wait is satisfied.
  176. Mutex - Stores the mutex that synchronizes access to the waiting thread
  177. count and the state.
  178. --*/
  179. typedef struct _PTHREAD_BARRIER {
  180. ULONG State;
  181. ULONG ThreadCount;
  182. ULONG WaitingThreadCount;
  183. pthread_mutex_t Mutex;
  184. } PTHREAD_BARRIER, *PPTHREAD_BARRIER;
  185. /*++
  186. Structure Description:
  187. This structure stores the internal structure of a barrier attribute.
  188. Members:
  189. Flags - Stores the flags for the barrier.
  190. --*/
  191. typedef struct _PTHREAD_BARRIER_ATTRIBUTE {
  192. ULONG Flags;
  193. } PTHREAD_BARRIER_ATTRIBUTE, *PPTHREAD_BARRIER_ATTRIBUTE;
  194. /*++
  195. Structure Description:
  196. This structure stores the internal structure of a thread.
  197. Members:
  198. ListEntry - Stores pointers to the next and previous threads in the global
  199. list.
  200. Attribute - Stores the thread attributes.
  201. ThreadRoutine - Stores the thread routine to call.
  202. ThreadParameter - Stores the thread parameter.
  203. ReturnValue - Stores the return value from the thread.
  204. ThreadAllocation - Stores the pointer to the allocation for this structure
  205. and perhaps the stack.
  206. ThreadAllocationSize - Stores the size of the allocation for this structure
  207. and perhaps the stack.
  208. StartMutex - Stores a mutex used to hold up the new thread until it is
  209. fully initialized.
  210. ThreadId - Stores the kernel thread identifier.
  211. State - Stores the state of the thread. This is of type
  212. PTHREAD_THREAD_STATE, but is defined as a 32-bit value so atomic
  213. operations can be done on it.
  214. CleanupStack - Stores a pointer to the top of the stack of cleanup routines
  215. to call.
  216. CancelState - Stores the current cancellation state. This is either
  217. PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE.
  218. CancelType - Stores the cancellation type. This is either
  219. PTHREAD_CANCEL_ASYNCHRONOUS or PTHREAD_CANCEL_DEFERRED.
  220. CancelRequested - Stores a boolean indicating whether a cancellation has
  221. been requested or not.
  222. KeyData - Stores a pointer to the key data for this thread
  223. OsData - Stores a pointer to the thread control data allocated by the OS
  224. library.
  225. SignalMask - Stores the original signal mask to restore once the thread
  226. is initialized. The thread starts with all signals blocked.
  227. --*/
  228. typedef struct _PTHREAD {
  229. LIST_ENTRY ListEntry;
  230. PTHREAD_ATTRIBUTE Attribute;
  231. PPTHREAD_ENTRY_ROUTINE ThreadRoutine;
  232. PVOID ThreadParameter;
  233. PVOID ReturnValue;
  234. PVOID ThreadAllocation;
  235. UINTN ThreadAllocationSize;
  236. pthread_mutex_t StartMutex;
  237. THREAD_ID ThreadId;
  238. ULONG State;
  239. __pthread_cleanup_t *CleanupStack;
  240. ULONG CancelState;
  241. ULONG CancelType;
  242. BOOL CancelRequested;
  243. PPTHREAD_KEY_DATA KeyData;
  244. PVOID OsData;
  245. SIGNAL_SET SignalMask;
  246. } PTHREAD, *PPTHREAD;
  247. //
  248. // -------------------------------------------------------------------- Globals
  249. //
  250. //
  251. // Store the global thread list.
  252. //
  253. extern LIST_ENTRY ClThreadList;
  254. extern pthread_mutex_t ClThreadListMutex;
  255. //
  256. // -------------------------------------------------------- Function Prototypes
  257. //
  258. void
  259. ClpSetIdSignalHandler (
  260. int Signal
  261. );
  262. /*++
  263. Routine Description:
  264. This routine is a signal handler called to fix up the user identity on a
  265. thread.
  266. Arguments:
  267. Signal - Supplies the signal that caused this handler to be invoked.
  268. Return Value:
  269. None.
  270. --*/
  271. VOID
  272. ClpDestroyThreadKeyData (
  273. PPTHREAD Thread
  274. );
  275. /*++
  276. Routine Description:
  277. This routine destroys the thread key data for the given thread and calls
  278. all destructor routines.
  279. Arguments:
  280. Thread - Supplies a pointer to the thread that is exiting.
  281. Return Value:
  282. None.
  283. --*/
  284. ULONG
  285. ClpConvertAbsoluteTimespecToRelativeMilliseconds (
  286. const struct timespec *AbsoluteTime,
  287. int Clock
  288. );
  289. /*++
  290. Routine Description:
  291. This routine converts an absolute timespec structure into a number of
  292. milliseconds from now.
  293. Arguments:
  294. AbsoluteTime - Supplies a pointer to the absolute timespec to convert.
  295. Clock - Supplies the clock to query from.
  296. Return Value:
  297. Returns the number of milliseconds from now the timespec expires in.
  298. 0 if the absolute time is in the past.
  299. --*/