所有的数据结构都是对象
rtdef.h 添加rt_object_class_type
enum rt_object_class_type{
RT_Object_Class_NULL=0,
RT_Object_Class_Thread ,
RT_Object_Class_Semaphore,
RT_Object_Class_Mutex,
RT_Object_Class_Event,
RT_Object_Class_MailBox,
RT_Object_Class_MessageQueue,
RT_Object_Class_MemHeap,
RT_Object_Class_MemPool,
RT_Object_Class_Device,
RT_Object_Class_Timer,
RT_Object_Class_Module,
RT_Object_Class_Unknown,
RT_Object_Class_Static = 0x80,
};
struct rt_object
{
char name[RT_NAME_MAX];
rt_uint8_t type;
rt_uint8_t flag;
rt_list_t list;
};
typedef struct rt_object *rt_object_t;
RT_NAME_MAX在rt_config.h中定义,默认为8.
struct rt_thread
{
char name[RT_NAME_MAX];
rt_uint8_t type;
rt_uint8_t flag;
rt_list_t list;
void *sp; /* 线程栈指针 */
void *entry; /* 线程入口地址 */
void *parameter; /* 线程形参 */
void *stack_addr; /* 线程起始地址 */
rt_uint32_t stack_size; /* 线程栈大小,单位为字节 */
方便管理,每创建一个对象,如线程就会放到对应的容器里。如finsh组件会获取各个内核对象的状态
代码角度,容器是数组,一个全局变量
redef.h 中
struct rt_object_information{
enum rt_object_class_type type;
rt_list_t object_list;
rt_size_t object_size;
};
object.c中
static struct rt_object_information rt_object_container[RT_Object_Info_Unknow]={
{RT_Object_Class_Thread,_OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread),sizeof(struct rt_thread)},
#ifdef RT_USING_SEMAPHORE
{RT_Object_Class_Semaphore,_OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore),sizeof(struct rt_semaphore)},
#endif
#ifdef RT_USING_MUTEX
/* initialize object container - mutex */
{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
#endif
#ifdef RT_USING_EVENT
/* initialize object container - event */
{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
#endif
#ifdef RT_USING_MAILBOX
/* initialize object container - mailbox */
{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
#endif
#ifdef RT_USING_MESSAGEQUEUE
/* initialize object container - message queue */
{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
#endif
#ifdef RT_USING_MEMHEAP
/* initialize object container - memory heap */
{RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
#endif
#ifdef RT_USING_MEMPOOL
/* initialize object container - memory pool */
{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
#endif
#ifdef RT_USING_DEVICE
/* initialize object container - device */
{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
#endif
/* initialize object container - timer */
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
#endif
};
容器大小控制如下
enum rt_object_info_type{
RT_Object_Info_Thread = 0,
#ifdef RT_USING_SEMAPHORE
RT_Object_Info_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. */
#endif
RT_Object_Info_Unknow,
};
struct rt_object_information *rt_object_get_information(enum rt_object_class_type type)
{
int index;
for(index = 0; index < RT_Object_Info_Unknow;index++)
{
if(rt_object_container[index].type == type)
return &rt_object_container[index];
}
return RT_NULL;
}
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 *info;
info = rt_object_get_information(type);
object->type = type | RT_Object_Class_Static;
rt_strncpy(object->name,name,RT_NAME_MAX);
temp = rt_hw_interrupt_disable();
rt_list_insert_after(&(info->object_list),&(object->list));
rt_hw_interrupt_enable(temp);
}
thread同步
rt_err_t rt_thread_init(struct rt_thread *thread,const char *name,
void (*entry)(void *parameter),
void *parameter,
void *stack_start,
rt_uint32_t stack_size,
rt_uint8_t priority)
{
rt_object_init((rt_object_t)thread,RT_Object_Class_Thread,name);
rt_list_init(&(thread->tlist));
thread->entry = (void *)entry;
thread->parameter = parameter;