object.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. */
  17. #include <rtthread.h>
  18. #include <rthw.h>
  19. #ifdef RT_USING_MODULE
  20. #include <dlmodule.h>
  21. #endif /* RT_USING_MODULE */
  22. /*
  23. * define object_info for the number of _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. {&(_object_container[c].object_list), &(_object_container[c].object_list)}
  60. static struct rt_object_information _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. * @brief This function will set a hook function, which will be invoked when object
  115. * attaches to kernel object system.
  116. *
  117. * @param hook is 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. * @brief This function will set a hook function, which will be invoked when object
  125. * detaches from kernel object system.
  126. *
  127. * @param hook is 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. * @brief 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 is 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. * @brief 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. * @brief This function will set a hook function, which will be invoked when object
  170. * is put to kernel object system.
  171. *
  172. * @param hook is 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 /* RT_USING_HOOK */
  180. /**
  181. * @addtogroup KernelObject
  182. */
  183. /**@{*/
  184. /**
  185. * @brief This function will return the specified type of object information.
  186. *
  187. * @param type is the type of object, which can be
  188. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  189. *
  190. * @return the object type information or RT_NULL
  191. */
  192. struct rt_object_information *
  193. rt_object_get_information(enum rt_object_class_type type)
  194. {
  195. int index;
  196. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  197. if (_object_container[index].type == type) return &_object_container[index];
  198. return RT_NULL;
  199. }
  200. RTM_EXPORT(rt_object_get_information);
  201. /**
  202. * @brief This function will return the length of object list in object container.
  203. *
  204. * @param type is the type of object, which can be
  205. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  206. *
  207. * @return the length of object list
  208. */
  209. int rt_object_get_length(enum rt_object_class_type type)
  210. {
  211. int count = 0;
  212. rt_ubase_t level;
  213. struct rt_list_node *node = RT_NULL;
  214. struct rt_object_information *information = RT_NULL;
  215. information = rt_object_get_information((enum rt_object_class_type)type);
  216. if (information == RT_NULL) return 0;
  217. level = rt_hw_interrupt_disable();
  218. /* get the count of objects */
  219. rt_list_for_each(node, &(information->object_list))
  220. {
  221. count ++;
  222. }
  223. rt_hw_interrupt_enable(level);
  224. return count;
  225. }
  226. RTM_EXPORT(rt_object_get_length);
  227. /**
  228. * @brief This function will copy the object pointer of the specified type,
  229. * with the maximum size specified by maxlen.
  230. *
  231. * @param type is the type of object, which can be
  232. * RT_Object_Class_Thread/Semaphore/Mutex... etc
  233. *
  234. * @param pointers is the pointer will be saved to.
  235. *
  236. * @param maxlen is the maximum number of pointers can be saved.
  237. *
  238. * @return the copied number of object pointers.
  239. */
  240. int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
  241. {
  242. int index = 0;
  243. rt_ubase_t level;
  244. struct rt_object *object;
  245. struct rt_list_node *node = RT_NULL;
  246. struct rt_object_information *information = RT_NULL;
  247. if (maxlen <= 0) return 0;
  248. information = rt_object_get_information((enum rt_object_class_type)type);
  249. if (information == RT_NULL) return 0;
  250. level = rt_hw_interrupt_disable();
  251. /* retrieve pointer of object */
  252. rt_list_for_each(node, &(information->object_list))
  253. {
  254. object = rt_list_entry(node, struct rt_object, list);
  255. pointers[index] = object;
  256. index ++;
  257. if (index >= maxlen) break;
  258. }
  259. rt_hw_interrupt_enable(level);
  260. return index;
  261. }
  262. RTM_EXPORT(rt_object_get_pointers);
  263. /**
  264. * @brief This function will initialize an object and add it to object system
  265. * management.
  266. *
  267. * @param object is the specified object to be initialized.
  268. *
  269. * @param type is the object type.
  270. *
  271. * @param name is the object name. In system, the object's name must be unique.
  272. */
  273. void rt_object_init(struct rt_object *object,
  274. enum rt_object_class_type type,
  275. const char *name)
  276. {
  277. register rt_base_t temp;
  278. struct rt_list_node *node = RT_NULL;
  279. struct rt_object_information *information;
  280. #ifdef RT_USING_MODULE
  281. struct rt_dlmodule *module = dlmodule_self();
  282. #endif /* RT_USING_MODULE */
  283. /* get object information */
  284. information = rt_object_get_information(type);
  285. RT_ASSERT(information != RT_NULL);
  286. /* check object type to avoid re-initialization */
  287. /* enter critical */
  288. rt_enter_critical();
  289. /* try to find object */
  290. for (node = information->object_list.next;
  291. node != &(information->object_list);
  292. node = node->next)
  293. {
  294. struct rt_object *obj;
  295. obj = rt_list_entry(node, struct rt_object, list);
  296. if (obj) /* skip warning when disable debug */
  297. {
  298. RT_ASSERT(obj != object);
  299. }
  300. }
  301. /* leave critical */
  302. rt_exit_critical();
  303. /* initialize object's parameters */
  304. /* set object type to static */
  305. object->type = type | RT_Object_Class_Static;
  306. /* copy name */
  307. rt_strncpy(object->name, name, RT_NAME_MAX);
  308. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  309. /* lock interrupt */
  310. temp = rt_hw_interrupt_disable();
  311. #ifdef RT_USING_MODULE
  312. if (module)
  313. {
  314. rt_list_insert_after(&(module->object_list), &(object->list));
  315. object->module_id = (void *)module;
  316. }
  317. else
  318. #endif /* RT_USING_MODULE */
  319. {
  320. /* insert object into information object list */
  321. rt_list_insert_after(&(information->object_list), &(object->list));
  322. }
  323. /* unlock interrupt */
  324. rt_hw_interrupt_enable(temp);
  325. }
  326. /**
  327. * @brief This function will detach a static object from object system,
  328. * and the memory of static object is not freed.
  329. *
  330. * @param object the specified object to be detached.
  331. */
  332. void rt_object_detach(rt_object_t object)
  333. {
  334. register rt_base_t temp;
  335. /* object check */
  336. RT_ASSERT(object != RT_NULL);
  337. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  338. /* reset object type */
  339. object->type = 0;
  340. /* lock interrupt */
  341. temp = rt_hw_interrupt_disable();
  342. /* remove from old list */
  343. rt_list_remove(&(object->list));
  344. /* unlock interrupt */
  345. rt_hw_interrupt_enable(temp);
  346. }
  347. #ifdef RT_USING_HEAP
  348. /**
  349. * @brief This function will allocate an object from object system.
  350. *
  351. * @param type is the type of object.
  352. *
  353. * @param name is the object name. In system, the object's name must be unique.
  354. *
  355. * @return object
  356. */
  357. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  358. {
  359. struct rt_object *object;
  360. register rt_base_t temp;
  361. struct rt_object_information *information;
  362. #ifdef RT_USING_MODULE
  363. struct rt_dlmodule *module = dlmodule_self();
  364. #endif /* RT_USING_MODULE */
  365. RT_DEBUG_NOT_IN_INTERRUPT;
  366. /* get object information */
  367. information = rt_object_get_information(type);
  368. RT_ASSERT(information != RT_NULL);
  369. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  370. if (object == RT_NULL)
  371. {
  372. /* no memory can be allocated */
  373. return RT_NULL;
  374. }
  375. /* clean memory data of object */
  376. rt_memset(object, 0x0, information->object_size);
  377. /* initialize object's parameters */
  378. /* set object type */
  379. object->type = type;
  380. /* set object flag */
  381. object->flag = 0;
  382. /* copy name */
  383. rt_strncpy(object->name, name, RT_NAME_MAX);
  384. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  385. /* lock interrupt */
  386. temp = rt_hw_interrupt_disable();
  387. #ifdef RT_USING_MODULE
  388. if (module)
  389. {
  390. rt_list_insert_after(&(module->object_list), &(object->list));
  391. object->module_id = (void *)module;
  392. }
  393. else
  394. #endif /* RT_USING_MODULE */
  395. {
  396. /* insert object into information object list */
  397. rt_list_insert_after(&(information->object_list), &(object->list));
  398. }
  399. /* unlock interrupt */
  400. rt_hw_interrupt_enable(temp);
  401. /* return object */
  402. return object;
  403. }
  404. /**
  405. * @brief This function will delete an object and release object memory.
  406. *
  407. * @param object is the specified object to be deleted.
  408. */
  409. void rt_object_delete(rt_object_t object)
  410. {
  411. register rt_base_t temp;
  412. /* object check */
  413. RT_ASSERT(object != RT_NULL);
  414. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  415. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  416. /* reset object type */
  417. object->type = RT_Object_Class_Null;
  418. /* lock interrupt */
  419. temp = rt_hw_interrupt_disable();
  420. /* remove from old list */
  421. rt_list_remove(&(object->list));
  422. /* unlock interrupt */
  423. rt_hw_interrupt_enable(temp);
  424. /* free the memory of object */
  425. RT_KERNEL_FREE(object);
  426. }
  427. #endif /* RT_USING_HEAP */
  428. /**
  429. * @brief This function will judge the object is system object or not.
  430. *
  431. * @note Normally, the system object is a static object and the type
  432. * of object set to RT_Object_Class_Static.
  433. *
  434. * @param object is the specified object to be judged.
  435. *
  436. * @return RT_TRUE if a system object, RT_FALSE for others.
  437. */
  438. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  439. {
  440. /* object check */
  441. RT_ASSERT(object != RT_NULL);
  442. if (object->type & RT_Object_Class_Static)
  443. return RT_TRUE;
  444. return RT_FALSE;
  445. }
  446. /**
  447. * @brief This function will return the type of object without
  448. * RT_Object_Class_Static flag.
  449. *
  450. * @param object is the specified object to be get type.
  451. *
  452. * @return the type of object.
  453. */
  454. rt_uint8_t rt_object_get_type(rt_object_t object)
  455. {
  456. /* object check */
  457. RT_ASSERT(object != RT_NULL);
  458. return object->type & ~RT_Object_Class_Static;
  459. }
  460. /**
  461. * @brief This function will find specified name object from object
  462. * container.
  463. *
  464. * @param name is the specified name of object.
  465. *
  466. * @param type is the type of object
  467. *
  468. * @return the found object or RT_NULL if there is no this object
  469. * in object container.
  470. *
  471. * @note this function shall not be invoked in interrupt status.
  472. */
  473. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  474. {
  475. struct rt_object *object = RT_NULL;
  476. struct rt_list_node *node = RT_NULL;
  477. struct rt_object_information *information = RT_NULL;
  478. information = rt_object_get_information((enum rt_object_class_type)type);
  479. /* parameter check */
  480. if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
  481. /* which is invoke in interrupt status */
  482. RT_DEBUG_NOT_IN_INTERRUPT;
  483. /* enter critical */
  484. rt_enter_critical();
  485. /* try to find object */
  486. rt_list_for_each(node, &(information->object_list))
  487. {
  488. object = rt_list_entry(node, struct rt_object, list);
  489. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  490. {
  491. /* leave critical */
  492. rt_exit_critical();
  493. return object;
  494. }
  495. }
  496. /* leave critical */
  497. rt_exit_critical();
  498. return RT_NULL;
  499. }
  500. /**@}*/