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