M
mrhicks
Hello all,
I have a question regarding efficeny and how to find the best
approach when trying to find flag with in a structure of bit fields. I
have several structures which look similar to
typedef unsigned long int uint32; /* 32 bits */
// Up to 36 request flags, so this will take up to 3
typedef struct {
uint32 notUsed :24,
flag36 :1,
flag35 :1,
...
flag2 :1,
flag1 :1;
} flag_def_t;
typedef union {
flag_def_t flags;
uint32 packedData[ sizeof( flag_def_t ) ];
} request_flags_t;
For this project we are using what my lead calls Input and Output
Data Stores. Data that needs to be transmitted or used elsewhere in
the project are placed in my module's Output Data Store, which is a
structure containing all outpust of my module. All data the module
needs to process its logic is contained in the Input Data Stores. All
the data for the Input Data Store is collected via a Data Router. The
piece of information that is important to realize is that my module is
part of the communications layer that is called at a rate of 100 Hz,
while the other module may only be called at a rate of 10 Hz. So my
data router will pull in data from other module's Output Data Stores.
So for the request flags these are or'd assignments like the
following..
//Module Level Globals
request_flags_t idsComm;
idsComm.flag2 |= odsModule1.flag2;
idsComm.flga2 |= odsModule2.flag2.
...
Okay, now what I need to do is determine if a flag for a request has
been raised and issue that request as quickly as possible. For now, I
have some code that looks like
void getStatusIfFlagPresent( void ) {
static int iStarvedState = 0; // So lower request flags don't get
starved
// Determine if a flag is present, if not exist...
if( idsComm.packedData[0] == 0x00 && idsComm.packedData[1] == 0x00
)
return;
/*
* Determine if a request needs to be submitted. To prevent
starvation
* for lower level each level, excluding last level, needs two
conditions
* to be met. For the lowest level the 2nd condition doesn't need
to be met.
*
* 1) Check Request Flag is high
* 2) If Current Starved State is greater than level starved no
* flag must be present on the lower starved levels
*
*/
// Starved State 1
if( idsComm.flags.flag36 == 0x1 &&
( (iStarvedState < 1) ||
( (idsComm.packedData[0] & 0x0007) == 0x00 &&
(idsComm.packedData[1] & 0xFFFF) == 0x00 ) ) )
{
//Set up specific comm cmd router data
iStarvedState = 0; // Starved State is incremented when
command
// is placed into the queue. Sometimes the
// queue may be full, so we want to attempt
// queue the same message on the next pass,
// hence the starved state is equal to the
// "Starved State 1" - minus one
} else
// Starved State 2
if( idsComm.flags.flag35 == 0x1 &&
( (iStarvedState < 2) ||
( (idsComm.packedData[0] & 0x0003) == 0x00 &&
(idsComm.packedData[1] & 0xFFFF) == 0x00 ) ) )
{
//Set up specific comm cmd router data
iStarvedState = 1;
} else
// etc.....
// Starved State 35
if( idsComm.flags.flag2 == 0x1 &&
((iStarvedState < 36) || ((idsComm.packedData[1] & 0x1) ==
0x00)) ) {
//Set up specific comm cmd router data
iStarvedState = 35;
} else
// Starved State 36
if( idsComm.flags.flag1 == 0x1 ) {
//Set up specific comm cmd router data
iStarvedState = 36;
}
}
As you can see this can get quite long for all conditions. I was
hoping for a more efficient way of performing all these checks. I need
to make sure each request is serviced and that no starvation of other
request flags happens, hence the starvation levels. Do anyone see a
better approach that is faster? Thanks...
Mark
I have a question regarding efficeny and how to find the best
approach when trying to find flag with in a structure of bit fields. I
have several structures which look similar to
typedef unsigned long int uint32; /* 32 bits */
// Up to 36 request flags, so this will take up to 3
typedef struct {
uint32 notUsed :24,
flag36 :1,
flag35 :1,
...
flag2 :1,
flag1 :1;
} flag_def_t;
typedef union {
flag_def_t flags;
uint32 packedData[ sizeof( flag_def_t ) ];
} request_flags_t;
For this project we are using what my lead calls Input and Output
Data Stores. Data that needs to be transmitted or used elsewhere in
the project are placed in my module's Output Data Store, which is a
structure containing all outpust of my module. All data the module
needs to process its logic is contained in the Input Data Stores. All
the data for the Input Data Store is collected via a Data Router. The
piece of information that is important to realize is that my module is
part of the communications layer that is called at a rate of 100 Hz,
while the other module may only be called at a rate of 10 Hz. So my
data router will pull in data from other module's Output Data Stores.
So for the request flags these are or'd assignments like the
following..
//Module Level Globals
request_flags_t idsComm;
idsComm.flag2 |= odsModule1.flag2;
idsComm.flga2 |= odsModule2.flag2.
...
Okay, now what I need to do is determine if a flag for a request has
been raised and issue that request as quickly as possible. For now, I
have some code that looks like
void getStatusIfFlagPresent( void ) {
static int iStarvedState = 0; // So lower request flags don't get
starved
// Determine if a flag is present, if not exist...
if( idsComm.packedData[0] == 0x00 && idsComm.packedData[1] == 0x00
)
return;
/*
* Determine if a request needs to be submitted. To prevent
starvation
* for lower level each level, excluding last level, needs two
conditions
* to be met. For the lowest level the 2nd condition doesn't need
to be met.
*
* 1) Check Request Flag is high
* 2) If Current Starved State is greater than level starved no
* flag must be present on the lower starved levels
*
*/
// Starved State 1
if( idsComm.flags.flag36 == 0x1 &&
( (iStarvedState < 1) ||
( (idsComm.packedData[0] & 0x0007) == 0x00 &&
(idsComm.packedData[1] & 0xFFFF) == 0x00 ) ) )
{
//Set up specific comm cmd router data
iStarvedState = 0; // Starved State is incremented when
command
// is placed into the queue. Sometimes the
// queue may be full, so we want to attempt
// queue the same message on the next pass,
// hence the starved state is equal to the
// "Starved State 1" - minus one
} else
// Starved State 2
if( idsComm.flags.flag35 == 0x1 &&
( (iStarvedState < 2) ||
( (idsComm.packedData[0] & 0x0003) == 0x00 &&
(idsComm.packedData[1] & 0xFFFF) == 0x00 ) ) )
{
//Set up specific comm cmd router data
iStarvedState = 1;
} else
// etc.....
// Starved State 35
if( idsComm.flags.flag2 == 0x1 &&
((iStarvedState < 36) || ((idsComm.packedData[1] & 0x1) ==
0x00)) ) {
//Set up specific comm cmd router data
iStarvedState = 35;
} else
// Starved State 36
if( idsComm.flags.flag1 == 0x1 ) {
//Set up specific comm cmd router data
iStarvedState = 36;
}
}
As you can see this can get quite long for all conditions. I was
hoping for a more efficient way of performing all these checks. I need
to make sure each request is serviced and that no starvation of other
request flags happens, hence the starvation levels. Do anyone see a
better approach that is faster? Thanks...
Mark