object.c 16 KB

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