From ac183957fc5bb3a775db3cb92d012815b6be15f6 Mon Sep 17 00:00:00 2001 From: wolf Date: Thu, 16 Jan 2003 18:12:00 +0000 Subject: [PATCH] Fix a problem with calling pthread_join on the same threads more than once. git-svn-id: https://svn.dealii.org/trunk@6924 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/thread_management.h | 15 ++++++++++- deal.II/base/source/thread_management.cc | 27 ++++++++++++++++--- deal.II/doc/news/2002/c-3-4.html | 15 ++++++++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/deal.II/base/include/base/thread_management.h b/deal.II/base/include/base/thread_management.h index 59926954a1..2f49b541cf 100644 --- a/deal.II/base/include/base/thread_management.h +++ b/deal.II/base/include/base/thread_management.h @@ -482,6 +482,13 @@ namespace Threads void wait () const; private: + /** + * Copy constructor. Made + * private since copying these + * kinds of objects is a no-go. + */ + PosixThreadManager (const PosixThreadManager &) {}; + /** * List of thread ids. This * variable actually points to @@ -493,7 +500,13 @@ namespace Threads * files including it, we use a * void pointer instead. */ - void * const thread_id_list; + volatile void * const thread_id_list; + + /** + * Mutex by which we guard all + * accesses to the thread list. + */ + PosixThreadMutex list_mutex; }; diff --git a/deal.II/base/source/thread_management.cc b/deal.II/base/source/thread_management.cc index 494be8a660..4252dc99ed 100644 --- a/deal.II/base/source/thread_management.cc +++ b/deal.II/base/source/thread_management.cc @@ -233,7 +233,11 @@ namespace Threads // wait for all threads, and // release memory wait (); - delete reinterpret_cast*>(thread_id_list); + list_mutex.acquire (); + if (thread_id_list != 0) + delete reinterpret_cast*>(thread_id_list); + thread_id_list = 0; + list_mutex.release (); } @@ -246,7 +250,10 @@ namespace Threads std::list &tid_list = *reinterpret_cast*>(thread_id_list); + list_mutex.acquire (); tid_list.push_back (pthread_t()); + pthread_t *tid = &tid_list.back(); + list_mutex.release (); // start new thread. retry until // we either succeed or get an @@ -254,11 +261,11 @@ namespace Threads int error = 0; do { - error = pthread_create (&tid_list.back(), 0, fun_ptr, fun_data); + error = pthread_create (tid, 0, fun_ptr, fun_data); } while (error == EAGAIN); - Assert (error == 0, ExcInternalError()); + AssertThrow (error == 0, ExcInternalError()); } @@ -266,12 +273,26 @@ namespace Threads void PosixThreadManager::wait () const { + list_mutex.acquire (); std::list &tid_list = *reinterpret_cast*>(thread_id_list); + // wait for all the threads in + // turn for (std::list::iterator i=tid_list.begin(); i != tid_list.end(); ++i) pthread_join (*i, 0); + + // now we know that these threads + // have finished, remove their + // tid's from the list. this way, + // when new threads are spawned + // and waited for, we won't wait + // for expired threads with their + // invalid handles again + tid_list.clear (); + + list_mutex.release (); } # endif diff --git a/deal.II/doc/news/2002/c-3-4.html b/deal.II/doc/news/2002/c-3-4.html index 17fdaa6f58..5dfd5e09fb 100644 --- a/deal.II/doc/news/2002/c-3-4.html +++ b/deal.II/doc/news/2002/c-3-4.html @@ -234,6 +234,19 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK

base

    +
  1. + Fixed: The PosixThreadManager called + its wait function in the + destructor. If this had been called before already, then the + same threads would have been waited for twice, which invokes + undefined behavior. This is fixed by making sure that wait removes the id's of the threads it + has already waited for, and so calling it more than once will + not wait for threads which have already been waited for. +
    + (Michael Anderson, WB 2003/01/16) +

    +
  2. Fixed: The Subscriptor uses a counter to count how many SmartPointer objects subscribe @@ -242,7 +255,7 @@ contributor's names are abbreviated by WB (Wolfgang Bangerth), GK the assumption that the variable cannot change between two subsequent reads.
    - (Michael Anderson, WB 2003/01/11) + (WB 2003/01/11)

  3. -- 2.39.5