From 9f216bd255c7d9a409a75b1ff9a16010579a2c10 Mon Sep 17 00:00:00 2001 From: bangerth Date: Fri, 7 Feb 2014 01:05:06 +0000 Subject: [PATCH] Provide ThreadLocalStorage::clear(). git-svn-id: https://svn.dealii.org/trunk@32429 0785d39b-7218-0410-832d-ea1e28bc413d --- tests/base/thread_local_storage_05.cc | 109 ++++++++++++++++ ...ad_local_storage_05.with_threads=on.output | 2 + tests/base/thread_local_storage_06.cc | 120 ++++++++++++++++++ ...ad_local_storage_06.with_threads=on.output | 2 + 4 files changed, 233 insertions(+) create mode 100644 tests/base/thread_local_storage_05.cc create mode 100644 tests/base/thread_local_storage_05.with_threads=on.output create mode 100644 tests/base/thread_local_storage_06.cc create mode 100644 tests/base/thread_local_storage_06.with_threads=on.output diff --git a/tests/base/thread_local_storage_05.cc b/tests/base/thread_local_storage_05.cc new file mode 100644 index 0000000000..5ff0c4a8bf --- /dev/null +++ b/tests/base/thread_local_storage_05.cc @@ -0,0 +1,109 @@ +// --------------------------------------------------------------------- +// $Id$ +// +// Copyright (C) 2008 - 2014 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test ThreadLocalStorage::clear + +#include "../tests.h" +#include +#include +#include + +#include +#include + + +struct X +{ + X () + : + i(1) + {}; + + int i; +}; + +Threads::ThreadLocalStorage tls_data; + + +void execute (Threads::Mutex &m) +{ + // check correct default initialization + bool exists; + int i = tls_data.get(exists).i; + Assert (i == 1, ExcInternalError()); + Assert (exists == false, ExcInternalError()); + + // set value + tls_data.get(exists).i = 2; + + // try again. should have existed this time around + i = tls_data.get(exists).i; + Assert (i == 2, ExcInternalError()); + Assert (exists == true, ExcInternalError()); + + // wait for the barrier to clear + m.acquire (); + m.release (); + + // at this point, the tls object should have been cleared and should + // be back at its original value + i = tls_data.get(exists).i; + Assert (i == 1, ExcInternalError()); + Assert (exists == false, ExcInternalError()); +} + + +void test () +{ + const unsigned int N = 10; + Threads::Mutex m[N]; + + // start N threads with mutices locked + Threads::ThreadGroup<> tg; + for (unsigned int i=0; i +#include +#include + +#include +#include + + +struct X +{ + X () + : + i(1) + {}; + + int i; +}; + +X initializer () +{ + X x; + x.i = 42; + return x; +} + +X fourty_two = initializer(); + + +Threads::ThreadLocalStorage tls_data(fourty_two); + + +void execute (Threads::Mutex &m) +{ + // check correct default initialization + bool exists; + int i = tls_data.get(exists).i; + Assert (i == 42, ExcInternalError()); + Assert (exists == false, ExcInternalError()); + + // set value + tls_data.get(exists).i = 2; + + // try again. should have existed this time around + i = tls_data.get(exists).i; + Assert (i == 2, ExcInternalError()); + Assert (exists == true, ExcInternalError()); + + // wait for the barrier to clear + m.acquire (); + m.release (); + + // at this point, the tls object should have been cleared and should + // be back at its original value + i = tls_data.get(exists).i; + Assert (i == 42, ExcInternalError()); + Assert (exists == false, ExcInternalError()); +} + + +void test () +{ + const unsigned int N = 10; + Threads::Mutex m[N]; + + // start N threads with mutices locked + Threads::ThreadGroup<> tg; + for (unsigned int i=0; i