thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * File : thread.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-28 Bernard first version
  13. * 2006-04-29 Bernard implement thread timer
  14. * 2006-04-30 Bernard added THREAD_DEBUG
  15. * 2006-05-27 Bernard fixed the rt_thread_yield bug
  16. * 2006-06-03 Bernard fixed the thread timer init bug
  17. * 2006-08-10 Bernard fixed the timer bug in thread_sleep
  18. * 2006-09-03 Bernard changed rt_timer_delete to rt_timer_detach
  19. * 2006-09-03 Bernard implement rt_thread_detach
  20. * 2008-02-16 Bernard fixed the rt_thread_timeout bug
  21. * 2010-03-21 Bernard change the errno of rt_thread_delay/sleep to RT_EOK.
  22. * 2010-11-10 Bernard add cleanup callback function in thread exit.
  23. * 2011-09-01 Bernard fixed rt_thread_exit issue when the current thread preempted,
  24. * which reported by Jiaxing Lee.
  25. * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup.
  26. */
  27. #include <rtthread.h>
  28. #include <rthw.h>
  29. extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  30. extern struct rt_thread *rt_current_thread;
  31. extern rt_list_t rt_thread_defunct;
  32. static void rt_thread_exit(void)
  33. {
  34. struct rt_thread *thread;
  35. register rt_base_t level;
  36. /* get current thread */
  37. thread = rt_current_thread;
  38. /* disable interrupt */
  39. level = rt_hw_interrupt_disable();
  40. /* remove from schedule */
  41. rt_schedule_remove_thread(thread);
  42. /* change stat */
  43. thread->stat = RT_THREAD_CLOSE;
  44. /* remove it from timer list */
  45. rt_list_remove(&(thread->thread_timer.list));
  46. rt_object_detach((rt_object_t)&(thread->thread_timer));
  47. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
  48. thread->cleanup == RT_NULL)
  49. {
  50. rt_object_detach((rt_object_t)thread);
  51. }
  52. else
  53. {
  54. /* insert to defunct thread list */
  55. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  56. }
  57. /* enable interrupt */
  58. rt_hw_interrupt_enable(level);
  59. /* switch to next task */
  60. rt_schedule();
  61. }
  62. static rt_err_t _rt_thread_init(struct rt_thread *thread,
  63. const char *name,
  64. void (*entry)(void *parameter), void *parameter,
  65. void *stack_start, rt_uint32_t stack_size,
  66. rt_uint8_t priority, rt_uint32_t tick)
  67. {
  68. /* init thread list */
  69. rt_list_init(&(thread->tlist));
  70. thread->entry = (void *)entry;
  71. thread->parameter = parameter;
  72. /* stack init */
  73. thread->stack_addr = stack_start;
  74. thread->stack_size = stack_size;
  75. /* init thread stack */
  76. rt_memset(thread->stack_addr, '#', thread->stack_size);
  77. thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
  78. (void *) ((char *)thread->stack_addr + thread->stack_size - 4),
  79. (void *)rt_thread_exit);
  80. /* priority init */
  81. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  82. thread->init_priority = priority;
  83. thread->current_priority = priority;
  84. /* tick init */
  85. thread->init_tick = tick;
  86. thread->remaining_tick = tick;
  87. /* error and flags */
  88. thread->error = RT_EOK;
  89. thread->stat = RT_THREAD_INIT;
  90. /* initialize cleanup function and user data */
  91. thread->cleanup = 0;
  92. thread->user_data = 0;
  93. /* init thread timer */
  94. rt_timer_init(&(thread->thread_timer),
  95. thread->name,
  96. rt_thread_timeout,
  97. thread,
  98. 0,
  99. RT_TIMER_FLAG_ONE_SHOT);
  100. return RT_EOK;
  101. }
  102. /**
  103. * @addtogroup Thread
  104. */
  105. /*@{*/
  106. /**
  107. * This function will initialize a thread, normally it's used to initialize a
  108. * static thread object.
  109. *
  110. * @param thread the static thread object
  111. * @param name the name of thread, which shall be unique
  112. * @param entry the entry function of thread
  113. * @param parameter the parameter of thread enter function
  114. * @param stack_start the start address of thread stack
  115. * @param stack_size the size of thread stack
  116. * @param priority the priority of thread
  117. * @param tick the time slice if there are same priority thread
  118. *
  119. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  120. */
  121. rt_err_t rt_thread_init(struct rt_thread *thread,
  122. const char *name,
  123. void (*entry)(void *parameter), void *parameter,
  124. void *stack_start, rt_uint32_t stack_size,
  125. rt_uint8_t priority, rt_uint32_t tick)
  126. {
  127. /* thread check */
  128. RT_ASSERT(thread != RT_NULL);
  129. RT_ASSERT(stack_start != RT_NULL);
  130. /* init thread object */
  131. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  132. return _rt_thread_init(thread, name, entry, parameter,
  133. stack_start, stack_size,
  134. priority, tick);
  135. }
  136. /**
  137. * This function will return self thread object
  138. *
  139. * @return the self thread object
  140. */
  141. rt_thread_t rt_thread_self(void)
  142. {
  143. return rt_current_thread;
  144. }
  145. /**
  146. * This function will start a thread and put it to system ready queue
  147. *
  148. * @param thread the thread to be started
  149. *
  150. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  151. */
  152. rt_err_t rt_thread_startup(rt_thread_t thread)
  153. {
  154. /* thread check */
  155. RT_ASSERT(thread != RT_NULL);
  156. RT_ASSERT(thread->stat == RT_THREAD_INIT);
  157. /* set current priority to init priority */
  158. thread->current_priority = thread->init_priority;
  159. /* calculate priority attribute */
  160. #if RT_THREAD_PRIORITY_MAX > 32
  161. thread->number = thread->current_priority >> 3; /* 5bit */
  162. thread->number_mask = 1L << thread->number;
  163. thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
  164. #else
  165. thread->number_mask = 1L << thread->current_priority;
  166. #endif
  167. RT_DEBUG_LOG(RT_DEBUG_THREAD,\
  168. ("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority));
  169. /* change thread stat */
  170. thread->stat = RT_THREAD_SUSPEND;
  171. /* then resume it */
  172. rt_thread_resume(thread);
  173. if (rt_thread_self() != RT_NULL)
  174. {
  175. /* do a scheduling */
  176. rt_schedule();
  177. }
  178. return RT_EOK;
  179. }
  180. /**
  181. * This function will detach a thread. The thread object will be removed from
  182. * thread queue and detached/deleted from system object management.
  183. *
  184. * @param thread the thread to be deleted
  185. *
  186. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  187. */
  188. rt_err_t rt_thread_detach(rt_thread_t thread)
  189. {
  190. rt_base_t lock;
  191. /* thread check */
  192. RT_ASSERT(thread != RT_NULL);
  193. /* remove from schedule */
  194. rt_schedule_remove_thread(thread);
  195. /* release thread timer */
  196. rt_timer_detach(&(thread->thread_timer));
  197. /* change stat */
  198. thread->stat = RT_THREAD_CLOSE;
  199. /* detach object */
  200. rt_object_detach((rt_object_t)thread);
  201. if (thread->cleanup != RT_NULL)
  202. {
  203. /* disable interrupt */
  204. lock = rt_hw_interrupt_disable();
  205. /* insert to defunct thread list */
  206. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  207. /* enable interrupt */
  208. rt_hw_interrupt_enable(lock);
  209. }
  210. return RT_EOK;
  211. }
  212. #ifdef RT_USING_HEAP
  213. /**
  214. * This function will create a thread object and allocate thread object memory
  215. * and stack.
  216. *
  217. * @param name the name of thread, which shall be unique
  218. * @param entry the entry function of thread
  219. * @param parameter the parameter of thread enter function
  220. * @param stack_size the size of thread stack
  221. * @param priority the priority of thread
  222. * @param tick the time slice if there are same priority thread
  223. *
  224. * @return the created thread object
  225. */
  226. rt_thread_t rt_thread_create(const char *name,
  227. void (*entry)(void *parameter), void *parameter,
  228. rt_uint32_t stack_size,
  229. rt_uint8_t priority,
  230. rt_uint32_t tick)
  231. {
  232. struct rt_thread *thread;
  233. void *stack_start;
  234. thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread, name);
  235. if (thread == RT_NULL)
  236. return RT_NULL;
  237. stack_start = (void *)rt_malloc(stack_size);
  238. if (stack_start == RT_NULL)
  239. {
  240. /* allocate stack failure */
  241. rt_object_delete((rt_object_t)thread);
  242. return RT_NULL;
  243. }
  244. _rt_thread_init(thread, name, entry, parameter,
  245. stack_start, stack_size,
  246. priority, tick);
  247. return thread;
  248. }
  249. /**
  250. * This function will delete a thread. The thread object will be removed from
  251. * thread queue and detached/deleted from system object management.
  252. *
  253. * @param thread the thread to be deleted
  254. *
  255. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  256. */
  257. rt_err_t rt_thread_delete(rt_thread_t thread)
  258. {
  259. rt_base_t lock;
  260. /* thread check */
  261. RT_ASSERT(thread != RT_NULL);
  262. /* remove from schedule */
  263. rt_schedule_remove_thread(thread);
  264. /* release thread timer */
  265. rt_timer_detach(&(thread->thread_timer));
  266. /* change stat */
  267. thread->stat = RT_THREAD_CLOSE;
  268. /* disable interrupt */
  269. lock = rt_hw_interrupt_disable();
  270. /* insert to defunct thread list */
  271. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  272. /* enable interrupt */
  273. rt_hw_interrupt_enable(lock);
  274. return RT_EOK;
  275. }
  276. #endif
  277. /**
  278. * This function will let current thread yield processor, and scheduler will
  279. * choose a highest thread to run. After yield processor, the current thread
  280. * is still in READY state.
  281. *
  282. * @return RT_EOK
  283. */
  284. rt_err_t rt_thread_yield(void)
  285. {
  286. register rt_base_t level;
  287. struct rt_thread *thread;
  288. /* disable interrupt */
  289. level = rt_hw_interrupt_disable();
  290. /* set to current thread */
  291. thread = rt_current_thread;
  292. /* if the thread stat is READY and on ready queue list */
  293. if (thread->stat == RT_THREAD_READY && thread->tlist.next != thread->tlist.prev)
  294. {
  295. /* remove thread from thread list */
  296. rt_list_remove(&(thread->tlist));
  297. /* put thread to end of ready queue */
  298. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  299. &(thread->tlist));
  300. /* enable interrupt */
  301. rt_hw_interrupt_enable(level);
  302. rt_schedule();
  303. return RT_EOK;
  304. }
  305. /* enable interrupt */
  306. rt_hw_interrupt_enable(level);
  307. return RT_EOK;
  308. }
  309. /**
  310. * This function will let current thread sleep for some ticks.
  311. *
  312. * @param tick the sleep ticks
  313. *
  314. * @return RT_EOK
  315. */
  316. rt_err_t rt_thread_sleep(rt_tick_t tick)
  317. {
  318. register rt_base_t temp;
  319. struct rt_thread *thread;
  320. /* disable interrupt */
  321. temp = rt_hw_interrupt_disable();
  322. /* set to current thread */
  323. thread = rt_current_thread;
  324. RT_ASSERT(thread != RT_NULL);
  325. /* suspend thread */
  326. rt_thread_suspend(thread);
  327. /* reset the timeout of thread timer and start it */
  328. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  329. rt_timer_start(&(thread->thread_timer));
  330. /* enable interrupt */
  331. rt_hw_interrupt_enable(temp);
  332. rt_schedule();
  333. /* clear error number of this thread to RT_EOK */
  334. if (thread->error == -RT_ETIMEOUT)
  335. thread->error = RT_EOK;
  336. return RT_EOK;
  337. }
  338. /**
  339. * This function will let current thread delay for some ticks.
  340. *
  341. * @param tick the delay ticks
  342. *
  343. * @return RT_EOK
  344. */
  345. rt_err_t rt_thread_delay(rt_tick_t tick)
  346. {
  347. return rt_thread_sleep(tick);
  348. }
  349. /**
  350. * This function will control thread behaviors according to control command.
  351. *
  352. * @param thread the specified thread to be controlled
  353. * @param cmd the control command, which includes
  354. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  355. * RT_THREAD_CTRL_STARTUP for starting a thread;
  356. * RT_THREAD_CTRL_CLOSE for delete a thread.
  357. * @param arg the argument of control command
  358. *
  359. * @return RT_EOK
  360. */
  361. rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg)
  362. {
  363. register rt_base_t temp;
  364. /* thread check */
  365. RT_ASSERT(thread != RT_NULL);
  366. switch (cmd)
  367. {
  368. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  369. /* disable interrupt */
  370. temp = rt_hw_interrupt_disable();
  371. /* for ready thread, change queue */
  372. if (thread->stat == RT_THREAD_READY)
  373. {
  374. /* remove thread from schedule queue first */
  375. rt_schedule_remove_thread(thread);
  376. /* change thread priority */
  377. thread->current_priority = *(rt_uint8_t *)arg;
  378. /* recalculate priority attribute */
  379. #if RT_THREAD_PRIORITY_MAX > 32
  380. thread->number = thread->current_priority >> 3; /* 5bit */
  381. thread->number_mask = 1 << thread->number;
  382. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  383. #else
  384. thread->number_mask = 1 << thread->current_priority;
  385. #endif
  386. /* insert thread to schedule queue again */
  387. rt_schedule_insert_thread(thread);
  388. }
  389. else
  390. {
  391. thread->current_priority = *(rt_uint8_t *)arg;
  392. /* recalculate priority attribute */
  393. #if RT_THREAD_PRIORITY_MAX > 32
  394. thread->number = thread->current_priority >> 3; /* 5bit */
  395. thread->number_mask = 1 << thread->number;
  396. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  397. #else
  398. thread->number_mask = 1 << thread->current_priority;
  399. #endif
  400. }
  401. /* enable interrupt */
  402. rt_hw_interrupt_enable(temp);
  403. break;
  404. case RT_THREAD_CTRL_STARTUP:
  405. return rt_thread_startup(thread);
  406. #ifdef RT_USING_HEAP
  407. case RT_THREAD_CTRL_CLOSE:
  408. return rt_thread_delete(thread);
  409. #endif
  410. default:
  411. break;
  412. }
  413. return RT_EOK;
  414. }
  415. /**
  416. * This function will suspend the specified thread.
  417. *
  418. * @param thread the thread to be suspended
  419. *
  420. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  421. *
  422. * @note if suspend self thread, after this function call, the
  423. * rt_schedule() must be invoked.
  424. */
  425. rt_err_t rt_thread_suspend(rt_thread_t thread)
  426. {
  427. register rt_base_t temp;
  428. /* thread check */
  429. RT_ASSERT(thread != RT_NULL);
  430. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  431. if (thread->stat != RT_THREAD_READY)
  432. {
  433. RT_DEBUG_LOG(RT_DEBUG_THREAD,\
  434. ("thread suspend: thread disorder, %d\n", thread->stat));
  435. return -RT_ERROR;
  436. }
  437. /* disable interrupt */
  438. temp = rt_hw_interrupt_disable();
  439. /* change thread stat */
  440. thread->stat = RT_THREAD_SUSPEND;
  441. rt_schedule_remove_thread(thread);
  442. /* enable interrupt */
  443. rt_hw_interrupt_enable(temp);
  444. return RT_EOK;
  445. }
  446. /**
  447. * This function will resume a thread and put it to system ready queue.
  448. *
  449. * @param thread the thread to be resumed
  450. *
  451. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  452. */
  453. rt_err_t rt_thread_resume(rt_thread_t thread)
  454. {
  455. register rt_base_t temp;
  456. /* thread check */
  457. RT_ASSERT(thread != RT_NULL);
  458. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  459. if (thread->stat != RT_THREAD_SUSPEND)
  460. {
  461. RT_DEBUG_LOG(RT_DEBUG_THREAD, \
  462. ("thread resume: thread disorder, %d\n", thread->stat));
  463. return -RT_ERROR;
  464. }
  465. /* disable interrupt */
  466. temp = rt_hw_interrupt_disable();
  467. /* remove from suspend list */
  468. rt_list_remove(&(thread->tlist));
  469. /* remove thread timer */
  470. rt_list_remove(&(thread->thread_timer.list));
  471. /* change timer state */
  472. thread->thread_timer.parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  473. /* enable interrupt */
  474. rt_hw_interrupt_enable(temp);
  475. /* insert to schedule ready list */
  476. rt_schedule_insert_thread(thread);
  477. return RT_EOK;
  478. }
  479. /**
  480. * This function is the timeout function for thread, normally which is invoked
  481. * when thread is timeout to wait some resource.
  482. *
  483. * @param parameter the parameter of thread timeout function
  484. */
  485. void rt_thread_timeout(void *parameter)
  486. {
  487. struct rt_thread *thread;
  488. thread = (struct rt_thread *)parameter;
  489. /* thread check */
  490. RT_ASSERT(thread != RT_NULL);
  491. RT_ASSERT(thread->stat == RT_THREAD_SUSPEND);
  492. /* set error number */
  493. thread->error = -RT_ETIMEOUT;
  494. /* remove from suspend list */
  495. rt_list_remove(&(thread->tlist));
  496. /* insert to schedule ready list */
  497. rt_schedule_insert_thread(thread);
  498. /* do schedule */
  499. rt_schedule();
  500. }
  501. /**
  502. * This function will find the specified thread.
  503. *
  504. * @param name the name of thread finding
  505. *
  506. * @return the found thread
  507. *
  508. * @note please don't invoke this function in interrupt status.
  509. */
  510. rt_thread_t rt_thread_find(char *name)
  511. {
  512. struct rt_object_information *information;
  513. struct rt_object *object;
  514. struct rt_list_node *node;
  515. extern struct rt_object_information rt_object_container[];
  516. /* enter critical */
  517. if (rt_thread_self() != RT_NULL)
  518. rt_enter_critical();
  519. /* try to find device object */
  520. information = &rt_object_container[RT_Object_Class_Thread];
  521. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  522. {
  523. object = rt_list_entry(node, struct rt_object, list);
  524. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  525. {
  526. /* leave critical */
  527. if (rt_thread_self() != RT_NULL)
  528. rt_exit_critical();
  529. return (rt_thread_t)object;
  530. }
  531. }
  532. /* leave critical */
  533. if (rt_thread_self() != RT_NULL)
  534. rt_exit_critical();
  535. /* not found */
  536. return RT_NULL;
  537. }
  538. /*@}*/