object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * File : object.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-14 Bernard the first version
  23. * 2006-04-21 Bernard change the scheduler lock to interrupt lock
  24. * 2006-05-18 Bernard fix the object init bug
  25. * 2006-08-03 Bernard add hook support
  26. * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
  27. * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
  28. * 2017-12-10 Bernard Add object_info enum.
  29. * 2018-01-25 Bernard Fix the object find issue when enable MODULE.
  30. */
  31. #include <rtthread.h>
  32. #include <rthw.h>
  33. /*
  34. * define object_info for the number of rt_object_container items.
  35. */
  36. enum rt_object_info_type
  37. {
  38. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  39. #ifdef RT_USING_SEMAPHORE
  40. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  41. #endif
  42. #ifdef RT_USING_MUTEX
  43. RT_Object_Info_Mutex, /**< The object is a mutex. */
  44. #endif
  45. #ifdef RT_USING_EVENT
  46. RT_Object_Info_Event, /**< The object is a event. */
  47. #endif
  48. #ifdef RT_USING_MAILBOX
  49. RT_Object_Info_MailBox, /**< The object is a mail box. */
  50. #endif
  51. #ifdef RT_USING_MESSAGEQUEUE
  52. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  53. #endif
  54. #ifdef RT_USING_MEMHEAP
  55. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  56. #endif
  57. #ifdef RT_USING_MEMPOOL
  58. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  59. #endif
  60. #ifdef RT_USING_DEVICE
  61. RT_Object_Info_Device, /**< The object is a device */
  62. #endif
  63. RT_Object_Info_Timer, /**< The object is a timer. */
  64. #ifdef RT_USING_MODULE
  65. RT_Object_Info_Module, /**< The object is a module. */
  66. #endif
  67. RT_Object_Info_Unknown, /**< The object is unknown. */
  68. };
  69. #define _OBJ_CONTAINER_LIST_INIT(c) \
  70. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  71. static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =
  72. {
  73. /* initialize object container - thread */
  74. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  75. #ifdef RT_USING_SEMAPHORE
  76. /* initialize object container - semaphore */
  77. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  78. #endif
  79. #ifdef RT_USING_MUTEX
  80. /* initialize object container - mutex */
  81. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  82. #endif
  83. #ifdef RT_USING_EVENT
  84. /* initialize object container - event */
  85. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  86. #endif
  87. #ifdef RT_USING_MAILBOX
  88. /* initialize object container - mailbox */
  89. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  90. #endif
  91. #ifdef RT_USING_MESSAGEQUEUE
  92. /* initialize object container - message queue */
  93. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  94. #endif
  95. #ifdef RT_USING_MEMHEAP
  96. /* initialize object container - memory heap */
  97. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  98. #endif
  99. #ifdef RT_USING_MEMPOOL
  100. /* initialize object container - memory pool */
  101. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  102. #endif
  103. #ifdef RT_USING_DEVICE
  104. /* initialize object container - device */
  105. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  106. #endif
  107. /* initialize object container - timer */
  108. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  109. #ifdef RT_USING_MODULE
  110. /* initialize object container - module */
  111. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_module)},
  112. #endif
  113. };
  114. #ifdef RT_USING_HOOK
  115. static void (*rt_object_attach_hook)(struct rt_object *object);
  116. static void (*rt_object_detach_hook)(struct rt_object *object);
  117. void (*rt_object_trytake_hook)(struct rt_object *object);
  118. void (*rt_object_take_hook)(struct rt_object *object);
  119. void (*rt_object_put_hook)(struct rt_object *object);
  120. /**
  121. * @addtogroup Hook
  122. */
  123. /**@{*/
  124. /**
  125. * This function will set a hook function, which will be invoked when object
  126. * attaches to kernel object system.
  127. *
  128. * @param hook the hook function
  129. */
  130. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  131. {
  132. rt_object_attach_hook = hook;
  133. }
  134. /**
  135. * This function will set a hook function, which will be invoked when object
  136. * detaches from kernel object system.
  137. *
  138. * @param hook the hook function
  139. */
  140. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  141. {
  142. rt_object_detach_hook = hook;
  143. }
  144. /**
  145. * This function will set a hook function, which will be invoked when object
  146. * is taken from kernel object system.
  147. *
  148. * The object is taken means:
  149. * semaphore - semaphore is taken by thread
  150. * mutex - mutex is taken by thread
  151. * event - event is received by thread
  152. * mailbox - mail is received by thread
  153. * message queue - message is received by thread
  154. *
  155. * @param hook the hook function
  156. */
  157. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  158. {
  159. rt_object_trytake_hook = hook;
  160. }
  161. /**
  162. * This function will set a hook function, which will be invoked when object
  163. * have been taken from kernel object system.
  164. *
  165. * The object have been taken means:
  166. * semaphore - semaphore have been taken by thread
  167. * mutex - mutex have been taken by thread
  168. * event - event have been received by thread
  169. * mailbox - mail have been received by thread
  170. * message queue - message have been received by thread
  171. * timer - timer is started
  172. *
  173. * @param hook the hook function
  174. */
  175. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  176. {
  177. rt_object_take_hook = hook;
  178. }
  179. /**
  180. * This function will set a hook function, which will be invoked when object
  181. * is put to kernel object system.
  182. *
  183. * @param hook the hook function
  184. */
  185. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  186. {
  187. rt_object_put_hook = hook;
  188. }
  189. /**@}*/
  190. #endif
  191. /**
  192. * @ingroup SystemInit
  193. *
  194. * This function will initialize system object management.
  195. *
  196. * @deprecated since 0.3.0, this function does not need to be invoked
  197. * in the system initialization.
  198. */
  199. void rt_system_object_init(void)
  200. {
  201. }
  202. /**
  203. * @addtogroup KernelObject
  204. */
  205. /**@{*/
  206. /**
  207. * This function will return the specified type of object information.
  208. *
  209. * @param type the type of object
  210. * @return the object type information or RT_NULL
  211. */
  212. struct rt_object_information *
  213. rt_object_get_information(enum rt_object_class_type type)
  214. {
  215. int index;
  216. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  217. if (rt_object_container[index].type == type) return &rt_object_container[index];
  218. return RT_NULL;
  219. }
  220. RTM_EXPORT(rt_object_get_information);
  221. /**
  222. * This function will initialize an object and add it to object system
  223. * management.
  224. *
  225. * @param object the specified object to be initialized.
  226. * @param type the object type.
  227. * @param name the object name. In system, the object's name must be unique.
  228. */
  229. void rt_object_init(struct rt_object *object,
  230. enum rt_object_class_type type,
  231. const char *name)
  232. {
  233. register rt_base_t temp;
  234. struct rt_object_information *information;
  235. /* get object information */
  236. information = rt_object_get_information(type);
  237. RT_ASSERT(information != RT_NULL);
  238. /* initialize object's parameters */
  239. /* set object type to static */
  240. object->type = type | RT_Object_Class_Static;
  241. /* copy name */
  242. rt_strncpy(object->name, name, RT_NAME_MAX);
  243. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  244. /* lock interrupt */
  245. temp = rt_hw_interrupt_disable();
  246. /* insert object into information object list */
  247. rt_list_insert_after(&(information->object_list), &(object->list));
  248. /* unlock interrupt */
  249. rt_hw_interrupt_enable(temp);
  250. }
  251. /**
  252. * This function will detach a static object from object system,
  253. * and the memory of static object is not freed.
  254. *
  255. * @param object the specified object to be detached.
  256. */
  257. void rt_object_detach(rt_object_t object)
  258. {
  259. register rt_base_t temp;
  260. /* object check */
  261. RT_ASSERT(object != RT_NULL);
  262. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  263. /* lock interrupt */
  264. temp = rt_hw_interrupt_disable();
  265. /* remove from old list */
  266. rt_list_remove(&(object->list));
  267. /* unlock interrupt */
  268. rt_hw_interrupt_enable(temp);
  269. }
  270. #ifdef RT_USING_HEAP
  271. /**
  272. * This function will allocate an object from object system
  273. *
  274. * @param type the type of object
  275. * @param name the object name. In system, the object's name must be unique.
  276. *
  277. * @return object
  278. */
  279. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  280. {
  281. struct rt_object *object;
  282. register rt_base_t temp;
  283. struct rt_object_information *information;
  284. RT_DEBUG_NOT_IN_INTERRUPT;
  285. /* get object information */
  286. information = rt_object_get_information(type);
  287. RT_ASSERT(information != RT_NULL);
  288. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  289. if (object == RT_NULL)
  290. {
  291. /* no memory can be allocated */
  292. return RT_NULL;
  293. }
  294. /* initialize object's parameters */
  295. /* set object type */
  296. object->type = type;
  297. /* set object flag */
  298. object->flag = 0;
  299. #ifdef RT_USING_MODULE
  300. if (rt_module_self() != RT_NULL)
  301. {
  302. object->flag |= RT_OBJECT_FLAG_MODULE;
  303. }
  304. object->module_id = (void *)rt_module_self();
  305. #endif
  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. /* insert object into information object list */
  312. rt_list_insert_after(&(information->object_list), &(object->list));
  313. /* unlock interrupt */
  314. rt_hw_interrupt_enable(temp);
  315. /* return object */
  316. return object;
  317. }
  318. /**
  319. * This function will delete an object and release object memory.
  320. *
  321. * @param object the specified object to be deleted.
  322. */
  323. void rt_object_delete(rt_object_t object)
  324. {
  325. register rt_base_t temp;
  326. /* object check */
  327. RT_ASSERT(object != RT_NULL);
  328. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  329. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  330. /* lock interrupt */
  331. temp = rt_hw_interrupt_disable();
  332. /* remove from old list */
  333. rt_list_remove(&(object->list));
  334. /* unlock interrupt */
  335. rt_hw_interrupt_enable(temp);
  336. /* free the memory of object */
  337. RT_KERNEL_FREE(object);
  338. }
  339. #endif
  340. /**
  341. * This function will judge the object is system object or not.
  342. * Normally, the system object is a static object and the type
  343. * of object set to RT_Object_Class_Static.
  344. *
  345. * @param object the specified object to be judged.
  346. *
  347. * @return RT_TRUE if a system object, RT_FALSE for others.
  348. */
  349. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  350. {
  351. /* object check */
  352. RT_ASSERT(object != RT_NULL);
  353. if (object->type & RT_Object_Class_Static)
  354. return RT_TRUE;
  355. return RT_FALSE;
  356. }
  357. /**
  358. * This function will find specified name object from object
  359. * container.
  360. *
  361. * @param name the specified name of object.
  362. * @param type the type of object
  363. *
  364. * @return the found object or RT_NULL if there is no this object
  365. * in object container.
  366. *
  367. * @note this function shall not be invoked in interrupt status.
  368. */
  369. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  370. {
  371. struct rt_object *object = RT_NULL;
  372. struct rt_list_node *node = RT_NULL;
  373. struct rt_object_information *information = RT_NULL;
  374. /* parameter check */
  375. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  376. return RT_NULL;
  377. /* which is invoke in interrupt status */
  378. RT_DEBUG_NOT_IN_INTERRUPT;
  379. /* enter critical */
  380. rt_enter_critical();
  381. /* try to find object */
  382. if (information == RT_NULL)
  383. {
  384. information = rt_object_get_information((enum rt_object_class_type)type);
  385. RT_ASSERT(information != RT_NULL);
  386. }
  387. for (node = information->object_list.next;
  388. node != &(information->object_list);
  389. node = node->next)
  390. {
  391. object = rt_list_entry(node, struct rt_object, list);
  392. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  393. {
  394. /* leave critical */
  395. rt_exit_critical();
  396. return object;
  397. }
  398. }
  399. /* leave critical */
  400. rt_exit_critical();
  401. return RT_NULL;
  402. }
  403. /**@}*/