0284da1c0d53f4cb2d324aa78dc16441c24be16d
[pazpar2-moved-to-github.git] / src / test_sel_thread.c
1 /* This file is part of Pazpar2.
2    Copyright (C) Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "sel_thread.h"
25 #include "eventl.h"
26 #include <yaz/test.h>
27 #include <yaz/xmalloc.h>
28
29 /** \brief stuff we work on in separate thread */
30 struct my_work_data {
31     int x;
32     int y;
33 };
34
35 /** \brief work to be carried out in separate thrad */
36 static void work_handler(void *vp)
37 {
38     struct my_work_data *p = vp;
39     p->y = p->x * 2;
40 }
41
42 /** \brief how work is destructed */
43 static void work_destroy(void *vp)
44 {
45     struct my_work_data *p = vp;
46     xfree(p);
47 }
48
49 /** \brief see if we can create and destroy without problems */
50 static void test_create_destroy(void)
51 {
52     int fd;
53     sel_thread_t p = sel_thread_create(work_handler, 0, &fd, 1);
54     YAZ_CHECK(p);
55     if (!p)
56         return;
57
58     sel_thread_destroy(p);
59 }
60
61
62 void iochan_handler(struct iochan *i, int event)
63 {
64     static int number = 0;
65     sel_thread_t p = iochan_getdata(i);
66
67     if (event & EVENT_INPUT)
68     {
69         struct my_work_data *work;
70
71         work = sel_thread_result(p);
72
73         YAZ_CHECK(work);
74         if (work)
75         {
76             YAZ_CHECK_EQ(work->x * 2, work->y);
77             /* stop work after a couple of iterations */
78             if (work->x > 10)
79                 iochan_destroy(i);
80
81             xfree(work);
82         }
83
84     }
85     if (event & EVENT_TIMEOUT)
86     {
87         struct my_work_data *work;
88
89         work = xmalloc(sizeof(*work));
90         work->x = number;
91         sel_thread_add(p, work);
92
93         work = xmalloc(sizeof(*work));
94         work->x = number+1;
95         sel_thread_add(p, work);
96
97         number += 10;
98     }
99 }
100
101 /** brief use the fd for something */
102 static void test_for_real_work(int no_threads)
103 {
104     int thread_fd;
105     sel_thread_t p = sel_thread_create(work_handler, work_destroy,
106                                        &thread_fd, no_threads);
107     YAZ_CHECK(p);
108     if (p)
109     {
110         iochan_man_t chan_man = iochan_man_create(10);
111         IOCHAN chan = iochan_create(thread_fd, iochan_handler,
112                                     EVENT_INPUT|EVENT_TIMEOUT, "test_chan");
113         iochan_settimeout(chan, 1);
114         iochan_setdata(chan, p);
115         iochan_add(chan_man, chan);
116
117         iochan_man_events(chan_man);
118         sel_thread_destroy(p);
119         iochan_man_destroy(&chan_man);
120     }
121 }
122
123 int main(int argc, char **argv)
124 {
125     YAZ_CHECK_INIT(argc, argv);
126     YAZ_CHECK_LOG();
127
128     test_create_destroy();
129     test_for_real_work(1);
130     test_for_real_work(3);
131
132     YAZ_CHECK_TERM;
133 }
134
135
136
137
138 /*
139  * Local variables:
140  * c-basic-offset: 4
141  * c-file-style: "Stroustrup"
142  * indent-tabs-mode: nil
143  * End:
144  * vim: shiftwidth=4 tabstop=8 expandtab
145  */
146