thread.c 19 KB

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