Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vcos_thread.h 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*=============================================================================
  26. VideoCore OS Abstraction Layer - public header file
  27. =============================================================================*/
  28. #ifndef VCOS_THREAD_H
  29. #define VCOS_THREAD_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include "vcos_types.h"
  34. #include "pthreads/vcos_platform.h"
  35. /**
  36. * \file vcos_thread.h
  37. *
  38. * \section thread Threads
  39. *
  40. * Under Nucleus, a thread is created by NU_Create_Task, passing in the stack
  41. * and various other parameters. To stop the thread, NU_Terminate_Thread() and
  42. * NU_Delete_Thread() are called.
  43. *
  44. * Unfortunately it's not possible to emulate this API under some fairly common
  45. * operating systems. Under Windows you can't pass in the stack, and you can't
  46. * safely terminate a thread.
  47. *
  48. * Therefore, an API which is similar to the pthreads API is used instead. This
  49. * API can (mostly) be emulated under all interesting operating systems.
  50. *
  51. * Obviously this makes the code somewhat more complicated on VideoCore than it
  52. * would otherwise be - we end up with an extra mutex per thread, and some code
  53. * that waits for it. The benefit is that we have a single way of creating
  54. * threads that works consistently on all platforms (apart from stack supplying).
  55. *
  56. * \subsection stack Stack
  57. *
  58. * It's still not possible to pass in the stack address, but this can be made
  59. * much more obvious in the API: the relevant function is missing and the
  60. * CPP symbol VCOS_CAN_SET_STACK_ADDR is zero rather than one.
  61. *
  62. * \subsection thr_create Creating a thread
  63. *
  64. * The simplest way to create a thread is with vcos_thread_create() passing in a
  65. * NULL thread parameter argument. To wait for the thread to exit, call
  66. * vcos_thread_join().
  67. *
  68. * \subsection back Backward compatibility
  69. *
  70. * To ease migration, a "classic" thread creation API is provided for code
  71. * that used to make use of Nucleus, vcos_thread_create_classic(). The
  72. * arguments are not exactly the same, as the PREEMPT parameter is dropped.
  73. *
  74. */
  75. #define VCOS_AFFINITY_CPU0 _VCOS_AFFINITY_CPU0
  76. #define VCOS_AFFINITY_CPU1 _VCOS_AFFINITY_CPU1
  77. #define VCOS_AFFINITY_MASK _VCOS_AFFINITY_MASK
  78. #define VCOS_AFFINITY_DEFAULT _VCOS_AFFINITY_DEFAULT
  79. #define VCOS_AFFINITY_THISCPU _VCOS_AFFINITY_THISCPU
  80. /** Report whether or not we have an RTOS at all, and hence the ability to
  81. * create threads.
  82. */
  83. VCOSPRE_ int VCOSPOST_ vcos_have_rtos(void);
  84. /** Create a thread. It must be cleaned up by calling vcos_thread_join().
  85. *
  86. * @param thread Filled in on return with thread
  87. * @param name A name for the thread. May be the empty string.
  88. * @param attrs Attributes; default attributes will be used if this is NULL.
  89. * @param entry Entry point.
  90. * @param arg Argument passed to the entry point.
  91. */
  92. VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_thread_create(VCOS_THREAD_T *thread,
  93. const char *name,
  94. VCOS_THREAD_ATTR_T *attrs,
  95. VCOS_THREAD_ENTRY_FN_T entry,
  96. void *arg);
  97. /** Exit the thread from within the thread function itself.
  98. * Resources must still be cleaned up via a call to thread_join().
  99. *
  100. * The thread can also be terminated by simply exiting the thread function.
  101. *
  102. * @param data Data passed to thread_join. May be NULL.
  103. */
  104. VCOSPRE_ void VCOSPOST_ vcos_thread_exit(void *data);
  105. /** Wait for a thread to terminate and then clean up its resources.
  106. *
  107. * @param thread Thread to wait for
  108. * @param pData Updated to point at data provided in vcos_thread_exit or exit
  109. * code of thread function.
  110. */
  111. VCOSPRE_ void VCOSPOST_ vcos_thread_join(VCOS_THREAD_T *thread,
  112. void **pData);
  113. /**
  114. * \brief Create a thread using an API similar to the one "traditionally"
  115. * used under Nucleus.
  116. *
  117. * This creates a thread which must be cleaned up by calling vcos_thread_join().
  118. * The thread cannot be simply terminated (as in Nucleus and ThreadX) as thread
  119. * termination is not universally supported.
  120. *
  121. * @param thread Filled in with thread instance
  122. * @param name An optional name for the thread. NULL or "" may be used (but
  123. * a name will aid in debugging).
  124. * @param entry Entry point
  125. * @param arg A single argument passed to the entry point function
  126. * @param stack Pointer to stack address
  127. * @param stacksz Size of stack in bytes
  128. * @param priaff Priority of task, between VCOS_PRI_LOW and VCOS_PRI_HIGH, ORed with the CPU affinity
  129. * @param autostart If non-zero the thread will start immediately.
  130. * @param timeslice Timeslice (system ticks) for this thread.
  131. *
  132. * @sa vcos_thread_terminate vcos_thread_delete
  133. */
  134. VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_thread_create_classic(VCOS_THREAD_T *thread,
  135. const char *name,
  136. void *(*entry)(void *arg),
  137. void *arg,
  138. void *stack,
  139. VCOS_UNSIGNED stacksz,
  140. VCOS_UNSIGNED priaff,
  141. VCOS_UNSIGNED timeslice,
  142. VCOS_UNSIGNED autostart);
  143. /**
  144. * \brief Set a thread's priority
  145. *
  146. * Set the priority for a thread.
  147. *
  148. * @param thread The thread
  149. * @param pri Thread priority in VCOS_PRI_MASK bits; affinity in VCOS_AFFINITY_MASK bits.
  150. */
  151. VCOS_INLINE_DECL
  152. void vcos_thread_set_priority(VCOS_THREAD_T *thread, VCOS_UNSIGNED pri);
  153. /**
  154. * \brief Return the currently executing thread.
  155. *
  156. */
  157. VCOS_INLINE_DECL
  158. VCOS_THREAD_T *vcos_thread_current(void);
  159. /**
  160. * \brief Return the thread's priority.
  161. */
  162. VCOS_INLINE_DECL
  163. VCOS_UNSIGNED vcos_thread_get_priority(VCOS_THREAD_T *thread);
  164. /**
  165. * \brief Return the thread's cpu affinity.
  166. */
  167. VCOS_INLINE_DECL
  168. VCOS_UNSIGNED vcos_thread_get_affinity(VCOS_THREAD_T *thread);
  169. /**
  170. * \brief Set the thread's cpu affinity.
  171. */
  172. VCOS_INLINE_DECL
  173. void vcos_thread_set_affinity(VCOS_THREAD_T *thread, VCOS_UNSIGNED affinity);
  174. /**
  175. * \brief Query whether we are in an interrupt.
  176. *
  177. * @return 1 if in interrupt context.
  178. */
  179. VCOS_INLINE_DECL
  180. int vcos_in_interrupt(void);
  181. /**
  182. * \brief Sleep a while.
  183. *
  184. * @param ms Number of milliseconds to sleep for
  185. *
  186. * This may actually sleep a whole number of ticks.
  187. */
  188. VCOS_INLINE_DECL
  189. void vcos_sleep(uint32_t ms);
  190. /**
  191. * \brief Return the value of the hardware microsecond counter.
  192. *
  193. */
  194. VCOS_INLINE_DECL
  195. uint32_t vcos_getmicrosecs(void);
  196. VCOS_INLINE_DECL
  197. uint64_t vcos_getmicrosecs64(void);
  198. #define vcos_get_ms() (vcos_getmicrosecs()/1000)
  199. /**
  200. * \brief Return a unique identifier for the current process
  201. *
  202. */
  203. VCOS_INLINE_DECL
  204. VCOS_UNSIGNED vcos_process_id_current(void);
  205. /** Relinquish this time slice. */
  206. VCOS_INLINE_DECL
  207. void vcos_thread_relinquish(void);
  208. /** Return the name of the given thread.
  209. */
  210. VCOSPRE_ const char * VCOSPOST_ vcos_thread_get_name(const VCOS_THREAD_T *thread);
  211. /** Change preemption. This is almost certainly not what you want, as it won't
  212. * work reliably in a multicore system: although you can affect the preemption
  213. * on *this* core, you won't affect what's happening on the other core(s).
  214. *
  215. * It's mainly here to ease migration. If you're using it in new code, you
  216. * probably need to think again.
  217. *
  218. * @param pe New preemption, VCOS_PREEMPT or VCOS_NO_PREEMPT
  219. * @return Old value of preemption.
  220. */
  221. VCOS_INLINE_DECL
  222. VCOS_UNSIGNED vcos_change_preemption(VCOS_UNSIGNED pe);
  223. /** Is a thread still running, or has it exited?
  224. *
  225. * Note: this exists for some fairly scary code in the video codec tests. Don't
  226. * try to use it for anything else, as it may well not do what you expect.
  227. *
  228. * @param thread thread to query
  229. * @return non-zero if thread is running, or zero if it has exited.
  230. */
  231. VCOS_INLINE_DECL
  232. int vcos_thread_running(VCOS_THREAD_T *thread);
  233. /** Resume a thread.
  234. *
  235. * @param thread thread to resume
  236. */
  237. VCOS_INLINE_DECL
  238. void vcos_thread_resume(VCOS_THREAD_T *thread);
  239. /*
  240. * Internal APIs - may not always be present and should not be used in
  241. * client code.
  242. */
  243. extern void _vcos_task_timer_set(void (*pfn)(void*), void *, VCOS_UNSIGNED ms);
  244. extern void _vcos_task_timer_cancel(void);
  245. #ifdef __cplusplus
  246. }
  247. #endif
  248. #endif