2009-07-03 06:48:23 +08:00
|
|
|
/*
|
2021-03-08 11:25:38 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2006-03-14 Bernard the first version
|
|
|
|
* 2006-04-21 Bernard change the scheduler lock to interrupt lock
|
|
|
|
* 2006-05-18 Bernard fix the object init bug
|
|
|
|
* 2006-08-03 Bernard add hook support
|
|
|
|
* 2007-01-28 Bernard rename RT_OBJECT_Class_Static to RT_Object_Class_Static
|
2010-10-28 09:21:47 +08:00
|
|
|
* 2010-10-26 yi.qiu add module support in rt_object_allocate and rt_object_free
|
2017-12-24 00:00:18 +08:00
|
|
|
* 2017-12-10 Bernard Add object_info enum.
|
2018-01-25 17:46:59 +08:00
|
|
|
* 2018-01-25 Bernard Fix the object find issue when enable MODULE.
|
2022-01-08 07:35:44 +08:00
|
|
|
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to object.c
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
#include <dlmodule.h>
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2018-08-30 20:27:45 +08:00
|
|
|
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <lwp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct rt_custom_object
|
|
|
|
{
|
|
|
|
struct rt_object parent;
|
|
|
|
rt_err_t (*destroy)(void *);
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2017-12-24 00:00:18 +08:00
|
|
|
/*
|
2021-08-06 02:56:42 +08:00
|
|
|
* define object_info for the number of _object_container items.
|
2017-12-24 00:00:18 +08:00
|
|
|
*/
|
|
|
|
enum rt_object_info_type
|
|
|
|
{
|
|
|
|
RT_Object_Info_Thread = 0, /**< The object is a thread. */
|
|
|
|
#ifdef RT_USING_SEMAPHORE
|
|
|
|
RT_Object_Info_Semaphore, /**< The object is a semaphore. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MUTEX
|
|
|
|
RT_Object_Info_Mutex, /**< The object is a mutex. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_EVENT
|
|
|
|
RT_Object_Info_Event, /**< The object is a event. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MAILBOX
|
|
|
|
RT_Object_Info_MailBox, /**< The object is a mail box. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MESSAGEQUEUE
|
|
|
|
RT_Object_Info_MessageQueue, /**< The object is a message queue. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MEMHEAP
|
|
|
|
RT_Object_Info_MemHeap, /**< The object is a memory heap */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MEMPOOL
|
|
|
|
RT_Object_Info_MemPool, /**< The object is a memory pool. */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_DEVICE
|
|
|
|
RT_Object_Info_Device, /**< The object is a device */
|
|
|
|
#endif
|
|
|
|
RT_Object_Info_Timer, /**< The object is a timer. */
|
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
RT_Object_Info_Module, /**< The object is a module. */
|
2021-12-16 16:23:58 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
RT_Object_Info_Memory, /**< The object is a memory. */
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_Object_Info_Channel, /**< The object is a IPC channel */
|
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
RT_Object_Info_Custom, /**< The object is a custom object */
|
2017-12-24 00:00:18 +08:00
|
|
|
#endif
|
|
|
|
RT_Object_Info_Unknown, /**< The object is unknown. */
|
|
|
|
};
|
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
#define _OBJ_CONTAINER_LIST_INIT(c) \
|
2021-08-06 02:56:42 +08:00
|
|
|
{&(_object_container[c].object_list), &(_object_container[c].object_list)}
|
|
|
|
|
|
|
|
static struct rt_object_information _object_container[RT_Object_Info_Unknown] =
|
2010-03-03 17:11:00 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - thread */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#ifdef RT_USING_SEMAPHORE
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - semaphore */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MUTEX
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - mutex */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_EVENT
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - event */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MAILBOX
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - mailbox */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_MESSAGEQUEUE
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - message queue */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
2012-04-14 11:52:56 +08:00
|
|
|
#ifdef RT_USING_MEMHEAP
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - memory heap */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
|
2012-04-14 11:52:56 +08:00
|
|
|
#endif
|
2010-03-03 17:11:00 +08:00
|
|
|
#ifdef RT_USING_MEMPOOL
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - memory pool */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
|
|
|
#ifdef RT_USING_DEVICE
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - device */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - timer */
|
2017-12-25 20:55:20 +08:00
|
|
|
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object container - module */
|
2018-08-30 20:27:45 +08:00
|
|
|
{RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
|
2010-03-03 17:11:00 +08:00
|
|
|
#endif
|
2021-12-16 16:23:58 +08:00
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/* initialize object container - small memory */
|
|
|
|
{RT_Object_Class_Memory, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Memory), sizeof(struct rt_memory)},
|
|
|
|
#endif
|
2022-12-16 18:38:28 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-03 12:07:44 +08:00
|
|
|
/* initialize object container - module */
|
|
|
|
{RT_Object_Class_Channel, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Channel), sizeof(struct rt_channel)},
|
|
|
|
{RT_Object_Class_Custom, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Custom), sizeof(struct rt_custom_object)},
|
|
|
|
#endif
|
2010-03-03 17:11:00 +08:00
|
|
|
};
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2022-01-08 07:35:44 +08:00
|
|
|
#ifndef __on_rt_object_attach_hook
|
|
|
|
#define __on_rt_object_attach_hook(obj) __ON_HOOK_ARGS(rt_object_attach_hook, (obj))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_object_detach_hook
|
|
|
|
#define __on_rt_object_detach_hook(obj) __ON_HOOK_ARGS(rt_object_detach_hook, (obj))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_object_trytake_hook
|
|
|
|
#define __on_rt_object_trytake_hook(parent) __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_object_take_hook
|
|
|
|
#define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_object_put_hook
|
|
|
|
#define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
|
2011-09-21 11:56:42 +08:00
|
|
|
static void (*rt_object_attach_hook)(struct rt_object *object);
|
|
|
|
static void (*rt_object_detach_hook)(struct rt_object *object);
|
|
|
|
void (*rt_object_trytake_hook)(struct rt_object *object);
|
|
|
|
void (*rt_object_take_hook)(struct rt_object *object);
|
|
|
|
void (*rt_object_put_hook)(struct rt_object *object);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Hook
|
|
|
|
*/
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2016-08-19 10:15:10 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when object
|
|
|
|
* attaches to kernel object system.
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param hook is the hook function.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
rt_object_attach_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when object
|
|
|
|
* detaches from kernel object system.
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param hook is the hook function
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
rt_object_detach_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when object
|
|
|
|
* is taken from kernel object system.
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-10 15:55:08 +08:00
|
|
|
* The object is taken means:
|
|
|
|
* semaphore - semaphore is taken by thread
|
|
|
|
* mutex - mutex is taken by thread
|
|
|
|
* event - event is received by thread
|
|
|
|
* mailbox - mail is received by thread
|
|
|
|
* message queue - message is received by thread
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param hook is the hook function.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
rt_object_trytake_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when object
|
|
|
|
* have been taken from kernel object system.
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-10 15:55:08 +08:00
|
|
|
* The object have been taken means:
|
|
|
|
* semaphore - semaphore have been taken by thread
|
|
|
|
* mutex - mutex have been taken by thread
|
|
|
|
* event - event have been received by thread
|
|
|
|
* mailbox - mail have been received by thread
|
|
|
|
* message queue - message have been received by thread
|
|
|
|
* timer - timer is started
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-10 15:55:08 +08:00
|
|
|
* @param hook the hook function.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_object_take_sethook(void (*hook)(struct rt_object *object))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
rt_object_take_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will set a hook function, which will be invoked when object
|
|
|
|
* is put to kernel object system.
|
2009-09-14 07:34:52 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param hook is the hook function
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_object_put_sethook(void (*hook)(struct rt_object *object))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
rt_object_put_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:15:10 +08:00
|
|
|
/**@}*/
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HOOK */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup KernelObject
|
|
|
|
*/
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2016-08-19 10:15:10 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2011-06-15 07:59:42 +08:00
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will return the specified type of object information.
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the type of object, which can be
|
2020-04-10 00:01:58 +08:00
|
|
|
* RT_Object_Class_Thread/Semaphore/Mutex... etc
|
|
|
|
*
|
2011-06-15 07:59:42 +08:00
|
|
|
* @return the object type information or RT_NULL
|
|
|
|
*/
|
2012-12-25 14:45:56 +08:00
|
|
|
struct rt_object_information *
|
|
|
|
rt_object_get_information(enum rt_object_class_type type)
|
2011-06-15 07:59:42 +08:00
|
|
|
{
|
2017-12-24 00:00:18 +08:00
|
|
|
int index;
|
2017-12-25 20:55:20 +08:00
|
|
|
|
2017-12-24 00:00:18 +08:00
|
|
|
for (index = 0; index < RT_Object_Info_Unknown; index ++)
|
2021-08-06 02:56:42 +08:00
|
|
|
if (_object_container[index].type == type) return &_object_container[index];
|
2017-12-25 20:55:20 +08:00
|
|
|
|
2017-12-24 00:00:18 +08:00
|
|
|
return RT_NULL;
|
2011-06-15 07:59:42 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_object_get_information);
|
2011-06-15 07:59:42 +08:00
|
|
|
|
2020-04-10 00:01:58 +08:00
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will return the length of object list in object container.
|
2020-04-10 00:01:58 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the type of object, which can be
|
2020-04-10 00:01:58 +08:00
|
|
|
* RT_Object_Class_Thread/Semaphore/Mutex... etc
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2020-04-10 00:01:58 +08:00
|
|
|
* @return the length of object list
|
|
|
|
*/
|
|
|
|
int rt_object_get_length(enum rt_object_class_type type)
|
|
|
|
{
|
|
|
|
int count = 0;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2020-04-10 00:01:58 +08:00
|
|
|
struct rt_list_node *node = RT_NULL;
|
|
|
|
struct rt_object_information *information = RT_NULL;
|
|
|
|
|
|
|
|
information = rt_object_get_information((enum rt_object_class_type)type);
|
|
|
|
if (information == RT_NULL) return 0;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* get the count of objects */
|
|
|
|
rt_list_for_each(node, &(information->object_list))
|
|
|
|
{
|
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_object_get_length);
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will copy the object pointer of the specified type,
|
|
|
|
* with the maximum size specified by maxlen.
|
2020-04-10 00:01:58 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the type of object, which can be
|
2020-04-10 00:01:58 +08:00
|
|
|
* RT_Object_Class_Thread/Semaphore/Mutex... etc
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param pointers is the pointer will be saved to.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param maxlen is the maximum number of pointers can be saved.
|
2020-04-10 00:01:58 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @return the copied number of object pointers.
|
2020-04-10 00:01:58 +08:00
|
|
|
*/
|
|
|
|
int rt_object_get_pointers(enum rt_object_class_type type, rt_object_t *pointers, int maxlen)
|
|
|
|
{
|
|
|
|
int index = 0;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2020-04-10 00:01:58 +08:00
|
|
|
|
|
|
|
struct rt_object *object;
|
|
|
|
struct rt_list_node *node = RT_NULL;
|
|
|
|
struct rt_object_information *information = RT_NULL;
|
|
|
|
|
|
|
|
if (maxlen <= 0) return 0;
|
|
|
|
|
|
|
|
information = rt_object_get_information((enum rt_object_class_type)type);
|
|
|
|
if (information == RT_NULL) return 0;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* retrieve pointer of object */
|
|
|
|
rt_list_for_each(node, &(information->object_list))
|
|
|
|
{
|
|
|
|
object = rt_list_entry(node, struct rt_object, list);
|
|
|
|
|
|
|
|
pointers[index] = object;
|
|
|
|
index ++;
|
2020-05-17 23:14:24 +08:00
|
|
|
|
|
|
|
if (index >= maxlen) break;
|
2020-04-10 00:01:58 +08:00
|
|
|
}
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_object_get_pointers);
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will initialize an object and add it to object system
|
|
|
|
* management.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param object is the specified object to be initialized.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the object type.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param name is the object name. In system, the object's name must be unique.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2012-12-25 14:45:56 +08:00
|
|
|
void rt_object_init(struct rt_object *object,
|
|
|
|
enum rt_object_class_type type,
|
|
|
|
const char *name)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2022-09-25 07:49:26 +08:00
|
|
|
#ifdef RT_DEBUG
|
2019-04-02 17:33:07 +08:00
|
|
|
struct rt_list_node *node = RT_NULL;
|
2022-09-25 07:49:26 +08:00
|
|
|
#endif
|
2012-12-25 14:45:56 +08:00
|
|
|
struct rt_object_information *information;
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
struct rt_dlmodule *module = dlmodule_self();
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* get object information */
|
2017-12-24 00:00:18 +08:00
|
|
|
information = rt_object_get_information(type);
|
|
|
|
RT_ASSERT(information != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2022-09-25 07:49:26 +08:00
|
|
|
#ifdef RT_DEBUG
|
2019-03-26 21:49:18 +08:00
|
|
|
/* check object type to avoid re-initialization */
|
|
|
|
|
2019-04-02 17:33:07 +08:00
|
|
|
/* enter critical */
|
|
|
|
rt_enter_critical();
|
|
|
|
/* try to find object */
|
|
|
|
for (node = information->object_list.next;
|
|
|
|
node != &(information->object_list);
|
|
|
|
node = node->next)
|
|
|
|
{
|
|
|
|
struct rt_object *obj;
|
|
|
|
|
|
|
|
obj = rt_list_entry(node, struct rt_object, list);
|
2022-09-25 07:49:26 +08:00
|
|
|
RT_ASSERT(obj != object);
|
2019-04-02 17:33:07 +08:00
|
|
|
}
|
|
|
|
/* leave critical */
|
|
|
|
rt_exit_critical();
|
2022-09-25 07:49:26 +08:00
|
|
|
#endif
|
2019-04-02 17:33:07 +08:00
|
|
|
|
|
|
|
/* initialize object's parameters */
|
2012-12-25 14:45:56 +08:00
|
|
|
/* set object type to static */
|
|
|
|
object->type = type | RT_Object_Class_Static;
|
|
|
|
/* copy name */
|
2022-11-22 10:27:52 +08:00
|
|
|
rt_strncpy(object->name, name, RT_NAME_MAX);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* lock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
rt_list_insert_after(&(module->object_list), &(object->list));
|
|
|
|
object->module_id = (void *)module;
|
|
|
|
}
|
|
|
|
else
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2018-08-30 20:27:45 +08:00
|
|
|
{
|
|
|
|
/* insert object into information object list */
|
|
|
|
rt_list_insert_after(&(information->object_list), &(object->list));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* unlock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will detach a static object from object system,
|
|
|
|
* and the memory of static object is not freed.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* @param object the specified object to be detached.
|
|
|
|
*/
|
|
|
|
void rt_object_detach(rt_object_t object)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* object check */
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-07-07 13:59:36 +08:00
|
|
|
/* reset object type */
|
|
|
|
object->type = 0;
|
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* lock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* remove from old list */
|
|
|
|
rt_list_remove(&(object->list));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* unlock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will allocate an object from object system.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the type of object.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param name is the object name. In system, the object's name must be unique.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
struct rt_object *object;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-12-25 14:45:56 +08:00
|
|
|
struct rt_object_information *information;
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
struct rt_dlmodule *module = dlmodule_self();
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
2011-06-12 18:01:48 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* get object information */
|
2017-12-24 00:00:18 +08:00
|
|
|
information = rt_object_get_information(type);
|
|
|
|
RT_ASSERT(information != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2013-04-25 14:22:07 +08:00
|
|
|
object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
|
2012-12-25 14:45:56 +08:00
|
|
|
if (object == RT_NULL)
|
|
|
|
{
|
|
|
|
/* no memory can be allocated */
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
2013-06-24 17:06:09 +08:00
|
|
|
|
2018-08-30 20:27:45 +08:00
|
|
|
/* clean memory data of object */
|
|
|
|
rt_memset(object, 0x0, information->object_size);
|
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* initialize object's parameters */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* set object type */
|
|
|
|
object->type = type;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* set object flag */
|
|
|
|
object->flag = 0;
|
2010-10-28 09:21:47 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* copy name */
|
|
|
|
rt_strncpy(object->name, name, RT_NAME_MAX);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* lock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-08-30 20:27:45 +08:00
|
|
|
#ifdef RT_USING_MODULE
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
rt_list_insert_after(&(module->object_list), &(object->list));
|
|
|
|
object->module_id = (void *)module;
|
|
|
|
}
|
|
|
|
else
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_MODULE */
|
2018-08-30 20:27:45 +08:00
|
|
|
{
|
|
|
|
/* insert object into information object list */
|
|
|
|
rt_list_insert_after(&(information->object_list), &(object->list));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* unlock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* return object */
|
|
|
|
return object;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will delete an object and release object memory.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param object is the specified object to be deleted.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
void rt_object_delete(rt_object_t object)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* object check */
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
|
|
|
RT_ASSERT(!(object->type & RT_Object_Class_Static));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-07-07 13:59:36 +08:00
|
|
|
/* reset object type */
|
2020-11-24 04:17:50 +08:00
|
|
|
object->type = RT_Object_Class_Null;
|
2018-07-07 13:59:36 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* lock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* remove from old list */
|
|
|
|
rt_list_remove(&(object->list));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* unlock interrupt */
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-06-10 18:46:11 +08:00
|
|
|
/* free the memory of object */
|
|
|
|
RT_KERNEL_FREE(object);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will judge the object is system object or not.
|
|
|
|
*
|
|
|
|
* @note Normally, the system object is a static object and the type
|
|
|
|
* of object set to RT_Object_Class_Static.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param object is the specified object to be judged.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2012-06-02 17:20:19 +08:00
|
|
|
* @return RT_TRUE if a system object, RT_FALSE for others.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2012-06-02 17:20:19 +08:00
|
|
|
rt_bool_t rt_object_is_systemobject(rt_object_t object)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-25 14:45:56 +08:00
|
|
|
/* object check */
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
if (object->type & RT_Object_Class_Static)
|
|
|
|
return RT_TRUE;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
return RT_FALSE;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2018-07-11 11:45:37 +08:00
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will return the type of object without
|
|
|
|
* RT_Object_Class_Static flag.
|
2018-07-11 11:45:37 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param object is the specified object to be get type.
|
2018-07-11 11:45:37 +08:00
|
|
|
*
|
|
|
|
* @return the type of object.
|
|
|
|
*/
|
|
|
|
rt_uint8_t rt_object_get_type(rt_object_t object)
|
|
|
|
{
|
|
|
|
/* object check */
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
|
|
|
|
|
|
|
return object->type & ~RT_Object_Class_Static;
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:57:33 +08:00
|
|
|
/**
|
2021-09-10 15:55:08 +08:00
|
|
|
* @brief This function will find specified name object from object
|
|
|
|
* container.
|
2010-11-22 07:57:33 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param name is the specified name of object.
|
2021-09-10 15:55:08 +08:00
|
|
|
*
|
2021-09-11 16:40:56 +08:00
|
|
|
* @param type is the type of object
|
2010-11-22 07:57:33 +08:00
|
|
|
*
|
|
|
|
* @return the found object or RT_NULL if there is no this object
|
|
|
|
* in object container.
|
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @note this function shall not be invoked in interrupt status.
|
2010-11-22 07:57:33 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
|
2010-11-22 07:57:33 +08:00
|
|
|
{
|
2015-08-03 16:02:02 +08:00
|
|
|
struct rt_object *object = RT_NULL;
|
|
|
|
struct rt_list_node *node = RT_NULL;
|
2014-07-21 06:28:43 +08:00
|
|
|
struct rt_object_information *information = RT_NULL;
|
2012-12-25 14:45:56 +08:00
|
|
|
|
2020-04-10 00:01:58 +08:00
|
|
|
information = rt_object_get_information((enum rt_object_class_type)type);
|
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* parameter check */
|
2020-04-10 00:01:58 +08:00
|
|
|
if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
|
2012-12-25 14:45:56 +08:00
|
|
|
|
|
|
|
/* which is invoke in interrupt status */
|
2014-07-21 06:28:43 +08:00
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
|
|
2012-12-25 14:45:56 +08:00
|
|
|
/* enter critical */
|
|
|
|
rt_enter_critical();
|
|
|
|
|
|
|
|
/* try to find object */
|
2020-04-10 00:01:58 +08:00
|
|
|
rt_list_for_each(node, &(information->object_list))
|
2012-12-25 14:45:56 +08:00
|
|
|
{
|
|
|
|
object = rt_list_entry(node, struct rt_object, list);
|
|
|
|
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
|
|
|
{
|
|
|
|
/* leave critical */
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* leave critical */
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
return RT_NULL;
|
2010-11-22 07:57:33 +08:00
|
|
|
}
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/**
|
|
|
|
* This function will create a custom object
|
|
|
|
* container.
|
|
|
|
*
|
|
|
|
* @param name the specified name of object.
|
|
|
|
* @param type the type of object
|
|
|
|
* @param data the custom data
|
|
|
|
* @param data_destroy the custom object destroy callback
|
|
|
|
*
|
|
|
|
* @return the found object or RT_NULL if there is no this object
|
|
|
|
* in object container.
|
|
|
|
*
|
|
|
|
* @note this function shall not be invoked in interrupt status.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rt_object_t rt_custom_object_create(const char *name, void *data, rt_err_t (*data_destroy)(void *))
|
|
|
|
{
|
|
|
|
struct rt_custom_object *cobj = RT_NULL;
|
|
|
|
|
|
|
|
cobj = (struct rt_custom_object *)rt_object_allocate(RT_Object_Class_Custom, name);
|
|
|
|
if (!cobj)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
cobj->destroy = data_destroy;
|
|
|
|
cobj->data = data;
|
|
|
|
return (struct rt_object *)cobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will destroy a custom object
|
|
|
|
* container.
|
|
|
|
*
|
|
|
|
* @param obj the specified name of object.
|
|
|
|
*
|
|
|
|
* @note this function shall not be invoked in interrupt status.
|
|
|
|
*/
|
|
|
|
rt_err_t rt_custom_object_destroy(rt_object_t obj)
|
|
|
|
{
|
|
|
|
rt_err_t ret = -1;
|
|
|
|
|
|
|
|
struct rt_custom_object *cobj = (struct rt_custom_object *)obj;
|
|
|
|
|
|
|
|
if (obj && obj->type == RT_Object_Class_Custom)
|
|
|
|
{
|
|
|
|
if (cobj->destroy)
|
|
|
|
{
|
|
|
|
ret = cobj->destroy(cobj->data);
|
|
|
|
}
|
|
|
|
rt_object_delete(obj);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-19 10:15:10 +08:00
|
|
|
/**@}*/
|