thread.c 16 KB

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