object.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright (c) 2006-2021, 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. * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to object.c
  17. */
  18. #include <rtthread.h>
  19. #include <rthw.h>
  20. #ifdef RT_USING_MODULE
  21. #include <dlmodule.h>
  22. #endif /* RT_USING_MODULE */
  23. /*
  24. * define object_info for the number of _object_container items.
  25. */
  26. enum rt_object_info_type
  27. {
  28. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  29. #ifdef RT_USING_SEMAPHORE
  30. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  31. #endif
  32. #ifdef RT_USING_MUTEX
  33. RT_Object_Info_Mutex, /**< The object is a mutex. */
  34. #endif
  35. #ifdef RT_USING_EVENT
  36. RT_Object_Info_Event, /**< The object is a event. */
  37. #endif
  38. #ifdef RT_USING_MAILBOX
  39. RT_Object_Info_MailBox, /**< The object is a mail box. */
  40. #endif
  41. #ifdef RT_USING_MESSAGEQUEUE
  42. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  43. #endif
  44. #ifdef RT_USING_MEMHEAP
  45. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  46. #endif
  47. #ifdef RT_USING_MEMPOOL
  48. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  49. #endif
  50. #ifdef RT_USING_DEVICE
  51. RT_Object_Info_Device, /**< The object is a device */
  52. #endif
  53. RT_Object_Info_Timer, /**< The object is a timer. */
  54. #ifdef RT_USING_MODULE
  55. RT_Object_Info_Module, /**< The object is a module. */
  56. #endif
  57. #ifdef RT_USING_HEAP
  58. RT_Object_Info_Memory, /**< The object is a memory. */
  59. #endif
  60. RT_Object_Info_Unknown, /**< The object is unknown. */
  61. };
  62. #define _OBJ_CONTAINER_LIST_INIT(c) \
  63. {&(_object_container[c].object_list), &(_object_container[c].object_list)}
  64. static struct rt_object_information _object_container[RT_Object_Info_Unknown] =
  65. {
  66. /* initialize object container - thread */
  67. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  68. #ifdef RT_USING_SEMAPHORE
  69. /* initialize object container - semaphore */
  70. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  71. #endif
  72. #ifdef RT_USING_MUTEX
  73. /* initialize object container - mutex */
  74. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  75. #endif
  76. #ifdef RT_USING_EVENT
  77. /* initialize object container - event */
  78. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  79. #endif
  80. #ifdef RT_USING_MAILBOX
  81. /* initialize object container - mailbox */
  82. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  83. #endif
  84. #ifdef RT_USING_MESSAGEQUEUE
  85. /* initialize object container - message queue */
  86. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  87. #endif
  88. #ifdef RT_USING_MEMHEAP
  89. /* initialize object container - memory heap */
  90. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  91. #endif
  92. #ifdef RT_USING_MEMPOOL
  93. /* initialize object container - memory pool */
  94. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  95. #endif
  96. #ifdef RT_USING_DEVICE
  97. /* initialize object container - device */
  98. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  99. #endif
  100. /* initialize object container - timer */
  101. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  102. #ifdef RT_USING_MODULE
  103. /* initialize object container - module */
  104. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
  105. #endif
  106. #ifdef RT_USING_HEAP
  107. /* initialize object container - small memory */
  108. {RT_Object_Class_Memory, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Memory), sizeof(struct rt_memory)},
  109. #endif
  110. };
  111. #ifndef __on_rt_object_attach_hook
  112. #define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
  113. #endif
  114. #ifndef __on_rt_object_detach_hook
  115. #define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
  116. #endif
  117. #ifndef __on_rt_object_trytake_hook
  118. #define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
  119. #endif
  120. #ifndef __on_rt_object_take_hook
  121. #define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
  122. #endif
  123. #ifndef __on_rt_object_put_hook
  124. #define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
  125. #endif
  126. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  127. static void (*rt_object_attach_hook)(struct rt_object *object);
  128. static void (*rt_object_detach_hook)(struct rt_object *object);
  129. void (*rt_object_trytake_hook)(struct rt_object *object);
  130. void (*rt_object_take_hook)(struct rt_object *object);
  131. void (*rt_object_put_hook)(struct rt_object *object);
  132. /**
  133. * @addtogroup Hook
  134. */
  135. /**@{*/
  136. /**
  137. * @brief This function will set a hook function, which will be invoked when object
  138. * attaches to kernel object system.
  139. *
  140. * @param hook is the hook function.
  141. */
  142. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  143. {
  144. rt_object_attach_hook = hook;
  145. }
  146. /**
  147. * @brief This function will set a hook function, which will be invoked when object
  148. * detaches from kernel object system.
  149. *
  150. * @param hook is the hook function
  151. */
  152. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  153. {
  154. rt_object_detach_hook = hook;
  155. }
  156. /**
  157. * @brief This function will set a hook function, which will be invoked when object
  158. * is taken from kernel object system.
  159. *
  160. * The object is taken means:
  161. * semaphore - semaphore is taken by thread
  162. * mutex - mutex is taken by thread
  163. * event - event is received by thread
  164. * mailbox - mail is received by thread
  165. * message queue - message is received by thread
  166. *
  167. * @param hook is the hook function.
  168. */
  169. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  170. {
  171. rt_object_trytake_hook = hook;
  172. }
  173. /**
  174. * @brief This function will set a hook function, which will be invoked when object
  175. * have been taken from kernel object system.
  176. *
  177. * The object have been taken means:
  178. * semaphore - semaphore have been taken by thread
  179. * mutex - mutex have been taken by thread
  180. * event - event have been received by thread
  181. * mailbox - mail have been received by thread
  182. * message queue - message have been received by thread
  183. * timer - timer is started
  184. *
  185. * @param hook the hook function.
  186. */
  187. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  188. {
  189. rt_object_take_hook = hook;
  190. }
  191. /**
  192. * @brief This function will set a hook function, which will be invoked when object
  193. * is put to kernel object system.
  194. *
  195. * @param hook is the hook function
  196. */
  197. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  198. {
  199. rt_object_put_hook = hook;
  200. }
  201. /**@}*/
  202. #endif /* RT_USING_HOOK */
  203. /**
  204. * @addtogroup KernelObject
  205. */
  206. /**@{*/
  207. /**
  208. * @brief This function will return the specified type of object information.
  209. *
  210. * @param type is the type of object, which can be
  211. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  212. *
  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 (_object_container[index].type == type) return &_object_container[index];
  221. return RT_NULL;
  222. }
  223. RTM_EXPORT(rt_object_get_information);
  224. /**
  225. * @brief This function will return the length of object list in object container.
  226. *
  227. * @param type is the type of object, which can be
  228. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  229. *
  230. * @return the length of object list
  231. */
  232. int rt_object_get_length(enum rt_object_class_type type)
  233. {
  234. int count = 0;
  235. rt_base_t level;
  236. struct rt_list_node *node = RT_NULL;
  237. struct rt_object_information *information = RT_NULL;
  238. information = rt_object_get_information((enum rt_object_class_type)type);
  239. if (information == RT_NULL) return 0;
  240. level = rt_hw_interrupt_disable();
  241. /* get the count of objects */
  242. rt_list_for_each(node, &(information->object_list))
  243. {
  244. count ++;
  245. }
  246. rt_hw_interrupt_enable(level);
  247. return count;
  248. }
  249. RTM_EXPORT(rt_object_get_length);
  250. /**
  251. * @brief This function will copy the object pointer of the specified type,
  252. * with the maximum size specified by maxlen.
  253. *
  254. * @param type is the type of object, which can be
  255. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  256. *
  257. * @param pointers is the pointer will be saved to.
  258. *
  259. * @param maxlen is the maximum number of pointers can be saved.
  260. *
  261. * @return the copied number of object pointers.
  262. */
  263. int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
  264. {
  265. int index = 0;
  266. rt_base_t level;
  267. struct rt_object *object;
  268. struct rt_list_node *node = RT_NULL;
  269. struct rt_object_information *information = RT_NULL;
  270. if (maxlen <= 0) return 0;
  271. information = rt_object_get_information((enum rt_object_class_type)type);
  272. if (information == RT_NULL) return 0;
  273. level = rt_hw_interrupt_disable();
  274. /* retrieve pointer of object */
  275. rt_list_for_each(node, &(information->object_list))
  276. {
  277. object = rt_list_entry(node, struct rt_object, list);
  278. pointers[index] = object;
  279. index ++;
  280. if (index >= maxlen) break;
  281. }
  282. rt_hw_interrupt_enable(level);
  283. return index;
  284. }
  285. RTM_EXPORT(rt_object_get_pointers);
  286. /**
  287. * @brief This function will initialize an object and add it to object system
  288. * management.
  289. *
  290. * @param object is the specified object to be initialized.
  291. *
  292. * @param type is the object type.
  293. *
  294. * @param name is the object name. In system, the object's name must be unique.
  295. */
  296. void rt_object_init(struct rt_object *object,
  297. enum rt_object_class_type type,
  298. const char *name)
  299. {
  300. rt_base_t level;
  301. #ifdef RT_DEBUG
  302. struct rt_list_node *node = RT_NULL;
  303. #endif
  304. struct rt_object_information *information;
  305. #ifdef RT_USING_MODULE
  306. struct rt_dlmodule *module = dlmodule_self();
  307. #endif /* RT_USING_MODULE */
  308. /* get object information */
  309. information = rt_object_get_information(type);
  310. RT_ASSERT(information != RT_NULL);
  311. #ifdef RT_DEBUG
  312. /* check object type to avoid re-initialization */
  313. /* enter critical */
  314. rt_enter_critical();
  315. /* try to find object */
  316. for (node = information->object_list.next;
  317. node != &(information->object_list);
  318. node = node->next)
  319. {
  320. struct rt_object *obj;
  321. obj = rt_list_entry(node, struct rt_object, list);
  322. RT_ASSERT(obj != object);
  323. }
  324. /* leave critical */
  325. rt_exit_critical();
  326. #endif
  327. /* initialize object's parameters */
  328. /* set object type to static */
  329. object->type = type | RT_Object_Class_Static;
  330. /* copy name */
  331. rt_strncpy(object->name, name, RT_NAME_MAX);
  332. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  333. /* lock interrupt */
  334. level = rt_hw_interrupt_disable();
  335. #ifdef RT_USING_MODULE
  336. if (module)
  337. {
  338. rt_list_insert_after(&(module->object_list), &(object->list));
  339. object->module_id = (void *)module;
  340. }
  341. else
  342. #endif /* RT_USING_MODULE */
  343. {
  344. /* insert object into information object list */
  345. rt_list_insert_after(&(information->object_list), &(object->list));
  346. }
  347. /* unlock interrupt */
  348. rt_hw_interrupt_enable(level);
  349. }
  350. /**
  351. * @brief This function will detach a static object from object system,
  352. * and the memory of static object is not freed.
  353. *
  354. * @param object the specified object to be detached.
  355. */
  356. void rt_object_detach(rt_object_t object)
  357. {
  358. rt_base_t level;
  359. /* object check */
  360. RT_ASSERT(object != RT_NULL);
  361. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  362. /* reset object type */
  363. object->type = 0;
  364. /* lock interrupt */
  365. level = rt_hw_interrupt_disable();
  366. /* remove from old list */
  367. rt_list_remove(&(object->list));
  368. /* unlock interrupt */
  369. rt_hw_interrupt_enable(level);
  370. }
  371. #ifdef RT_USING_HEAP
  372. /**
  373. * @brief This function will allocate an object from object system.
  374. *
  375. * @param type is the type of object.
  376. *
  377. * @param name is the object name. In system, the object's name must be unique.
  378. *
  379. * @return object
  380. */
  381. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  382. {
  383. struct rt_object *object;
  384. rt_base_t level;
  385. struct rt_object_information *information;
  386. #ifdef RT_USING_MODULE
  387. struct rt_dlmodule *module = dlmodule_self();
  388. #endif /* RT_USING_MODULE */
  389. RT_DEBUG_NOT_IN_INTERRUPT;
  390. /* get object information */
  391. information = rt_object_get_information(type);
  392. RT_ASSERT(information != RT_NULL);
  393. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  394. if (object == RT_NULL)
  395. {
  396. /* no memory can be allocated */
  397. return RT_NULL;
  398. }
  399. /* clean memory data of object */
  400. rt_memset(object, 0x0, information->object_size);
  401. /* initialize object's parameters */
  402. /* set object type */
  403. object->type = type;
  404. /* set object flag */
  405. object->flag = 0;
  406. /* copy name */
  407. rt_strncpy(object->name, name, RT_NAME_MAX);
  408. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  409. /* lock interrupt */
  410. level = rt_hw_interrupt_disable();
  411. #ifdef RT_USING_MODULE
  412. if (module)
  413. {
  414. rt_list_insert_after(&(module->object_list), &(object->list));
  415. object->module_id = (void *)module;
  416. }
  417. else
  418. #endif /* RT_USING_MODULE */
  419. {
  420. /* insert object into information object list */
  421. rt_list_insert_after(&(information->object_list), &(object->list));
  422. }
  423. /* unlock interrupt */
  424. rt_hw_interrupt_enable(level);
  425. /* return object */
  426. return object;
  427. }
  428. /**
  429. * @brief This function will delete an object and release object memory.
  430. *
  431. * @param object is the specified object to be deleted.
  432. */
  433. void rt_object_delete(rt_object_t object)
  434. {
  435. rt_base_t level;
  436. /* object check */
  437. RT_ASSERT(object != RT_NULL);
  438. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  439. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  440. /* reset object type */
  441. object->type = RT_Object_Class_Null;
  442. /* lock interrupt */
  443. level = rt_hw_interrupt_disable();
  444. /* remove from old list */
  445. rt_list_remove(&(object->list));
  446. /* unlock interrupt */
  447. rt_hw_interrupt_enable(level);
  448. /* free the memory of object */
  449. RT_KERNEL_FREE(object);
  450. }
  451. #endif /* RT_USING_HEAP */
  452. /**
  453. * @brief This function will judge the object is system object or not.
  454. *
  455. * @note Normally, the system object is a static object and the type
  456. * of object set to RT_Object_Class_Static.
  457. *
  458. * @param object is the specified object to be judged.
  459. *
  460. * @return RT_TRUE if a system object, RT_FALSE for others.
  461. */
  462. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  463. {
  464. /* object check */
  465. RT_ASSERT(object != RT_NULL);
  466. if (object->type & RT_Object_Class_Static)
  467. return RT_TRUE;
  468. return RT_FALSE;
  469. }
  470. /**
  471. * @brief This function will return the type of object without
  472. * RT_Object_Class_Static flag.
  473. *
  474. * @param object is the specified object to be get type.
  475. *
  476. * @return the type of object.
  477. */
  478. rt_uint8_t rt_object_get_type(rt_object_t object)
  479. {
  480. /* object check */
  481. RT_ASSERT(object != RT_NULL);
  482. return object->type & ~RT_Object_Class_Static;
  483. }
  484. /**
  485. * @brief This function will find specified name object from object
  486. * container.
  487. *
  488. * @param name is the specified name of object.
  489. *
  490. * @param type is the type of object
  491. *
  492. * @return the found object or RT_NULL if there is no this object
  493. * in object container.
  494. *
  495. * @note this function shall not be invoked in interrupt status.
  496. */
  497. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  498. {
  499. struct rt_object *object = RT_NULL;
  500. struct rt_list_node *node = RT_NULL;
  501. struct rt_object_information *information = RT_NULL;
  502. information = rt_object_get_information((enum rt_object_class_type)type);
  503. /* parameter check */
  504. if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
  505. /* which is invoke in interrupt status */
  506. RT_DEBUG_NOT_IN_INTERRUPT;
  507. /* enter critical */
  508. rt_enter_critical();
  509. /* try to find object */
  510. rt_list_for_each(node, &(information->object_list))
  511. {
  512. object = rt_list_entry(node, struct rt_object, list);
  513. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  514. {
  515. /* leave critical */
  516. rt_exit_critical();
  517. return object;
  518. }
  519. }
  520. /* leave critical */
  521. rt_exit_critical();
  522. return RT_NULL;
  523. }
  524. /**@}*/