Updated source file headers with new year and no CVS Id.
[pazpar2-moved-to-github.git] / src / test_sel_thread.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 "cconfig.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 chan = iochan_create(thread_fd, iochan_handler,
111                                     EVENT_INPUT|EVENT_TIMEOUT);
112         iochan_settimeout(chan, 1);
113         iochan_setdata(chan, p);
114
115         event_loop(&chan);
116         sel_thread_destroy(p);
117     }
118 }
119
120 int main(int argc, char **argv)
121 {
122     YAZ_CHECK_INIT(argc, argv); 
123     YAZ_CHECK_LOG(); 
124
125     test_create_destroy();
126     test_for_real_work(1);
127     test_for_real_work(3);
128
129     YAZ_CHECK_TERM;
130 }
131
132
133
134
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * indent-tabs-mode: nil
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */