From: Wolfgang Bangerth Date: Thu, 8 Sep 2011 00:10:27 +0000 (+0000) Subject: Add member functions that make the TLS object look more like the underlying object... X-Git-Tag: v8.0.0~3497 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7241f1bebe166d2d93a53123fef29ed833d5335;p=dealii.git Add member functions that make the TLS object look more like the underlying object on the current thread. git-svn-id: https://svn.dealii.org/trunk@24283 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/include/deal.II/base/thread_local_storage.h b/deal.II/include/deal.II/base/thread_local_storage.h index e59d0dad2b..692f267742 100644 --- a/deal.II/include/deal.II/base/thread_local_storage.h +++ b/deal.II/include/deal.II/base/thread_local_storage.h @@ -98,6 +98,27 @@ namespace Threads */ T & get (); + /** + * Conversion operator that simply converts the thread-local object + * to the data type that it stores. This function is equivalent to + * calling the get() member function; it's purpose is to make the + * TLS object look more like the object it is storing. + */ + operator T & (); + + /** + * Copy the given argument into the storage space used to represent + * the current thread. Calling this function as tls_data = object + * is equivalent to calling tls_data.get() = object. The + * intent of this operator is to make the ThreadLocalStorage object + * look more like the object it represents on the current thread. + * + * @param t The object to be copied into the storage space used + * for the current thread. + * + * @return The current object, after the changes have been made + **/ + ThreadLocalStorage & operator = (const T &t); private: #if DEAL_II_USE_MT == 1 /** @@ -148,7 +169,24 @@ namespace Threads #endif } + + + template + inline + ThreadLocalStorage::operator T& () + { + return get(); + } + + template + inline + ThreadLocalStorage & + ThreadLocalStorage::operator = (const T &t) + { + get() = t; + return *this; + } } // end of implementation of namespace Threads /** diff --git a/tests/base/thread_local_storage_03.cc b/tests/base/thread_local_storage_03.cc new file mode 100644 index 0000000000..65fb1eb751 --- /dev/null +++ b/tests/base/thread_local_storage_03.cc @@ -0,0 +1,66 @@ +//----------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2008, 2011 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------------------------------------------------------------- + +// test ThreadLocalStorage::operator T& + +#include "../tests.h" +#include +#include + +#include +#include + + +struct X +{ + Threads::ThreadLocalStorage tls_data; + + X () + : + tls_data (42) + {} + + int f () + { + // access TLS data and have it + // converted to the right data type + // without the need to call + // tls_data.get() + return tls_data; + } +}; + + +void test () +{ + X x; + Threads::Thread t; + t = Threads::new_thread (&X::f, x); + + Assert (t.return_value() == 42, + ExcInternalError()); +} + + + + +int main() +{ + std::ofstream logfile("thread_local_storage_03/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + test (); + deallog << "OK" << std::endl; +} diff --git a/tests/base/thread_local_storage_03/cmp/generic b/tests/base/thread_local_storage_03/cmp/generic new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/thread_local_storage_03/cmp/generic @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/base/thread_local_storage_04.cc b/tests/base/thread_local_storage_04.cc new file mode 100644 index 0000000000..aa9b53d9d3 --- /dev/null +++ b/tests/base/thread_local_storage_04.cc @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2008, 2011 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------------------------------------------------------------- + +// test ThreadLocalStorage::operator= (const T&) + +#include "../tests.h" +#include +#include + +#include +#include + +int counter = 10; + +struct X +{ + Threads::ThreadLocalStorage tls_data; + + X () + : + tls_data (42) + {} + + int f () + { + // use TLS::operator= + tls_data = counter++; + // access TLS data and have it + // converted to the right data type + // without the need to call + // tls_data.get() + return tls_data; + } +}; + + +void test () +{ + X x; + { + Threads::Thread t; + t = Threads::new_thread (&X::f, x); + Assert (t.return_value() == 10, + ExcInternalError()); + } + { + Threads::Thread t; + t = Threads::new_thread (&X::f, x); + Assert (t.return_value() == 11, + ExcInternalError()); + } + + Assert (counter == 12, ExcInternalError()); +} + + + + +int main() +{ + std::ofstream logfile("thread_local_storage_04/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + test (); + deallog << "OK" << std::endl; +} diff --git a/tests/base/thread_local_storage_04/cmp/generic b/tests/base/thread_local_storage_04/cmp/generic new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/thread_local_storage_04/cmp/generic @@ -0,0 +1,2 @@ + +DEAL::OK