object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * File : object.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-14 Bernard the first version
  23. * 2006-04-21 Bernard change the scheduler lock to interrupt lock
  24. * 2006-05-18 Bernard fix the object init bug
  25. * 2006-08-03 Bernard add hook support
  26. * 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
  27. * 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
  28. */
  29. #include <rtthread.h>
  30. #include <rthw.h>
  31. #define _OBJ_CONTAINER_LIST_INIT(c) \
  32. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  33. struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
  34. {
  35. /* initialize object container - thread */
  36. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
  37. #ifdef RT_USING_SEMAPHORE
  38. /* initialize object container - semaphore */
  39. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
  40. #endif
  41. #ifdef RT_USING_MUTEX
  42. /* initialize object container - mutex */
  43. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},
  44. #endif
  45. #ifdef RT_USING_EVENT
  46. /* initialize object container - event */
  47. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
  48. #endif
  49. #ifdef RT_USING_MAILBOX
  50. /* initialize object container - mailbox */
  51. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},
  52. #endif
  53. #ifdef RT_USING_MESSAGEQUEUE
  54. /* initialize object container - message queue */
  55. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},
  56. #endif
  57. #ifdef RT_USING_MEMHEAP
  58. /* initialize object container - memory heap */
  59. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemHeap), sizeof(struct rt_memheap)},
  60. #endif
  61. #ifdef RT_USING_MEMPOOL
  62. /* initialize object container - memory pool */
  63. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
  64. #endif
  65. #ifdef RT_USING_DEVICE
  66. /* initialize object container - device */
  67. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
  68. #endif
  69. /* initialize object container - timer */
  70. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
  71. #ifdef RT_USING_MODULE
  72. /* initialize object container - module */
  73. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
  74. #endif
  75. };
  76. #ifdef RT_USING_HOOK
  77. static void (*rt_object_attach_hook)(struct rt_object *object);
  78. static void (*rt_object_detach_hook)(struct rt_object *object);
  79. void (*rt_object_trytake_hook)(struct rt_object *object);
  80. void (*rt_object_take_hook)(struct rt_object *object);
  81. void (*rt_object_put_hook)(struct rt_object *object);
  82. /**
  83. * @addtogroup Hook
  84. */
  85. /**@{*/
  86. /**
  87. * This function will set a hook function, which will be invoked when object
  88. * attaches to kernel object system.
  89. *
  90. * @param hook the hook function
  91. */
  92. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  93. {
  94. rt_object_attach_hook = hook;
  95. }
  96. /**
  97. * This function will set a hook function, which will be invoked when object
  98. * detaches from kernel object system.
  99. *
  100. * @param hook the hook function
  101. */
  102. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  103. {
  104. rt_object_detach_hook = hook;
  105. }
  106. /**
  107. * This function will set a hook function, which will be invoked when object
  108. * is taken from kernel object system.
  109. *
  110. * The object is taken means:
  111. * semaphore - semaphore is taken by thread
  112. * mutex - mutex is taken by thread
  113. * event - event is received by thread
  114. * mailbox - mail is received by thread
  115. * message queue - message is received by thread
  116. *
  117. * @param hook the hook function
  118. */
  119. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  120. {
  121. rt_object_trytake_hook = hook;
  122. }
  123. /**
  124. * This function will set a hook function, which will be invoked when object
  125. * have been taken from kernel object system.
  126. *
  127. * The object have been taken means:
  128. * semaphore - semaphore have been taken by thread
  129. * mutex - mutex have been taken by thread
  130. * event - event have been received by thread
  131. * mailbox - mail have been received by thread
  132. * message queue - message have been received by thread
  133. * timer - timer is started
  134. *
  135. * @param hook the hook function
  136. */
  137. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  138. {
  139. rt_object_take_hook = hook;
  140. }
  141. /**
  142. * This function will set a hook function, which will be invoked when object
  143. * is put to kernel object system.
  144. *
  145. * @param hook the hook function
  146. */
  147. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  148. {
  149. rt_object_put_hook = hook;
  150. }
  151. /**@}*/
  152. #endif
  153. /**
  154. * @ingroup SystemInit
  155. *
  156. * This function will initialize system object management.
  157. *
  158. * @deprecated since 0.3.0, this function does not need to be invoked
  159. * in the system initialization.
  160. */
  161. void rt_system_object_init(void)
  162. {
  163. }
  164. /**
  165. * @addtogroup KernelObject
  166. */
  167. /**@{*/
  168. /**
  169. * This function will return the specified type of object information.
  170. *
  171. * @param type the type of object
  172. * @return the object type information or RT_NULL
  173. */
  174. struct rt_object_information *
  175. rt_object_get_information(enum rt_object_class_type type)
  176. {
  177. return &rt_object_container[type];
  178. }
  179. RTM_EXPORT(rt_object_get_information);
  180. /**
  181. * This function will initialize an object and add it to object system
  182. * management.
  183. *
  184. * @param object the specified object to be initialized.
  185. * @param type the object type.
  186. * @param name the object name. In system, the object's name must be unique.
  187. */
  188. void rt_object_init(struct rt_object *object,
  189. enum rt_object_class_type type,
  190. const char *name)
  191. {
  192. register rt_base_t temp;
  193. struct rt_object_information *information;
  194. #ifdef RT_USING_MODULE
  195. /* get module object information */
  196. information = (rt_module_self() != RT_NULL) ?
  197. &rt_module_self()->module_object[type] : &rt_object_container[type];
  198. #else
  199. /* get object information */
  200. information = &rt_object_container[type];
  201. #endif
  202. /* initialize object's parameters */
  203. /* set object type to static */
  204. object->type = type | RT_Object_Class_Static;
  205. /* copy name */
  206. rt_strncpy(object->name, name, RT_NAME_MAX);
  207. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  208. /* lock interrupt */
  209. temp = rt_hw_interrupt_disable();
  210. /* insert object into information object list */
  211. rt_list_insert_after(&(information->object_list), &(object->list));
  212. /* unlock interrupt */
  213. rt_hw_interrupt_enable(temp);
  214. }
  215. /**
  216. * This function will detach a static object from object system,
  217. * and the memory of static object is not freed.
  218. *
  219. * @param object the specified object to be detached.
  220. */
  221. void rt_object_detach(rt_object_t object)
  222. {
  223. register rt_base_t temp;
  224. /* object check */
  225. RT_ASSERT(object != RT_NULL);
  226. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  227. /* lock interrupt */
  228. temp = rt_hw_interrupt_disable();
  229. /* remove from old list */
  230. rt_list_remove(&(object->list));
  231. /* unlock interrupt */
  232. rt_hw_interrupt_enable(temp);
  233. }
  234. #ifdef RT_USING_HEAP
  235. /**
  236. * This function will allocate an object from object system
  237. *
  238. * @param type the type of object
  239. * @param name the object name. In system, the object's name must be unique.
  240. *
  241. * @return object
  242. */
  243. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  244. {
  245. struct rt_object *object;
  246. register rt_base_t temp;
  247. struct rt_object_information *information;
  248. RT_DEBUG_NOT_IN_INTERRUPT;
  249. #ifdef RT_USING_MODULE
  250. /*
  251. * get module object information,
  252. * module object should be managed by kernel object container
  253. */
  254. information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?
  255. &rt_module_self()->module_object[type] : &rt_object_container[type];
  256. #else
  257. /* get object information */
  258. information = &rt_object_container[type];
  259. #endif
  260. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  261. if (object == RT_NULL)
  262. {
  263. /* no memory can be allocated */
  264. return RT_NULL;
  265. }
  266. /* initialize object's parameters */
  267. /* set object type */
  268. object->type = type;
  269. /* set object flag */
  270. object->flag = 0;
  271. #ifdef RT_USING_MODULE
  272. if (rt_module_self() != RT_NULL)
  273. {
  274. object->flag |= RT_OBJECT_FLAG_MODULE;
  275. }
  276. object->module_id = (void *)rt_module_self();
  277. #endif
  278. /* copy name */
  279. rt_strncpy(object->name, name, RT_NAME_MAX);
  280. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  281. /* lock interrupt */
  282. temp = rt_hw_interrupt_disable();
  283. /* insert object into information object list */
  284. rt_list_insert_after(&(information->object_list), &(object->list));
  285. /* unlock interrupt */
  286. rt_hw_interrupt_enable(temp);
  287. /* return object */
  288. return object;
  289. }
  290. /**
  291. * This function will delete an object and release object memory.
  292. *
  293. * @param object the specified object to be deleted.
  294. */
  295. void rt_object_delete(rt_object_t object)
  296. {
  297. register rt_base_t temp;
  298. /* object check */
  299. RT_ASSERT(object != RT_NULL);
  300. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  301. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  302. /* lock interrupt */
  303. temp = rt_hw_interrupt_disable();
  304. /* remove from old list */
  305. rt_list_remove(&(object->list));
  306. /* unlock interrupt */
  307. rt_hw_interrupt_enable(temp);
  308. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  309. if (object->flag & RT_OBJECT_FLAG_MODULE)
  310. rt_module_free((rt_module_t)object->module_id, object);
  311. else
  312. #endif
  313. /* free the memory of object */
  314. RT_KERNEL_FREE(object);
  315. }
  316. #endif
  317. /**
  318. * This function will judge the object is system object or not.
  319. * Normally, the system object is a static object and the type
  320. * of object set to RT_Object_Class_Static.
  321. *
  322. * @param object the specified object to be judged.
  323. *
  324. * @return RT_TRUE if a system object, RT_FALSE for others.
  325. */
  326. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  327. {
  328. /* object check */
  329. RT_ASSERT(object != RT_NULL);
  330. if (object->type & RT_Object_Class_Static)
  331. return RT_TRUE;
  332. return RT_FALSE;
  333. }
  334. /**
  335. * This function will find specified name object from object
  336. * container.
  337. *
  338. * @param name the specified name of object.
  339. * @param type the type of object
  340. *
  341. * @return the found object or RT_NULL if there is no this object
  342. * in object container.
  343. *
  344. * @note this function shall not be invoked in interrupt status.
  345. */
  346. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  347. {
  348. struct rt_object *object = RT_NULL;
  349. struct rt_list_node *node = RT_NULL;
  350. struct rt_object_information *information = RT_NULL;
  351. /* parameter check */
  352. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  353. return RT_NULL;
  354. /* which is invoke in interrupt status */
  355. RT_DEBUG_NOT_IN_INTERRUPT;
  356. #ifdef RT_USING_MODULE
  357. /* check whether to find a object inside a module. */
  358. {
  359. const char *name_ptr;
  360. int module_name_length;
  361. name_ptr = name;
  362. while ((*name_ptr != '\0') && (*name_ptr != '/'))
  363. name_ptr ++;
  364. if (*name_ptr == '/')
  365. {
  366. struct rt_module *module = RT_NULL;
  367. /* get the name length of module */
  368. module_name_length = name_ptr - name;
  369. /* enter critical */
  370. rt_enter_critical();
  371. /* find module */
  372. information = &rt_object_container[RT_Object_Class_Module];
  373. for (node = information->object_list.next;
  374. node != &(information->object_list);
  375. node = node->next)
  376. {
  377. object = rt_list_entry(node, struct rt_object, list);
  378. if ((rt_strncmp(object->name, name, module_name_length) == 0) &&
  379. (module_name_length == RT_NAME_MAX || object->name[module_name_length] == '\0'))
  380. {
  381. /* get module */
  382. module = (struct rt_module *)object;
  383. break;
  384. }
  385. }
  386. rt_exit_critical();
  387. /* there is no this module inside the system */
  388. if (module == RT_NULL) return RT_NULL;
  389. /* get the object pool of module */
  390. information = &(module->module_object[type]);
  391. /* get object name */
  392. while ((*name_ptr == '/') && (*name_ptr != '\0')) name_ptr ++;
  393. if (*name_ptr == '\0')
  394. {
  395. if (type == RT_Object_Class_Module) return object;
  396. return RT_NULL;
  397. }
  398. /* point to the object name */
  399. name = name_ptr;
  400. }
  401. }
  402. #endif
  403. /* enter critical */
  404. rt_enter_critical();
  405. /* try to find object */
  406. if (information == RT_NULL) information = &rt_object_container[type];
  407. for (node = information->object_list.next;
  408. node != &(information->object_list);
  409. node = node->next)
  410. {
  411. object = rt_list_entry(node, struct rt_object, list);
  412. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  413. {
  414. /* leave critical */
  415. rt_exit_critical();
  416. return object;
  417. }
  418. }
  419. /* leave critical */
  420. rt_exit_critical();
  421. return RT_NULL;
  422. }
  423. /**@}*/