object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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_object_information *information;
  224. #ifdef RT_USING_MODULE
  225. struct rt_dlmodule *module = dlmodule_self();
  226. #endif
  227. /* get object information */
  228. information = rt_object_get_information(type);
  229. RT_ASSERT(information != RT_NULL);
  230. /* initialize object's parameters */
  231. /* set object type to static */
  232. object->type = type | RT_Object_Class_Static;
  233. /* copy name */
  234. rt_strncpy(object->name, name, RT_NAME_MAX);
  235. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  236. /* lock interrupt */
  237. temp = rt_hw_interrupt_disable();
  238. #ifdef RT_USING_MODULE
  239. if (module)
  240. {
  241. rt_list_insert_after(&(module->object_list), &(object->list));
  242. object->module_id = (void *)module;
  243. }
  244. else
  245. #endif
  246. {
  247. /* insert object into information object list */
  248. rt_list_insert_after(&(information->object_list), &(object->list));
  249. }
  250. /* unlock interrupt */
  251. rt_hw_interrupt_enable(temp);
  252. }
  253. /**
  254. * This function will detach a static object from object system,
  255. * and the memory of static object is not freed.
  256. *
  257. * @param object the specified object to be detached.
  258. */
  259. void rt_object_detach(rt_object_t object)
  260. {
  261. register rt_base_t temp;
  262. /* object check */
  263. RT_ASSERT(object != RT_NULL);
  264. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  265. /* reset object type */
  266. object->type = 0;
  267. /* lock interrupt */
  268. temp = rt_hw_interrupt_disable();
  269. /* remove from old list */
  270. rt_list_remove(&(object->list));
  271. /* unlock interrupt */
  272. rt_hw_interrupt_enable(temp);
  273. }
  274. #ifdef RT_USING_HEAP
  275. /**
  276. * This function will allocate an object from object system
  277. *
  278. * @param type the type of object
  279. * @param name the object name. In system, the object's name must be unique.
  280. *
  281. * @return object
  282. */
  283. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  284. {
  285. struct rt_object *object;
  286. register rt_base_t temp;
  287. struct rt_object_information *information;
  288. #ifdef RT_USING_MODULE
  289. struct rt_dlmodule *module = dlmodule_self();
  290. #endif
  291. RT_DEBUG_NOT_IN_INTERRUPT;
  292. /* get object information */
  293. information = rt_object_get_information(type);
  294. RT_ASSERT(information != RT_NULL);
  295. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  296. if (object == RT_NULL)
  297. {
  298. /* no memory can be allocated */
  299. return RT_NULL;
  300. }
  301. /* clean memory data of object */
  302. rt_memset(object, 0x0, information->object_size);
  303. /* initialize object's parameters */
  304. /* set object type */
  305. object->type = type;
  306. /* set object flag */
  307. object->flag = 0;
  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. #ifdef RT_USING_MODULE
  314. if (module)
  315. {
  316. rt_list_insert_after(&(module->object_list), &(object->list));
  317. object->module_id = (void *)module;
  318. }
  319. else
  320. #endif
  321. {
  322. /* insert object into information object list */
  323. rt_list_insert_after(&(information->object_list), &(object->list));
  324. }
  325. /* unlock interrupt */
  326. rt_hw_interrupt_enable(temp);
  327. /* return object */
  328. return object;
  329. }
  330. /**
  331. * This function will delete an object and release object memory.
  332. *
  333. * @param object the specified object to be deleted.
  334. */
  335. void rt_object_delete(rt_object_t object)
  336. {
  337. register rt_base_t temp;
  338. /* object check */
  339. RT_ASSERT(object != RT_NULL);
  340. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  341. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  342. /* reset object type */
  343. object->type = 0;
  344. /* lock interrupt */
  345. temp = rt_hw_interrupt_disable();
  346. /* remove from old list */
  347. rt_list_remove(&(object->list));
  348. /* unlock interrupt */
  349. rt_hw_interrupt_enable(temp);
  350. /* free the memory of object */
  351. RT_KERNEL_FREE(object);
  352. }
  353. #endif
  354. /**
  355. * This function will judge the object is system object or not.
  356. * Normally, the system object is a static object and the type
  357. * of object set to RT_Object_Class_Static.
  358. *
  359. * @param object the specified object to be judged.
  360. *
  361. * @return RT_TRUE if a system object, RT_FALSE for others.
  362. */
  363. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  364. {
  365. /* object check */
  366. RT_ASSERT(object != RT_NULL);
  367. if (object->type & RT_Object_Class_Static)
  368. return RT_TRUE;
  369. return RT_FALSE;
  370. }
  371. /**
  372. * This function will return the type of object without
  373. * RT_Object_Class_Static flag.
  374. *
  375. * @param object the specified object to be get type.
  376. *
  377. * @return the type of object.
  378. */
  379. rt_uint8_t rt_object_get_type(rt_object_t object)
  380. {
  381. /* object check */
  382. RT_ASSERT(object != RT_NULL);
  383. return object->type & ~RT_Object_Class_Static;
  384. }
  385. /**
  386. * This function will find specified name object from object
  387. * container.
  388. *
  389. * @param name the specified name of object.
  390. * @param type the type of object
  391. *
  392. * @return the found object or RT_NULL if there is no this object
  393. * in object container.
  394. *
  395. * @note this function shall not be invoked in interrupt status.
  396. */
  397. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  398. {
  399. struct rt_object *object = RT_NULL;
  400. struct rt_list_node *node = RT_NULL;
  401. struct rt_object_information *information = RT_NULL;
  402. /* parameter check */
  403. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  404. return RT_NULL;
  405. /* which is invoke in interrupt status */
  406. RT_DEBUG_NOT_IN_INTERRUPT;
  407. /* enter critical */
  408. rt_enter_critical();
  409. /* try to find object */
  410. if (information == RT_NULL)
  411. {
  412. information = rt_object_get_information((enum rt_object_class_type)type);
  413. RT_ASSERT(information != RT_NULL);
  414. }
  415. for (node = information->object_list.next;
  416. node != &(information->object_list);
  417. node = node->next)
  418. {
  419. object = rt_list_entry(node, struct rt_object, list);
  420. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  421. {
  422. /* leave critical */
  423. rt_exit_critical();
  424. return object;
  425. }
  426. }
  427. /* leave critical */
  428. rt_exit_critical();
  429. return RT_NULL;
  430. }
  431. /**@}*/