pte_generic_osal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * pte_cancellable_wait.c
  3. *
  4. * Description:
  5. *
  6. * --------------------------------------------------------------------------
  7. *
  8. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  9. * Copyright(C) 2008 Jason Schmidlapp
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library in the file COPYING.LIB;
  25. * if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  27. */
  28. #ifndef _GENERIC_OS_SUPPORT_H_
  29. #define _GENERIC_OS_SUPPORT_H_
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif // __cplusplus
  33. /** @name Misc */
  34. //@{
  35. typedef enum pte_osResult
  36. {
  37. /** Operation completed successfully */
  38. PTE_OS_OK = 0,
  39. /** Operation failed because there insufficient resources */
  40. PTE_OS_NO_RESOURCES,
  41. /** Operation failed due to a general failure */
  42. PTE_OS_GENERAL_FAILURE,
  43. /** Operation did not complete because a user specified timeout expired. */
  44. PTE_OS_TIMEOUT,
  45. /** The operation was interrupted before it could complete. */
  46. PTE_OS_INTERRUPTED,
  47. /** An invalid parameter was specified */
  48. PTE_OS_INVALID_PARAM
  49. } pte_osResult;
  50. /**
  51. * Provides a hook for the OSAL to implement any OS specific initialization. This is guaranteed to be
  52. * called before any other OSAL function.
  53. */
  54. pte_osResult pte_osInit(void);
  55. //@}
  56. /** @name Mutexes */
  57. //@{
  58. /**
  59. * Creates a mutex
  60. *
  61. * @param pHandle Set to the handle of the newly created mutex.
  62. *
  63. * @return PTE_OS_OK - Mutex successfully created
  64. * @return PTE_OS_NO_RESOURCESs - Insufficient resources to create mutex
  65. */
  66. pte_osResult pte_osMutexCreate(pte_osMutexHandle *pHandle);
  67. /**
  68. * Deletes a mutex and frees any associated resources.
  69. *
  70. * @param handle Handle of mutex to delete.
  71. *
  72. * @return PTE_OS_OK - Mutex successfully deleted.
  73. */
  74. pte_osResult pte_osMutexDelete(pte_osMutexHandle handle);
  75. /**
  76. * Locks the mutex
  77. *
  78. * @param handle Handle of mutex to lock.
  79. *
  80. * @return PTE_OS_OK - Mutex successfully locked.
  81. */
  82. pte_osResult pte_osMutexLock(pte_osMutexHandle handle);
  83. /**
  84. * Locks the mutex, returning after @p timeoutMsecs if the resources is not
  85. * available. Can be used for polling mutex by using @p timeoutMsecs of zero.
  86. *
  87. * @param handle Handle of mutex to lock.
  88. * @param timeoutMsecs Number of milliseconds to wait for resource before returning.
  89. *
  90. * @return PTE_OS_OK - Mutex successfully locked.
  91. * @return PTE_OS_TIMEOUT - Timeout expired before lock was obtained.
  92. */
  93. pte_osResult pte_osMutexTimedLock(pte_osMutexHandle handle, unsigned int timeoutMsecs);
  94. /**
  95. * Unlocks the mutex
  96. *
  97. * @param handle Handle of mutex to unlock
  98. *
  99. * @return PTE_OS_OK - Mutex successfully unlocked.
  100. */
  101. pte_osResult pte_osMutexUnlock(pte_osMutexHandle handle);
  102. //@}
  103. /** @name Threads */
  104. //@{
  105. typedef int (*pte_osThreadEntryPoint)(void *params);
  106. /**
  107. * Creates a new thread. The thread must be started in a suspended state - it will be
  108. * explicitly started when pte_osThreadStart() is called.
  109. *
  110. * @param entryPoint Entry point to the new thread.
  111. * @param stackSize The initial stack size, in bytes. Note that this can be considered a minimum -
  112. * for instance if the OS requires a larger stack space than what the caller specified.
  113. * @param initialPriority The priority that the new thread should be initially set to.
  114. * @param argv Parameter to pass to the new thread.
  115. * @param ppte_osThreadHandle set to the handle of the new thread.
  116. *
  117. * @return PTE_OS_OK - New thread successfully created.
  118. * @return PTE_OS_NO_RESOURCESs - Insufficient resources to create thread
  119. */
  120. pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
  121. int stackSize,
  122. int initialPriority,
  123. void *argv,
  124. pte_osThreadHandle* ppte_osThreadHandle);
  125. /**
  126. * Starts executing the specified thread.
  127. *
  128. * @param osThreadHandle handle of the thread to start.
  129. *
  130. * @return PTE_OS_OK - thread successfully started.
  131. */
  132. pte_osResult pte_osThreadStart(pte_osThreadHandle osThreadHandle);
  133. /**
  134. * Causes the current thread to stop executing.
  135. *
  136. * @return Never returns (thread terminated)
  137. */
  138. void pte_osThreadExit();
  139. /**
  140. * Waits for the specified thread to end. If the thread has already terminated, this returns
  141. * immediately.
  142. *
  143. * @param threadHandle Handle fo thread to wait for.
  144. *
  145. * @return PTE_OS_OK - specified thread terminated.
  146. */
  147. pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle threadHandle);
  148. /**
  149. * Returns the handle of the currently executing thread.
  150. */
  151. pte_osThreadHandle pte_osThreadGetHandle(void);
  152. /**
  153. * Returns the priority of the specified thread.
  154. */
  155. int pte_osThreadGetPriority(pte_osThreadHandle threadHandle);
  156. /**
  157. * Sets the priority of the specified thread.
  158. *
  159. * @return PTE_OS_OK - thread priority successfully set
  160. */
  161. pte_osResult pte_osThreadSetPriority(pte_osThreadHandle threadHandle, int newPriority);
  162. /**
  163. * Frees resources associated with the specified thread. This is called after the thread has terminated
  164. * and is no longer needed (e.g. after pthread_join returns). This call will always be made
  165. * from a different context than that of the target thread.
  166. */
  167. pte_osResult pte_osThreadDelete(pte_osThreadHandle handle);
  168. /**
  169. * Frees resources associated with the specified thread and then causes the thread to exit.
  170. * This is called after the thread has terminated and is no longer needed (e.g. after
  171. * pthread_join returns). This call will always be made from the context of the target thread.
  172. */
  173. pte_osResult pte_osThreadExitAndDelete(pte_osThreadHandle handle);
  174. /**
  175. * Cancels the specified thread. This should cause pte_osSemaphoreCancellablePend() and for pte_osThreadCheckCancel()
  176. * to return @p PTE_OS_INTERRUPTED.
  177. *
  178. * @param threadHandle handle to the thread to cancel.
  179. *
  180. * @return Thread successfully canceled.
  181. */
  182. pte_osResult pte_osThreadCancel(pte_osThreadHandle threadHandle);
  183. /**
  184. * Check if pte_osThreadCancel() has been called on the specified thread.
  185. *
  186. * @param threadHandle handle of thread to check the state of.
  187. *
  188. * @return PTE_OS_OK - Thread has not been cancelled
  189. * @return PTE_OS_INTERRUPTED - Thread has been cancelled.
  190. */
  191. pte_osResult pte_osThreadCheckCancel(pte_osThreadHandle threadHandle);
  192. /**
  193. * Causes the current thread to sleep for the specified number of milliseconds.
  194. */
  195. void pte_osThreadSleep(unsigned int msecs);
  196. /**
  197. * Returns the maximum allowable priority
  198. */
  199. int pte_osThreadGetMaxPriority();
  200. /**
  201. * Returns the minimum allowable priority
  202. */
  203. int pte_osThreadGetMinPriority();
  204. /**
  205. * Returns the priority that should be used if the caller to pthread_create doesn't
  206. * explicitly set one.
  207. */
  208. int pte_osThreadGetDefaultPriority();
  209. //@}
  210. /** @name Semaphores */
  211. //@{
  212. /**
  213. * Creates a semaphore
  214. *
  215. * @param initialValue Initial value of the semaphore
  216. * @param pHandle Set to the handle of the newly created semaphore.
  217. *
  218. * @return PTE_OS_OK - Semaphore successfully created
  219. * @return PTE_OS_NO_RESOURCESs - Insufficient resources to create semaphore
  220. */
  221. pte_osResult pte_osSemaphoreCreate(int initialValue, pte_osSemaphoreHandle *pHandle);
  222. /**
  223. * Deletes a semaphore and frees any associated resources.
  224. *
  225. * @param handle Handle of semaphore to delete.
  226. *
  227. * @return PTE_OS_OK - Semaphore successfully deleted.
  228. */
  229. pte_osResult pte_osSemaphoreDelete(pte_osSemaphoreHandle handle);
  230. /**
  231. * Posts to the semaphore
  232. *
  233. * @param handle Semaphore to release
  234. * @param count Amount to increment the semaphore by.
  235. *
  236. * @return PTE_OS_OK - semaphore successfully released.
  237. */
  238. pte_osResult pte_osSemaphorePost(pte_osSemaphoreHandle handle, int count);
  239. /**
  240. * Acquire a semaphore, returning after @p timeoutMsecs if the semaphore is not
  241. * available. Can be used for polling a semaphore by using @p timeoutMsecs of zero.
  242. *
  243. * @param handle Handle of semaphore to acquire.
  244. * @param pTimeout Pointer to the number of milliseconds to wait to acquire the semaphore
  245. * before returning. If set to NULL, wait forever.
  246. *
  247. * @return PTE_OS_OK - Semaphore successfully acquired.
  248. * @return PTE_OS_TIMEOUT - Timeout expired before semaphore was obtained.
  249. */
  250. pte_osResult pte_osSemaphorePend(pte_osSemaphoreHandle handle, unsigned int *pTimeout);
  251. /**
  252. * Acquire a semaphore, returning after @p timeoutMsecs if the semaphore is not
  253. * available. Can be used for polling a semaphore by using @p timeoutMsecs of zero.
  254. * Call must return immediately if pte_osThreadCancel() is called on the thread waiting for
  255. * the semaphore.
  256. *
  257. * @param handle Handle of semaphore to acquire.
  258. * @param pTimeout Pointer to the number of milliseconds to wait to acquire the semaphore
  259. * before returning. If set to NULL, wait forever.
  260. *
  261. * @return PTE_OS_OK - Semaphore successfully acquired.
  262. * @return PTE_OS_TIMEOUT - Timeout expired before semaphore was obtained.
  263. */
  264. pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle handle, unsigned int *pTimeout);
  265. //@}
  266. /** @name Thread Local Storage */
  267. //@{
  268. /**
  269. * Sets the thread specific value for the specified key for the
  270. * currently executing thread.
  271. *
  272. * @param index The TLS key for the value.
  273. * @param value The value to save
  274. */
  275. pte_osResult pte_osTlsSetValue(unsigned int key, void * value);
  276. /**
  277. * Retrieves the thread specific value for the specified key for
  278. * the currently executing thread. If a value has not been set
  279. * for this key, NULL should be returned (i.e. TLS values default
  280. * to NULL).
  281. *
  282. * @param index The TLS key for the value.
  283. *
  284. * @return The value associated with @p key for the current thread.
  285. */
  286. void * pte_osTlsGetValue(unsigned int key);
  287. /**
  288. * Initializes the OS TLS support. This is called by the PTE library
  289. * prior to performing ANY TLS operation.
  290. */
  291. void pte_osTlsInit(void);
  292. /**
  293. * Allocates a new TLS key.
  294. *
  295. * @param pKey On success will be set to the newly allocated key.
  296. *
  297. * @return PTE_OS_OK - TLS key successfully allocated.
  298. * @return PTE_OS_NO_RESOURCESs - Insufficient resources to allocate key (e.g.
  299. * maximum number of keys reached).
  300. */
  301. pte_osResult pte_osTlsAlloc(unsigned int *pKey);
  302. /**
  303. * Frees the specified TLS key.
  304. *
  305. * @param index TLS key to free
  306. *
  307. * @return PTE_OS_OK - TLS key was successfully freed.
  308. */
  309. pte_osResult pte_osTlsFree(unsigned int key);
  310. //@}
  311. /** @name Atomic operations */
  312. //@{
  313. /**
  314. * Sets the target to the specified value as an atomic operation.
  315. *
  316. * \code
  317. * origVal = *ptarg
  318. * *ptarg = val
  319. * return origVal
  320. * \endcode
  321. *
  322. * @param pTarg Pointer to the value to be exchanged.
  323. * @param val Value to be exchanged
  324. *
  325. * @return original value of destination
  326. */
  327. int pte_osAtomicExchange(int *pTarg, int val);
  328. /**
  329. * Performs an atomic compare-and-exchange oepration on the specified
  330. * value. That is:
  331. *
  332. * \code
  333. * origVal = *pdest
  334. * if (*pdest == comp)
  335. * then *pdest = exchange
  336. * return origVal
  337. * \endcode
  338. *
  339. * @param pdest Pointer to the destination value.
  340. * @param exchange Exchange value (value to set destination to if destination == comparand)
  341. * @param comp The value to compare to destination.
  342. *
  343. * @return Original value of destination
  344. */
  345. int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp);
  346. /**
  347. * Adds the value to target as an atomic operation
  348. *
  349. * \code
  350. * origVal = *pdest
  351. * *pAddend += value
  352. * return origVal
  353. * \endcode
  354. *
  355. * @param pdest Pointer to the variable to be updated.
  356. * @param value Value to be added to the variable.
  357. *
  358. * @return Original value of destination
  359. */
  360. int pte_osAtomicExchangeAdd(int volatile* pdest, int value);
  361. /**
  362. * Decrements the destination.
  363. *
  364. * \code
  365. * origVal = *pdest
  366. * *pdest++
  367. * return origVal
  368. * \endcode
  369. *
  370. * @param pdest Destination value to decrement
  371. *
  372. * @return Original destination value
  373. */
  374. int pte_osAtomicDecrement(int *pdest);
  375. /**
  376. * Increments the destination value
  377. *
  378. * \code
  379. * origVal = *pdest;
  380. * *pdest++;
  381. * return origVal;
  382. */
  383. int pte_osAtomicIncrement(int *pdest);
  384. //@}
  385. struct timeb;
  386. int ftime(struct timeb *tb);
  387. #ifdef __cplusplus
  388. }
  389. #endif // __cplusplus
  390. #endif // _OS_SUPPORT_H_