object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-14 Bernard the first version
  13. * 2006-04-21 Bernard change the scheduler lock to interrupt lock
  14. * 2006-05-18 Bernard fix the object init bug
  15. * 2006-08-03 Bernard add hook support
  16. * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
  17. * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
  18. */
  19. #include <rtthread.h>
  20. #include <rthw.h>
  21. #define _OBJ_CONTAINER_LIST_INIT(c) \
  22. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  23. struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
  24. {
  25. /* initialize object container - thread */
  26. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
  27. #ifdef RT_USING_SEMAPHORE
  28. /* initialize object container - semaphore */
  29. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
  30. #endif
  31. #ifdef RT_USING_MUTEX
  32. /* initialize object container - mutex */
  33. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},
  34. #endif
  35. #ifdef RT_USING_EVENT
  36. /* initialize object container - event */
  37. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
  38. #endif
  39. #ifdef RT_USING_MAILBOX
  40. /* initialize object container - mailbox */
  41. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},
  42. #endif
  43. #ifdef RT_USING_MESSAGEQUEUE
  44. /* initialize object container - message queue */
  45. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},
  46. #endif
  47. #ifdef RT_USING_MEMHEAP
  48. /* initialize object container - memory heap */
  49. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemHeap), sizeof(struct rt_memheap)},
  50. #endif
  51. #ifdef RT_USING_MEMPOOL
  52. /* initialize object container - memory pool */
  53. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
  54. #endif
  55. #ifdef RT_USING_DEVICE
  56. /* initialize object container - device */
  57. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
  58. #endif
  59. /* initialize object container - timer */
  60. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
  61. #ifdef RT_USING_MODULE
  62. /* initialize object container - module */
  63. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
  64. #endif
  65. };
  66. #ifdef RT_USING_HOOK
  67. static void (*rt_object_attach_hook)(struct rt_object *object);
  68. static void (*rt_object_detach_hook)(struct rt_object *object);
  69. void (*rt_object_trytake_hook)(struct rt_object *object);
  70. void (*rt_object_take_hook)(struct rt_object *object);
  71. void (*rt_object_put_hook)(struct rt_object *object);
  72. /**
  73. * @addtogroup Hook
  74. */
  75. /*@{*/
  76. /**
  77. * This function will set a hook function, which will be invoked when object
  78. * attaches to kernel object system.
  79. *
  80. * @param hook the hook function
  81. */
  82. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  83. {
  84. rt_object_attach_hook = hook;
  85. }
  86. /**
  87. * This function will set a hook function, which will be invoked when object
  88. * detaches from kernel object system.
  89. *
  90. * @param hook the hook function
  91. */
  92. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  93. {
  94. rt_object_detach_hook = hook;
  95. }
  96. /**
  97. * This function will set a hook function, which will be invoked when object
  98. * is taken from kernel object system.
  99. *
  100. * The object is taken means:
  101. * semaphore - semaphore is taken by thread
  102. * mutex - mutex is taken by thread
  103. * event - event is received by thread
  104. * mailbox - mail is received by thread
  105. * message queue - message is received by thread
  106. *
  107. * @param hook the hook function
  108. */
  109. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  110. {
  111. rt_object_trytake_hook = hook;
  112. }
  113. /**
  114. * This function will set a hook function, which will be invoked when object
  115. * have been taken from kernel object system.
  116. *
  117. * The object have been taken means:
  118. * semaphore - semaphore have been taken by thread
  119. * mutex - mutex have been taken by thread
  120. * event - event have been received by thread
  121. * mailbox - mail have been received by thread
  122. * message queue - message have been received by thread
  123. * timer - timer is started
  124. *
  125. * @param hook the hook function
  126. */
  127. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  128. {
  129. rt_object_take_hook = hook;
  130. }
  131. /**
  132. * This function will set a hook function, which will be invoked when object
  133. * is put to kernel object system.
  134. *
  135. * @param hook the hook function
  136. */
  137. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  138. {
  139. rt_object_put_hook = hook;
  140. }
  141. /*@}*/
  142. #endif
  143. /**
  144. * @ingroup SystemInit
  145. *
  146. * This function will initialize system object management.
  147. *
  148. * @deprecated since 0.3.0, this function does not need to be invoked
  149. * in the system initialization.
  150. */
  151. void rt_system_object_init(void)
  152. {
  153. }
  154. /**
  155. * @addtogroup KernelObject
  156. */
  157. /*@{*/
  158. /**
  159. * This function will return the specified type of object information.
  160. *
  161. * @param type the type of object
  162. * @return the object type information or RT_NULL
  163. */
  164. struct rt_object_information *
  165. rt_object_get_information(enum rt_object_class_type type)
  166. {
  167. return &rt_object_container[type];
  168. }
  169. RTM_EXPORT(rt_object_get_information);
  170. /**
  171. * This function will initialize an object and add it to object system
  172. * management.
  173. *
  174. * @param object the specified object to be initialized.
  175. * @param type the object type.
  176. * @param name the object name. In system, the object's name must be unique.
  177. */
  178. void rt_object_init(struct rt_object *object,
  179. enum rt_object_class_type type,
  180. const char *name)
  181. {
  182. register rt_base_t temp;
  183. struct rt_object_information *information;
  184. #ifdef RT_USING_MODULE
  185. /* get module object information */
  186. information = (rt_module_self() != RT_NULL) ?
  187. &rt_module_self()->module_object[type] : &rt_object_container[type];
  188. #else
  189. /* get object information */
  190. information = &rt_object_container[type];
  191. #endif
  192. /* initialize object's parameters */
  193. /* set object type to static */
  194. object->type = type | RT_Object_Class_Static;
  195. /* copy name */
  196. rt_strncpy(object->name, name, RT_NAME_MAX);
  197. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  198. /* lock interrupt */
  199. temp = rt_hw_interrupt_disable();
  200. /* insert object into information object list */
  201. rt_list_insert_after(&(information->object_list), &(object->list));
  202. /* unlock interrupt */
  203. rt_hw_interrupt_enable(temp);
  204. }
  205. /**
  206. * This function will detach a static object from object system,
  207. * and the memory of static object is not freed.
  208. *
  209. * @param object the specified object to be detached.
  210. */
  211. void rt_object_detach(rt_object_t object)
  212. {
  213. register rt_base_t temp;
  214. /* object check */
  215. RT_ASSERT(object != RT_NULL);
  216. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  217. /* lock interrupt */
  218. temp = rt_hw_interrupt_disable();
  219. /* remove from old list */
  220. rt_list_remove(&(object->list));
  221. /* unlock interrupt */
  222. rt_hw_interrupt_enable(temp);
  223. }
  224. #ifdef RT_USING_HEAP
  225. /**
  226. * This function will allocate an object from object system
  227. *
  228. * @param type the type of object
  229. * @param name the object name. In system, the object's name must be unique.
  230. *
  231. * @return object
  232. */
  233. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  234. {
  235. struct rt_object *object;
  236. register rt_base_t temp;
  237. struct rt_object_information *information;
  238. RT_DEBUG_NOT_IN_INTERRUPT;
  239. #ifdef RT_USING_MODULE
  240. /*
  241. * get module object information,
  242. * module object should be managed by kernel object container
  243. */
  244. information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?
  245. &rt_module_self()->module_object[type] : &rt_object_container[type];
  246. #else
  247. /* get object information */
  248. information = &rt_object_container[type];
  249. #endif
  250. object = (struct rt_object *)rt_malloc(information->object_size);
  251. if (object == RT_NULL)
  252. {
  253. /* no memory can be allocated */
  254. return RT_NULL;
  255. }
  256. /* initialize object's parameters */
  257. /* set object type */
  258. object->type = type;
  259. /* set object flag */
  260. object->flag = 0;
  261. #ifdef RT_USING_MODULE
  262. if (rt_module_self() != RT_NULL)
  263. {
  264. object->flag |= RT_OBJECT_FLAG_MODULE;
  265. }
  266. object->module_id = (void *)rt_module_self();
  267. #endif
  268. /* copy name */
  269. rt_strncpy(object->name, name, RT_NAME_MAX);
  270. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  271. /* lock interrupt */
  272. temp = rt_hw_interrupt_disable();
  273. /* insert object into information object list */
  274. rt_list_insert_after(&(information->object_list), &(object->list));
  275. /* unlock interrupt */
  276. rt_hw_interrupt_enable(temp);
  277. /* return object */
  278. return object;
  279. }
  280. /**
  281. * This function will delete an object and release object memory.
  282. *
  283. * @param object the specified object to be deleted.
  284. */
  285. void rt_object_delete(rt_object_t object)
  286. {
  287. register rt_base_t temp;
  288. /* object check */
  289. RT_ASSERT(object != RT_NULL);
  290. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  291. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  292. /* lock interrupt */
  293. temp = rt_hw_interrupt_disable();
  294. /* remove from old list */
  295. rt_list_remove(&(object->list));
  296. /* unlock interrupt */
  297. rt_hw_interrupt_enable(temp);
  298. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  299. if (object->flag & RT_OBJECT_FLAG_MODULE)
  300. rt_module_free((rt_module_t)object->module_id, object);
  301. else
  302. #endif
  303. /* free the memory of object */
  304. rt_free(object);
  305. }
  306. #endif
  307. /**
  308. * This function will judge the object is system object or not.
  309. * Normally, the system object is a static object and the type
  310. * of object set to RT_Object_Class_Static.
  311. *
  312. * @param object the specified object to be judged.
  313. *
  314. * @return RT_TRUE if a system object, RT_FALSE for others.
  315. */
  316. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  317. {
  318. /* object check */
  319. RT_ASSERT(object != RT_NULL);
  320. if (object->type & RT_Object_Class_Static)
  321. return RT_TRUE;
  322. return RT_FALSE;
  323. }
  324. /**
  325. * This function will find specified name object from object
  326. * container.
  327. *
  328. * @param name the specified name of object.
  329. * @param type the type of object
  330. *
  331. * @return the found object or RT_NULL if there is no this object
  332. * in object container.
  333. *
  334. * @note this function shall not be invoked in interrupt status.
  335. */
  336. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  337. {
  338. struct rt_object *object;
  339. struct rt_list_node *node;
  340. struct rt_object_information *information;
  341. extern volatile rt_uint8_t rt_interrupt_nest;
  342. /* parameter check */
  343. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  344. return RT_NULL;
  345. /* which is invoke in interrupt status */
  346. if (rt_interrupt_nest != 0)
  347. RT_ASSERT(0);
  348. /* enter critical */
  349. rt_enter_critical();
  350. /* try to find object */
  351. information = &rt_object_container[type];
  352. for (node = information->object_list.next;
  353. node != &(information->object_list);
  354. node = node->next)
  355. {
  356. object = rt_list_entry(node, struct rt_object, list);
  357. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  358. {
  359. /* leave critical */
  360. rt_exit_critical();
  361. return object;
  362. }
  363. }
  364. /* leave critical */
  365. rt_exit_critical();
  366. return RT_NULL;
  367. }
  368. /*@}*/