作業系統工程 Operating System Programming note
, 110-1
, 2021
Learn More →
Learn More →
struct rt_object
{
char name[RT_NAME_MAX]; /* kernel object 的名稱 */
rt_uint8_t type; /* kernel object 的類型 */
rt_uint8_t flag; /* kernel object 的參數 */
rt_list_t list; /* kernel object's list*/
#ifdef RT_USING_MODULE
void *module_id; /* id of application module */
#endif
};
typedef struct rt_object *rt_object_t;/*Yype for kernel objects. */
enum rt_object_class_type
{
RT_Object_Class_Null = 0x00, /* object is not used. */
RT_Object_Class_Thread = 0x01, /* object is a thread. */
RT_Object_Class_Semaphore = 0x02, /* object is a semaphore. */
RT_Object_Class_Mutex = 0x03, /* object is a mutex. */
RT_Object_Class_Event = 0x04, /* object is a event. */
RT_Object_Class_MailBox = 0x05, /* object is a mail box. */
RT_Object_Class_MessageQueue = 0x06, /* object is a message queue. */
RT_Object_Class_MemHeap = 0x07, /* object is a memory heap. */
RT_Object_Class_MemPool = 0x08, /* object is a memory pool. */
RT_Object_Class_Device = 0x09, /* object is a device. */
RT_Object_Class_Timer = 0x0a, /* object is a timer. */
RT_Object_Class_Module = 0x0b, /* object is a module. */
RT_Object_Class_Memory = 0x0c, /* object is a memory. */
RT_Object_Class_Unknown = 0x0e, /* object is unknown. */
RT_Object_Class_Static = 0x80 /* object is a static object. */
};
struct rt_object_information
{
enum rt_object_class_type type;/* object class type */
rt_list_t object_list; /* object list */
rt_size_t object_size; /* object size */
};
rt_uint8_t rt_object_get_type(rt_object_t object)
{
RT_ASSERT(object != RT_NULL);/* object check */
return object->type & ~RT_Object_Class_Static;
}
void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name)
{
register rt_base_t temp;
struct rt_list_node *node = RT_NULL;
struct rt_object_information *information;
#ifdef RT_USING_MODULE
struct rt_dlmodule *module = dlmodule_self();
#endif /* RT_USING_MODULE */
/* get object information */
information = rt_object_get_information(type);
RT_ASSERT(information != RT_NULL);
/* check object type to avoid re-initialization */
/* 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);
if (obj) /* skip warning when disable debug */
RT_ASSERT(obj != object);
}
/* leave critical */
rt_exit_critical();
/* initialize object's parameters */
object->type = type | RT_Object_Class_Static;/* set object type to static */
rt_strncpy(object->name, name, RT_NAME_MAX);/* copy name */
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
/* lock interrupt */
temp = rt_hw_interrupt_disable();
#ifdef RT_USING_MODULE
if (module)
{
rt_list_insert_after(&(module->object_list), &(object->list));
object->module_id = (void *)module;
}
else
#endif /* RT_USING_MODULE */
{
/* insert object into information object list */
rt_list_insert_after(&(information->object_list), &(object->list));
}
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
}
void rt_object_detach(rt_object_t object)
{
register rt_base_t temp;
/* object check */
RT_ASSERT(object != RT_NULL);
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
/* reset object type */
object->type = 0;
/* lock interrupt */
temp = rt_hw_interrupt_disable();
/* remove from old list */
rt_list_remove(&(object->list));
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
}
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;
#ifdef RT_USING_MODULE
struct rt_dlmodule *module = dlmodule_self();
#endif /* RT_USING_MODULE */
RT_DEBUG_NOT_IN_INTERRUPT;
/* get object information */
information = rt_object_get_information(type);
RT_ASSERT(information != RT_NULL);
object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
if (object == RT_NULL)//動態配置可能拿不到可以用的空間
return RT_NULL;/* no memory can be allocated */
/* clean memory data of object */
rt_memset(object, 0x0, information->object_size);
/* initialize object's parameters */
object->type = type;/* set object type */
object->flag = 0;/* set object flag */
rt_strncpy(object->name, name, RT_NAME_MAX);/* copy name */
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
/* lock interrupt */
temp = rt_hw_interrupt_disable();
#ifdef RT_USING_MODULE
if (module)
{
rt_list_insert_after(&(module->object_list), &(object->list));
object->module_id = (void *)module;
}
else
#endif /* RT_USING_MODULE */
{
/* 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;
}
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));
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
/* reset object type */
object->type = RT_Object_Class_Null;
/* 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_KERNEL_FREE(object);
}
struct rt_object
{
char name[RT_NAME_MAX];/*name of kernel object */
rt_uint8_t type; /*type of kernel object */
rt_uint8_t flag; /*flag of kernel object */
rt_list_t list; /*list node of kernel object */
#ifdef RT_USING_MODULE
void *module_id; /*id of application module */
#endif
};
typedef struct rt_object *rt_object_t;/* Type for kernel objects. */
struct rt_timer
{
struct rt_object parent; /* inherit from rt_object */
rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL];
void (*timeout_func)(void *parameter);/* timeout function */
void *parameter; /* timeout function's parameter */
rt_tick_t init_tick; /* timer timeout tick */
rt_tick_t timeout_tick; /* timeout tick */
};
typedef struct rt_timer *rt_timer_t;
struct rt_object
{
char name[RT_NAME_MAX];/*name of kernel object */
rt_uint8_t type; /*type of kernel object */
rt_uint8_t flag; /*flag of kernel object */
rt_list_t list; /*list node of kernel object */
#ifdef RT_USING_MODULE
void *module_id; /*id of application module */
#endif
};
typedef struct rt_object *rt_object_t;/* Type for kernel objects. */
struct rt_ipc_object
{
struct rt_object parent; /* inherit from rt_object */
rt_list_t suspend_thread;/* threads pended on this resource */
};
struct rt_mutex
{
struct rt_ipc_object parent; /* inherit from ipc_object */
rt_uint16_t value; /* value of mutex */
rt_uint8_t original_priority;/* priority of last thread hold the mutex */
rt_uint8_t hold; /* numbers of thread hold the mutex */
struct rt_thread *owner; /* current owner of mutex */
};
typedef struct rt_mutex *rt_mutex_t;
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up