Started to do multithreading, but was using fundamentally incorrect
[adns] / src / mthread.c
1 /*
2 * mthread.c
3 * - multithreading layer
4 */
5 /*
6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include "adns-mt.h"
24
25 struct adns_mt__state {
26 adns_state state;
27 pthread_t executor;
28 pthread_mutex_t mutex;
29 };
30
31 struct adns_mt__query {
32 adns_query query;
33 adns_answer *answer;
34 void *context;
35 pthread_cond_t cond;
36 };
37
38 int adns_mt_init(adns_mt_state *newstate_r, adns_initflags flags, FILE *diagfile) {
39 adns_mt_state adts;
40 int r;
41
42 adts= malloc(sizeof(*adts)); if (!adts) return errno;
43 r= pthread_mutex_init(&adts->mutex,0); assert(!r);
44 r= pthread_mutex_lock(&adts->mutex); assert(!r);
45 r= pthread_create(&adts->executor,0,executor_start,adts);
46 if (r) { pthread_mutex_destroy(&adts->mutex); free(adts); return r; }
47
48
49
50 int adns_mt_init_strcfg(adns_mt_state *newstate_r, adns_initflags flags,
51 FILE *diagfile /*0=>discard*/, const char *configtext);
52
53 int adns_mt_synchronous(adns_mt_state adts,
54 const char *owner,
55 adns_rrtype type,
56 adns_queryflags flags,
57 adns_answer **answer_r);
58
59 int adns_mt_submit(adns_mt_state adts,
60 const char *owner,
61 adns_rrtype type,
62 adns_queryflags flags,
63 void *context,
64 adns_mt_query *query_r);
65
66 int adns_mt_wait(adns_mt_state adts,
67 adns_mt_query *query_io,
68 adns_answer **answer_r,
69 void **context_r);
70
71 void adns_mt_cancel(adns_mt_query query);
72 /* Do not cancel a query while another thread is waiting for it ! */
73
74 void adns_mt_finish(adns_mt_state);
75 /* You may call this even if you have queries outstanding;
76 * they will be cancelled. NB the comment about _mt_cancel above.
77 */