thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * File : thread.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, 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 add THREAD_DEBUG
  15. * 2006-05-27 Bernard fix the rt_thread_yield bug
  16. * 2006-06-03 Bernard fix the thread timer init bug
  17. * 2006-08-10 Bernard fix the timer bug in thread_sleep
  18. * 2006-09-03 Bernard change rt_timer_delete to rt_timer_detach
  19. * 2006-09-03 Bernard implement rt_thread_detach
  20. * 2008-02-16 Bernard fix 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. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include "kservice.h"
  27. /*#define THREAD_DEBUG */
  28. extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  29. extern struct rt_thread* rt_current_thread;
  30. extern rt_uint8_t rt_current_priority;
  31. #ifdef RT_USING_HEAP
  32. extern rt_list_t rt_thread_defunct;
  33. #endif
  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; //1L means long int,fixed compile mistake with IAR EW M16C v3.401,fify 20100410
  181. #endif
  182. #ifdef THREAD_DEBUG
  183. rt_kprintf("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority);
  184. #endif
  185. /* change thread stat */
  186. thread->stat = RT_THREAD_SUSPEND;
  187. /* then resume it */
  188. rt_thread_resume(thread);
  189. return RT_EOK;
  190. }
  191. static void rt_thread_exit()
  192. {
  193. struct rt_thread* thread;
  194. register rt_base_t temp;
  195. /* disable interrupt */
  196. temp = rt_hw_interrupt_disable();
  197. /* get current thread */
  198. thread = rt_current_thread;
  199. /* remove from schedule */
  200. rt_schedule_remove_thread(thread);
  201. /* change stat */
  202. thread->stat = RT_THREAD_CLOSE;
  203. /* release thread timer */
  204. rt_timer_detach(&(thread->thread_timer));
  205. /* enable interrupt */
  206. rt_hw_interrupt_enable(temp);
  207. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_EOK) &&
  208. thread->cleanup == RT_NULL)
  209. {
  210. rt_object_detach((rt_object_t)thread);
  211. }
  212. #ifdef RT_USING_HEAP
  213. else
  214. {
  215. /* disable interrupt */
  216. temp = rt_hw_interrupt_disable();
  217. /* insert to defunct thread list */
  218. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  219. /* enable interrupt */
  220. rt_hw_interrupt_enable(temp);
  221. }
  222. #endif
  223. /* switch to next task */
  224. rt_schedule();
  225. }
  226. /**
  227. * This function will detach a thread. The thread object will be removed from
  228. * thread queue and detached/deleted from system object management.
  229. *
  230. * @param thread the thread to be deleted
  231. *
  232. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  233. *
  234. */
  235. rt_err_t rt_thread_detach (rt_thread_t thread)
  236. {
  237. rt_base_t lock;
  238. /* thread check */
  239. RT_ASSERT(thread != RT_NULL);
  240. /* remove from schedule */
  241. rt_schedule_remove_thread(thread);
  242. /* release thread timer */
  243. rt_timer_detach(&(thread->thread_timer));
  244. /* change stat */
  245. thread->stat = RT_THREAD_CLOSE;
  246. /* detach object */
  247. rt_object_detach((rt_object_t)thread);
  248. if (thread->cleanup != RT_NULL)
  249. {
  250. /* disable interrupt */
  251. lock = rt_hw_interrupt_disable();
  252. /* insert to defunct thread list */
  253. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  254. /* enable interrupt */
  255. rt_hw_interrupt_enable(lock);
  256. }
  257. return RT_EOK;
  258. }
  259. #ifdef RT_USING_HEAP
  260. /**
  261. * This function will delete a thread. The thread object will be removed from
  262. * thread queue and detached/deleted from system object management.
  263. *
  264. * @param thread the thread to be deleted
  265. *
  266. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  267. *
  268. */
  269. rt_err_t rt_thread_delete (rt_thread_t thread)
  270. {
  271. rt_base_t lock;
  272. /* thread check */
  273. RT_ASSERT(thread != RT_NULL);
  274. /* remove from schedule */
  275. rt_schedule_remove_thread(thread);
  276. /* release thread timer */
  277. rt_timer_detach(&(thread->thread_timer));
  278. /* change stat */
  279. thread->stat = RT_THREAD_CLOSE;
  280. /* disable interrupt */
  281. lock = rt_hw_interrupt_disable();
  282. /* insert to defunct thread list */
  283. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  284. /* enable interrupt */
  285. rt_hw_interrupt_enable(lock);
  286. return RT_EOK;
  287. }
  288. #endif
  289. /**
  290. * This function will let current thread yield processor, and scheduler will
  291. * choose a highest thread to run. After yield processor, the current thread
  292. * is still in READY state.
  293. *
  294. * @return RT_EOK
  295. *
  296. */
  297. rt_err_t rt_thread_yield ()
  298. {
  299. register rt_base_t level;
  300. struct rt_thread *thread;
  301. /* disable interrupt */
  302. level = rt_hw_interrupt_disable();
  303. /* set to current thread */
  304. thread = rt_current_thread;
  305. /* if the thread stat is READY and on ready queue list */
  306. if (thread->stat == RT_THREAD_READY && thread->tlist.next != thread->tlist.prev)
  307. {
  308. /* remove thread from thread list */
  309. rt_list_remove(&(thread->tlist));
  310. /* put thread to end of ready queue */
  311. rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
  312. &(thread->tlist));
  313. /* enable interrupt */
  314. rt_hw_interrupt_enable(level);
  315. rt_schedule();
  316. return RT_EOK;
  317. }
  318. /* enable interrupt */
  319. rt_hw_interrupt_enable(level);
  320. return RT_EOK;
  321. }
  322. /**
  323. * This function will let current thread sleep for some ticks.
  324. *
  325. * @param tick the sleep ticks
  326. *
  327. * @return RT_EOK
  328. *
  329. */
  330. rt_err_t rt_thread_sleep (rt_tick_t tick)
  331. {
  332. register rt_base_t temp;
  333. struct rt_thread *thread;
  334. /* disable interrupt */
  335. temp = rt_hw_interrupt_disable();
  336. /* set to current thread */
  337. thread = rt_current_thread;
  338. RT_ASSERT(thread != RT_NULL);
  339. /* suspend thread */
  340. rt_thread_suspend(thread);
  341. /* reset the timeout of thread timer and start it */
  342. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
  343. rt_timer_start(&(thread->thread_timer));
  344. /* enable interrupt */
  345. rt_hw_interrupt_enable(temp);
  346. rt_schedule();
  347. /* clear error number of this thread to RT_EOK */
  348. if (thread->error == -RT_ETIMEOUT)
  349. thread->error = RT_EOK;
  350. return RT_EOK;
  351. }
  352. /**
  353. * This function will let current thread delay for some ticks.
  354. *
  355. * @param tick the delay ticks
  356. *
  357. * @return RT_EOK
  358. *
  359. */
  360. rt_err_t rt_thread_delay(rt_tick_t tick)
  361. {
  362. return rt_thread_sleep(tick);
  363. }
  364. /**
  365. * This function will control thread behaviors according to control command.
  366. *
  367. * @param thread the specified thread to be controlled
  368. * @param cmd the control command, which includes
  369. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  370. * RT_THREAD_CTRL_STARTUP for starting a thread;
  371. * RT_THREAD_CTRL_CLOSE for delete a thread.
  372. * @param arg the argument of control command
  373. *
  374. * @return RT_EOK
  375. */
  376. rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg)
  377. {
  378. register rt_base_t temp;
  379. /* thread check */
  380. RT_ASSERT(thread != RT_NULL);
  381. switch (cmd)
  382. {
  383. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  384. /* disable interrupt */
  385. temp = rt_hw_interrupt_disable();
  386. /* for ready thread, change queue */
  387. if (thread->stat == RT_THREAD_READY)
  388. {
  389. /* remove thread from schedule queue first */
  390. rt_schedule_remove_thread(thread);
  391. /* change thread priority */
  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. /* insert thread to schedule queue again */
  402. rt_schedule_insert_thread(thread);
  403. }
  404. else
  405. {
  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. }
  416. /* enable interrupt */
  417. rt_hw_interrupt_enable(temp);
  418. break;
  419. case RT_THREAD_CTRL_STARTUP:
  420. return rt_thread_startup(thread);
  421. #ifdef RT_USING_HEAP
  422. case RT_THREAD_CTRL_CLOSE:
  423. return rt_thread_delete(thread);
  424. #endif
  425. default:
  426. break;
  427. }
  428. return RT_EOK;
  429. }
  430. /**
  431. * This function will suspend the specified thread.
  432. *
  433. * @param thread the thread to be suspended
  434. *
  435. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  436. *
  437. * @note if suspend self thread, after this function call, the
  438. * rt_schedule() must be invoked.
  439. */
  440. rt_err_t rt_thread_suspend (rt_thread_t thread)
  441. {
  442. register rt_base_t temp;
  443. /* thread check */
  444. RT_ASSERT(thread != RT_NULL);
  445. #ifdef THREAD_DEBUG
  446. rt_kprintf("thread suspend: %s\n", thread->name);
  447. #endif
  448. if (thread->stat != RT_THREAD_READY)
  449. {
  450. #ifdef THREAD_DEBUG
  451. rt_kprintf("thread suspend: thread disorder, %d\n", thread->stat);
  452. #endif
  453. return -RT_ERROR;
  454. }
  455. /* disable interrupt */
  456. temp = rt_hw_interrupt_disable();
  457. /* change thread stat */
  458. thread->stat = RT_THREAD_SUSPEND;
  459. rt_schedule_remove_thread(thread);
  460. /* enable interrupt */
  461. rt_hw_interrupt_enable(temp);
  462. return RT_EOK;
  463. }
  464. /**
  465. * This function will resume a thread and put it to system ready queue.
  466. *
  467. * @param thread the thread to be resumed
  468. *
  469. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  470. *
  471. */
  472. rt_err_t rt_thread_resume (rt_thread_t thread)
  473. {
  474. register rt_base_t temp;
  475. /* thread check */
  476. RT_ASSERT(thread != RT_NULL);
  477. #ifdef THREAD_DEBUG
  478. rt_kprintf("thread resume: %s\n", thread->name);
  479. #endif
  480. if (thread->stat != RT_THREAD_SUSPEND)
  481. {
  482. #ifdef THREAD_DEBUG
  483. rt_kprintf("thread resume: thread disorder, %d\n", thread->stat);
  484. #endif
  485. return -RT_ERROR;
  486. }
  487. /* disable interrupt */
  488. temp = rt_hw_interrupt_disable();
  489. /* remove from suspend list */
  490. rt_list_remove(&(thread->tlist));
  491. /* remove thread timer */
  492. rt_list_remove(&(thread->thread_timer.list));
  493. /* change timer state */
  494. thread->thread_timer.parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  495. /* enable interrupt */
  496. rt_hw_interrupt_enable(temp);
  497. /* insert to schedule ready list */
  498. rt_schedule_insert_thread(thread);
  499. return RT_EOK;
  500. }
  501. /**
  502. * This function is the timeout function for thread, normally which is invoked
  503. * when thread is timeout to wait some recourse.
  504. *
  505. * @param parameter the parameter of thread timeout function
  506. *
  507. */
  508. void rt_thread_timeout(void* parameter)
  509. {
  510. struct rt_thread* thread;
  511. thread = (struct rt_thread*) parameter;
  512. /* thread check */
  513. RT_ASSERT(thread != RT_NULL);
  514. RT_ASSERT(thread->stat == RT_THREAD_SUSPEND);
  515. /* set error number */
  516. thread->error = -RT_ETIMEOUT;
  517. /* remove from suspend list */
  518. rt_list_remove(&(thread->tlist));
  519. /* insert to schedule ready list */
  520. rt_schedule_insert_thread(thread);
  521. /* do schedule */
  522. rt_schedule();
  523. }
  524. /**
  525. * This function will find the specified thread.
  526. *
  527. * @param name the name of thread finding
  528. *
  529. * @return the found thread
  530. *
  531. * @note please don't invoke this function in interrupt status.
  532. */
  533. rt_thread_t rt_thread_find(char* name)
  534. {
  535. struct rt_object_information *information;
  536. struct rt_object* object;
  537. struct rt_list_node* node;
  538. extern struct rt_object_information rt_object_container[];
  539. /* enter critical */
  540. if (rt_thread_self() != RT_NULL)
  541. rt_enter_critical();
  542. /* try to find device object */
  543. information = &rt_object_container[RT_Object_Class_Thread];
  544. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  545. {
  546. object = rt_list_entry(node, struct rt_object, list);
  547. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  548. {
  549. /* leave critical */
  550. if (rt_thread_self() != RT_NULL)
  551. rt_exit_critical();
  552. return (rt_thread_t)object;
  553. }
  554. }
  555. /* leave critical */
  556. if (rt_thread_self() != RT_NULL)
  557. rt_exit_critical();
  558. /* not found */
  559. return RT_NULL;
  560. }
  561. /*@}*/