object.c 11 KB

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