thread.c 19 KB

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