object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * File : object.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-14 Bernard the first version
  23. * 2006-04-21 Bernard change the scheduler lock to interrupt lock
  24. * 2006-05-18 Bernard fix the object init bug
  25. * 2006-08-03 Bernard add hook support
  26. * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
  27. * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
  28. * 2017-12-10 Bernard Add object_info enum.
  29. * 2018-01-25 Bernard Fix the object find issue when enable MODULE.
  30. */
  31. #include <rtthread.h>
  32. #include <rthw.h>
  33. /*
  34. * define object_info for the number of rt_object_container items.
  35. */
  36. enum rt_object_info_type
  37. {
  38. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  39. #ifdef RT_USING_SEMAPHORE
  40. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  41. #endif
  42. #ifdef RT_USING_MUTEX
  43. RT_Object_Info_Mutex, /**< The object is a mutex. */
  44. #endif
  45. #ifdef RT_USING_EVENT
  46. RT_Object_Info_Event, /**< The object is a event. */
  47. #endif
  48. #ifdef RT_USING_MAILBOX
  49. RT_Object_Info_MailBox, /**< The object is a mail box. */
  50. #endif
  51. #ifdef RT_USING_MESSAGEQUEUE
  52. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  53. #endif
  54. #ifdef RT_USING_MEMHEAP
  55. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  56. #endif
  57. #ifdef RT_USING_MEMPOOL
  58. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  59. #endif
  60. #ifdef RT_USING_DEVICE
  61. RT_Object_Info_Device, /**< The object is a device */
  62. #endif
  63. RT_Object_Info_Timer, /**< The object is a timer. */
  64. #ifdef RT_USING_MODULE
  65. RT_Object_Info_Module, /**< The object is a module. */
  66. #endif
  67. RT_Object_Info_Unknown, /**< The object is unknown. */
  68. };
  69. #define _OBJ_CONTAINER_LIST_INIT(c) \
  70. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  71. static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =
  72. {
  73. /* initialize object container - thread */
  74. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  75. #ifdef RT_USING_SEMAPHORE
  76. /* initialize object container - semaphore */
  77. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  78. #endif
  79. #ifdef RT_USING_MUTEX
  80. /* initialize object container - mutex */
  81. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  82. #endif
  83. #ifdef RT_USING_EVENT
  84. /* initialize object container - event */
  85. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  86. #endif
  87. #ifdef RT_USING_MAILBOX
  88. /* initialize object container - mailbox */
  89. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  90. #endif
  91. #ifdef RT_USING_MESSAGEQUEUE
  92. /* initialize object container - message queue */
  93. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  94. #endif
  95. #ifdef RT_USING_MEMHEAP
  96. /* initialize object container - memory heap */
  97. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  98. #endif
  99. #ifdef RT_USING_MEMPOOL
  100. /* initialize object container - memory pool */
  101. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  102. #endif
  103. #ifdef RT_USING_DEVICE
  104. /* initialize object container - device */
  105. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  106. #endif
  107. /* initialize object container - timer */
  108. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  109. #ifdef RT_USING_MODULE
  110. /* initialize object container - module */
  111. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_module)},
  112. #endif
  113. };
  114. #ifdef RT_USING_HOOK
  115. static void (*rt_object_attach_hook)(struct rt_object *object);
  116. static void (*rt_object_detach_hook)(struct rt_object *object);
  117. void (*rt_object_trytake_hook)(struct rt_object *object);
  118. void (*rt_object_take_hook)(struct rt_object *object);
  119. void (*rt_object_put_hook)(struct rt_object *object);
  120. /**
  121. * @addtogroup Hook
  122. */
  123. /**@{*/
  124. /**
  125. * This function will set a hook function, which will be invoked when object
  126. * attaches to kernel object system.
  127. *
  128. * @param hook the hook function
  129. */
  130. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  131. {
  132. rt_object_attach_hook = hook;
  133. }
  134. /**
  135. * This function will set a hook function, which will be invoked when object
  136. * detaches from kernel object system.
  137. *
  138. * @param hook the hook function
  139. */
  140. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  141. {
  142. rt_object_detach_hook = hook;
  143. }
  144. /**
  145. * This function will set a hook function, which will be invoked when object
  146. * is taken from kernel object system.
  147. *
  148. * The object is taken means:
  149. * semaphore - semaphore is taken by thread
  150. * mutex - mutex is taken by thread
  151. * event - event is received by thread
  152. * mailbox - mail is received by thread
  153. * message queue - message is received by thread
  154. *
  155. * @param hook the hook function
  156. */
  157. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  158. {
  159. rt_object_trytake_hook = hook;
  160. }
  161. /**
  162. * This function will set a hook function, which will be invoked when object
  163. * have been taken from kernel object system.
  164. *
  165. * The object have been taken means:
  166. * semaphore - semaphore have been taken by thread
  167. * mutex - mutex have been taken by thread
  168. * event - event have been received by thread
  169. * mailbox - mail have been received by thread
  170. * message queue - message have been received by thread
  171. * timer - timer is started
  172. *
  173. * @param hook the hook function
  174. */
  175. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  176. {
  177. rt_object_take_hook = hook;
  178. }
  179. /**
  180. * This function will set a hook function, which will be invoked when object
  181. * is put to kernel object system.
  182. *
  183. * @param hook the hook function
  184. */
  185. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  186. {
  187. rt_object_put_hook = hook;
  188. }
  189. /**@}*/
  190. #endif
  191. /**
  192. * @ingroup SystemInit
  193. *
  194. * This function will initialize system object management.
  195. *
  196. * @deprecated since 0.3.0, this function does not need to be invoked
  197. * in the system initialization.
  198. */
  199. void rt_system_object_init(void)
  200. {
  201. }
  202. /**
  203. * @addtogroup KernelObject
  204. */
  205. /**@{*/
  206. /**
  207. * This function will return the specified type of object information.
  208. *
  209. * @param type the type of object
  210. * @return the object type information or RT_NULL
  211. */
  212. struct rt_object_information *
  213. rt_object_get_information(enum rt_object_class_type type)
  214. {
  215. int index;
  216. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  217. if (rt_object_container[index].type == type) return &rt_object_container[index];
  218. return RT_NULL;
  219. }
  220. RTM_EXPORT(rt_object_get_information);
  221. /**
  222. * This function will initialize an object and add it to object system
  223. * management.
  224. *
  225. * @param object the specified object to be initialized.
  226. * @param type the object type.
  227. * @param name the object name. In system, the object's name must be unique.
  228. */
  229. void rt_object_init(struct rt_object *object,
  230. enum rt_object_class_type type,
  231. const char *name)
  232. {
  233. register rt_base_t temp;
  234. struct rt_object_information *information;
  235. /* get object information */
  236. information = rt_object_get_information(type);
  237. RT_ASSERT(information != RT_NULL);
  238. /* initialize object's parameters */
  239. /* set object type to static */
  240. object->type = type | RT_Object_Class_Static;
  241. /* copy name */
  242. rt_strncpy(object->name, name, RT_NAME_MAX);
  243. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  244. /* lock interrupt */
  245. temp = rt_hw_interrupt_disable();
  246. /* insert object into information object list */
  247. rt_list_insert_after(&(information->object_list), &(object->list));
  248. /* unlock interrupt */
  249. rt_hw_interrupt_enable(temp);
  250. }
  251. /**
  252. * This function will detach a static object from object system,
  253. * and the memory of static object is not freed.
  254. *
  255. * @param object the specified object to be detached.
  256. */
  257. void rt_object_detach(rt_object_t object)
  258. {
  259. register rt_base_t temp;
  260. /* object check */
  261. RT_ASSERT(object != RT_NULL);
  262. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  263. /* reset object type */
  264. object->type = 0;
  265. /* lock interrupt */
  266. temp = rt_hw_interrupt_disable();
  267. /* remove from old list */
  268. rt_list_remove(&(object->list));
  269. /* unlock interrupt */
  270. rt_hw_interrupt_enable(temp);
  271. }
  272. #ifdef RT_USING_HEAP
  273. /**
  274. * This function will allocate an object from object system
  275. *
  276. * @param type the type of object
  277. * @param name the object name. In system, the object's name must be unique.
  278. *
  279. * @return object
  280. */
  281. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  282. {
  283. struct rt_object *object;
  284. register rt_base_t temp;
  285. struct rt_object_information *information;
  286. RT_DEBUG_NOT_IN_INTERRUPT;
  287. /* get object information */
  288. information = rt_object_get_information(type);
  289. RT_ASSERT(information != RT_NULL);
  290. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  291. if (object == RT_NULL)
  292. {
  293. /* no memory can be allocated */
  294. return RT_NULL;
  295. }
  296. /* initialize object's parameters */
  297. /* set object type */
  298. object->type = type;
  299. /* set object flag */
  300. object->flag = 0;
  301. #ifdef RT_USING_MODULE
  302. if (rt_module_self() != RT_NULL)
  303. {
  304. object->flag |= RT_OBJECT_FLAG_MODULE;
  305. }
  306. object->module_id = (void *)rt_module_self();
  307. #endif
  308. /* copy name */
  309. rt_strncpy(object->name, name, RT_NAME_MAX);
  310. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  311. /* lock interrupt */
  312. temp = rt_hw_interrupt_disable();
  313. /* insert object into information object list */
  314. rt_list_insert_after(&(information->object_list), &(object->list));
  315. /* unlock interrupt */
  316. rt_hw_interrupt_enable(temp);
  317. /* return object */
  318. return object;
  319. }
  320. /**
  321. * This function will delete an object and release object memory.
  322. *
  323. * @param object the specified object to be deleted.
  324. */
  325. void rt_object_delete(rt_object_t object)
  326. {
  327. register rt_base_t temp;
  328. /* object check */
  329. RT_ASSERT(object != RT_NULL);
  330. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  331. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  332. /* reset object type */
  333. object->type = 0;
  334. /* lock interrupt */
  335. temp = rt_hw_interrupt_disable();
  336. /* remove from old list */
  337. rt_list_remove(&(object->list));
  338. /* unlock interrupt */
  339. rt_hw_interrupt_enable(temp);
  340. /* free the memory of object */
  341. RT_KERNEL_FREE(object);
  342. }
  343. #endif
  344. /**
  345. * This function will judge the object is system object or not.
  346. * Normally, the system object is a static object and the type
  347. * of object set to RT_Object_Class_Static.
  348. *
  349. * @param object the specified object to be judged.
  350. *
  351. * @return RT_TRUE if a system object, RT_FALSE for others.
  352. */
  353. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  354. {
  355. /* object check */
  356. RT_ASSERT(object != RT_NULL);
  357. if (object->type & RT_Object_Class_Static)
  358. return RT_TRUE;
  359. return RT_FALSE;
  360. }
  361. /**
  362. * This function will return the type of object without
  363. * RT_Object_Class_Static flag.
  364. *
  365. * @param object the specified object to be get type.
  366. *
  367. * @return the type of object.
  368. */
  369. rt_uint8_t rt_object_get_type(rt_object_t object)
  370. {
  371. /* object check */
  372. RT_ASSERT(object != RT_NULL);
  373. return object->type & ~RT_Object_Class_Static;
  374. }
  375. /**
  376. * This function will find specified name object from object
  377. * container.
  378. *
  379. * @param name the specified name of object.
  380. * @param type the type of object
  381. *
  382. * @return the found object or RT_NULL if there is no this object
  383. * in object container.
  384. *
  385. * @note this function shall not be invoked in interrupt status.
  386. */
  387. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  388. {
  389. struct rt_object *object = RT_NULL;
  390. struct rt_list_node *node = RT_NULL;
  391. struct rt_object_information *information = RT_NULL;
  392. /* parameter check */
  393. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  394. return RT_NULL;
  395. /* which is invoke in interrupt status */
  396. RT_DEBUG_NOT_IN_INTERRUPT;
  397. /* enter critical */
  398. rt_enter_critical();
  399. /* try to find object */
  400. if (information == RT_NULL)
  401. {
  402. information = rt_object_get_information((enum rt_object_class_type)type);
  403. RT_ASSERT(information != RT_NULL);
  404. }
  405. for (node = information->object_list.next;
  406. node != &(information->object_list);
  407. node = node->next)
  408. {
  409. object = rt_list_entry(node, struct rt_object, list);
  410. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  411. {
  412. /* leave critical */
  413. rt_exit_critical();
  414. return object;
  415. }
  416. }
  417. /* leave critical */
  418. rt_exit_critical();
  419. return RT_NULL;
  420. }
  421. /**@}*/