dsi_thread.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. #if !defined(DSI_THREAD_H)
  9. #define DSI_THREAD_H
  10. #include "types.h"
  11. #if defined(DSI_TYPES_WINDOWS)
  12. #include <windows.h>
  13. #elif defined(DSI_TYPES_MACINTOSH) || defined(DSI_TYPES_LINUX)
  14. #define _GNU_SOURCE
  15. #include <pthread.h>
  16. #endif
  17. //////////////////////////////////////////////////////////////////////////////////
  18. // Public Definitions
  19. //////////////////////////////////////////////////////////////////////////////////
  20. #define DSI_THREAD_INFINITE MAX_ULONG
  21. // Error codes.
  22. #define DSI_THREAD_ENONE ((UCHAR) 0x00)
  23. #define DSI_THREAD_ETIMEDOUT ((UCHAR) 0x01)
  24. #define DSI_THREAD_EBUSY ((UCHAR) 0x02)
  25. #define DSI_THREAD_ENOTFOUND ((UCHAR) 0x03)
  26. #define DSI_THREAD_EINVALID ((UCHAR) 0x04)
  27. #define DSI_THREAD_EOTHER ((UCHAR) 0xFF)
  28. typedef void * DSI_THREAD_RETURN;
  29. #if defined(DSI_TYPES_WINDOWS)
  30. //#if (INFINITE != MAX_ULONG)
  31. //#error "!!! INFINITE is not defined as expected in the windows headers !!!"
  32. //#endif
  33. typedef struct
  34. {
  35. // All members are for internal use only. Do not modify!
  36. LONG iWaitersCount;
  37. CRITICAL_SECTION stStartWaiterCritSec;
  38. HANDLE hBroadcastDoneEvent;
  39. HANDLE ahBcastSmph0SignalEvnt1[2]; //In an array for the WaitForMultipleObjects call
  40. } DSI_CONDITION_VAR;
  41. typedef HANDLE DSI_MUTEX;
  42. typedef HANDLE DSI_THREAD_ID;
  43. typedef DWORD DSI_THREAD_IDNUM;
  44. #elif defined(DSI_TYPES_MACINTOSH) || defined(DSI_TYPES_LINUX)
  45. typedef pthread_cond_t DSI_CONDITION_VAR;
  46. typedef pthread_mutex_t DSI_MUTEX;
  47. typedef pthread_t DSI_THREAD_ID;
  48. typedef pthread_t DSI_THREAD_IDNUM;
  49. #endif
  50. //////////////////////////////////////////////////////////////////////////////////
  51. // Public Function Prototypes
  52. //////////////////////////////////////////////////////////////////////////////////
  53. #if defined(__cplusplus)
  54. extern "C" {
  55. #endif
  56. UCHAR DSIThread_MutexInit(DSI_MUTEX *pstMutex_);
  57. ////////////////////////////////////////////////////////////////////
  58. // Initializes a mutex object.
  59. // Parameters:
  60. // *pstMutex_: A pointer to a mutex object to be
  61. // initialized.
  62. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  63. // the error code.
  64. ////////////////////////////////////////////////////////////////////
  65. UCHAR DSIThread_MutexDestroy(DSI_MUTEX *pstMutex_);
  66. ////////////////////////////////////////////////////////////////////
  67. // Cleans up a mutex object that is no longer required.
  68. // Parameters:
  69. // *pstMutex_: A pointer to a mutex object to be cleaned
  70. // up.
  71. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  72. // the error code.
  73. ////////////////////////////////////////////////////////////////////
  74. UCHAR DSIThread_MutexLock(DSI_MUTEX *pstMutex_);
  75. ////////////////////////////////////////////////////////////////////
  76. // Locks a mutex. If the mutex is already locked, the calling
  77. // thread blocks until the mutex becomes available.
  78. // Parameters:
  79. // *pstMutex_: A pointer to a mutex object to be locked.
  80. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  81. // the error code.
  82. ////////////////////////////////////////////////////////////////////
  83. UCHAR DSIThread_MutexTryLock(DSI_MUTEX *pstMutex_);
  84. ////////////////////////////////////////////////////////////////////
  85. // Locks a mutex if it is not locked. The calling thread returns
  86. // immediately.
  87. // Parameters:
  88. // *pstMutex_: A pointer to a mutex object to be locked.
  89. // Returns DSI_THREAD_ENONE if successful. If the mutex object is
  90. // already locked, DSI_THREAD_EBUSY is returned. If there is an
  91. // error, DSI_THREAD_EOTHER is returned.
  92. ////////////////////////////////////////////////////////////////////
  93. UCHAR DSIThread_MutexUnlock(DSI_MUTEX *pstMutex_);
  94. ////////////////////////////////////////////////////////////////////
  95. // Unlocks a mutex that is already locked by the calling thread.
  96. // Parameters:
  97. // *pstMutex_: A pointer to a mutex object to be
  98. // unlocked.
  99. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  100. // the error code.
  101. ////////////////////////////////////////////////////////////////////
  102. UCHAR DSIThread_CondInit(DSI_CONDITION_VAR *pstConditionVariable_);
  103. ////////////////////////////////////////////////////////////////////
  104. // Initializes a condition variable.
  105. // Parameters:
  106. // *pstConditionVariable_: A pointer to a condition variable
  107. // object to be initialized.
  108. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  109. // the error code.
  110. ////////////////////////////////////////////////////////////////////
  111. UCHAR DSIThread_CondDestroy(DSI_CONDITION_VAR *pstConditionVariable_);
  112. ////////////////////////////////////////////////////////////////////
  113. // Cleans up a condition variable that is no longer required.
  114. // Parameters:
  115. // *pstConditionVariable_: A pointer to a condition variable
  116. // object to be cleaned up.
  117. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  118. // the error code.
  119. ////////////////////////////////////////////////////////////////////
  120. UCHAR DSIThread_CondTimedWait(DSI_CONDITION_VAR *pstConditionVariable_, DSI_MUTEX *pstExternalMutex_, ULONG ulMilliseconds_);
  121. ////////////////////////////////////////////////////////////////////
  122. // Waits for a condition variable to be signaled from another
  123. // thread.
  124. // Parameters:
  125. // *pstConditionVariable_: A pointer to a condition variable
  126. // object to be waited on.
  127. // *pstExternalMutex_: A pointer to a mutex object that must be
  128. // locked by the calling thread at some
  129. // point prior to calling this function.
  130. // ulMilliseconds_: The minimum time to allow before
  131. // cancelling the wait. If this parameter
  132. // is set to DSI_THREAD_INFINTE, this
  133. // function blocks indefinitely or until the
  134. // condition variable is signaled by
  135. // another thread.
  136. // Returns DSI_THREAD_ENONE if successful. Returns
  137. // DSI_THREAD_ETIMEDOUT if the time in ulMilliseconds_ expired
  138. // before receiving a signal. If there is an error,
  139. // DSI_THREAD_EOTHER is returned.
  140. ////////////////////////////////////////////////////////////////////
  141. UCHAR DSIThread_CondSignal(DSI_CONDITION_VAR *pstConditionVariable_);
  142. ////////////////////////////////////////////////////////////////////
  143. // Unblocks at least one of the threads that are waiting on the
  144. // specified condition variable, if any threads are waiting. If no
  145. // threads are waiting, this function has no effect.
  146. // Parameters:
  147. // *pstConditionVariable_: A pointer to a condition variable
  148. // object to be signaled.
  149. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  150. // the error code.
  151. ////////////////////////////////////////////////////////////////////
  152. UCHAR DSIThread_CondBroadcast(DSI_CONDITION_VAR *pstConditionVariable_);
  153. ////////////////////////////////////////////////////////////////////
  154. // Unblocks all of the threads that are waiting on the specified
  155. // condition variable, if any threads are waiting. If no threads
  156. // are waiting, this function has no effect.
  157. // Parameters:
  158. // *pstConditionVariable_: A pointer to a condition variable
  159. // object to be signaled.
  160. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  161. // the error code.
  162. ////////////////////////////////////////////////////////////////////
  163. DSI_THREAD_ID DSIThread_CreateThread(DSI_THREAD_RETURN (*fnThreadStart_)(void *), void *pvParameter_);
  164. ////////////////////////////////////////////////////////////////////
  165. // Creates a thread to begin execution at any time after this
  166. // function is called.
  167. // Parameters:
  168. // *fnThreadStart_: A pointer to a function that will become
  169. // the start routine of the new thread. The
  170. // function should be declared as follows:
  171. // DSI_THREAD_RETURN FunctionName(void *pvParameter_);
  172. // *pvParameter_: A parameter that will be passed into the
  173. // the start routine.
  174. // Returns a non-zero thread ID if successfull. Otherwise, it
  175. // returns NULL.
  176. ////////////////////////////////////////////////////////////////////
  177. UCHAR DSIThread_DestroyThread(DSI_THREAD_ID hThreadID);
  178. ////////////////////////////////////////////////////////////////////
  179. // Kills the thread specified.
  180. // Parameters:
  181. // hThreadID: ID of the thread to destroy.
  182. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  183. // the error code.
  184. // NOTE: This function will destroy the thread without letting it
  185. // clean up!
  186. ////////////////////////////////////////////////////////////////////
  187. UCHAR DSIThread_ReleaseThreadID(DSI_THREAD_ID hThreadID);
  188. ////////////////////////////////////////////////////////////////////
  189. // Gives up the thread handle to the operating system.
  190. // Does not affect the execution of the thread.
  191. // Parameters:
  192. // hThreadID: ID of the thread to destroy.
  193. // Returns DSI_THREAD_ENONE if successful. Otherwise, it returns
  194. // the error code.
  195. ////////////////////////////////////////////////////////////////////
  196. DSI_THREAD_IDNUM DSIThread_GetCurrentThreadIDNum(void);
  197. ////////////////////////////////////////////////////////////////////
  198. // Gets the current thread ID number of the caller.
  199. //
  200. // Returns the current thread ID of the caller.
  201. // Does not return any error codes.
  202. ////////////////////////////////////////////////////////////////////
  203. BOOL DSIThread_CompareThreads(DSI_THREAD_IDNUM hThreadIDNum1, DSI_THREAD_IDNUM hThreadIDNum2);
  204. ////////////////////////////////////////////////////////////////////
  205. // Compares two thread ID numbers.
  206. // Parameters:
  207. // hThreadIDNum1/hThreadIDNum2: the two thread ID numbers to be
  208. // compared.
  209. // Returns TRUE if the two threads are the same.
  210. // Does not return any error codes.
  211. // NOTE: Behaviour is undefined if either thread id numbers are
  212. // invalid.
  213. ////////////////////////////////////////////////////////////////////
  214. ULONG DSIThread_GetSystemTime(void);
  215. ////////////////////////////////////////////////////////////////////
  216. // Gets the current system time in milliseconds.
  217. //
  218. // Returns the current system time in milliseconds.
  219. ////////////////////////////////////////////////////////////////////
  220. BOOL DSIThread_GetWorkingDirectory(UCHAR* pucDirectory, USHORT usLength);
  221. ////////////////////////////////////////////////////////////////////
  222. // Gets the current working directory.
  223. // Parameters:
  224. // pucDirectory: the current working directory of the system
  225. // usLength: the length of the directory's string
  226. //
  227. // When successful, pucDirectory will hold the current working
  228. // directory, else pucDirectory will be undefined.
  229. //
  230. // Returns TRUE if successful.
  231. ////////////////////////////////////////////////////////////////////
  232. void DSIThread_Sleep(ULONG ulMilliseconds);
  233. ////////////////////////////////////////////////////////////////////
  234. // Puts the current thread to sleep for the number of milliseconds
  235. // specified.
  236. // Parameters:
  237. // ulMilliseconds: Number of milliseconds to sleep.
  238. ////////////////////////////////////////////////////////////////////
  239. #if defined(__cplusplus)
  240. }
  241. #endif
  242. #endif // !defined(DSI_THREAD_H)