thread.c 19 KB

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