thread.c 14 KB

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