thread.c 16 KB

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