rtemsobjects.docx
《rtemsobjects.docx》由会员分享,可在线阅读,更多相关《rtemsobjects.docx(29页珍藏版)》请在冰豆网上搜索。
rtemsobjects
1.对象的结构体
typedefstruct{
/**ThisfieldindicatestheAPIofthisobjectclass.*/
Objects_APIsthe_api;/*哪种对象*/
/**Thisistheclassofthisobjectset.*/
uint16_tthe_class;/*对象中的哪一类*/
/**Thisistheminimumvalididofthisobjectclass.*/
Objects_Idminimum_id;/*一类中的最小有效ID*/
/**Thisisthemaximumvalididofthisobjectclass.*/
Objects_Idmaximum_id;/*一类中的最大有效ID*/
/**Thisisthemaximumnumberofobjectsinthisclass.*/
Objects_Maximummaximum;/*一类中管理的对象数目*/
/**Thisisthetrueifunlimitedobjectsinthisclass.*/
boolauto_extend;/*一类的对象数目是否受限制*/
/**Thisisthenumberofobjectsinablock.*/
Objects_Maximumallocation_size;/*一个block可以有多少对象*/
/**Thisisthesizeinbytesofeachobjectinstance.*/
//size_tsize;
intsize;/*一个对象实例有多少字节*/
/**Thispointstothetableoflocalobjects.*/
Objects_Control**local_table;
/**Thisisthechainofinactivecontrolblocks.*/
Chain_ControlInactive;
/**ThisisthenumberofobjectsontheInactivelist.*/
Objects_Maximuminactive;
/**Thisisthenumberofinactiveobjectsperblock.*/
uint32_t*inactive_per_block;
/**Thisisatabletothechainofinactiveobjectmemoryblocks.*/
void**object_blocks;
#ifdefined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
/**Thisistrueifnamesarestrings.*/
boolis_string;
#endif
/**Thisisthemaximumlengthofnames.*/
uint16_tname_length;
/**Thisisthisobjectclass'methodcalledwhenextractingathread.*/
Objects_Thread_queue_Extract_calloutextract;
#ifdefined(RTEMS_MULTIPROCESSING)
/**Thisisthisobjectclass'pointertotheglobalnametable*/
Chain_Control*global_table;
#endif
}Objects_Information;
1.1.对象类型共有4种:
Objects_APIsthe_api;/*哪种对象*/
typedefenum{
OBJECTS_NO_API=0,/*无接口类型*/
OBJECTS_INTERNAL_API=1,/*rtems内核使用的api,提供给内核使用*/
OBJECTS_CLASSIC_API=2,/*经典api,提供给用户使用*/
OBJECTS_POSIX_API=3,/*posixapi*/
OBJECTS_ITRON_API=4/*ITRONapi*/
}Objects_APIs;
#defineOBJECTS_APIS_LASTOBJECTS_ITRON_API
1.1.1.内部API
OBJECTS_INTERNAL_API有3种,这里主要是内核使用的API:
typedefenum{
OBJECTS_INTERNAL_NO_CLASS=0,
OBJECTS_INTERNAL_THREADS=1,/*用于管理内核使用的任务,如管理idle任务,如果有多核处理器运行rtems,
则还会有负责通信的任务*/
OBJECTS_INTERNAL_MUTEXES=2/*用于管理内核对临界资源的访问*/
}Objects_Internal_API;
#defineOBJECTS_INTERNAL_CLASSES_LASTOBJECTS_INTERNAL_MUTEXES
1.1.2.经典API
经典api就是RTEMS提供给用户的接口,共有10个:
typedefenum{
OBJECTS_CLASSIC_NO_CLASS=0,
OBJECTS_RTEMS_TASKS=1,
OBJECTS_RTEMS_TIMERS=2,
OBJECTS_RTEMS_SEMAPHORES=3,
OBJECTS_RTEMS_MESSAGE_QUEUES=4,
OBJECTS_RTEMS_PARTITIONS=5,
OBJECTS_RTEMS_REGIONS=6,
OBJECTS_RTEMS_PORTS=7,
OBJECTS_RTEMS_PERIODS=8,
OBJECTS_RTEMS_EXTENSIONS=9,
OBJECTS_RTEMS_BARRIERS=10
}Objects_Classic_API;
#defineOBJECTS_RTEMS_CLASSES_LASTOBJECTS_RTEMS_BARRIERS
1.1.3.posixAPI
/**
*ThisenumeratedtypeisusedintheclassfieldoftheobjectID
*forthePOSIXAPI.
*/
typedefenum{
OBJECTS_POSIX_NO_CLASS=0,
OBJECTS_POSIX_THREADS=1,
OBJECTS_POSIX_KEYS=2,
OBJECTS_POSIX_INTERRUPTS=3,
OBJECTS_POSIX_MESSAGE_QUEUE_FDS=4,
OBJECTS_POSIX_MESSAGE_QUEUES=5,
OBJECTS_POSIX_MUTEXES=6,
OBJECTS_POSIX_SEMAPHORES=7,
OBJECTS_POSIX_CONDITION_VARIABLES=8,
OBJECTS_POSIX_TIMERS=9,
OBJECTS_POSIX_BARRIERS=10,
OBJECTS_POSIX_SPINLOCKS=11,
OBJECTS_POSIX_RWLOCKS=12
}Objects_POSIX_API;
/**ThismacroisusedtogenericallyspecifythelastAPIindex.*/
#defineOBJECTS_POSIX_CLASSES_LASTOBJECTS_POSIX_RWLOCKS
1.1.4.ITRONAPI
/**
*ThisenumeratedtypeisusedintheclassfieldoftheobjectID
*fortheITRONAPI.
*/
typedefenum{
OBJECTS_ITRON_NO_CLASS=0,
OBJECTS_ITRON_TASKS=1,
OBJECTS_ITRON_EVENTFLAGS=2,
OBJECTS_ITRON_MAILBOXES=3,
OBJECTS_ITRON_MESSAGE_BUFFERS=4,
OBJECTS_ITRON_PORTS=5,
OBJECTS_ITRON_SEMAPHORES=6,
OBJECTS_ITRON_VARIABLE_MEMORY_POOLS=7,
OBJECTS_ITRON_FIXED_MEMORY_POOLS=8
}Objects_ITRON_API;
/**ThismacroisusedtogenericallyspecifythelastAPIindex.*/
#defineOBJECTS_ITRON_CLASSES_LASTOBJECTS_ITRON_FIXED_MEMORY_POOLS
1.2.对象控制块
typedefstruct{
/**Thisisthechainnodeportionofanobject.*/
Chain_NodeNode;/*链表结点next,previous*/
/**Thisistheobject'sID.*/
Objects_Idid;
/**Thisistheobject'sname.*/
Objects_Namename;
}Objects_Control;
1.3.总体结构
SCORE_EXTERNObjects_Information**_Objects_Information_table[OBJECTS_APIS_LAST+1];
rtems中的所有对象都是由上述的数据结构联合起来的,Object_information_table[5]的每个元素都是一个指针的指针,_Objects_Information_table[x]指向一个元素是_Objects_Information_table指针类型的数组,数组中的每个指针指向Objects_information结构体。
图1.3.1对象的总体结构图
1.4.Objects_Information的成员含义
typedefstruct{
/**ThisfieldindicatestheAPIofthisobjectclass.*/
Objects_APIsthe_api;/*哪种对象*/
/**Thisistheclassofthisobjectset.*/
uint16_tthe_class;/*对象中的哪一类*/
/**Thisistheminimumvalididofthisobjectclass.*/
Objects_Idminimum_id;/*一类中的最小有效ID*/
/**Thisisthemaximumvalididofthisobjectclass.*/
Objects_Idmaximum_id;/*一类中的最大有效ID*/
/**Thisisthemaximumnumberofobjectsinthisclass.*/
Objects_Maximummaximum;/*一类中管理的对象数目*/
/**Thisisthetrueifunlimitedobjectsinthisclass.*/
boolauto_extend;/*一类的对象数目是否受限制*/
/**Thisisthenumberofobjectsinablock.*/
Objects_Maximumallocation_size;/*一个block可以有多少对象*/
/**Thisisthesizeinbytesofeachobjectinstance.*/
//size_tsize;
intsize;/*一个对象实例有多少字节*/
/**Thispointstothetableoflocalobjects.*/
Objects_Control**local_table;
/**Thisisthechainofinactivecontrolblocks.*/
Chain_ControlInactive;
/**ThisisthenumberofobjectsontheInactivelist.*/
Objects_Maximuminactive;
/**Thisisthenumberofinactiveobjectsperblock.*/
uint32_t*inactive_per_block;
/**Thisisatabletothechainofinactiveobjectmemoryblocks.*/
void**object_blocks;
#ifdefined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
/**Thisistrueifnamesarestrings.*/
boolis_string;
#endif
/**Thisisthemaximumlengthofnames.*/
uint16_tname_length;
/**Thisisthisobjectclass'methodcalledwhenextractingathread.*/
Objects_Thread_queue_Extract_calloutextract;
#ifdefined(RTEMS_MULTIPROCESSING)
/**Thisisthisobjectclass'pointertotheglobalnametable*/
Chain_Control*global_table;
#endif
}Objects_Information;
1.4.1.标识成员
/**ThisfieldindicatestheAPIofthisobjectclass.*/
Objects_APIsthe_api;/*哪种对象,是内核API,还是POSIXAPI等*/
/**Thisistheclassofthisobjectset.*/
uint16_tthe_class;/*对象中的哪一类*/
/**Thisistheminimumvalididofthisobjectclass.*/
Objects_Idminimum_id;/*一类中的最小有效ID*/
/**Thisisthemaximumvalididofthisobjectclass.*/
Objects_Idmaximum_id;/*一类中的最大有效ID*/
/**Thisisthemaximumnumberofobjectsinthisclass.*/
Objects_Maximummaximum;/*一类中最多可以有多少个对象*/
boolauto_extend;/*一类的对象数目是否受限制*/
/**Thisisthenumberofobjectsinablock.*/
Objects_Maximumallocation_size;/*一个block块可以有多少对象*/
/**Thisisthesizeinbytesofeachobjectinstance.*/
intsize;/*一个对象实例有多少字节*/
Object_Id中的信息:
图上面的是32位的,下面是16位的情景。
以32位的为例,31~27表示对象中的哪一类,比如如果对象是OBJECTS_INTERNAL_API,那么31~27就表示OBJECTS_INTERNAL_NO_CLASS=0,OBJECTS_INTERNAL_THREADS=1,OBJECTS_INTERNAL_MUTEXES=2中的一个。
26~24位表示属于哪一个对象,内核api,还是posixapi等。
23~16表示是那个CPU结点,15~0表示是class中的实例编号。
1.4.2.表及链表成员
/**Thispointstothetableoflocalobjects.
*所有对象控制块的指针存放在一个表中,该字段指向该表的地址
*该数组元素的数量比实际使用的数量多一个,因为第一个不使用,
*本地的对象编号最小从1开始,使用从0开始不方便。
*/
Objects_Control**local_table;
/**Thisisthechainofinactivecontrolblocks.
*空闲控制块的链表,该链表是一个双向链表,链表的头和尾在该字段中
*/
Chain_ControlInactive;(I大写)
/**ThisisthenumberofobjectsontheInactivelist.*/
/*空闲控制块的数量,也就是在Inactive上的控制块的数量*/
Objects_Maximuminactive;(i小写)
/**Thisisthenumberofinactiveobjectsperblock.
*inactive_per_block是一个整数数组,有多少个块(内存块)就有多少个数组元素,
每个元*素表示对应的块中未被申请的实例数量。
每个内存块中有多少个未分配的实例。
*/
uint32_t*inactive_per_block;
/**Thisisatabletothechainofinactiveobjectmemoryblocks
*object_block是一个无类型指针(void*)数组,有多少个块就有多少个数组元素,每个元素指向尺寸为allocation_size*size的内存块首地址。
指向内存块的地址。
.*/
void**object_blocks;
图1.4.1表及链表示意图
每个内存块有allocation_size个对象控制块,每个对象控制块的大小为size字节,因此对应内存块的大小为allocation_size*size个字节。
1.5.对象初始化函数_Objects_Initialize_information()
初始化函数的原型为void_Objects_Initialize_information(
Objects_Information*information,/*对象信息表*/
Objects_APIsthe_api,/*哪一类API*/
uint32_tthe_class,/*api中的哪一个*/
uint32_tmaximum,/*对象实例的最大数目*/
uint16_tsize,/*对象的大小字节*/
boolis_string,/*对象名是否可以为字符串*/
uint32_tmaximum_name_length/*名字的最大长度*/
#ifdefined(RTEMS_MULTIPROCESSING)/*多处理器相关*/
boolsupports_global,
Objects_Thread_queue_Extract_calloutextract
#endif
);
函数先根据提供的参数对Objects_Information*information的内容进行初步的初始化,如内存块可以有多少个对象实例,是否可扩展等。
然后是对内存的申请,函数申请一个内存块存放maximum个对象,对内存区域的分配是在_Objects_Extend_informatio()中完成,在调用该函数之前主要是对一些元数据的初始化。
如果以后所需的对象块超出初始时分配的,可以调用_Objects_Extend_informatio()再重新分配一块内存。
图1.5.1显示了函数调用关系。
该函数主要负责对内存的分配与组织以便于管理,在_Objects_Extend_informatio()中首先申请存放对象实例的内存块,然后再申请用于管理内存块的数据结构(*local_table;*inactive_per_block;object_blocks)所用的内存空间,这些空间都从workspace上申请。
图1.5.1函数调用关系
void_Objects_Initialize_information()的具体实现在下面给出,注释在sourceinsight中给出,重要的将用黑体加粗。
information->maximum的值为0,在_Objects_Extend_information()中将会更新,这样做的原因我想是因为要等到分配好内存以后才能确定最终information->maximum的值是否有效,如果还没有分配到内存,information->maximum的值是无效的,因此赋值0。
图1.5.1调用对象初始化函数后得到的对象拓扑结构
调用_Objects_Initialize_information()后会建立如图1.5.1中的结构,local_table表的第一个元素不使用,其余的指向非空闲对象控制块,内存块中的对象控制数目的多少是根据参数maximum来确定,内存控制块的大小是根据maximum*size来确定。
初始化阶段只申请一个内存块,如果在以后的使用中不够用,将调用对象扩展信息函数进行扩展,如图1.4.1所示。
void_Objects_Initialize_information(
Objects_Information*information,
Objects_APIsthe_api,
uint32_tthe_class,