9 #include <sys/eventfd.h> 66 #define SMACHINE_LOCK(this) DPUTIL_LOCK(&this->msglist.lock); 67 #define SMACHINE_UNLOCK DPUTIL_UNLOCK 121 if(!state_manager->
states) {
128 return state_manager;
162 while(state_manager) {
171 while(state_manager) {
172 if(state_manager->
event == event) {
175 state_manager=state_manager->
next;
177 return state_manager;
233 msg->
args = (
void *)(msg + 1);
235 memcpy(msg->
args, arg, arglen);
256 this->tid = pthread_self();
260 ret = eventfd_read(socketfd, &cnt);
285 pthread_mutex_init(&this->msglist.lock, NULL);
286 this->msglist.eventfd = eventfd(0,EFD_CLOEXEC | EFD_NONBLOCK);
287 return this->msglist.eventfd;
292 close(this->msglist.eventfd);
298 return this->msglist.eventfd;
306 return eventfd_write(this->msglist.eventfd, 1);
329 for( i = 0; i < event_num; i ++ ) {
354 return instance_info;
361 if(!
this || !event_info) {
375 if(!state_manager->
states) {
395 while(state_manager) {
397 state_manager=state_manager->
next;
406 if(this->state_machine->head) {
418 if(this->state_machine->tid && this->state_machine->tid == pthread_self() ) {
419 return this->state_machine->call_single_event(this->state_machine, event, arg, arglen, response);
421 return this->state_machine->call_event(this->state_machine, event, arg, arglen, response);
431 while(state_manager) {
432 printf(
"===[event: %d]===\n", state_manager->
event);
434 state_manager=state_manager->
next;
443 this->state_machine->free(this->state_machine);
444 free(this->state_machine);
void state_machine_show(StateMachineInfo this)
set state
state_info_t * state_infos
state list, please see state_manager.h defition
information of state machine message list
static void state_machine_thread_main(int socketfd, int eventflag, void *event_arg)
for multi thread, thread main callback for threadpool
void state_manager_free(StateManager this)
free StateManager class
StateManagerListData state_machine_find_states(StateMachine this, int event)
Find state.
state_machine_msg_list_t msglist
static int state_machine_get_read(StateMachine this)
for multi thread, get read sock
StateManager class member definition, detail is defined in C file.
static int state_machine_call_event_normally(StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
for single thread, call event
void state_manager_set_state(StateManager this, int state)
set state
StateManagerListData head
void(* free)(EventInstance this)
static int state_machine_call_event_normal(StateMachine this, int event, void *arg)
static StateManagerListData state_machine_manager_list_new(const state_event_info_t *event_info)
Add new state.
void(* response)(int result)
void state_machine_free(StateMachineInfo this)
free StateMachine class
struct state_machine_msg_t * StateMachineMsg
message definition for multi thread
static void state_machine_free_states_normally(StateMachine this)
free normally
event_tpool_add_result_t event_tpool_add(EventTPoolManager this, EventSubscriber subscriber, void *arg)
add EventSubscriber to threadpool
void(* free)(StateMachine this)
StateMachineInfo state_machine_new(size_t event_num, const state_event_info_t *event_infos, EventTPoolManager threadpool)
Create StateMachineInfo class.
int(* call_single_event)(StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
struct state_machine_t * StateMachine
StateMachine class definition.
int state_manager_get_current_state(StateManager this)
get current state
struct state_manager_list_data_t * StateManagerListData
int state_manager_call(StateManager this, void *arg)
call state method
EventTPoolManager class instance definition.
EventTPoolManager threadpool
static void state_machine_manager_list_free(StateManagerListData this)
static int state_machine_write(StateMachine this, state_machine_msg_t *msg)
for multi thread, write
static void state_machine_close_socket(StateMachine this)
for multi thread, close socket
int state_machine_get_current_state(StateMachineInfo this)
get state
#define STATE_MNG_SUCCESS
StateMachine state_machine
int state_machine_call_event(StateMachineInfo this, int event, void *arg, int arglen, void(*response)(int result))
call event trigger
EventSubscriber class instance definition, this is storaged in any threads.
information of StateMachine, to use as list
DPUtilListData dputil_list_pop(DPUtilList this)
list pop
void event_tpool_del(EventTPoolManager this, int fd)
delete EventSubscriber to threadapool.
void dputil_list_push(DPUtilList this, DPUtilListData data)
list push
void state_machine_set_state(StateMachineInfo this, int state)
set state
event ID and related state functions
int state_machine_update_machine(StateMachineInfo this, const state_event_info_t *event_info)
update sate
StateManagerListData next
static int state_machine_add_new_states(StateMachine this, const state_event_info_t *event_info)
Add new state.
int fd
file descripter of this subscriber
StateMachine class member definition.
static int state_machine_open_socket(StateMachine this)
for multi thread, open socket
void state_manager_show(StateManager this)
show current state table
StateManager state_manager_new(size_t state_info_num, const state_info_t *state)
Create StateManager class.
This is API for Sate machine.
static int state_machine_initial_thread(StateMachine this)
for multi thread, initialize
#define DEBUG_ERRPRINT(...)
static int state_machine_call_event_multithread(StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
for multi thread, call event
#define SMACHINE_LOCK(this)
int(* call_event)(StateMachine this, int event, void *arg, int arglen, void(*response)(int result))
size_t state_num
state num
StateManagerListData tail
StateManagerListData prev
static void state_machine_free_states_multithread(StateMachine this)
for multi thread, free