thread.c 15 KB

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