object.c 9.5 KB

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