From 0ae839e0bb21b0d647e638a6093e623efeedb620 Mon Sep 17 00:00:00 2001 From: "William E. Kempf" Date: Thu, 12 Jul 2001 20:29:43 +0000 Subject: [PATCH] Applied patches for Unix/GNU toolset. [SVN r10600] --- Jamfile | 2 ++ example/tennis/tennis.cpp | 9 +++++--- include/boost/thread/condition.hpp | 1 + include/boost/thread/config.hpp | 18 +++++++++++++-- include/boost/thread/exceptions.hpp | 36 +++++++++++++++++++++++++++++ include/boost/thread/thread.hpp | 11 ++++----- include/boost/thread/tss.hpp | 3 ++- include/boost/thread/xlock.hpp | 1 + include/boost/thread/xtime.hpp | 3 ++- src/condition.cpp | 2 +- src/mutex.cpp | 2 +- src/recursive_mutex.cpp | 2 +- src/semaphore.cpp | 3 ++- src/thread.cpp | 5 +++- src/timeconv.inl | 27 +++++++++++++++++++++- src/xtime.cpp | 2 ++ test/test_thread.cpp | 4 +++- 17 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 include/boost/thread/exceptions.hpp diff --git a/Jamfile b/Jamfile index 2e7d31ce..976da385 100644 --- a/Jamfile +++ b/Jamfile @@ -1,5 +1,7 @@ # Declare this to be the project root directory project-root ; +#NEEDLIBS = "-lpthread" + # Read subproject Jamfiles subinclude src test example ; diff --git a/example/tennis/tennis.cpp b/example/tennis/tennis.cpp index 7f2d207e..2afd0fdc 100644 --- a/example/tennis/tennis.cpp +++ b/example/tennis/tennis.cpp @@ -4,8 +4,11 @@ #include #include #include -#include -#include + +#if defined(BOOST_HAS_WINTHREADS) +# include +# include +#endif enum game_state { @@ -115,4 +118,4 @@ int main(int argc, char* argv[]) thrdb.join(); return 0; -} \ No newline at end of file +} diff --git a/include/boost/thread/condition.hpp b/include/boost/thread/condition.hpp index 7192b4e2..d42c604f 100644 --- a/include/boost/thread/condition.hpp +++ b/include/boost/thread/condition.hpp @@ -17,6 +17,7 @@ # error Thread support is unavailable! #endif +#include #include #if defined(BOOST_HAS_PTHREADS) diff --git a/include/boost/thread/config.hpp b/include/boost/thread/config.hpp index 5276d4ee..2e24e524 100644 --- a/include/boost/thread/config.hpp +++ b/include/boost/thread/config.hpp @@ -63,7 +63,7 @@ # if defined(_MT) # define BOOST_HAS_THREADS # endif -//# define BOOST_HAS_WINTHREADS // comment out this to test pthreads-win32. +# define BOOST_HAS_WINTHREADS // comment out this to test pthreads-win32. # if !defined(BOOST_HAS_WINTHREADS) # define BOOST_HAS_PTHREADS # define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE @@ -71,6 +71,20 @@ # pragma comment(lib, "pthreadVCE.lib") # endif # define BOOST_HAS_FTIME + // pdm: this is for linux - is there a better #define to #if on? +#elif defined( __GNUC__ ) +# define BOOST_HAS_THREADS +# define BOOST_HAS_PTHREADS +# define BOOST_HAS_NANOSLEEP +# define BOOST_HAS_GETTIMEOFDAY + // pdm: From the pthread.h header, one of these macros + // must be defined for this stuff to exist. +# if defined( __USE_UNIX98 ) +# define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE +# elif defined( __USE_GNU ) +# define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE +# define BOOST_HAS_PTHREAD_YIELD +# endif #endif -#endif // BOOST_THREAD_CONFIG_WEK070601_HPP \ No newline at end of file +#endif // BOOST_THREAD_CONFIG_WEK070601_HPP diff --git a/include/boost/thread/exceptions.hpp b/include/boost/thread/exceptions.hpp new file mode 100644 index 00000000..2a02d94b --- /dev/null +++ b/include/boost/thread/exceptions.hpp @@ -0,0 +1,36 @@ +// Copyright (C) 2001 +// William E. Kempf +// +// Permission to use, copy, modify, distribute and sell this software +// and its documentation for any purpose is hereby granted without fee, +// provided that the above copyright notice appear in all copies and +// that both that copyright notice and this permission notice appear +// in supporting documentation. William E. Kempf makes no representations +// about the suitability of this software for any purpose. +// It is provided "as is" without express or implied warranty. + +// This file is used to configure Boost.Threads during development +// in order to decouple dependency on any Boost release. Once +// accepted into Boost these contents will be moved to +// or some other appropriate build configuration and all +// #include statements will be changed +// accordingly. + +#ifndef BOOST_THREAD_EXCEPTIONS_PDM070801_H +#define BOOST_THREAD_EXCEPTIONS_PDM070801_H + +// Sorry, but this class is used all over the place & I end up +// with recursive headers if I don't separate it +#include + +namespace boost { + +class lock_error : public std::runtime_error +{ +public: + lock_error(); +}; + +} // namespace boost + +#endif // BOOST_THREAD_CONFIG_PDM070801_H diff --git a/include/boost/thread/thread.hpp b/include/boost/thread/thread.hpp index 777da9ad..f7566a7f 100644 --- a/include/boost/thread/thread.hpp +++ b/include/boost/thread/thread.hpp @@ -17,11 +17,12 @@ # error Thread support is unavailable! #endif +#include + #include #include #include #include -#include #include #if defined(BOOST_HAS_PTHREADS) @@ -32,12 +33,6 @@ namespace boost { struct xtime; -class lock_error : public std::runtime_error -{ -public: - lock_error(); -}; - class thread : private noncopyable { public: @@ -60,7 +55,9 @@ private: unsigned long m_thread; unsigned int m_id; #elif defined(BOOST_HAS_PTHREADS) +public: // pdm: sorry about this, I'm getting an error in libs/boost/src/thread.cpp - you can work out how to best fix it ;) class thread_list; +private: friend class thread_list; pthread_t m_thread; diff --git a/include/boost/thread/tss.hpp b/include/boost/thread/tss.hpp index b50ffc79..0d12cd78 100644 --- a/include/boost/thread/tss.hpp +++ b/include/boost/thread/tss.hpp @@ -47,4 +47,5 @@ private: // Change Log: // 6 Jun 01 WEKEMPF Initial version. -#endif // BOOST_TSS_WEK070601_HPP \ No newline at end of file +#endif // BOOST_TSS_WEK070601_HPP + diff --git a/include/boost/thread/xlock.hpp b/include/boost/thread/xlock.hpp index 40430bb9..087514a6 100644 --- a/include/boost/thread/xlock.hpp +++ b/include/boost/thread/xlock.hpp @@ -13,6 +13,7 @@ #define BOOST_XLOCK_WEK070601_HPP #include +#include namespace boost { diff --git a/include/boost/thread/xtime.hpp b/include/boost/thread/xtime.hpp index 5163f672..2b60d460 100644 --- a/include/boost/thread/xtime.hpp +++ b/include/boost/thread/xtime.hpp @@ -45,4 +45,5 @@ int xtime_get(struct xtime* xtp, int clock_type); // Change Log: // 8 Feb 01 WEKEMPF Initial version. -#endif // BOOST_XTIME_WEK070601_HPP \ No newline at end of file +#endif // BOOST_XTIME_WEK070601_HPP + diff --git a/src/condition.cpp b/src/condition.cpp index 08eaeb6f..1af39d8f 100644 --- a/src/condition.cpp +++ b/src/condition.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include "timeconv.inl" diff --git a/src/mutex.cpp b/src/mutex.cpp index ee627072..8584cffb 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include "timeconv.inl" diff --git a/src/recursive_mutex.cpp b/src/recursive_mutex.cpp index d4268cce..815f34a7 100644 --- a/src/recursive_mutex.cpp +++ b/src/recursive_mutex.cpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include "timeconv.inl" diff --git a/src/semaphore.cpp b/src/semaphore.cpp index 4e7de6ea..3227fdda 100644 --- a/src/semaphore.cpp +++ b/src/semaphore.cpp @@ -11,9 +11,10 @@ #include #include -#include +#include #include #include +#include #include "timeconv.inl" #if defined(BOOST_HAS_WINTHREADS) diff --git a/src/thread.cpp b/src/thread.cpp index ffc80d44..1148ef0a 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -271,7 +271,10 @@ void thread::sleep(const xtime& xt) assert(res == 0); # elif defined(BOOST_HAS_NANOSLEEP) timespec ts; - to_timespec(xt, ts); + to_timespec_duration(xt, ts); + + // nanosleep takes a timespec that is an offset, not + // an absolute time. nanosleep(&ts, 0); # else semaphore sema; diff --git a/src/timeconv.inl b/src/timeconv.inl index a5a9518d..c1fbe3f7 100644 --- a/src/timeconv.inl +++ b/src/timeconv.inl @@ -47,6 +47,30 @@ namespace { to_time(milliseconds, xt); to_timespec(xt, ts); } + + inline void to_timespec_duration(const boost::xtime& xt, timespec& ts) + { + boost::xtime cur; + int res = boost::xtime_get(&cur, boost::TIME_UTC); + assert(res == boost::TIME_UTC); + + if (xt.sec < cur.sec || (xt.sec == cur.sec && xt.nsec < cur.nsec)) + { + ts.tv_sec = 0; + ts.tv_nsec = 0; + } + else + { + ts.tv_sec = xt.sec - cur.sec; + ts.tv_nsec = xt.nsec - cur.nsec; + + if( ts.tv_nsec < 0 ) + { + ts.tv_sec -= 1; + ts.tv_nsec += NANOSECONDS_PER_SECOND; + } + } + } #endif inline void to_duration(const boost::xtime& xt, unsigned& milliseconds) @@ -64,4 +88,5 @@ namespace { / NANOSECONDS_PER_MILLISECOND); } } -} \ No newline at end of file +} + diff --git a/src/xtime.cpp b/src/xtime.cpp index baeff44c..05bd8c86 100644 --- a/src/xtime.cpp +++ b/src/xtime.cpp @@ -14,6 +14,8 @@ #if defined(BOOST_HAS_FTIME) # include +#elif defined(BOOST_HAS_GETTIMEOFDAY) +# include #endif namespace boost { diff --git a/test/test_thread.cpp b/test/test_thread.cpp index af4bd35b..4dc32dce 100644 --- a/test/test_thread.cpp +++ b/test/test_thread.cpp @@ -11,7 +11,9 @@ #define BOOST_INCLUDE_MAIN #include -#include +#if defined(BOOST_HAS_WINTHREADS) +# include +#endif template void test_lock(M* dummy=0)