I have been developing an RTOS based embedded software in C and I have encountered a problem regarding shared resource access from several threads. I have two problems. The first one is seting and getting a value of state variable in state machine. Below is the header file for StateMachine "object":
typedef enum{ STATE_01, STATE_02, STATE_03, STATE_04}state_e;// state machine instancetypedef struct{ state_e currState;}StateMachine;extern state_e GetState(StateMachine*);extern void SetState(StateMachine*, state_e);
The implementation of the access methods is following:
state_e GetState(StateMachine *sm){ return sm->currState;}void SetState(StateMachine *sm, state_e state){ sm->currState = state;}
My problem is that I am not sure whether I should use a mutex for controlling the access to state variable. I meant that reading and writing of 32 bit variable on 32 bit MCU is atomic operation.
The second problem regards reading the value of one item of an array containing the unsigned 32 bits integers where each bit stores a value of one bit variable.Here again I am not sure whether it is necessary to use a mutex. From the same reason as above I think no but I would like to hear an opinion of some more experienced programmer. The associated header file for bits array "object":
typedef struct{ uint32_t BitsArray[NO_WORDS];}BitsArray;extern uint32_t GetWordValue(BitsArray*, uint8_t);
The access method implementation:
uint32_t GetWordValue(BitsArray *ba, uint8_t word){ return *(ba->BitsArray + word);}
Thank you for any ideas.