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(1, FALSE);
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, 0, 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, 0, (void *)100, FALSE);
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)
95 if (argc > 1 && !strcmp(argv[1], "-d")) {
97 silc_log_debug_hexdump(TRUE);
98 silc_log_set_debug_string("*thread*");
101 SILC_LOG_DEBUG(("Allocating scheduler"));
102 schedule = silc_schedule_init(0, NULL, NULL, NULL);
106 SILC_LOG_DEBUG(("Allocating FSM context"));
107 fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule);
110 silc_fsm_start(fsm, test_st_start);
112 SILC_LOG_DEBUG(("Running scheduler"));
113 silc_schedule(schedule);
115 silc_schedule_uninit(schedule);
118 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
119 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
121 silc_runtime_uninit();