thread.c 16 KB

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