From: bangerth Date: Fri, 7 Feb 2014 01:05:06 +0000 (+0000) Subject: Provide ThreadLocalStorage::clear(). X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f216bd255c7d9a409a75b1ff9a16010579a2c10;p=dealii-svn.git Provide ThreadLocalStorage::clear(). git-svn-id: https://svn.dealii.org/trunk@32429 0785d39b-7218-0410-832d-ea1e28bc413d --- 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