object.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* get object information */
  168. information = &rt_object_container[type];
  169. /* init object's parameters */
  170. /* set object type to static */
  171. object->type = type | RT_Object_Class_Static;
  172. /* copy name */
  173. for (temp = 0; temp < RT_NAME_MAX; temp ++)
  174. {
  175. object->name[temp] = name[temp];
  176. }
  177. #ifdef RT_USING_HOOK
  178. if (rt_object_attach_hook != RT_NULL)
  179. {
  180. rt_object_attach_hook(object);
  181. }
  182. #endif
  183. /* lock interrupt */
  184. temp = rt_hw_interrupt_disable();
  185. /* insert object into information object list */
  186. rt_list_insert_after(&(information->object_list), &(object->list));
  187. /* unlock interrupt */
  188. rt_hw_interrupt_enable(temp);
  189. }
  190. /**
  191. * This function will detach a static object from object system,
  192. * and the memory of static object is not freed.
  193. *
  194. * @param object the specified object to be detached.
  195. */
  196. void rt_object_detach(rt_object_t object)
  197. {
  198. register rt_base_t temp;
  199. /* object check */
  200. RT_ASSERT(object != RT_NULL);
  201. #ifdef RT_USING_HOOK
  202. if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object);
  203. #endif
  204. /* lock interrupt */
  205. temp = rt_hw_interrupt_disable();
  206. /* remove from old list */
  207. rt_list_remove(&(object->list));
  208. /* unlock interrupt */
  209. rt_hw_interrupt_enable(temp);
  210. }
  211. #ifdef RT_USING_HEAP
  212. /**
  213. * This function will allocate an object from object system
  214. *
  215. * @param type the type of object
  216. * @param name the object name. In system, the object's name must be unique.
  217. *
  218. * @return object
  219. */
  220. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
  221. {
  222. struct rt_object* object;
  223. register rt_base_t temp;
  224. struct rt_object_information* information;
  225. #ifdef RT_USING_MODULE
  226. /* get module object information */
  227. information = (rt_current_module != RT_NULL) ?
  228. &rt_current_module->module_object[type] : &rt_object_container[type];
  229. #else
  230. /* get object information */
  231. information = &rt_object_container[type];
  232. #endif
  233. object = (struct rt_object*)rt_malloc(information->object_size);
  234. if (object == RT_NULL)
  235. {
  236. /* no memory can be allocated */
  237. return RT_NULL;
  238. }
  239. /* init object's parameters */
  240. /* set object type */
  241. object->type = type;
  242. /* copy name */
  243. for (temp = 0; temp < RT_NAME_MAX; temp ++)
  244. {
  245. object->name[temp] = name[temp];
  246. }
  247. #ifdef RT_USING_HOOK
  248. if (rt_object_attach_hook != RT_NULL) rt_object_attach_hook(object);
  249. #endif
  250. /* lock interrupt */
  251. temp = rt_hw_interrupt_disable();
  252. /* insert object into information object list */
  253. rt_list_insert_after(&(information->object_list), &(object->list));
  254. /* unlock interrupt */
  255. rt_hw_interrupt_enable(temp);
  256. /* return object */
  257. return object;
  258. }
  259. /**
  260. * This function will delete an object and release object memory.
  261. *
  262. * @param object the specified object to be deleted.
  263. */
  264. void rt_object_delete(rt_object_t object)
  265. {
  266. register rt_base_t temp;
  267. /* object check */
  268. RT_ASSERT(object != RT_NULL);
  269. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  270. #ifdef RT_USING_HOOK
  271. if (rt_object_detach_hook != RT_NULL) rt_object_detach_hook(object);
  272. #endif
  273. /* lock interrupt */
  274. temp = rt_hw_interrupt_disable();
  275. /* remove from old list */
  276. rt_list_remove(&(object->list));
  277. /* unlock interrupt */
  278. rt_hw_interrupt_enable(temp);
  279. /* free the memory of object */
  280. rt_free(object);
  281. }
  282. #endif
  283. /**
  284. * This fucntion will find the object id by specified object name
  285. *
  286. * @param type the type of object
  287. * @param name the specified object name
  288. *
  289. * @return object id for successful
  290. */
  291. rt_object_t rt_object_find(enum rt_object_class_type type, const char* name)
  292. {
  293. struct rt_object_information *information;
  294. struct rt_object* object;
  295. struct rt_list_node* node;
  296. information = &rt_object_container[type];
  297. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  298. {
  299. object = rt_list_entry(node, struct rt_object, list);
  300. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  301. {
  302. return object;
  303. }
  304. }
  305. /* not found */
  306. return RT_NULL;
  307. }
  308. /**
  309. * This function will judge the object is system object or not.
  310. * Normally, the system object is a static object and the type
  311. * of object set to RT_Object_Class_Static.
  312. *
  313. * @param object the specified object to be judged.
  314. *
  315. * @return RT_EOK if a system object, RT_ERROR for others.
  316. */
  317. rt_err_t rt_object_is_systemobject(rt_object_t object)
  318. {
  319. /* object check */
  320. RT_ASSERT(object != RT_NULL);
  321. if (object->type & RT_Object_Class_Static) return RT_EOK;
  322. return -RT_ERROR;
  323. }
  324. /*@}*/