thread.c 16 KB

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