From 0b316a8c5ee9c36f183c2ad0ef6686e2ed5f47c1 Mon Sep 17 00:00:00 2001 From: curt Date: Thu, 5 Apr 2001 15:58:22 +0000 Subject: [PATCH] Initial revision. --- simgear/threads/Makefile.in | 371 +++++++++++++++++++++++++++++++++++ simgear/threads/SGThread.cxx | 52 +++++ simgear/threads/SGThread.hxx | 270 +++++++++++++++++++++++++ 3 files changed, 693 insertions(+) create mode 100644 simgear/threads/Makefile.in create mode 100644 simgear/threads/SGThread.cxx create mode 100644 simgear/threads/SGThread.hxx diff --git a/simgear/threads/Makefile.in b/simgear/threads/Makefile.in new file mode 100644 index 00000000..74b06586 --- /dev/null +++ b/simgear/threads/Makefile.in @@ -0,0 +1,371 @@ +# Makefile.in generated automatically by automake 1.4 from Makefile.am + +# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +oldincludedir = /usr/include + +DESTDIR = + +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ + +top_builddir = ../.. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS) +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +transform = @program_transform_name@ + +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +CC = @CC@ +CXX = @CXX@ +LN_S = @LN_S@ +MAKEINFO = @MAKEINFO@ +PACKAGE = @PACKAGE@ +RANLIB = @RANLIB@ +VERSION = @VERSION@ +base_LIBS = @base_LIBS@ +opengl_LIBS = @opengl_LIBS@ + +includedir = @includedir@/threads + +lib_LIBRARIES = libsgthreads.a + +include_HEADERS = SGThread.hxx + + +libsgthreads_a_SOURCES = SGThread.cxx + + +INCLUDES = -I$(top_srcdir) +mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs +CONFIG_HEADER = ../../simgear/config.h +CONFIG_CLEAN_FILES = +LIBRARIES = $(lib_LIBRARIES) + + +DEFS = @DEFS@ -I. -I$(srcdir) -I../../simgear +CPPFLAGS = @CPPFLAGS@ +LDFLAGS = @LDFLAGS@ +LIBS = @LIBS@ +X_CFLAGS = @X_CFLAGS@ +X_LIBS = @X_LIBS@ +X_EXTRA_LIBS = @X_EXTRA_LIBS@ +X_PRE_LIBS = @X_PRE_LIBS@ +libsgthreads_a_LIBADD = +libsgthreads_a_OBJECTS = SGThread.o +AR = ar +CXXFLAGS = @CXXFLAGS@ +CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) +CXXLD = $(CXX) +CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ +HEADERS = $(include_HEADERS) + +DIST_COMMON = Makefile.am Makefile.in + + +DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST) + +TAR = tar +GZIP_ENV = --best +DEP_FILES = .deps/SGThread.P +SOURCES = $(libsgthreads_a_SOURCES) +OBJECTS = $(libsgthreads_a_OBJECTS) + +all: all-redirect +.SUFFIXES: +.SUFFIXES: .S .c .cxx .o .s +$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) + cd $(top_srcdir) && $(AUTOMAKE) --gnu simgear/threads/Makefile + +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status $(BUILT_SOURCES) + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + + +mostlyclean-libLIBRARIES: + +clean-libLIBRARIES: + -test -z "$(lib_LIBRARIES)" || rm -f $(lib_LIBRARIES) + +distclean-libLIBRARIES: + +maintainer-clean-libLIBRARIES: + +install-libLIBRARIES: $(lib_LIBRARIES) + @$(NORMAL_INSTALL) + $(mkinstalldirs) $(DESTDIR)$(libdir) + @list='$(lib_LIBRARIES)'; for p in $$list; do \ + if test -f $$p; then \ + echo " $(INSTALL_DATA) $$p $(DESTDIR)$(libdir)/$$p"; \ + $(INSTALL_DATA) $$p $(DESTDIR)$(libdir)/$$p; \ + else :; fi; \ + done + @$(POST_INSTALL) + @list='$(lib_LIBRARIES)'; for p in $$list; do \ + if test -f $$p; then \ + echo " $(RANLIB) $(DESTDIR)$(libdir)/$$p"; \ + $(RANLIB) $(DESTDIR)$(libdir)/$$p; \ + else :; fi; \ + done + +uninstall-libLIBRARIES: + @$(NORMAL_UNINSTALL) + list='$(lib_LIBRARIES)'; for p in $$list; do \ + rm -f $(DESTDIR)$(libdir)/$$p; \ + done + +.s.o: + $(COMPILE) -c $< + +.S.o: + $(COMPILE) -c $< + +mostlyclean-compile: + -rm -f *.o core *.core + +clean-compile: + +distclean-compile: + -rm -f *.tab.c + +maintainer-clean-compile: + +libsgthreads.a: $(libsgthreads_a_OBJECTS) $(libsgthreads_a_DEPENDENCIES) + -rm -f libsgthreads.a + $(AR) cru libsgthreads.a $(libsgthreads_a_OBJECTS) $(libsgthreads_a_LIBADD) + $(RANLIB) libsgthreads.a +.cxx.o: + $(CXXCOMPILE) -c $< + +install-includeHEADERS: $(include_HEADERS) + @$(NORMAL_INSTALL) + $(mkinstalldirs) $(DESTDIR)$(includedir) + @list='$(include_HEADERS)'; for p in $$list; do \ + if test -f "$$p"; then d= ; else d="$(srcdir)/"; fi; \ + echo " $(INSTALL_DATA) $$d$$p $(DESTDIR)$(includedir)/$$p"; \ + $(INSTALL_DATA) $$d$$p $(DESTDIR)$(includedir)/$$p; \ + done + +uninstall-includeHEADERS: + @$(NORMAL_UNINSTALL) + list='$(include_HEADERS)'; for p in $$list; do \ + rm -f $(DESTDIR)$(includedir)/$$p; \ + done + +tags: TAGS + +ID: $(HEADERS) $(SOURCES) $(LISP) + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + awk ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + here=`pwd` && cd $(srcdir) \ + && mkid -f$$here/ID $$unique $(LISP) + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) $(LISP) + tags=; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + awk ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + test -z "$(ETAGS_ARGS)$$unique$(LISP)$$tags" \ + || (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags $$unique $(LISP) -o $$here/TAGS) + +mostlyclean-tags: + +clean-tags: + +distclean-tags: + -rm -f TAGS ID + +maintainer-clean-tags: + +distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir) + +subdir = simgear/threads + +distdir: $(DISTFILES) + here=`cd $(top_builddir) && pwd`; \ + top_distdir=`cd $(top_distdir) && pwd`; \ + distdir=`cd $(distdir) && pwd`; \ + cd $(top_srcdir) \ + && $(AUTOMAKE) --include-deps --build-dir=$$here --srcdir-name=$(top_srcdir) --output-dir=$$top_distdir --gnu simgear/threads/Makefile + @for file in $(DISTFILES); do \ + d=$(srcdir); \ + if test -d $$d/$$file; then \ + cp -pr $$d/$$file $(distdir)/$$file; \ + else \ + test -f $(distdir)/$$file \ + || ln $$d/$$file $(distdir)/$$file 2> /dev/null \ + || cp -p $$d/$$file $(distdir)/$$file || :; \ + fi; \ + done + +DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :) + +-include $(DEP_FILES) + +mostlyclean-depend: + +clean-depend: + +distclean-depend: + -rm -rf .deps + +maintainer-clean-depend: + +%.o: %.c + @echo '$(COMPILE) -c $<'; \ + $(COMPILE) -Wp,-MD,.deps/$(*F).pp -c $< + @-cp .deps/$(*F).pp .deps/$(*F).P; \ + tr ' ' '\012' < .deps/$(*F).pp \ + | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \ + >> .deps/$(*F).P; \ + rm .deps/$(*F).pp + +%.lo: %.c + @echo '$(LTCOMPILE) -c $<'; \ + $(LTCOMPILE) -Wp,-MD,.deps/$(*F).pp -c $< + @-sed -e 's/^\([^:]*\)\.o[ ]*:/\1.lo \1.o :/' \ + < .deps/$(*F).pp > .deps/$(*F).P; \ + tr ' ' '\012' < .deps/$(*F).pp \ + | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \ + >> .deps/$(*F).P; \ + rm -f .deps/$(*F).pp + +%.o: %.cxx + @echo '$(CXXCOMPILE) -c $<'; \ + $(CXXCOMPILE) -Wp,-MD,.deps/$(*F).pp -c $< + @-cp .deps/$(*F).pp .deps/$(*F).P; \ + tr ' ' '\012' < .deps/$(*F).pp \ + | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \ + >> .deps/$(*F).P; \ + rm .deps/$(*F).pp + +%.lo: %.cxx + @echo '$(LTCXXCOMPILE) -c $<'; \ + $(LTCXXCOMPILE) -Wp,-MD,.deps/$(*F).pp -c $< + @-sed -e 's/^\([^:]*\)\.o[ ]*:/\1.lo \1.o :/' \ + < .deps/$(*F).pp > .deps/$(*F).P; \ + tr ' ' '\012' < .deps/$(*F).pp \ + | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \ + >> .deps/$(*F).P; \ + rm -f .deps/$(*F).pp +info-am: +info: info-am +dvi-am: +dvi: dvi-am +check-am: all-am +check: check-am +installcheck-am: +installcheck: installcheck-am +install-exec-am: install-libLIBRARIES +install-exec: install-exec-am + +install-data-am: install-includeHEADERS +install-data: install-data-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am +install: install-am +uninstall-am: uninstall-libLIBRARIES uninstall-includeHEADERS +uninstall: uninstall-am +all-am: Makefile $(LIBRARIES) $(HEADERS) +all-redirect: all-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) AM_INSTALL_PROGRAM_FLAGS=-s install +installdirs: + $(mkinstalldirs) $(DESTDIR)$(libdir) $(DESTDIR)$(includedir) + + +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) + -rm -f config.cache config.log stamp-h stamp-h[0-9]* + +maintainer-clean-generic: +mostlyclean-am: mostlyclean-libLIBRARIES mostlyclean-compile \ + mostlyclean-tags mostlyclean-depend mostlyclean-generic + +mostlyclean: mostlyclean-am + +clean-am: clean-libLIBRARIES clean-compile clean-tags clean-depend \ + clean-generic mostlyclean-am + +clean: clean-am + +distclean-am: distclean-libLIBRARIES distclean-compile distclean-tags \ + distclean-depend distclean-generic clean-am + +distclean: distclean-am + +maintainer-clean-am: maintainer-clean-libLIBRARIES \ + maintainer-clean-compile maintainer-clean-tags \ + maintainer-clean-depend maintainer-clean-generic \ + distclean-am + @echo "This command is intended for maintainers to use;" + @echo "it deletes files that may require special tools to rebuild." + +maintainer-clean: maintainer-clean-am + +.PHONY: mostlyclean-libLIBRARIES distclean-libLIBRARIES \ +clean-libLIBRARIES maintainer-clean-libLIBRARIES uninstall-libLIBRARIES \ +install-libLIBRARIES mostlyclean-compile distclean-compile \ +clean-compile maintainer-clean-compile uninstall-includeHEADERS \ +install-includeHEADERS tags mostlyclean-tags distclean-tags clean-tags \ +maintainer-clean-tags distdir mostlyclean-depend distclean-depend \ +clean-depend maintainer-clean-depend info-am info dvi-am dvi check \ +check-am installcheck-am installcheck install-exec-am install-exec \ +install-data-am install-data install-am install uninstall-am uninstall \ +all-redirect all-am all installdirs mostlyclean-generic \ +distclean-generic clean-generic maintainer-clean-generic clean \ +mostlyclean distclean maintainer-clean + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/simgear/threads/SGThread.cxx b/simgear/threads/SGThread.cxx new file mode 100644 index 00000000..27f56082 --- /dev/null +++ b/simgear/threads/SGThread.cxx @@ -0,0 +1,52 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include + +#include "SGThread.hxx" + +void* +start_handler( void* arg ) +{ + SGThread* thr = static_cast(arg); + thr->run(); + return 0; +} + +bool +SGMutex::trylock() +{ + int status = pthread_mutex_lock( &mutex ); + if (status == EBUSY) + { + return false; + } + assert( status == 0 ); + return true; +} + +bool +SGCondition::wait( SGMutex& mutex, unsigned long ms ) +{ + struct timeval now; + ::gettimeofday( &now, 0 ); + + // Wait time is now + ms milliseconds + unsigned int sec = ms / 1000; + unsigned int nsec = (ms % 1000) * 1000; + struct timespec abstime; + abstime.tv_sec = now.tv_sec + sec; + abstime.tv_nsec = now.tv_usec*1000 + nsec; + + int status = pthread_cond_timedwait( &cond, &mutex.mutex, &abstime ); + if (status == ETIMEDOUT) + { + return false; + } + + assert( status == 0 ); + return true; +} + diff --git a/simgear/threads/SGThread.hxx b/simgear/threads/SGThread.hxx new file mode 100644 index 00000000..9440a356 --- /dev/null +++ b/simgear/threads/SGThread.hxx @@ -0,0 +1,270 @@ +// SGThread - Simple pthread class wrappers. +// +// Written by Bernie Bright, started April 2001. +// +// Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au +// +// 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 of the +// License, 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., 675 Mass Ave, Cambridge, MA 02139, USA. +// +// $Id$ + +#ifndef SGTHREAD_HXX_INCLUDED +#define SGTHREAD_HXX_INCLUDED 1 + +#include +#include +#include + +class SGThread; + +extern "C" { + void* start_handler( void* ); +}; + +/** + * Encapsulate generic threading methods. + * Users derive a class from SGThread and implement the run() member function. + */ +class SGThread +{ +public: + + /** + * + */ + SGThread(); + + /** + * + */ + virtual ~SGThread(); + + /** + * + */ + int start(); + + /** + * + */ + void cancel(); + + /** + * + */ + void join(); + +protected: + + /** + * + */ + virtual void run() = 0; + +private: + + /** + * + */ + pthread_t tid; + + friend void* start_handler( void* ); + +private: + // Disable copying. + SGThread( const SGThread& ); + SGThread& operator=( const SGThread& ); +}; + +inline +SGThread::SGThread() +{ +} + +inline +SGThread::~SGThread() +{ +} + +inline int +SGThread::start() +{ + int status = pthread_create( &tid, 0, start_handler, this ); + assert( status == 0 ); + return status; +} + +inline void +SGThread::join() +{ + int status = pthread_join( tid, 0 ); + assert( status == 0 ); +} + +inline void +SGThread::cancel() +{ + int status = pthread_cancel( tid ); + assert( status == 0 ); +} + +/** + * A mutex is used to protect a section of code such that at any time + * only a single thread can execute the code. + */ +class SGMutex +{ + friend class SGCondition; + +public: + + /** + * Create a new mutex. + */ + SGMutex(); + + /** + * Destroy a mutex object. + */ + ~SGMutex(); + + /** + * + */ + void lock(); + + /** + * + */ + bool trylock(); + + /** + * + */ + void unlock(); + +protected: + pthread_mutex_t mutex; +}; + +inline SGMutex::SGMutex() +{ + int status = pthread_mutex_init( &mutex, 0 ); + assert( status == 0 ); +} + +inline SGMutex::~SGMutex() +{ + int status = pthread_mutex_destroy( &mutex ); + assert( status == 0 ); +} + +inline void SGMutex::lock() +{ + int status = pthread_mutex_lock( &mutex ); + assert( status == 0 ); +} + +inline void SGMutex::unlock() +{ + int status = pthread_mutex_unlock( &mutex ); + assert( status == 0 ); +} + +/** + * + */ +class SGCondition +{ +public: + /** + * Create a new condition variable. + */ + SGCondition(); + + /** + * Destroy the condition object. + */ + ~SGCondition(); + + /** + * Wait for this condition variable to be signaled. + * + * @param SGMutex& reference to a locked mutex. + */ + void wait( SGMutex& ); + + /** + * Wait for this condition variable to be signaled for at most + * 'ms' milliseconds. + * + * @param SGMutex& reference to a locked mutex. + * @param unsigned long milliseconds to wait for a signal. + * + * @return + */ + bool wait( SGMutex& mutex, unsigned long ms ); + + /** + * Wake one thread waiting on this condition variable. + */ + void signal(); + + /** + * Wake all threads waiting on this condition variable. + */ + void broadcast(); + +private: + // Disable copying. + SGCondition(const SGCondition& ); + SGCondition& operator=(const SGCondition& ); + +private: + + pthread_cond_t cond; +}; + +inline SGCondition::SGCondition() +{ + int status = pthread_cond_init( &cond, 0 ); + assert( status == 0 ); +} + +inline SGCondition::~SGCondition() +{ + int status = pthread_cond_destroy( &cond ); + assert( status == 0 ); +} + +inline void SGCondition::signal() +{ + int status = pthread_cond_signal( &cond ); + assert( status == 0 ); +} + +inline void SGCondition::broadcast() +{ + int status = pthread_cond_broadcast( &cond ); + assert( status == 0 ); +} + +inline void SGCondition::wait( SGMutex& mutex ) +{ + int status = pthread_cond_wait( &cond, &mutex.mutex ); + assert( status == 0 ); +} + +#endif /* SGTHREAD_HXX_INCLUDED */ -- 2.39.5