object.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. */
  18. #include <rtthread.h>
  19. #include <rthw.h>
  20. #include "kservice.h"
  21. #ifdef RT_USING_MODULE
  22. extern struct rt_module* rt_current_module;
  23. #endif
  24. #define _OBJ_CONTAINER_LIST_INIT(c) \
  25. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  26. struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
  27. {
  28. /* init object container - thread */
  29. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
  30. #ifdef RT_USING_SEMAPHORE
  31. /* init object container - semaphore */
  32. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
  33. #endif
  34. #ifdef RT_USING_MUTEX
  35. /* init object container - mutex */
  36. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},
  37. #endif
  38. #ifdef RT_USING_EVENT
  39. /* init object container - event */
  40. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
  41. #endif
  42. #ifdef RT_USING_MAILBOX
  43. /* init object container - mailbox */
  44. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},
  45. #endif
  46. #ifdef RT_USING_MESSAGEQUEUE
  47. /* init object container - message queue */
  48. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},
  49. #endif
  50. #ifdef RT_USING_MEMPOOL
  51. /* init object container - memory pool */
  52. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
  53. #endif
  54. #ifdef RT_USING_DEVICE
  55. /* init object container - device */
  56. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
  57. #endif
  58. /* init object container - timer */
  59. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
  60. #ifdef RT_USING_MODULE
  61. /* init object container - module */
  62. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
  63. #endif
  64. };
  65. #ifdef RT_USING_HOOK
  66. static void (*rt_object_attach_hook)(struct rt_object* object);
  67. static void (*rt_object_detach_hook)(struct rt_object* object);
  68. void (*rt_object_trytake_hook)(struct rt_object* object);
  69. void (*rt_object_take_hook)(struct rt_object* object);
  70. void (*rt_object_put_hook)(struct rt_object* object);
  71. /**
  72. * @addtogroup Hook
  73. */
  74. /*@{*/
  75. /**
  76. * This function will set a hook function, which will be invoked when object
  77. * attaches to kernel object system.
  78. *
  79. * @param hook the hook function
  80. */
  81. void rt_object_attach_sethook(void (*hook)(struct rt_object* object))
  82. {
  83. rt_object_attach_hook = hook;
  84. }
  85. /**
  86. * This function will set a hook function, which will be invoked when object
  87. * detaches from kernel object system.
  88. *
  89. * @param hook the hook function
  90. */
  91. void rt_object_detach_sethook(void (*hook)(struct rt_object* object))
  92. {
  93. rt_object_detach_hook = hook;
  94. }
  95. /**
  96. * This function will set a hook function, which will be invoked when object
  97. * is taken from kernel object system.
  98. *
  99. * The object is taken means that
  100. * semaphore - semaphore is taken by thread
  101. * mutex - mutex is taken by thread
  102. * event - event is received by thread
  103. * mailbox - mail is received by thread
  104. * message queue - message is received by thread
  105. *
  106. * @param hook the hook function
  107. */
  108. void rt_object_trytake_sethook(void (*hook)(struct rt_object* object))
  109. {
  110. rt_object_trytake_hook = hook;
  111. }
  112. /**
  113. * This function will set a hook function, which will be invoked when object
  114. * have been taken from kernel object system.
  115. *
  116. * The object have been taken means that
  117. * semaphore - semaphore have been taken by thread
  118. * mutex - mutex have been taken by thread
  119. * event - event have been received by thread
  120. * mailbox - mail have been received by thread
  121. * message queue - message have been received by thread
  122. * timer - timer is started
  123. *
  124. * @param hook the hook function
  125. */
  126. void rt_object_take_sethook(void (*hook)(struct rt_object* object))
  127. {
  128. rt_object_take_hook = hook;
  129. }
  130. /**
  131. * This function will set a hook function, which will be invoked when object
  132. * is put to kernel object system.
  133. *
  134. * @param hook the hook function
  135. */
  136. void rt_object_put_sethook(void (*hook)(struct rt_object* object))
  137. {
  138. rt_object_put_hook = hook;
  139. }
  140. /*@}*/
  141. #endif
  142. /**
  143. * @ingroup SystemInit
  144. *
  145. * This function will initialize system object management
  146. *
  147. */
  148. void rt_system_object_init(void)
  149. {
  150. }
  151. /**
  152. * @addtogroup KernelObject
  153. */
  154. /*@{*/
  155. /**
  156. * This function will init 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
  161. * be unique.
  162. */
  163. void rt_object_init(struct rt_object* object, enum rt_object_class_type type, const char* name)
  164. {
  165. register rt_base_t temp;
  166. struct rt_object_information* information;
  167. #ifdef RT_USING_MODULE
  168. /* get module object information */
  169. information = (rt_current_module != RT_NULL) ?
  170. &rt_current_module->module_object[type] : &rt_object_container[type];
  171. #else
  172. /* get object information */
  173. information = &rt_object_container[type];
  174. #endif
  175. /* init object's parameters */
  176. /* set object type to static */
  177. object->type = type | RT_Object_Class_Static;
  178. /* copy name */
  179. for (temp = 0; temp < RT_NAME_MAX; temp ++)
  180. {
  181. object->name[temp] = name[temp];
  182. }
  183. #ifdef RT_USING_HOOK
  184. if (rt_object_attach_hook != RT_NULL)
  185. {
  186. rt_object_attach_hook(object);
  187. }
  188. #endif
  189. /* lock interrupt */
  190. temp = rt_hw_interrupt_disable();
  191. /* insert object into information object list */
  192. rt_list_insert_after(&(information->object_list), &(object->list));
  193. /* unlock interrupt */
  194. rt_hw_interrupt_enable(temp);
  195. }
  196. /**
  197. * This function will detach a static object from object system,
  198. * and the memory of static object is not freed.
  199. *
  200. * @param object the specified object to be detached.
  201. */
  202. void rt_object_detach(rt_object_t object)
  203. {
  204. register rt_base_t temp;
  205. /* object check */
  206. RT_ASSERT(object != RT_NULL);
  207. #ifdef RT_USING_HOOK
  208. if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object);
  209. #endif
  210. /* lock interrupt */
  211. temp = rt_hw_interrupt_disable();
  212. /* remove from old list */
  213. rt_list_remove(&(object->list));
  214. /* unlock interrupt */
  215. rt_hw_interrupt_enable(temp);
  216. }
  217. #ifdef RT_USING_HEAP
  218. /**
  219. * This function will allocate an object from object system
  220. *
  221. * @param type the type of object
  222. * @param name the object name. In system, the object's name must be unique.
  223. *
  224. * @return object
  225. */
  226. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
  227. {
  228. struct rt_object* object;
  229. register rt_base_t temp;
  230. struct rt_object_information* information;
  231. #ifdef RT_USING_MODULE
  232. /* get module object information */
  233. information = (rt_current_module != RT_NULL) ?
  234. &rt_current_module->module_object[type] : &rt_object_container[type];
  235. #else
  236. /* get object information */
  237. information = &rt_object_container[type];
  238. #endif
  239. object = (struct rt_object*)rt_malloc(information->object_size);
  240. if (object == RT_NULL)
  241. {
  242. /* no memory can be allocated */
  243. return RT_NULL;
  244. }
  245. /* init object's parameters */
  246. /* set object type */
  247. object->type = type;
  248. /* copy name */
  249. for (temp = 0; temp < RT_NAME_MAX; temp ++)
  250. {
  251. object->name[temp] = name[temp];
  252. }
  253. #ifdef RT_USING_HOOK
  254. if (rt_object_attach_hook != RT_NULL) rt_object_attach_hook(object);
  255. #endif
  256. /* lock interrupt */
  257. temp = rt_hw_interrupt_disable();
  258. /* insert object into information object list */
  259. rt_list_insert_after(&(information->object_list), &(object->list));
  260. /* unlock interrupt */
  261. rt_hw_interrupt_enable(temp);
  262. /* return object */
  263. return object;
  264. }
  265. /**
  266. * This function will delete an object and release object memory.
  267. *
  268. * @param object the specified object to be deleted.
  269. */
  270. void rt_object_delete(rt_object_t object)
  271. {
  272. register rt_base_t temp;
  273. /* object check */
  274. RT_ASSERT(object != RT_NULL);
  275. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  276. #ifdef RT_USING_HOOK
  277. if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object);
  278. #endif
  279. /* lock interrupt */
  280. temp = rt_hw_interrupt_disable();
  281. /* remove from old list */
  282. rt_list_remove(&(object->list));
  283. /* unlock interrupt */
  284. rt_hw_interrupt_enable(temp);
  285. /* free the memory of object */
  286. rt_free(object);
  287. }
  288. #endif
  289. /**
  290. * This function will judge the object is system object or not.
  291. * Normally, the system object is a static object and the type
  292. * of object set to RT_Object_Class_Static.
  293. *
  294. * @param object the specified object to be judged.
  295. *
  296. * @return RT_EOK if a system object, RT_ERROR for others.
  297. */
  298. rt_err_t rt_object_is_systemobject(rt_object_t object)
  299. {
  300. /* object check */
  301. RT_ASSERT(object != RT_NULL);
  302. if (object->type & RT_Object_Class_Static) return RT_EOK;
  303. return -RT_ERROR;
  304. }
  305. /*@}*/