object.c 20 KB

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