thread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
  28. extern struct rt_thread* rt_current_thread;
  29. extern rt_uint8_t rt_current_priority;
  30. extern rt_list_t rt_thread_defunct;
  31. static void rt_thread_exit(void);
  32. void rt_thread_timeout(void* parameter);
  33. static rt_err_t _rt_thread_init(struct rt_thread* thread,
  34. const char* name,
  35. void (*entry)(void* parameter), void* parameter,
  36. void* stack_start, rt_uint32_t stack_size,
  37. rt_uint8_t priority, rt_uint32_t tick)
  38. {
  39. /* init thread list */
  40. rt_list_init(&(thread->tlist));
  41. thread->entry = (void*)entry;
  42. thread->parameter = parameter;
  43. /* stack init */
  44. thread->stack_addr = stack_start;
  45. thread->stack_size = stack_size;
  46. /* init thread stack */
  47. rt_memset(thread->stack_addr, '#', thread->stack_size);
  48. thread->sp = (void*)rt_hw_stack_init(thread->entry, thread->parameter,
  49. (void *) ((char *)thread->stack_addr + thread->stack_size - 4),
  50. (void*)rt_thread_exit);
  51. /* priority init */
  52. RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
  53. thread->init_priority = priority;
  54. thread->current_priority = priority;
  55. /* tick init */
  56. thread->init_tick = tick;
  57. thread->remaining_tick = tick;
  58. /* error and flags */
  59. thread->error = RT_EOK;
  60. thread->stat = RT_THREAD_INIT;
  61. /* initialize cleanup function and user data */
  62. thread->cleanup = 0;
  63. thread->user_data = 0;
  64. /* init thread timer */
  65. rt_timer_init(&(thread->thread_timer),
  66. thread->name,
  67. rt_thread_timeout,
  68. thread,
  69. 0,
  70. RT_TIMER_FLAG_ONE_SHOT);
  71. return RT_EOK;
  72. }
  73. /**
  74. * @addtogroup Thread
  75. */
  76. /*@{*/
  77. /**
  78. * This function will initialize a thread, normally it's used to initialize a
  79. * static thread object.
  80. *
  81. * @param thread the static thread object
  82. * @param name the name of thread, which shall be unique
  83. * @param entry the entry function of thread
  84. * @param parameter the parameter of thread enter function
  85. * @param stack_start the start address of thread stack
  86. * @param stack_size the size of thread stack
  87. * @param priority the priority of thread
  88. * @param tick the time slice if there are same priority thread
  89. *
  90. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  91. *
  92. */
  93. rt_err_t rt_thread_init(struct rt_thread* thread,
  94. const char* name,
  95. void (*entry)(void* parameter), void* parameter,
  96. void* stack_start, rt_uint32_t stack_size,
  97. rt_uint8_t priority, rt_uint32_t tick)
  98. {
  99. /* thread check */
  100. RT_ASSERT(thread != RT_NULL);
  101. RT_ASSERT(stack_start != RT_NULL);
  102. /* init thread object */
  103. rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
  104. return _rt_thread_init(thread, name, entry, parameter,
  105. stack_start, stack_size,
  106. priority, tick);
  107. }
  108. #ifdef RT_USING_HEAP
  109. /**
  110. * This function will create a thread object and allocate thread object memory
  111. * 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. RT_DEBUG_LOG(RT_DEBUG_THREAD,\
  180. ("startup a thread:%s with priority:%d\n", thread->name, thread->init_priority));
  181. /* change thread stat */
  182. thread->stat = RT_THREAD_SUSPEND;
  183. /* then resume it */
  184. rt_thread_resume(thread);
  185. return RT_EOK;
  186. }
  187. static void rt_thread_exit()
  188. {
  189. struct rt_thread* thread;
  190. register rt_base_t temp;
  191. /* disable interrupt */
  192. temp = rt_hw_interrupt_disable();
  193. /* get current thread */
  194. thread = rt_current_thread;
  195. /* remove from schedule */
  196. rt_schedule_remove_thread(thread);
  197. /* change stat */
  198. thread->stat = RT_THREAD_CLOSE;
  199. /* release thread timer */
  200. rt_timer_detach(&(thread->thread_timer));
  201. /* enable interrupt */
  202. rt_hw_interrupt_enable(temp);
  203. if ((rt_object_is_systemobject((rt_object_t)thread) == RT_EOK) &&
  204. thread->cleanup == RT_NULL)
  205. {
  206. rt_object_detach((rt_object_t)thread);
  207. }
  208. #ifdef RT_USING_HEAP
  209. else
  210. {
  211. /* disable interrupt */
  212. temp = rt_hw_interrupt_disable();
  213. /* insert to defunct thread list */
  214. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  215. /* enable interrupt */
  216. rt_hw_interrupt_enable(temp);
  217. }
  218. #endif
  219. /* switch to next task */
  220. rt_schedule();
  221. }
  222. /**
  223. * This function will detach a thread. The thread object will be removed from
  224. * thread queue and detached/deleted from system object management.
  225. *
  226. * @param thread the thread to be deleted
  227. *
  228. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  229. *
  230. */
  231. rt_err_t rt_thread_detach (rt_thread_t thread)
  232. {
  233. rt_base_t lock;
  234. /* thread check */
  235. RT_ASSERT(thread != RT_NULL);
  236. /* remove from schedule */
  237. rt_schedule_remove_thread(thread);
  238. /* release thread timer */
  239. rt_timer_detach(&(thread->thread_timer));
  240. /* change stat */
  241. thread->stat = RT_THREAD_CLOSE;
  242. /* detach object */
  243. rt_object_detach((rt_object_t)thread);
  244. if (thread->cleanup != RT_NULL)
  245. {
  246. /* disable interrupt */
  247. lock = rt_hw_interrupt_disable();
  248. /* insert to defunct thread list */
  249. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  250. /* enable interrupt */
  251. rt_hw_interrupt_enable(lock);
  252. }
  253. return RT_EOK;
  254. }
  255. #ifdef RT_USING_HEAP
  256. /**
  257. * This function will delete a thread. The thread object will be removed from
  258. * thread queue and detached/deleted from system object management.
  259. *
  260. * @param thread the thread to be deleted
  261. *
  262. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  263. *
  264. */
  265. rt_err_t rt_thread_delete (rt_thread_t thread)
  266. {
  267. rt_base_t lock;
  268. /* thread check */
  269. RT_ASSERT(thread != RT_NULL);
  270. /* remove from schedule */
  271. rt_schedule_remove_thread(thread);
  272. /* release thread timer */
  273. rt_timer_detach(&(thread->thread_timer));
  274. /* change stat */
  275. thread->stat = RT_THREAD_CLOSE;
  276. /* disable interrupt */
  277. lock = rt_hw_interrupt_disable();
  278. /* insert to defunct thread list */
  279. rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
  280. /* enable interrupt */
  281. rt_hw_interrupt_enable(lock);
  282. return RT_EOK;
  283. }
  284. #endif
  285. /**
  286. * This function will let current thread yield processor, and scheduler will
  287. * choose a highest thread to run. After yield processor, the current thread
  288. * is still in READY state.
  289. *
  290. * @return RT_EOK
  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 RT_EOK
  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 RT_EOK
  354. *
  355. */
  356. rt_err_t rt_thread_delay(rt_tick_t tick)
  357. {
  358. return rt_thread_sleep(tick);
  359. }
  360. /**
  361. * This function will control thread behaviors according to control command.
  362. *
  363. * @param thread the specified thread to be controlled
  364. * @param cmd the control command, which includes
  365. * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
  366. * RT_THREAD_CTRL_STARTUP for starting a thread;
  367. * RT_THREAD_CTRL_CLOSE for delete a thread.
  368. * @param arg the argument of control command
  369. *
  370. * @return RT_EOK
  371. */
  372. rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg)
  373. {
  374. register rt_base_t temp;
  375. /* thread check */
  376. RT_ASSERT(thread != RT_NULL);
  377. switch (cmd)
  378. {
  379. case RT_THREAD_CTRL_CHANGE_PRIORITY:
  380. /* disable interrupt */
  381. temp = rt_hw_interrupt_disable();
  382. /* for ready thread, change queue */
  383. if (thread->stat == RT_THREAD_READY)
  384. {
  385. /* remove thread from schedule queue first */
  386. rt_schedule_remove_thread(thread);
  387. /* change thread priority */
  388. thread->current_priority = *(rt_uint8_t*) arg;
  389. /* recalculate priority attribute */
  390. #if RT_THREAD_PRIORITY_MAX > 32
  391. thread->number = thread->current_priority >> 3; /* 5bit */
  392. thread->number_mask = 1 << thread->number;
  393. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  394. #else
  395. thread->number_mask = 1 << thread->current_priority;
  396. #endif
  397. /* insert thread to schedule queue again */
  398. rt_schedule_insert_thread(thread);
  399. }
  400. else
  401. {
  402. thread->current_priority = *(rt_uint8_t*) arg;
  403. /* recalculate priority attribute */
  404. #if RT_THREAD_PRIORITY_MAX > 32
  405. thread->number = thread->current_priority >> 3; /* 5bit */
  406. thread->number_mask = 1 << thread->number;
  407. thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
  408. #else
  409. thread->number_mask = 1 << thread->current_priority;
  410. #endif
  411. }
  412. /* enable interrupt */
  413. rt_hw_interrupt_enable(temp);
  414. break;
  415. case RT_THREAD_CTRL_STARTUP:
  416. return rt_thread_startup(thread);
  417. #ifdef RT_USING_HEAP
  418. case RT_THREAD_CTRL_CLOSE:
  419. return rt_thread_delete(thread);
  420. #endif
  421. default:
  422. break;
  423. }
  424. return RT_EOK;
  425. }
  426. /**
  427. * This function will suspend the specified thread.
  428. *
  429. * @param thread the thread to be suspended
  430. *
  431. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  432. *
  433. * @note if suspend self thread, after this function call, the
  434. * rt_schedule() must be invoked.
  435. */
  436. rt_err_t rt_thread_suspend (rt_thread_t thread)
  437. {
  438. register rt_base_t temp;
  439. /* thread check */
  440. RT_ASSERT(thread != RT_NULL);
  441. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
  442. if (thread->stat != RT_THREAD_READY)
  443. {
  444. RT_DEBUG_LOG(RT_DEBUG_THREAD,\
  445. ("thread suspend: thread disorder, %d\n", thread->stat));
  446. return -RT_ERROR;
  447. }
  448. /* disable interrupt */
  449. temp = rt_hw_interrupt_disable();
  450. /* change thread stat */
  451. thread->stat = RT_THREAD_SUSPEND;
  452. rt_schedule_remove_thread(thread);
  453. /* enable interrupt */
  454. rt_hw_interrupt_enable(temp);
  455. return RT_EOK;
  456. }
  457. /**
  458. * This function will resume a thread and put it to system ready queue.
  459. *
  460. * @param thread the thread to be resumed
  461. *
  462. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  463. *
  464. */
  465. rt_err_t rt_thread_resume (rt_thread_t thread)
  466. {
  467. register rt_base_t temp;
  468. /* thread check */
  469. RT_ASSERT(thread != RT_NULL);
  470. RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
  471. if (thread->stat != RT_THREAD_SUSPEND)
  472. {
  473. RT_DEBUG_LOG(RT_DEBUG_THREAD, \
  474. ("thread resume: thread disorder, %d\n", thread->stat));
  475. return -RT_ERROR;
  476. }
  477. /* disable interrupt */
  478. temp = rt_hw_interrupt_disable();
  479. /* remove from suspend list */
  480. rt_list_remove(&(thread->tlist));
  481. /* remove thread timer */
  482. rt_list_remove(&(thread->thread_timer.list));
  483. /* change timer state */
  484. thread->thread_timer.parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  485. /* enable interrupt */
  486. rt_hw_interrupt_enable(temp);
  487. /* insert to schedule ready list */
  488. rt_schedule_insert_thread(thread);
  489. return RT_EOK;
  490. }
  491. /**
  492. * This function is the timeout function for thread, normally which is invoked
  493. * when thread is timeout to wait some recourse.
  494. *
  495. * @param parameter the parameter of thread timeout function
  496. *
  497. */
  498. void rt_thread_timeout(void* parameter)
  499. {
  500. struct rt_thread* thread;
  501. thread = (struct rt_thread*) parameter;
  502. /* thread check */
  503. RT_ASSERT(thread != RT_NULL);
  504. RT_ASSERT(thread->stat == RT_THREAD_SUSPEND);
  505. /* set error number */
  506. thread->error = -RT_ETIMEOUT;
  507. /* remove from suspend list */
  508. rt_list_remove(&(thread->tlist));
  509. /* insert to schedule ready list */
  510. rt_schedule_insert_thread(thread);
  511. /* do schedule */
  512. rt_schedule();
  513. }
  514. /**
  515. * This function will find the specified thread.
  516. *
  517. * @param name the name of thread finding
  518. *
  519. * @return the found thread
  520. *
  521. * @note please don't invoke this function in interrupt status.
  522. */
  523. rt_thread_t rt_thread_find(char* name)
  524. {
  525. struct rt_object_information *information;
  526. struct rt_object* object;
  527. struct rt_list_node* node;
  528. extern struct rt_object_information rt_object_container[];
  529. /* enter critical */
  530. if (rt_thread_self() != RT_NULL)
  531. rt_enter_critical();
  532. /* try to find device object */
  533. information = &rt_object_container[RT_Object_Class_Thread];
  534. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  535. {
  536. object = rt_list_entry(node, struct rt_object, list);
  537. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  538. {
  539. /* leave critical */
  540. if (rt_thread_self() != RT_NULL)
  541. rt_exit_critical();
  542. return (rt_thread_t)object;
  543. }
  544. }
  545. /* leave critical */
  546. if (rt_thread_self() != RT_NULL)
  547. rt_exit_critical();
  548. /* not found */
  549. return RT_NULL;
  550. }
  551. /*@}*/