pazpar2  1.13.0
test_sel_thread.c
Go to the documentation of this file.
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 
30 struct my_work_data {
31  int x;
32  int y;
33 };
34 
36 static void work_handler(void *vp)
37 {
38  struct my_work_data *p = vp;
39  p->y = p->x * 2;
40 }
41 
43 static void work_destroy(void *vp)
44 {
45  struct my_work_data *p = vp;
46  xfree(p);
47 }
48 
50 static void test_create_destroy(void)
51 {
52  int fd;
54  YAZ_CHECK(p);
55  if (!p)
56  return;
57 
59 }
60 
61 
62 void iochan_handler(struct iochan *i, int event)
63 {
64  static int number = 0;
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 
102 static void test_for_real_work(int no_threads)
103 {
104  int thread_fd;
106  &thread_fd, no_threads);
107  YAZ_CHECK(p);
108  if (p)
109  {
110  iochan_man_t chan_man = iochan_man_create(10, 0);
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, -1);
116 
117  iochan_man_events(chan_man);
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 
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 
#define EVENT_INPUT
Definition: eventl.h:35
void sel_thread_destroy(sel_thread_t p)
destorys select thread
Definition: sel_thread.c:182
void iochan_destroy(IOCHAN chan)
Definition: eventl.c:201
IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
Definition: eventl.c:209
int main(int argc, char **argv)
void iochan_man_events(iochan_man_t man)
Definition: eventl.c:433
static void test_create_destroy(void)
see if we can create and destroy without problems
void iochan_handler(struct iochan *i, int event)
int iochan_add(iochan_man_t man, IOCHAN chan, int slack)
Definition: eventl.c:173
stuff we work on in separate thread
#define iochan_setdata(i, d)
Definition: eventl.h:63
iochan_man_t iochan_man_create(int no_threads, int max_sockets)
Definition: eventl.c:113
static void test_for_real_work(int no_threads)
static void work_destroy(void *vp)
how work is destructed
void sel_thread_add(sel_thread_t p, void *data)
adds work to be carried out in thread
Definition: sel_thread.c:205
#define EVENT_TIMEOUT
Definition: eventl.h:38
sel_thread_t sel_thread_create(void(*work_handler)(void *work_data), void(*work_destroy)(void *work_data), int *read_fd, int no_of_threads)
creates select thread
Definition: sel_thread.c:129
#define iochan_getdata(i)
Definition: eventl.h:62
void * sel_thread_result(sel_thread_t p)
gets result of work
Definition: sel_thread.c:230
Definition: eventl.h:31
void iochan_man_destroy(iochan_man_t *mp)
Definition: eventl.c:152
static void work_handler(void *vp)
work to be carried out in separate thrad
#define iochan_settimeout(i, t)
Definition: eventl.h:68