object.c 14 KB

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