thread.c 14 KB

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