⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.c.svn-base

📁 RT-Thread是发展中的下一代微内核嵌入式实时操作系统
💻 SVN-BASE
字号:
/* * File      : object.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * http://www.fayfayspace.org/license/LICENSE. * * 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 */#include <rtthread.h>#include <rthw.h>#include "kservice.h"/** * @addtogroup Kernel *//*@{*/struct rt_object_information rt_object_container[RT_Object_Class_Unknown];void rt_system_object_init(){	/* init object container - thread */	rt_list_init(&(rt_object_container[RT_Object_Class_Thread].object_list));	rt_object_container[RT_Object_Class_Thread].object_size = sizeof(struct rt_thread);	rt_object_container[RT_Object_Class_Thread].type = RT_Object_Class_Thread;#ifdef RT_USING_PROCESS	/* init object container - process */	rt_list_init(&(rt_object_container[RT_Object_Class_Process].object_list));	rt_object_container[RT_Object_Class_Process].object_size = sizeof(struct rt_process);	rt_object_container[RT_Object_Class_Process].type = RT_Object_Class_Process;#endif#ifdef RT_USING_SEMAPHORE	/* init object container - semaphore */	rt_list_init(&(rt_object_container[RT_Object_Class_Semaphore].object_list));	rt_object_container[RT_Object_Class_Semaphore].object_size = sizeof(struct rt_semaphore);	rt_object_container[RT_Object_Class_Semaphore].type = RT_Object_Class_Semaphore;#endif#ifdef RT_USING_MUTEX	/* init object container - mutex */	rt_list_init(&(rt_object_container[RT_Object_Class_Mutex].object_list));	rt_object_container[RT_Object_Class_Mutex].object_size = sizeof(struct rt_mutex);	rt_object_container[RT_Object_Class_Mutex].type = RT_Object_Class_Mutex;#endif#ifdef RT_USING_FASTEVENT	/* init object container - fast event */	rt_list_init(&(rt_object_container[RT_Object_Class_FastEvent].object_list));	rt_object_container[RT_Object_Class_FastEvent].object_size = sizeof(struct rt_fast_event);	rt_object_container[RT_Object_Class_FastEvent].type = RT_Object_Class_FastEvent;#endif#ifdef RT_USING_EVENT	/* init object container - event */	rt_list_init(&(rt_object_container[RT_Object_Class_Event].object_list));	rt_object_container[RT_Object_Class_Event].object_size = sizeof(struct rt_event);	rt_object_container[RT_Object_Class_Event].type = RT_Object_Class_Event;#endif#ifdef RT_USING_MAILBOX	/* init object container - mailbox */	rt_list_init(&(rt_object_container[RT_Object_Class_MailBox].object_list));	rt_object_container[RT_Object_Class_MailBox].object_size = sizeof(struct rt_mailbox);	rt_object_container[RT_Object_Class_MailBox].type = RT_Object_Class_MailBox;#endif#ifdef RT_USING_MESSAGEQUEUE	/* init object container - message queue */	rt_list_init(&(rt_object_container[RT_Object_Class_MessageQueue].object_list));	rt_object_container[RT_Object_Class_MessageQueue].object_size = sizeof(struct rt_messagequeue);	rt_object_container[RT_Object_Class_MessageQueue].type = RT_Object_Class_MessageQueue;#endif#ifdef RT_USING_MEMPOOL	/* init object container - memory pool */	rt_list_init(&(rt_object_container[RT_Object_Class_MemPool].object_list));	rt_object_container[RT_Object_Class_MemPool].object_size = sizeof(struct rt_mempool);	rt_object_container[RT_Object_Class_MemPool].type = RT_Object_Class_MemPool;#endif	/* init object container - timer */	rt_list_init(&(rt_object_container[RT_Object_Class_Timer].object_list));	rt_object_container[RT_Object_Class_Timer].object_size = sizeof(struct rt_timer);	rt_object_container[RT_Object_Class_Timer].type = RT_Object_Class_Timer;}/** * @brief init an object to object pool * * This fuction will init an object and add it to object pool management. * * @param object the specified object to be initialized. * @param type the object type. * @param name the object name. In system, the object's name must  * be unique. */void rt_object_init(struct rt_object* object, enum rt_object_class_type type, const char* name){	register rt_base_t temp;	struct rt_object_information* information;	/* get object information */	information = &rt_object_container[type];	/* init object's parameters */	/* set object type to static */	object->type = type | RT_OBJECT_Class_Static;	/* copy name */	for (temp = 0; temp < RT_NAME_MAX; temp ++)	{		object->name[temp] = name[temp];	}	/* lock interrupt */	temp = rt_hw_interrupt_disable();	/* insert object into information object list */	rt_list_insert_after(&(information->object_list), &(object->list));	/* unlock interrupt */	rt_hw_interrupt_enable(temp);}/** * @brief detach a static object from object pool * * This fuction will detach a static object from system object pool, * and the memory of static object is not freed. * * @param object the specified object to be detached. */void rt_object_detach(rt_object_t object){	register rt_base_t temp;	/* object check */	RT_ASSERT(object != RT_NULL);	/* lock interrupt */	temp = rt_hw_interrupt_disable();	/* remove from old list */	rt_list_remove(&(object->list));	/* unlock interrupt */	rt_hw_interrupt_enable(temp);}/** * @brief allocate an object from object pool * * This fuction will allocate an object from system object pool * * @param name the object name. In system, the object's name must  * be unique. * * @return object */rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name){	struct rt_object* object;	register rt_base_t temp;	struct rt_object_information* information;	/* get object information */	information = &rt_object_container[type];	object = (struct rt_object*)rt_malloc(information->object_size);	if (object == RT_NULL)	{		/* no memory can be allocated */		return RT_NULL;	}	/* init object's parameters */	/* set object type */	object->type = type;	/* copy name */	for (temp = 0; temp < RT_NAME_MAX; temp ++)	{		object->name[temp] = name[temp];	}	/* lock interrupt */	temp = rt_hw_interrupt_disable();	/* insert object into information object list */	rt_list_insert_after(&(information->object_list), &(object->list));	/* unlock interrupt */	rt_hw_interrupt_enable(temp);	/* return object */	return object;}/** * @brief delete an object * * This function will delete an object * * @param object the specified object to be deleted. */void rt_object_delete(rt_object_t object){	register rt_base_t temp;		/* object check */	RT_ASSERT(object != RT_NULL);	RT_ASSERT(!(object->type & RT_OBJECT_Class_Static));	/* lock interrupt */	temp = rt_hw_interrupt_disable();	/* remove from old list */	rt_list_remove(&(object->list));	/* unlock interrupt */	rt_hw_interrupt_enable(temp);	/* free the memory of object */	rt_free(object);}/** * @brief find an object according specified name * * This fucntion will find the object id by specified object name * * @param name the specified object name * * @return object id for successful */rt_object_t rt_object_find(enum rt_object_class_type type, const char* name){	struct rt_object_information *information;	struct rt_object* object;	struct rt_list_node* node;	information = &rt_object_container[type];		for (node = information->object_list.next; node != &(information->object_list); node = node->next)	{		object = rt_list_entry(node, struct rt_object, list);		if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)		{			return object;		}	}	/* not found */	return RT_NULL;}/** * @brief judge the object is a system object or not * * This fucntion will judge the object is system object or not. * Normailly, the system object is a static object and the type * of object set to RT_OBJECT_Class_Static. * * @param object the specified object to be judged. * * @return RT_EOK if a system object, RT_ERROR for others. */rt_err_t rt_object_is_systemobject(rt_object_t object){	/* object check */	RT_ASSERT(object != RT_NULL);	if (object->type & RT_OBJECT_Class_Static) return RT_EOK;	return -RT_ERROR;}/*@}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -