1 /* SilcThreadQueue tests */
3 #include "silcruntime.h"
7 SilcBool success = FALSE;
9 SILC_FSM_STATE(test_st_start);
10 SILC_FSM_STATE(test_st_wait);
11 SILC_FSM_STATE(test_st_thread_start);
12 SILC_FSM_STATE(test_st_finish);
14 SILC_FSM_STATE(test_st_start)
18 SILC_LOG_DEBUG(("test_st_start"));
20 queue = silc_thread_queue_alloc();
22 silc_fsm_next(fsm, test_st_finish);
23 return SILC_FSM_CONTINUE;
26 thread = silc_fsm_thread_alloc(fsm, NULL, NULL, NULL, TRUE);
28 silc_fsm_next(fsm, test_st_finish);
29 return SILC_FSM_CONTINUE;
32 silc_fsm_start(thread, test_st_thread_start);
33 silc_fsm_set_state_context(fsm, thread);
35 silc_fsm_next(fsm, test_st_wait);
36 return SILC_FSM_YIELD;
39 SILC_FSM_STATE(test_st_wait)
43 SILC_LOG_DEBUG(("Wait for data"));
46 data = silc_thread_queue_pop(queue, TRUE);
47 if (!data || data != (void *)100) {
48 silc_fsm_next(fsm, test_st_finish);
49 return SILC_FSM_CONTINUE;
53 silc_fsm_next(fsm, test_st_finish);
54 SILC_FSM_THREAD_WAIT(state_context);
57 SILC_FSM_STATE(test_st_thread_start)
59 silc_thread_queue_connect(queue);
64 SILC_LOG_DEBUG(("Send data"));
65 silc_thread_queue_push(queue, (void *)100);
67 silc_thread_queue_disconnect(queue);
68 return SILC_FSM_FINISH;
71 SILC_FSM_STATE(test_st_finish)
73 SILC_LOG_DEBUG(("test_st_finish"));
75 silc_thread_queue_disconnect(queue);
77 SILC_LOG_DEBUG(("Finish machine"));
78 return SILC_FSM_FINISH;
81 static void destructor(SilcFSM fsm, void *fsm_context,
82 void *destructor_context)
84 SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
86 silc_schedule_stop(schedule);
89 int main(int argc, char **argv)
93 if (argc > 1 && !strcmp(argv[1], "-d")) {
95 silc_log_debug_hexdump(TRUE);
96 silc_log_set_debug_string("*thread*");
99 SILC_LOG_DEBUG(("Allocating scheduler"));
100 schedule = silc_schedule_init(0, NULL, NULL, NULL);
104 SILC_LOG_DEBUG(("Allocating FSM context"));
105 fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule);
108 silc_fsm_start(fsm, test_st_start);
110 SILC_LOG_DEBUG(("Running scheduler"));
111 silc_schedule(schedule);
113 silc_schedule_uninit(schedule);
116 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
117 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");