object.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. * 2017-12-10 Bernard Add object_info enum.
  29. * 2018-01-25 Bernard Fix the object find issue when enable MODULE.
  30. */
  31. #include <rtthread.h>
  32. #include <rthw.h>
  33. /*
  34. * define object_info for the number of rt_object_container items.
  35. */
  36. enum rt_object_info_type
  37. {
  38. RT_Object_Info_Thread = 0, /**< The object is a thread. */
  39. #ifdef RT_USING_SEMAPHORE
  40. RT_Object_Info_Semaphore, /**< The object is a semaphore. */
  41. #endif
  42. #ifdef RT_USING_MUTEX
  43. RT_Object_Info_Mutex, /**< The object is a mutex. */
  44. #endif
  45. #ifdef RT_USING_EVENT
  46. RT_Object_Info_Event, /**< The object is a event. */
  47. #endif
  48. #ifdef RT_USING_MAILBOX
  49. RT_Object_Info_MailBox, /**< The object is a mail box. */
  50. #endif
  51. #ifdef RT_USING_MESSAGEQUEUE
  52. RT_Object_Info_MessageQueue, /**< The object is a message queue. */
  53. #endif
  54. #ifdef RT_USING_MEMHEAP
  55. RT_Object_Info_MemHeap, /**< The object is a memory heap */
  56. #endif
  57. #ifdef RT_USING_MEMPOOL
  58. RT_Object_Info_MemPool, /**< The object is a memory pool. */
  59. #endif
  60. #ifdef RT_USING_DEVICE
  61. RT_Object_Info_Device, /**< The object is a device */
  62. #endif
  63. RT_Object_Info_Timer, /**< The object is a timer. */
  64. #ifdef RT_USING_MODULE
  65. RT_Object_Info_Module, /**< The object is a module. */
  66. #endif
  67. RT_Object_Info_Unknown, /**< The object is unknown. */
  68. };
  69. #define _OBJ_CONTAINER_LIST_INIT(c) \
  70. {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
  71. static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =
  72. {
  73. /* initialize object container - thread */
  74. {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
  75. #ifdef RT_USING_SEMAPHORE
  76. /* initialize object container - semaphore */
  77. {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
  78. #endif
  79. #ifdef RT_USING_MUTEX
  80. /* initialize object container - mutex */
  81. {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
  82. #endif
  83. #ifdef RT_USING_EVENT
  84. /* initialize object container - event */
  85. {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
  86. #endif
  87. #ifdef RT_USING_MAILBOX
  88. /* initialize object container - mailbox */
  89. {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
  90. #endif
  91. #ifdef RT_USING_MESSAGEQUEUE
  92. /* initialize object container - message queue */
  93. {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
  94. #endif
  95. #ifdef RT_USING_MEMHEAP
  96. /* initialize object container - memory heap */
  97. {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
  98. #endif
  99. #ifdef RT_USING_MEMPOOL
  100. /* initialize object container - memory pool */
  101. {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
  102. #endif
  103. #ifdef RT_USING_DEVICE
  104. /* initialize object container - device */
  105. {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
  106. #endif
  107. /* initialize object container - timer */
  108. {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
  109. #ifdef RT_USING_MODULE
  110. /* initialize object container - module */
  111. {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_module)},
  112. #endif
  113. };
  114. #ifdef RT_USING_HOOK
  115. static void (*rt_object_attach_hook)(struct rt_object *object);
  116. static void (*rt_object_detach_hook)(struct rt_object *object);
  117. void (*rt_object_trytake_hook)(struct rt_object *object);
  118. void (*rt_object_take_hook)(struct rt_object *object);
  119. void (*rt_object_put_hook)(struct rt_object *object);
  120. /**
  121. * @addtogroup Hook
  122. */
  123. /**@{*/
  124. /**
  125. * This function will set a hook function, which will be invoked when object
  126. * attaches to kernel object system.
  127. *
  128. * @param hook the hook function
  129. */
  130. void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
  131. {
  132. rt_object_attach_hook = hook;
  133. }
  134. /**
  135. * This function will set a hook function, which will be invoked when object
  136. * detaches from kernel object system.
  137. *
  138. * @param hook the hook function
  139. */
  140. void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
  141. {
  142. rt_object_detach_hook = hook;
  143. }
  144. /**
  145. * This function will set a hook function, which will be invoked when object
  146. * is taken from kernel object system.
  147. *
  148. * The object is taken means:
  149. * semaphore - semaphore is taken by thread
  150. * mutex - mutex is taken by thread
  151. * event - event is received by thread
  152. * mailbox - mail is received by thread
  153. * message queue - message is received by thread
  154. *
  155. * @param hook the hook function
  156. */
  157. void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
  158. {
  159. rt_object_trytake_hook = hook;
  160. }
  161. /**
  162. * This function will set a hook function, which will be invoked when object
  163. * have been taken from kernel object system.
  164. *
  165. * The object have been taken means:
  166. * semaphore - semaphore have been taken by thread
  167. * mutex - mutex have been taken by thread
  168. * event - event have been received by thread
  169. * mailbox - mail have been received by thread
  170. * message queue - message have been received by thread
  171. * timer - timer is started
  172. *
  173. * @param hook the hook function
  174. */
  175. void rt_object_take_sethook(void (*hook)(struct rt_object *object))
  176. {
  177. rt_object_take_hook = hook;
  178. }
  179. /**
  180. * This function will set a hook function, which will be invoked when object
  181. * is put to kernel object system.
  182. *
  183. * @param hook the hook function
  184. */
  185. void rt_object_put_sethook(void (*hook)(struct rt_object *object))
  186. {
  187. rt_object_put_hook = hook;
  188. }
  189. /**@}*/
  190. #endif
  191. /**
  192. * @ingroup SystemInit
  193. *
  194. * This function will initialize system object management.
  195. *
  196. * @deprecated since 0.3.0, this function does not need to be invoked
  197. * in the system initialization.
  198. */
  199. void rt_system_object_init(void)
  200. {
  201. }
  202. /**
  203. * @addtogroup KernelObject
  204. */
  205. /**@{*/
  206. /**
  207. * This function will return the specified type of object information.
  208. *
  209. * @param type the type of object
  210. * @return the object type information or RT_NULL
  211. */
  212. struct rt_object_information *
  213. rt_object_get_information(enum rt_object_class_type type)
  214. {
  215. int index;
  216. for (index = 0; index < RT_Object_Info_Unknown; index ++)
  217. if (rt_object_container[index].type == type) return &rt_object_container[index];
  218. return RT_NULL;
  219. }
  220. RTM_EXPORT(rt_object_get_information);
  221. /**
  222. * This function will initialize an object and add it to object system
  223. * management.
  224. *
  225. * @param object the specified object to be initialized.
  226. * @param type the object type.
  227. * @param name the object name. In system, the object's name must be unique.
  228. */
  229. void rt_object_init(struct rt_object *object,
  230. enum rt_object_class_type type,
  231. const char *name)
  232. {
  233. register rt_base_t temp;
  234. struct rt_object_information *information;
  235. #ifdef RT_USING_MODULE
  236. /* get module object information */
  237. information = (rt_module_self() != RT_NULL) ?
  238. &rt_module_self()->module_object[type] : rt_object_get_information(type);
  239. #else
  240. /* get object information */
  241. information = rt_object_get_information(type);
  242. RT_ASSERT(information != RT_NULL);
  243. #endif
  244. /* initialize object's parameters */
  245. /* set object type to static */
  246. object->type = type | RT_Object_Class_Static;
  247. /* copy name */
  248. rt_strncpy(object->name, name, RT_NAME_MAX);
  249. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  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. }
  257. /**
  258. * This function will detach a static object from object system,
  259. * and the memory of static object is not freed.
  260. *
  261. * @param object the specified object to be detached.
  262. */
  263. void rt_object_detach(rt_object_t object)
  264. {
  265. register rt_base_t temp;
  266. /* object check */
  267. RT_ASSERT(object != RT_NULL);
  268. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  269. /* lock interrupt */
  270. temp = rt_hw_interrupt_disable();
  271. /* remove from old list */
  272. rt_list_remove(&(object->list));
  273. /* unlock interrupt */
  274. rt_hw_interrupt_enable(temp);
  275. }
  276. #ifdef RT_USING_HEAP
  277. /**
  278. * This function will allocate an object from object system
  279. *
  280. * @param type the type of object
  281. * @param name the object name. In system, the object's name must be unique.
  282. *
  283. * @return object
  284. */
  285. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
  286. {
  287. struct rt_object *object;
  288. register rt_base_t temp;
  289. struct rt_object_information *information;
  290. RT_DEBUG_NOT_IN_INTERRUPT;
  291. #ifdef RT_USING_MODULE
  292. /*
  293. * get module object information,
  294. * module object should be managed by kernel object container
  295. */
  296. information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?
  297. &rt_module_self()->module_object[type] : rt_object_get_information(type);
  298. #else
  299. /* get object information */
  300. information = rt_object_get_information(type);
  301. RT_ASSERT(information != RT_NULL);
  302. #endif
  303. object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
  304. if (object == RT_NULL)
  305. {
  306. /* no memory can be allocated */
  307. return RT_NULL;
  308. }
  309. /* initialize object's parameters */
  310. /* set object type */
  311. object->type = type;
  312. /* set object flag */
  313. object->flag = 0;
  314. #ifdef RT_USING_MODULE
  315. if (rt_module_self() != RT_NULL)
  316. {
  317. object->flag |= RT_OBJECT_FLAG_MODULE;
  318. }
  319. object->module_id = (void *)rt_module_self();
  320. #endif
  321. /* copy name */
  322. rt_strncpy(object->name, name, RT_NAME_MAX);
  323. RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
  324. /* lock interrupt */
  325. temp = rt_hw_interrupt_disable();
  326. /* insert object into information object list */
  327. rt_list_insert_after(&(information->object_list), &(object->list));
  328. /* unlock interrupt */
  329. rt_hw_interrupt_enable(temp);
  330. /* return object */
  331. return object;
  332. }
  333. /**
  334. * This function will delete an object and release object memory.
  335. *
  336. * @param object the specified object to be deleted.
  337. */
  338. void rt_object_delete(rt_object_t object)
  339. {
  340. register rt_base_t temp;
  341. /* object check */
  342. RT_ASSERT(object != RT_NULL);
  343. RT_ASSERT(!(object->type & RT_Object_Class_Static));
  344. RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
  345. /* lock interrupt */
  346. temp = rt_hw_interrupt_disable();
  347. /* remove from old list */
  348. rt_list_remove(&(object->list));
  349. /* unlock interrupt */
  350. rt_hw_interrupt_enable(temp);
  351. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
  352. if (object->flag & RT_OBJECT_FLAG_MODULE)
  353. rt_module_free((rt_module_t)object->module_id, object);
  354. else
  355. #endif
  356. /* free the memory of object */
  357. RT_KERNEL_FREE(object);
  358. }
  359. #endif
  360. /**
  361. * This function will judge the object is system object or not.
  362. * Normally, the system object is a static object and the type
  363. * of object set to RT_Object_Class_Static.
  364. *
  365. * @param object the specified object to be judged.
  366. *
  367. * @return RT_TRUE if a system object, RT_FALSE for others.
  368. */
  369. rt_bool_t rt_object_is_systemobject(rt_object_t object)
  370. {
  371. /* object check */
  372. RT_ASSERT(object != RT_NULL);
  373. if (object->type & RT_Object_Class_Static)
  374. return RT_TRUE;
  375. return RT_FALSE;
  376. }
  377. /**
  378. * This function will find specified name object from object
  379. * container.
  380. *
  381. * @param name the specified name of object.
  382. * @param type the type of object
  383. *
  384. * @return the found object or RT_NULL if there is no this object
  385. * in object container.
  386. *
  387. * @note this function shall not be invoked in interrupt status.
  388. */
  389. rt_object_t rt_object_find(const char *name, rt_uint8_t type)
  390. {
  391. struct rt_object *object = RT_NULL;
  392. struct rt_list_node *node = RT_NULL;
  393. struct rt_object_information *information = RT_NULL;
  394. /* parameter check */
  395. if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
  396. return RT_NULL;
  397. /* which is invoke in interrupt status */
  398. RT_DEBUG_NOT_IN_INTERRUPT;
  399. #ifdef RT_USING_MODULE
  400. /* check whether to find a object inside a module. */
  401. {
  402. const char *name_ptr;
  403. int module_name_length;
  404. name_ptr = name;
  405. while ((*name_ptr != '\0') && (*name_ptr != '/'))
  406. name_ptr ++;
  407. if (*name_ptr == '/')
  408. {
  409. struct rt_module *module = RT_NULL;
  410. /* get the name length of module */
  411. module_name_length = name_ptr - name;
  412. /* enter critical */
  413. rt_enter_critical();
  414. /* find module */
  415. information = rt_object_get_information(RT_Object_Class_Module);
  416. RT_ASSERT(information != RT_NULL);
  417. for (node = information->object_list.next;
  418. node != &(information->object_list);
  419. node = node->next)
  420. {
  421. object = rt_list_entry(node, struct rt_object, list);
  422. if ((rt_strncmp(object->name, name, module_name_length) == 0) &&
  423. (module_name_length == RT_NAME_MAX || object->name[module_name_length] == '\0'))
  424. {
  425. /* get module */
  426. module = (struct rt_module *)object;
  427. break;
  428. }
  429. }
  430. rt_exit_critical();
  431. /* there is no this module inside the system */
  432. if (module == RT_NULL) return RT_NULL;
  433. /* get the object pool of module */
  434. information = &(module->module_object[type]);
  435. /* get object name */
  436. while ((*name_ptr == '/') && (*name_ptr != '\0')) name_ptr ++;
  437. if (*name_ptr == '\0')
  438. {
  439. if (type == RT_Object_Class_Module) return object;
  440. return RT_NULL;
  441. }
  442. /* point to the object name */
  443. name = name_ptr;
  444. }
  445. }
  446. #endif
  447. /* enter critical */
  448. rt_enter_critical();
  449. /* try to find object */
  450. if (information == RT_NULL)
  451. {
  452. information = rt_object_get_information((enum rt_object_class_type)type);
  453. RT_ASSERT(information != RT_NULL);
  454. }
  455. for (node = information->object_list.next;
  456. node != &(information->object_list);
  457. node = node->next)
  458. {
  459. object = rt_list_entry(node, struct rt_object, list);
  460. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  461. {
  462. /* leave critical */
  463. rt_exit_critical();
  464. return object;
  465. }
  466. }
  467. /* leave critical */
  468. rt_exit_critical();
  469. return RT_NULL;
  470. }
  471. /**@}*/