thread.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. * -
  33. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  34. *
  35. * This file is distributed in the hope that it will be useful, but WITHOUT
  36. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  37. * FITNESS FOR A PARTICULAR PURPOSE.
  38. *
  39. * You can redistribute this file and/or modify it under the terms of the GNU
  40. * General Public License (GPL) as published by the Free Software Foundation;
  41. * either version 2 of the License, or (at your discretion) any later version.
  42. * See the accompanying file "copying-gpl.txt" for more details.
  43. *
  44. * As a special exception to the GPL, permission is granted for additional
  45. * uses of the text contained in this file. See the accompanying file
  46. * "copying-liquorice.txt" for details.
  47. */
  48. /*!
  49. * \file os/thread.c
  50. * \brief Multi-threading support.
  51. *
  52. * This kernel module implements the platform independent part of the Nut/OS
  53. * cooperative multi-threading.
  54. *
  55. * \verbatim File version $Id: thread.c 5472 2013-12-06 00:16:28Z olereinhardt $ \endverbatim
  56. */
  57. #include <cfg/os.h>
  58. #include <cfg/memory.h>
  59. #include <string.h>
  60. #include <sys/types.h>
  61. #include <sys/heap.h>
  62. #include <sys/atom.h>
  63. #include <sys/timer.h>
  64. #include <sys/event.h>
  65. #include <sys/thread.h>
  66. #include <sys/nutdebug.h>
  67. #include <sys/osdebug.h>
  68. #ifdef NUTTRACER
  69. #include <sys/tracer.h>
  70. #endif
  71. /*!
  72. * \addtogroup xgThread
  73. */
  74. /*@{*/
  75. #if defined(NUT_CRITICAL_NESTING) && !defined(NUT_CRITICAL_NESTING_STACK)
  76. unsigned int critical_nesting_level;
  77. #endif
  78. #ifdef __NUT_EMULATION__
  79. // prototype
  80. extern void NutUnixThreadYieldHook(void); // from unix_nutinit.c
  81. #endif
  82. NUTTHREADINFO * runningThread;
  83. /*!
  84. * \brief Thread to be killed.
  85. *
  86. * Pointer to the NUTTHREADINFO structure of the latest
  87. * killed thread.
  88. *
  89. * \todo Should be static, because it's locally used only.
  90. */
  91. NUTTHREADINFO * killedThread;
  92. NUTTHREADINFO * nutThreadList;
  93. NUTTHREADINFO * runQueue;
  94. void NutThreadAddPriQueue(NUTTHREADINFO * td, NUTTHREADINFO * volatile *tqpp)
  95. {
  96. NUTTHREADINFO *tqp;
  97. NUTASSERT(td != NULL);
  98. td->td_queue = (HANDLE) tqpp;
  99. td->td_qpec = 0; // start with clean event count
  100. /*
  101. * Be most careful not to override an intermediate event from interrupt
  102. * context, which may change a queue from empty to signaled state. Many
  103. * thanks to Michael Jones, who detected and corrected this bug.
  104. */
  105. NutEnterCritical();
  106. tqp = *tqpp;
  107. if (tqp == SIGNALED) {
  108. tqp = 0;
  109. td->td_qpec++; // transfer the signaled state
  110. } else if (tqp) {
  111. NutExitCritical(); // there are other threads in queue
  112. // so its save to leave critical.
  113. while (tqp && tqp->td_priority <= td->td_priority) {
  114. tqpp = &tqp->td_qnxt;
  115. tqp = tqp->td_qnxt;
  116. }
  117. NutEnterCritical(); // back into critical
  118. }
  119. td->td_qnxt = tqp;
  120. *tqpp = td;
  121. if (td->td_qnxt && td->td_qnxt->td_qpec) {
  122. td->td_qpec += td->td_qnxt->td_qpec; // don't overwrite count
  123. td->td_qnxt->td_qpec = 0;
  124. }
  125. NutExitCritical();
  126. }
  127. void NutThreadRemoveQueue(NUTTHREADINFO * td, NUTTHREADINFO * volatile *tqpp)
  128. {
  129. NUTTHREADINFO *tqp;
  130. NutEnterCritical();
  131. tqp = *tqpp;
  132. NutExitCritical();
  133. if (tqp != SIGNALED) {
  134. while (tqp) {
  135. if (tqp == td) {
  136. NutEnterCritical();
  137. *tqpp = td->td_qnxt;
  138. if (td->td_qpec) {
  139. if (td->td_qnxt) {
  140. td->td_qnxt->td_qpec = td->td_qpec;
  141. }
  142. td->td_qpec = 0;
  143. }
  144. NutExitCritical();
  145. td->td_qnxt = 0;
  146. td->td_queue = 0;
  147. break;
  148. }
  149. tqpp = &tqp->td_qnxt;
  150. tqp = tqp->td_qnxt;
  151. }
  152. }
  153. }
  154. void NutThreadResume(void)
  155. {
  156. NUTTHREADINFO *td;
  157. NUTTHREADINFO **qhp;
  158. NUTTHREADINFO *tqp;
  159. unsigned int cnt;
  160. /*
  161. * Process events that have been posted from interrupt context.
  162. */
  163. td = nutThreadList;
  164. while (td) {
  165. NutEnterCritical();
  166. cnt = td->td_qpec;
  167. NutExitCritical();
  168. if (cnt) {
  169. /* In order to reduce context switching time, it is sufficient
  170. * to remove the thread on top of the priority ordered list. */
  171. qhp = (NUTTHREADINFO **)(td->td_queue);
  172. NutEnterCritical();
  173. td->td_qpec--;
  174. tqp = *qhp;
  175. NutExitCritical();
  176. if (tqp != SIGNALED) {
  177. NutEventPostAsync((HANDLE *)qhp);
  178. }
  179. }
  180. td = td->td_next;
  181. }
  182. /*
  183. * Process elapsed timers. Must be done after processing the
  184. * events from interupt routines.
  185. */
  186. NutTimerProcessElapsed();
  187. /* Check for context switch. */
  188. if (runningThread != runQueue) {
  189. #ifdef NUTTRACER
  190. TRACE_ADD_ITEM(TRACE_TAG_THREAD_YIELD,(int)runningThread);
  191. #endif
  192. if (runningThread->td_state == TDS_RUNNING) {
  193. runningThread->td_state = TDS_READY;
  194. }
  195. NutEnterCritical();
  196. NutThreadSwitch();
  197. NutExitCritical();
  198. }
  199. }
  200. void NutThreadWake(HANDLE timer, HANDLE th)
  201. {
  202. NUTASSERT(th != NULL);
  203. /* clear pointer on timer and waiting queue */
  204. ((NUTTHREADINFO *) th)->td_timer = 0;
  205. ((NUTTHREADINFO *) th)->td_state = TDS_READY;
  206. NutThreadAddPriQueue(th, (NUTTHREADINFO **) & runQueue);
  207. }
  208. void NutThreadYield(void)
  209. {
  210. #ifdef __NUT_EMULATION__
  211. NutEnterCritical();
  212. NutUnixThreadYieldHook();
  213. NutExitCritical();
  214. #endif
  215. /*
  216. * Remove current thread from runQueue and reinsert it.
  217. * The idle thread is the last one in the queue and will
  218. * never be removed.
  219. */
  220. if (runningThread->td_qnxt) {
  221. NutThreadRemoveQueue(runningThread, (NUTTHREADINFO **) & runQueue);
  222. NutThreadAddPriQueue(runningThread, (NUTTHREADINFO **) & runQueue);
  223. }
  224. /* Continue with the highest priority thread, which is ready to run. */
  225. NutThreadResume();
  226. }
  227. uint8_t NutThreadSetPriority(uint8_t level)
  228. {
  229. uint8_t last = runningThread->td_priority;
  230. /*
  231. * Remove the thread from the run queue and re-insert it with a new
  232. * priority, if this new priority level is below 255. A priotity of
  233. * 255 will kill the thread.
  234. */
  235. NutThreadRemoveQueue(runningThread, &runQueue);
  236. runningThread->td_priority = level;
  237. if (level < 255) {
  238. NutThreadAddPriQueue(runningThread, (NUTTHREADINFO **) & runQueue);
  239. } else {
  240. NutThreadKill();
  241. }
  242. /*
  243. * Are we still on top of the queue? If yes, then change our status
  244. * back to running, otherwise do a context switch.
  245. */
  246. if (runningThread == runQueue) {
  247. runningThread->td_state = TDS_RUNNING;
  248. } else {
  249. runningThread->td_state = TDS_READY;
  250. #ifdef NUTTRACER
  251. TRACE_ADD_ITEM(TRACE_TAG_THREAD_SETPRIO,(int)runningThread);
  252. #endif
  253. NutEnterCritical();
  254. NutThreadSwitch();
  255. NutExitCritical();
  256. }
  257. return last;
  258. }
  259. void NutThreadExit(void)
  260. {
  261. NutThreadSetPriority(255);
  262. }
  263. void NutThreadDestroy(void)
  264. {
  265. if (killedThread) {
  266. NutStackFree(killedThread->td_memory);
  267. killedThread = 0;
  268. }
  269. }
  270. void NutThreadKill(void)
  271. {
  272. NUTTHREADINFO *pCur = nutThreadList;
  273. NUTTHREADINFO **pp = (NUTTHREADINFO **) & nutThreadList;
  274. /* Free up any unfinished already killed threads. */
  275. NutThreadDestroy();
  276. /* Remove from the thread list. */
  277. while (pCur) {
  278. if (pCur == runningThread) {
  279. *pp = pCur->td_next;
  280. break;
  281. }
  282. pp = (NUTTHREADINFO **) & pCur->td_next;
  283. pCur = pCur->td_next;
  284. }
  285. /* Schedule for cleanup. */
  286. killedThread = runningThread;
  287. }
  288. HANDLE GetThreadByName(char * name)
  289. {
  290. NUTTHREADINFO *tdp;
  291. if (name) {
  292. for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
  293. if (strcmp(tdp->td_name, name) == 0)
  294. return tdp;
  295. }
  296. } else {
  297. return runningThread;
  298. }
  299. return NULL;
  300. }
  301. /* Calculate the size if untouched stack space. */
  302. static size_t StackAvail(NUTTHREADINFO *td)
  303. {
  304. uint32_t *sp = (uint32_t *)td->td_memory;
  305. while(*sp++ == DEADBEEF);
  306. return (size_t)((uintptr_t)sp - (uintptr_t)td->td_memory);
  307. }
  308. size_t NutThreadStackAvailable(char *name)
  309. {
  310. NUTTHREADINFO *tdp = (NUTTHREADINFO *)GetThreadByName(name);
  311. return tdp ? StackAvail(tdp) : 0;
  312. }
  313. #if defined(NUTDEBUG_CHECK_STACKMIN) || defined(NUTDEBUG_CHECK_STACK)
  314. NUTTHREADINFO *NutThreadStackCheck(size_t minsiz)
  315. {
  316. NUTTHREADINFO *tdp;
  317. for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
  318. if (StackAvail(tdp) < minsiz) {
  319. break;
  320. }
  321. }
  322. return tdp;
  323. }
  324. #endif
  325. /*@}*/