]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Provide ThreadLocalStorage::clear().
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Fri, 7 Feb 2014 01:05:06 +0000 (01:05 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Fri, 7 Feb 2014 01:05:06 +0000 (01:05 +0000)
git-svn-id: https://svn.dealii.org/trunk@32429 0785d39b-7218-0410-832d-ea1e28bc413d

tests/base/thread_local_storage_05.cc [new file with mode: 0644]
tests/base/thread_local_storage_05.with_threads=on.output [new file with mode: 0644]
tests/base/thread_local_storage_06.cc [new file with mode: 0644]
tests/base/thread_local_storage_06.with_threads=on.output [new file with mode: 0644]

diff --git a/tests/base/thread_local_storage_05.cc b/tests/base/thread_local_storage_05.cc
new file mode 100644 (file)
index 0000000..5ff0c4a
--- /dev/null
@@ -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 <iomanip>
+#include <fstream>
+#include <unistd.h>
+
+#include <deal.II/base/thread_management.h>
+#include <deal.II/base/thread_local_storage.h>
+
+
+struct X
+{
+  X ()
+  :
+  i(1)
+  {};
+
+  int i;
+};
+
+Threads::ThreadLocalStorage<X> 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<N; ++i)
+    {
+      m[i].acquire ();
+      tg += Threads::new_thread (execute, m[i]);
+    }
+
+  // let threads work through their first part
+  sleep (3);
+
+  // then reset the thread local object and release the mutices so the
+  // threads can actually run to an end
+  tls_data.clear ();
+  for (unsigned int i=0; i<N; ++i)
+    m[i].release ();
+
+  // now make sure the threads all finish
+  tg.join_all ();
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/thread_local_storage_05.with_threads=on.output b/tests/base/thread_local_storage_05.with_threads=on.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
diff --git a/tests/base/thread_local_storage_06.cc b/tests/base/thread_local_storage_06.cc
new file mode 100644 (file)
index 0000000..484fc07
--- /dev/null
@@ -0,0 +1,120 @@
+// ---------------------------------------------------------------------
+// $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 but using a sample object to
+// initialize the thread local storage
+
+#include "../tests.h"
+#include <iomanip>
+#include <fstream>
+#include <unistd.h>
+
+#include <deal.II/base/thread_management.h>
+#include <deal.II/base/thread_local_storage.h>
+
+
+struct X
+{
+  X ()
+  :
+  i(1)
+  {};
+
+  int i;
+};
+
+X initializer ()
+{
+  X x;
+  x.i = 42;
+  return x;
+}
+
+X fourty_two = initializer();
+
+
+Threads::ThreadLocalStorage<X> 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<N; ++i)
+    {
+      m[i].acquire ();
+      tg += Threads::new_thread (execute, m[i]);
+    }
+
+  // let threads work through their first part
+  sleep (3);
+
+  // then reset the thread local object and release the mutices so the
+  // threads can actually run to an end
+  tls_data.clear ();
+  for (unsigned int i=0; i<N; ++i)
+    m[i].release ();
+
+  // now make sure the threads all finish
+  tg.join_all ();
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/thread_local_storage_06.with_threads=on.output b/tests/base/thread_local_storage_06.with_threads=on.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.