From afaf231248c590c477a18d221593e6aa111ba033 Mon Sep 17 00:00:00 2001 From: ian Date: Sat, 10 Apr 1999 16:31:20 +0000 Subject: [PATCH] Started to do multithreading, but was using fundamentally incorrect algorithm and don't know what correct algorithm is. --- src/adns-mt.h | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ src/mthread.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/adns-mt.h create mode 100644 src/mthread.c diff --git a/src/adns-mt.h b/src/adns-mt.h new file mode 100644 index 0000000..2591bb6 --- /dev/null +++ b/src/adns-mt.h @@ -0,0 +1,65 @@ +/* + * adnsmt.h + * - adns multi-threaded API + */ +/* + * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifndef ADNSMT_H_INCLUDED +#define ADNSMT_H_INCLUDED + +#include + +typedef struct adns_mt__state *adns_mt_state; +typedef struct adns_mt__query *adns_mt_query; + +int adns_mt_init(adns_mt_state *newstate_r, adns_initflags flags, + FILE *diagfile /*0=>stderr*/); + +int adns_mt_init_strcfg(adns_mt_state *newstate_r, adns_initflags flags, + FILE *diagfile /*0=>discard*/, const char *configtext); + +int adns_mt_synchronous(adns_mt_state adts, + const char *owner, + adns_rrtype type, + adns_queryflags flags, + adns_answer **answer_r); + +int adns_mt_submit(adns_mt_state adts, + const char *owner, + adns_rrtype type, + adns_queryflags flags, + void *context, + adns_mt_query *query_r); + +int adns_mt_wait(adns_mt_state adts, + adns_mt_query *query_io, + adns_answer **answer_r, + void **context_r); + +void adns_mt_cancel(adns_mt_query query); +/* Do not cancel a query while another thread is waiting for it ! */ + +void adns_mt_finish(adns_mt_state); +/* You may call this even if you have queries outstanding; + * they will be cancelled. NB the comment about _mt_cancel above. + */ + +#endif diff --git a/src/mthread.c b/src/mthread.c new file mode 100644 index 0000000..89164fc --- /dev/null +++ b/src/mthread.c @@ -0,0 +1,77 @@ +/* + * mthread.c + * - multithreading layer + */ +/* + * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "adns-mt.h" + +struct adns_mt__state { + adns_state state; + pthread_t executor; + pthread_mutex_t mutex; +}; + +struct adns_mt__query { + adns_query query; + adns_answer *answer; + void *context; + pthread_cond_t cond; +}; + +int adns_mt_init(adns_mt_state *newstate_r, adns_initflags flags, FILE *diagfile) { + adns_mt_state adts; + int r; + + adts= malloc(sizeof(*adts)); if (!adts) return errno; + r= pthread_mutex_init(&adts->mutex,0); assert(!r); + r= pthread_mutex_lock(&adts->mutex); assert(!r); + r= pthread_create(&adts->executor,0,executor_start,adts); + if (r) { pthread_mutex_destroy(&adts->mutex); free(adts); return r; } + + + +int adns_mt_init_strcfg(adns_mt_state *newstate_r, adns_initflags flags, + FILE *diagfile /*0=>discard*/, const char *configtext); + +int adns_mt_synchronous(adns_mt_state adts, + const char *owner, + adns_rrtype type, + adns_queryflags flags, + adns_answer **answer_r); + +int adns_mt_submit(adns_mt_state adts, + const char *owner, + adns_rrtype type, + adns_queryflags flags, + void *context, + adns_mt_query *query_r); + +int adns_mt_wait(adns_mt_state adts, + adns_mt_query *query_io, + adns_answer **answer_r, + void **context_r); + +void adns_mt_cancel(adns_mt_query query); +/* Do not cancel a query while another thread is waiting for it ! */ + +void adns_mt_finish(adns_mt_state); +/* You may call this even if you have queries outstanding; + * they will be cancelled. NB the comment about _mt_cancel above. + */ -- 2.11.0