object.c 19 KB

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