1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
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
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
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
24 #include "sel_thread.h"
27 #include <yaz/xmalloc.h>
29 /** \brief stuff we work on in separate thread */
35 /** \brief work to be carried out in separate thrad */
36 static void work_handler(void *vp)
38 struct my_work_data *p = vp;
42 /** \brief how work is destructed */
43 static void work_destroy(void *vp)
45 struct my_work_data *p = vp;
49 /** \brief see if we can create and destroy without problems */
50 static void test_create_destroy(void)
53 sel_thread_t p = sel_thread_create(work_handler, 0, &fd, 1);
58 sel_thread_destroy(p);
62 void iochan_handler(struct iochan *i, int event)
64 static int number = 0;
65 sel_thread_t p = iochan_getdata(i);
67 if (event & EVENT_INPUT)
69 struct my_work_data *work;
71 work = sel_thread_result(p);
76 YAZ_CHECK_EQ(work->x * 2, work->y);
77 /* stop work after a couple of iterations */
85 if (event & EVENT_TIMEOUT)
87 struct my_work_data *work;
89 work = xmalloc(sizeof(*work));
91 sel_thread_add(p, work);
93 work = xmalloc(sizeof(*work));
95 sel_thread_add(p, work);
101 /** brief use the fd for something */
102 static void test_for_real_work(int no_threads)
105 sel_thread_t p = sel_thread_create(work_handler, work_destroy,
106 &thread_fd, no_threads);
110 IOCHAN chan = iochan_create(thread_fd, iochan_handler,
111 EVENT_INPUT|EVENT_TIMEOUT);
112 iochan_settimeout(chan, 1);
113 iochan_setdata(chan, p);
116 sel_thread_destroy(p);
120 int main(int argc, char **argv)
122 YAZ_CHECK_INIT(argc, argv);
125 test_create_destroy();
126 test_for_real_work(1);
127 test_for_real_work(3);
138 * indent-tabs-mode: nil
140 * vim: shiftwidth=4 tabstop=8 expandtab