object.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #ifdef RT_USING_MODULE
  34. #include <dlmodule.h>
  35. #endif
  36. /*
  37. * define object_info for the number of rt_object_container items.
  38. */
  39. enum rt_object_info_type
  40. {
  41. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  42. #ifdef RT_USING_SEMAPHORE
  43. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  44. #endif
  45. #ifdef RT_USING_MUTEX
  46. RT_Object_Info_Mutex, /**< The object is a mutex. */
  47. #endif
  48. #ifdef RT_USING_EVENT
  49. RT_Object_Info_Event, /**< The object is a event. */
  50. #endif
  51. #ifdef RT_USING_MAILBOX
  52. RT_Object_Info_MailBox, /**< The object is a mail box. */
  53. #endif
  54. #ifdef RT_USING_MESSAGEQUEUE
  55. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  56. #endif
  57. #ifdef RT_USING_MEMHEAP
  58. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  59. #endif
  60. #ifdef RT_USING_MEMPOOL
  61. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  62. #endif
  63. #ifdef RT_USING_DEVICE
  64. RT_Object_Info_Device, /**< The object is a device */
  65. #endif
  66. RT_Object_Info_Timer, /**< The object is a timer. */
  67. #ifdef RT_USING_MODULE
  68. RT_Object_Info_Module, /**< The object is a module. */
  69. #endif
  70. RT_Object_Info_Unknown, /**< The object is unknown. */
  71. };
  72. #define _OBJ_CONTAINER_LIST_INIT(c) \
  73. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  74. static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =
  75. {
  76. /* initialize object container - thread */
  77. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  78. #ifdef RT_USING_SEMAPHORE
  79. /* initialize object container - semaphore */
  80. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  81. #endif
  82. #ifdef RT_USING_MUTEX
  83. /* initialize object container - mutex */
  84. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  85. #endif
  86. #ifdef RT_USING_EVENT
  87. /* initialize object container - event */
  88. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  89. #endif
  90. #ifdef RT_USING_MAILBOX
  91. /* initialize object container - mailbox */
  92. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  93. #endif
  94. #ifdef RT_USING_MESSAGEQUEUE
  95. /* initialize object container - message queue */
  96. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  97. #endif
  98. #ifdef RT_USING_MEMHEAP
  99. /* initialize object container - memory heap */
  100. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  101. #endif
  102. #ifdef RT_USING_MEMPOOL
  103. /* initialize object container - memory pool */
  104. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  105. #endif
  106. #ifdef RT_USING_DEVICE
  107. /* initialize object container - device */
  108. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  109. #endif
  110. /* initialize object container - timer */
  111. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  112. #ifdef RT_USING_MODULE
  113. /* initialize object container - module */
  114. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
  115. #endif
  116. };
  117. #ifdef RT_USING_HOOK
  118. static void (*rt_object_attach_hook)(struct rt_object *object);
  119. static void (*rt_object_detach_hook)(struct rt_object *object);
  120. void (*rt_object_trytake_hook)(struct rt_object *object);
  121. void (*rt_object_take_hook)(struct rt_object *object);
  122. void (*rt_object_put_hook)(struct rt_object *object);
  123. /**
  124. * @addtogroup Hook
  125. */
  126. /**@{*/
  127. /**
  128. * This function will set a hook function, which will be invoked when object
  129. * attaches to kernel object system.
  130. *
  131. * @param hook the hook function
  132. */
  133. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  134. {
  135. rt_object_attach_hook = hook;
  136. }
  137. /**
  138. * This function will set a hook function, which will be invoked when object
  139. * detaches from kernel object system.
  140. *
  141. * @param hook the hook function
  142. */
  143. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  144. {
  145. rt_object_detach_hook = hook;
  146. }
  147. /**
  148. * This function will set a hook function, which will be invoked when object
  149. * is taken from kernel object system.
  150. *
  151. * The object is taken means:
  152. * semaphore - semaphore is taken by thread
  153. * mutex - mutex is taken by thread
  154. * event - event is received by thread
  155. * mailbox - mail is received by thread
  156. * message queue - message is received by thread
  157. *
  158. * @param hook the hook function
  159. */
  160. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  161. {
  162. rt_object_trytake_hook = hook;
  163. }
  164. /**
  165. * This function will set a hook function, which will be invoked when object
  166. * have been taken from kernel object system.
  167. *
  168. * The object have been taken means:
  169. * semaphore - semaphore have been taken by thread
  170. * mutex - mutex have been taken by thread
  171. * event - event have been received by thread
  172. * mailbox - mail have been received by thread
  173. * message queue - message have been received by thread
  174. * timer - timer is started
  175. *
  176. * @param hook the hook function
  177. */
  178. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  179. {
  180. rt_object_take_hook = hook;
  181. }
  182. /**
  183. * This function will set a hook function, which will be invoked when object
  184. * is put to kernel object system.
  185. *
  186. * @param hook the hook function
  187. */
  188. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  189. {
  190. rt_object_put_hook = hook;
  191. }
  192. /**@}*/
  193. #endif
  194. /**
  195. * @ingroup SystemInit
  196. *
  197. * This function will initialize system object management.
  198. *
  199. * @deprecated since 0.3.0, this function does not need to be invoked
  200. * in the system initialization.
  201. */
  202. void rt_system_object_init(void)
  203. {
  204. }
  205. /**
  206. * @addtogroup KernelObject
  207. */
  208. /**@{*/
  209. /**
  210. * This function will return the specified type of object information.
  211. *
  212. * @param type the type of object
  213. * @return the object type information or RT_NULL
  214. */
  215. struct rt_object_information *
  216. rt_object_get_information(enum rt_object_class_type type)
  217. {
  218. int index;
  219. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  220. if (rt_object_container[index].type == type) return &rt_object_container[index];
  221. return RT_NULL;
  222. }
  223. RTM_EXPORT(rt_object_get_information);
  224. /**
  225. * This function will initialize an object and add it to object system
  226. * management.
  227. *
  228. * @param object the specified object to be initialized.
  229. * @param type the object type.
  230. * @param name the object name. In system, the object's name must be unique.
  231. */
  232. void rt_object_init(struct rt_object *object,
  233. enum rt_object_class_type type,
  234. const char *name)
  235. {
  236. register rt_base_t temp;
  237. struct rt_object_information *information;
  238. #ifdef RT_USING_MODULE
  239. struct rt_dlmodule *module = dlmodule_self();
  240. #endif
  241. /* get object information */
  242. information = rt_object_get_information(type);
  243. RT_ASSERT(information != RT_NULL);
  244. /* initialize object's parameters */
  245. /* set object type to static */
  246. object->type = type | RT_Object_Class_Static;
  247. /* copy name */
  248. rt_strncpy(object->name, name, RT_NAME_MAX);
  249. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  250. /* lock interrupt */
  251. temp = rt_hw_interrupt_disable();
  252. #ifdef RT_USING_MODULE
  253. if (module)
  254. {
  255. rt_list_insert_after(&(module->object_list), &(object->list));
  256. object->module_id = (void *)module;
  257. }
  258. else
  259. #endif
  260. {
  261. /* insert object into information object list */
  262. rt_list_insert_after(&(information->object_list), &(object->list));
  263. }
  264. /* unlock interrupt */
  265. rt_hw_interrupt_enable(temp);
  266. }
  267. /**
  268. * This function will detach a static object from object system,
  269. * and the memory of static object is not freed.
  270. *
  271. * @param object the specified object to be detached.
  272. */
  273. void rt_object_detach(rt_object_t object)
  274. {
  275. register rt_base_t temp;
  276. /* object check */
  277. RT_ASSERT(object != RT_NULL);
  278. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  279. /* reset object type */
  280. object->type = 0;
  281. /* lock interrupt */
  282. temp = rt_hw_interrupt_disable();
  283. /* remove from old list */
  284. rt_list_remove(&(object->list));
  285. /* unlock interrupt */
  286. rt_hw_interrupt_enable(temp);
  287. }
  288. #ifdef RT_USING_HEAP
  289. /**
  290. * This function will allocate an object from object system
  291. *
  292. * @param type the type of object
  293. * @param name the object name. In system, the object's name must be unique.
  294. *
  295. * @return object
  296. */
  297. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  298. {
  299. struct rt_object *object;
  300. register rt_base_t temp;
  301. struct rt_object_information *information;
  302. #ifdef RT_USING_MODULE
  303. struct rt_dlmodule *module = dlmodule_self();
  304. #endif
  305. RT_DEBUG_NOT_IN_INTERRUPT;
  306. /* get object information */
  307. information = rt_object_get_information(type);
  308. RT_ASSERT(information != RT_NULL);
  309. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  310. if (object == RT_NULL)
  311. {
  312. /* no memory can be allocated */
  313. return RT_NULL;
  314. }
  315. /* clean memory data of object */
  316. rt_memset(object, 0x0, information->object_size);
  317. /* initialize object's parameters */
  318. /* set object type */
  319. object->type = type;
  320. /* set object flag */
  321. object->flag = 0;
  322. /* copy name */
  323. rt_strncpy(object->name, name, RT_NAME_MAX);
  324. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  325. /* lock interrupt */
  326. temp = rt_hw_interrupt_disable();
  327. #ifdef RT_USING_MODULE
  328. if (module)
  329. {
  330. rt_list_insert_after(&(module->object_list), &(object->list));
  331. object->module_id = (void *)module;
  332. }
  333. else
  334. #endif
  335. {
  336. /* insert object into information object list */
  337. rt_list_insert_after(&(information->object_list), &(object->list));
  338. }
  339. /* unlock interrupt */
  340. rt_hw_interrupt_enable(temp);
  341. /* return object */
  342. return object;
  343. }
  344. /**
  345. * This function will delete an object and release object memory.
  346. *
  347. * @param object the specified object to be deleted.
  348. */
  349. void rt_object_delete(rt_object_t object)
  350. {
  351. register rt_base_t temp;
  352. /* object check */
  353. RT_ASSERT(object != RT_NULL);
  354. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  355. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  356. /* reset object type */
  357. object->type = 0;
  358. /* lock interrupt */
  359. temp = rt_hw_interrupt_disable();
  360. /* remove from old list */
  361. rt_list_remove(&(object->list));
  362. /* unlock interrupt */
  363. rt_hw_interrupt_enable(temp);
  364. /* free the memory of object */
  365. RT_KERNEL_FREE(object);
  366. }
  367. #endif
  368. /**
  369. * This function will judge the object is system object or not.
  370. * Normally, the system object is a static object and the type
  371. * of object set to RT_Object_Class_Static.
  372. *
  373. * @param object the specified object to be judged.
  374. *
  375. * @return RT_TRUE if a system object, RT_FALSE for others.
  376. */
  377. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  378. {
  379. /* object check */
  380. RT_ASSERT(object != RT_NULL);
  381. if (object->type & RT_Object_Class_Static)
  382. return RT_TRUE;
  383. return RT_FALSE;
  384. }
  385. /**
  386. * This function will return the type of object without
  387. * RT_Object_Class_Static flag.
  388. *
  389. * @param object the specified object to be get type.
  390. *
  391. * @return the type of object.
  392. */
  393. rt_uint8_t rt_object_get_type(rt_object_t object)
  394. {
  395. /* object check */
  396. RT_ASSERT(object != RT_NULL);
  397. return object->type & ~RT_Object_Class_Static;
  398. }
  399. /**
  400. * This function will find specified name object from object
  401. * container.
  402. *
  403. * @param name the specified name of object.
  404. * @param type the type of object
  405. *
  406. * @return the found object or RT_NULL if there is no this object
  407. * in object container.
  408. *
  409. * @note this function shall not be invoked in interrupt status.
  410. */
  411. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  412. {
  413. struct rt_object *object = RT_NULL;
  414. struct rt_list_node *node = RT_NULL;
  415. struct rt_object_information *information = RT_NULL;
  416. /* parameter check */
  417. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  418. return RT_NULL;
  419. /* which is invoke in interrupt status */
  420. RT_DEBUG_NOT_IN_INTERRUPT;
  421. /* enter critical */
  422. rt_enter_critical();
  423. /* try to find object */
  424. if (information == RT_NULL)
  425. {
  426. information = rt_object_get_information((enum rt_object_class_type)type);
  427. RT_ASSERT(information != RT_NULL);
  428. }
  429. for (node = information->object_list.next;
  430. node != &(information->object_list);
  431. node = node->next)
  432. {
  433. object = rt_list_entry(node, struct rt_object, list);
  434. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  435. {
  436. /* leave critical */
  437. rt_exit_critical();
  438. return object;
  439. }
  440. }
  441. /* leave critical */
  442. rt_exit_critical();
  443. return RT_NULL;
  444. }
  445. /**@}*/