Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcthread.c
1 /* SilcThreadPool tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6
7 static void func(SilcSchedule schedule, void *context)
8 {
9   silc_thread_tls_set(context);
10   SILC_LOG_DEBUG(("func: %d", (int)silc_thread_tls_get()));
11   sleep(1);
12 }
13
14 SILC_TASK_CALLBACK(compl)
15 {
16   SILC_LOG_DEBUG(("completion: %d", (int)context));
17   if ((int)context == 0xff)
18     silc_schedule_stop(schedule);
19 }
20
21 int main(int argc, char **argv)
22 {
23   SilcBool success = FALSE;
24   SilcThreadPool tp;
25   int i;
26
27   if (argc > 1 && !strcmp(argv[1], "-d")) {
28     silc_log_debug(TRUE);
29     silc_log_quick(TRUE);
30     silc_log_debug_hexdump(TRUE);
31     silc_log_set_debug_string("*thread*,*errno*");
32   }
33
34   schedule = silc_schedule_init(0, NULL, NULL);
35   if (!schedule)
36     goto err;
37
38   SILC_LOG_DEBUG(("Allocate thread pool"));
39   tp = silc_thread_pool_alloc(NULL, 2, 4, TRUE);
40   if (!tp)
41     goto err;
42   SILC_LOG_DEBUG(("Stop thread pool"));
43   silc_thread_pool_free(tp, TRUE);
44
45
46   SILC_LOG_DEBUG(("Allocate thread pool"));
47   tp = silc_thread_pool_alloc(NULL, 0, 2, FALSE);
48   if (!tp)
49     goto err;
50   for (i = 0; i < 6; i++) {
51     SILC_LOG_DEBUG(("Run thread %d", i + 1));
52     if (!silc_thread_pool_run(tp, TRUE, NULL, func, (void *) i + 1,
53                               compl, (void *)i + 1))
54       goto err;
55   }
56   sleep(6);
57   SILC_LOG_DEBUG(("Stop thread pool"));
58   silc_thread_pool_free(tp, TRUE);
59
60   SILC_LOG_DEBUG(("Allocate thread pool"));
61   tp = silc_thread_pool_alloc(NULL, 0, 2, TRUE);
62   if (!tp)
63     goto err;
64   for (i = 0; i < 2; i++) {
65     SILC_LOG_DEBUG(("Run thread %d", i + 1));
66     if (!silc_thread_pool_run(tp, FALSE, NULL, func, (void *) i + 1,
67                               compl, (void *)i + 1))
68       goto err;
69   }
70   if (silc_thread_pool_run(tp, FALSE, NULL, func, (void *)3,
71                            compl, (void *)3))
72     goto err;
73   sleep(3);
74   SILC_LOG_DEBUG(("Stop thread pool"));
75   silc_thread_pool_free(tp, TRUE);
76
77   SILC_LOG_DEBUG(("Allocate thread pool"));
78   tp = silc_thread_pool_alloc(NULL, 3, 20, TRUE);
79   if (!tp)
80     goto err;
81   for (i = 0; i < 8; i++) {
82     SILC_LOG_DEBUG(("Run thread %d", i + 1));
83     if (!silc_thread_pool_run(tp, FALSE, schedule, func, (void *) i + 1,
84                               compl, (void *)i + 1))
85       goto err;
86   }
87   if (!silc_thread_pool_run(tp, FALSE, schedule, func, (void *)0xff,
88                             compl, (void *)0xff))
89     goto err;
90   sleep(1);
91
92   silc_thread_pool_purge(tp);
93
94   silc_schedule(schedule);
95
96   SILC_LOG_DEBUG(("Stop thread pool"));
97   silc_thread_pool_free(tp, TRUE);
98
99   silc_schedule_uninit(schedule);
100   success = TRUE;
101
102  err:
103   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
104   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
105
106   return success;
107 }