1 /* $Id: test_sel_thread.c,v 1.5 2007-04-23 08:06:21 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
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
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
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
26 #include "sel_thread.h"
29 #include <yaz/xmalloc.h>
31 /** \brief stuff we work on in separate thread */
37 /** \brief work to be carried out in separate thrad */
38 static void work_handler(void *vp)
40 struct my_work_data *p = vp;
44 /** \brief how work is destructed */
45 static void work_destroy(void *vp)
47 struct my_work_data *p = vp;
51 /** \brief see if we can create and destroy without problems */
52 static void test_create_destroy(void)
55 sel_thread_t p = sel_thread_create(work_handler, 0, &fd, 1);
60 sel_thread_destroy(p);
64 void iochan_handler(struct iochan *i, int event)
66 static int number = 0;
67 sel_thread_t p = iochan_getdata(i);
69 if (event & EVENT_INPUT)
71 struct my_work_data *work;
73 work = sel_thread_result(p);
78 YAZ_CHECK_EQ(work->x * 2, work->y);
79 /* stop work after a couple of iterations */
87 if (event & EVENT_TIMEOUT)
89 struct my_work_data *work;
91 work = xmalloc(sizeof(*work));
93 sel_thread_add(p, work);
95 work = xmalloc(sizeof(*work));
97 sel_thread_add(p, work);
103 /** brief use the fd for something */
104 static void test_for_real_work(int no_threads)
107 sel_thread_t p = sel_thread_create(work_handler, work_destroy,
108 &thread_fd, no_threads);
112 IOCHAN chan = iochan_create(thread_fd, iochan_handler,
113 EVENT_INPUT|EVENT_TIMEOUT);
114 iochan_settimeout(chan, 1);
115 iochan_setdata(chan, p);
118 sel_thread_destroy(p);
122 int main(int argc, char **argv)
124 YAZ_CHECK_INIT(argc, argv);
127 test_create_destroy();
128 test_for_real_work(1);
129 test_for_real_work(3);
140 * indent-tabs-mode: nil
142 * vim: shiftwidth=4 tabstop=8 expandtab