From f4c589ff6c653d1d2a09c26e46ead3c8a15655d8 Mon Sep 17 00:00:00 2001 From: "bk@work.mysql.com" <> Date: Mon, 31 Jul 2000 21:29:14 +0200 Subject: Import changeset --- mit-pthreads/lib/.cvsignore | 1 + mit-pthreads/lib/Makefile.in | 48 +++++++ mit-pthreads/lib/libpthreadutil/.cvsignore | 1 + mit-pthreads/lib/libpthreadutil/Makefile.in | 65 +++++++++ mit-pthreads/lib/libpthreadutil/pthread_atexit.c | 135 ++++++++++++++++++ mit-pthreads/lib/libpthreadutil/pthread_tad.c | 170 +++++++++++++++++++++++ mit-pthreads/lib/libpthreadutil/pthreadutil.h | 75 ++++++++++ 7 files changed, 495 insertions(+) create mode 100644 mit-pthreads/lib/.cvsignore create mode 100644 mit-pthreads/lib/Makefile.in create mode 100644 mit-pthreads/lib/libpthreadutil/.cvsignore create mode 100755 mit-pthreads/lib/libpthreadutil/Makefile.in create mode 100755 mit-pthreads/lib/libpthreadutil/pthread_atexit.c create mode 100755 mit-pthreads/lib/libpthreadutil/pthread_tad.c create mode 100755 mit-pthreads/lib/libpthreadutil/pthreadutil.h (limited to 'mit-pthreads/lib') diff --git a/mit-pthreads/lib/.cvsignore b/mit-pthreads/lib/.cvsignore new file mode 100644 index 00000000000..f3c7a7c5da6 --- /dev/null +++ b/mit-pthreads/lib/.cvsignore @@ -0,0 +1 @@ +Makefile diff --git a/mit-pthreads/lib/Makefile.in b/mit-pthreads/lib/Makefile.in new file mode 100644 index 00000000000..821d293d896 --- /dev/null +++ b/mit-pthreads/lib/Makefile.in @@ -0,0 +1,48 @@ +# === GNUmakefile ============================================================ +# Copyright (c) 1993 Chris Provenzano, proven@athena.mit.edu +# +# Description: This file is for creating the test programs for libpthread.a +# +# 1.00 93/08/03 proven +# -Initial cut for pthreads. +# + +CC = ../pgcc -notinstalled +srctop = @srctop@ +srcdir = @srctop@/lib +VPATH = @srctop@/lib +CDEBUGFLAGS = @CFLAGS@ + +CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(ADDL_CFLAGS) -DSRCDIR=\"$(srcdir)\" + +# +DIRS = libpthreadutil + +################################################################################ +# +all: + (for i in $(DIRS); do cd $$i; $(MAKE) all; cd ..; done) + +clean: + (for i in $(DIRS); do cd $$i; $(MAKE) clean; cd ..; done) + rm -f *.o $(TESTS) $(BENCHMARKS) a.out core maketmp makeout + +depend: + (for i in $(DIRS); do cd $$i; $(MAKE) depend; cd ..; done) + sed '/\#\#\# Dependencies/q' < Makefile > maketmp + (for i in $(CSRC);do $(CPP) -M $$i;done) >> maketmp + cp maketmp Makefile + +install: + (for i in $(DIRS); do cd $$i; $(MAKE) install; cd ..; done) + +realclean: clean + (for i in $(DIRS); do cd $$i; $(MAKE) realclean; cd ..; done) + rm -f Makefile + +Makefile: Makefile.in + (cd .. ; sh config.status) + +################################################################################ +### Do not remove the following line. It is for depend ######################### +### Dependencies: diff --git a/mit-pthreads/lib/libpthreadutil/.cvsignore b/mit-pthreads/lib/libpthreadutil/.cvsignore new file mode 100644 index 00000000000..f3c7a7c5da6 --- /dev/null +++ b/mit-pthreads/lib/libpthreadutil/.cvsignore @@ -0,0 +1 @@ +Makefile diff --git a/mit-pthreads/lib/libpthreadutil/Makefile.in b/mit-pthreads/lib/libpthreadutil/Makefile.in new file mode 100755 index 00000000000..94034f426b3 --- /dev/null +++ b/mit-pthreads/lib/libpthreadutil/Makefile.in @@ -0,0 +1,65 @@ +# === makefile ============================================================ +# Copyright (c) 1993 Chris Provenzano, proven@athena.mit.edu +# +# Description: This file is for creating the test programs for libpthread.a +# +# 1.00 93/08/03 proven +# -Initial cut for pthreads. +# + +srctop = @srctop@ +srcdir = @srctop@/lib/libpthreadutil +VPATH = @srctop@/lib/libpthreadutil +prefix= @prefix@ +exec_prefix= @exec_prefix@ + +INSTALL_PATH = @exec_prefix@ + BINDIR = $(INSTALL_PATH)/bin + LIBDIR = $(INSTALL_PATH)/lib + MANDIR = $(INSTALL_PATH)/man + INCDIR = $(INSTALL_PATH)/include + + CC = ../../pgcc -notinstalled + CDEBUGFLAGS = @CFLAGS@ + CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(ADDL_CFLAGS) -DSRCDIR=\"$(srcdir)\" + RANLIB = @RANLIB@ + + OBJS = pthread_tad.o pthread_atexit.o + LIBRARY = libpthreadutil.a + HEADERS = pthreadutil.h + +################################################################################ +# +all : $(LIBRARY) + +clean: + rm -f *.o $(TESTS) $(BENCHMARKS) a.out core maketmp makeout + +depend: + sed '/\#\#\# Dependencies/q' < Makefile > maketmp + (for i in $(CSRC);do $(CPP) -M $$i;done) >> maketmp + cp maketmp Makefile + +install: $(LIBRARY) + install $(LIBRARY) $(LIBDIR) + for x in $(HEADERS); \ + do cp $(srcdir)/$$x $(INCDIR); \ + done + +realclean: clean + rm -f Makefile + +Makefile: Makefile.in + (cd ../.. ; sh config.status) + +.c.o: + $(CC) $(CFLAGS) -c $< + +$(LIBRARY) : ${OBJS} + ar r new.a ${OBJS} && \ + $(RANLIB) new.a && \ + mv -f new.a $(LIBRARY) + +################################################################################ +### Do not remove the following line. It is for depend ######################### +### Dependencies: diff --git a/mit-pthreads/lib/libpthreadutil/pthread_atexit.c b/mit-pthreads/lib/libpthreadutil/pthread_atexit.c new file mode 100755 index 00000000000..f244fbfb0c8 --- /dev/null +++ b/mit-pthreads/lib/libpthreadutil/pthread_atexit.c @@ -0,0 +1,135 @@ +/* ==== pthread_atexit.c ===================================================== + * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Chris Provenzano. + * 4. The name of Chris Provenzano may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Description : Pthread attribute functions. + * + * 1.20 94/02/13 proven + * -Started coding this file. + */ + +#ifndef lint +static const char rcsid[] = "$Id$"; +#endif + +#define PTHREAD_KERNEL + +#include +#include +#include +#include "pthreadutil.h" + +static int pthread_atexit_inited = 0; +static pthread_key_t pthread_atexit_key; +static pthread_mutex_t pthread_atexit_mutex = PTHREAD_MUTEX_INITIALIZER; + +/* ========================================================================== + * pthread_atexit_done() + */ +static void pthread_atexit_done(void * arg) +{ + pthread_atexit_t id, id_next; + + for (id = arg; id; id = id_next) { + id_next = id->next; + id->rtn(id->arg); + free(id); + } +} + +/* ========================================================================== + * pthread_atexit_add() + */ +int pthread_atexit_add(pthread_atexit_t *id, void (*rtn)(void *), void * arg) +{ + int ret; + + if (ret = pthread_mutex_lock(&pthread_atexit_mutex)) { + return(ret); + } + if (!pthread_atexit_inited) { + if (ret = pthread_key_create(&pthread_atexit_key, pthread_atexit_done)){ + pthread_mutex_unlock(&pthread_atexit_mutex); + return(ret); + } + pthread_atexit_inited++; + } + pthread_mutex_unlock(&pthread_atexit_mutex); + + if ((*id) = (pthread_atexit_t)malloc(sizeof(struct pthread_atexit))) { + if ((*id)->next = pthread_getspecific(pthread_atexit_key)) { + (*id)->next->prev = (*id); + } + pthread_setspecific(pthread_atexit_key, (void *)*id); + (*id)->prev = NULL; + (*id)->rtn = rtn; + (*id)->arg = arg; + return(OK); + } + return(ENOMEM); +} + +/* ========================================================================== + * pthread_atexit_remove() + */ +int pthread_atexit_remove(pthread_atexit_t * id, int execute) +{ + pthread_atexit_t old; + + if (old = pthread_getspecific(pthread_atexit_key)) { + if (old == *id) { + old = old->next; + old->prev = NULL; + pthread_setspecific(pthread_atexit_key, old); + } else { + if ((*id)->next) { + (*id)->next->prev = (*id)->prev; + } + (*id)->prev->next = (*id)->next; + } + if (execute) { + (*id)->rtn((*id)->arg); + } + free((*id)); + return(OK); + } + return(EINVAL); +} + +/* ========================================================================== + * A few non void functions that are often used as void functions + */ +void fflush_nrv(void * fp) { fflush((FILE *)fp); } +void fclose_nrv(void * fp) { fclose((FILE *)fp); } + +void pthread_attr_destroy_nrv(void * attr) +{ + pthread_attr_destroy((pthread_attr_t *)attr); +} diff --git a/mit-pthreads/lib/libpthreadutil/pthread_tad.c b/mit-pthreads/lib/libpthreadutil/pthread_tad.c new file mode 100755 index 00000000000..a59fd9b87bf --- /dev/null +++ b/mit-pthreads/lib/libpthreadutil/pthread_tad.c @@ -0,0 +1,170 @@ +/* ==== pthread_tad.c ========================================================= + * Copyright (c) 1995 by Chris Provenzano, proven@athena.mit.edu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Chris Provenzano, + * and its contributors. + * 4. Neither the name of Chris Provenzano, nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO, AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef lint +static char copyright[] = + "@(#) Copyright (c) 1995 Chris Provenzano.\nAll rights reserved.\n"; +#endif /* not lint */ + +/* tad = thread allocation domain */ +#define PTHREAD_KERNEL + +#include "pthreadutil.h" +#include +#include + +int pthread_tad_count(pthread_tad_t * tad) +{ + int ret; + + pthread_mutex_lock(&tad->mutex); + ret = tad->count_current; + pthread_mutex_unlock(&tad->mutex); + return(ret); +} + +static void pthread_tad_done(void * arg) +{ + pthread_tad_t * tad = arg; + pthread_mutex_lock(&tad->mutex); + --tad->count_current; +/* if (--tad->count_current < tad->count_max) */ + pthread_cond_broadcast(&tad->cond); + pthread_mutex_unlock(&tad->mutex); +} + +#ifndef PTHREAD_KERNEL +struct tad_start { + pthread_tad_t * tad; + void * (*routine)(); + void * arg; +}; + +static void * pthread_tad_start(struct tad_start * tad_start) +{ + void * (*routine)() = tad_start->routine; + void * arg = tad_start->arg; + + pthread_mutex_lock(&tad_start->tad->mutex); + pthread_cleanup_push(pthread_tad_done, tad_start->tad); + pthread_mutex_unlock(&tad_start->tad->mutex); + free(tad_start); + return(routine(arg)); +} +#else +static void * pthread_tad_start(void * tad_start_arg) +{ + pthread_tad_t * tad = tad_start_arg; + void * (*routine)() = tad->routine; + void * arg = tad->arg; + + tad->count_current++; + pthread_cleanup_push(pthread_tad_done, tad); + pthread_mutex_unlock(&tad->mutex); + return(routine(arg)); +} +#endif + +int pthread_tad_create(pthread_tad_t * tad, pthread_t *thread_id, + pthread_attr_t *attr, void * (*routine)(), void * arg) +{ +#ifndef PTHREAD_KERNEL + struct tad_start tad; +#endif + int ret; + + pthread_mutex_lock(&tad->mutex); + while (tad->count_max && (tad->count_current > tad->count_max)) + pthread_cond_wait(&tad->cond, &tad->mutex); + +#ifndef PTHREAD_KERNEL + if ((tad_start = malloc(sizeof(struct tad_start))) == NULL) { + pthread_mutex_unlock(&tad->mutex); + return(ENOMEM); + } + tad_start->routine = routine; + tad_start->arg = arg; + tad_start->tad = tad; + if ((ret = pthread_create(thread_id, attr, + pthread_tad_start, tad_start)) == OK) + tad->count_current++; + pthread_mutex_unlock(&tad->mutex); +#else + tad->routine = routine; + tad->arg = arg; + if (ret = pthread_create(thread_id, attr, pthread_tad_start, tad)) + pthread_mutex_unlock(&tad->mutex); +#endif + return(ret); +} + +int pthread_tad_wait(pthread_tad_t * tad, unsigned int count) +{ + if ((tad->count_max) && (tad->count_max < count)) { + return(EINVAL); + } + pthread_mutex_lock(&tad->mutex); + while (tad->count_current > count) + pthread_cond_wait(&tad->cond, &tad->mutex); + pthread_mutex_unlock(&tad->mutex); + return(OK); +} + +int pthread_tad_init(pthread_tad_t * tad, unsigned int max_count) +{ + int ret; + + if ((ret = pthread_mutex_init(&tad->mutex, NULL)) == OK) { + if (ret = pthread_cond_init(&tad->cond, NULL)) { + pthread_mutex_destroy(&tad->mutex); + } else { + tad->count_max = max_count; + tad->count_current = 0; + } + } + return(ret); +} + +/* User is responsible to make sure their are no threads running */ +int pthread_tad_destroy(pthread_tad_t * tad) +{ + int ret; + + if ((ret = pthread_mutex_destroy(&tad->mutex)) == OK) { + ret = pthread_cond_destroy(&tad->cond); + } else { + pthread_cond_destroy(&tad->cond); + } + tad->count_max = NOTOK; + return(ret); +} diff --git a/mit-pthreads/lib/libpthreadutil/pthreadutil.h b/mit-pthreads/lib/libpthreadutil/pthreadutil.h new file mode 100755 index 00000000000..0d8e6a6ef5f --- /dev/null +++ b/mit-pthreads/lib/libpthreadutil/pthreadutil.h @@ -0,0 +1,75 @@ +/* ==== pthread_tad.h ======================================================== + * Copyright (c) 1995 by Chris Provenzano, proven@athena.mit.edu + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Chris Provenzano, + * and its contributors. + * 4. Neither the name of Chris Provenzano, nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO, AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +typedef struct pthread_tad_t { + pthread_mutex_t mutex; + pthread_cond_t cond; + unsigned int count_current; + unsigned int count_max; + void * arg; + void * (*routine)(); +} pthread_tad_t; + +typedef struct pthread_atexit { + struct pthread_atexit * next; + struct pthread_atexit * prev; + void (*rtn)(void *); + void * arg; +} * pthread_atexit_t; + +/* + * New functions + */ + +__BEGIN_DECLS + +int pthread_tad_count __P_((pthread_tad_t *)); +int pthread_tad_create __P_((pthread_tad_t *, pthread_t *, pthread_attr_t *, + void *(*routine)(), void *)); +int pthread_tad_wait __P_((pthread_tad_t *, unsigned int)); +int pthread_tad_init __P_((pthread_tad_t *, unsigned int)); +int pthread_tad_destroy __P_((pthread_tad_t *)); + +int pthread_atexit_add __P_((pthread_atexit_t *, void (*)(void *), void *)); +int pthread_atexit_remove __P_((pthread_atexit_t *, int)); + + +void fclose_nrv __P_((void *)); +void fflush_nrv __P_((void *)); +void pthread_attr_destroy_nrv __P_((void *)); + +__END_DECLS + -- cgit v1.2.1