object.c 11 KB

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