]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix ThreadLocalStorage assigment operators 11596/head
authorNils Much <much@lnm.mw.tum.de>
Thu, 21 Jan 2021 14:27:29 +0000 (15:27 +0100)
committerNils Much <much@lnm.mw.tum.de>
Fri, 22 Jan 2021 08:33:46 +0000 (09:33 +0100)
Copy/move exemplar
Add tests

include/deal.II/base/thread_local_storage.h
tests/multithreading/thread_local_storage_07.cc [new file with mode: 0644]
tests/multithreading/thread_local_storage_07.output [new file with mode: 0644]
tests/multithreading/thread_local_storage_08.cc [new file with mode: 0644]
tests/multithreading/thread_local_storage_08.output [new file with mode: 0644]

index 1bd0716b605df07776701f0f73d7058bea0cbdae..89ca9f57011e6fcd6656f39fedf7fe7a1ee9e3ff 100644 (file)
@@ -334,7 +334,10 @@ namespace Threads
     std::shared_lock<decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
     std::unique_lock<decltype(insertion_mutex)> writer_lock(insertion_mutex);
 
-    data = t.data;
+    data     = t.data;
+    exemplar = t.exemplar;
+
+    return *this;
   }
 
 
@@ -352,7 +355,8 @@ namespace Threads
     std::unique_lock<decltype(insertion_mutex)> reader_lock(t.insertion_mutex);
     std::unique_lock<decltype(insertion_mutex)> writer_lock(insertion_mutex);
 
-    data = std::move(t.data);
+    data     = std::move(t.data);
+    exemplar = std::move(t.exemplar);
 
     return *this;
   }
diff --git a/tests/multithreading/thread_local_storage_07.cc b/tests/multithreading/thread_local_storage_07.cc
new file mode 100644 (file)
index 0000000..6375ebd
--- /dev/null
@@ -0,0 +1,79 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2008 - 2018 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+// test if ThreadLocalStorage::operator= (const ThreadLocalStorage<T> &t) copies
+// the shared ptr to the exemplar
+
+#include <deal.II/base/thread_local_storage.h>
+#include <deal.II/base/thread_management.h>
+
+#include "../tests.h"
+
+
+struct X
+{
+  X()
+  {
+    deallog << "Creating" << std::endl;
+  };
+  X(const X &)
+  {
+    deallog << "Copying" << std::endl;
+  };
+  int i;
+};
+
+Threads::ThreadLocalStorage<X> tls_data;
+
+void
+execute(int i)
+{
+  tls_data.get().i = i;
+}
+
+
+void
+test()
+{
+  // create a thread local storage object
+  X                              exemplar;
+  Threads::ThreadLocalStorage<X> temp(exemplar);
+
+  // copy assign thread local storage object
+  tls_data = temp;
+
+  // create 5 threads and wait for their
+  // return. the OS will create 5 individual
+  // thread ids, which means that we will
+  // create 5 individual thread specific
+  // storage locations
+  Threads::ThreadGroup<> tg;
+  for (unsigned int i = 10; i < 15; ++i)
+    tg += Threads::new_thread(execute, i);
+
+  // now make sure the threads all finish
+  tg.join_all();
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test();
+}
diff --git a/tests/multithreading/thread_local_storage_07.output b/tests/multithreading/thread_local_storage_07.output
new file mode 100644 (file)
index 0000000..f66f4ca
--- /dev/null
@@ -0,0 +1,8 @@
+
+DEAL::Creating
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
diff --git a/tests/multithreading/thread_local_storage_08.cc b/tests/multithreading/thread_local_storage_08.cc
new file mode 100644 (file)
index 0000000..6d11dbd
--- /dev/null
@@ -0,0 +1,79 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2008 - 2018 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+// test if ThreadLocalStorage::operator= (ThreadLocalStorage<T> &&t) moves the
+// shared ptr to the exemplar
+
+#include <deal.II/base/thread_local_storage.h>
+#include <deal.II/base/thread_management.h>
+
+#include "../tests.h"
+
+
+struct X
+{
+  X()
+  {
+    deallog << "Creating" << std::endl;
+  };
+  X(const X &)
+  {
+    deallog << "Copying" << std::endl;
+  };
+  int i;
+};
+
+Threads::ThreadLocalStorage<X> tls_data;
+
+void
+execute(int i)
+{
+  tls_data.get().i = i;
+}
+
+
+void
+test()
+{
+  // create a thread local storage object
+  X                              exemplar;
+  Threads::ThreadLocalStorage<X> temp(exemplar);
+
+  // move assign thread local storage object
+  tls_data = std::move(temp);
+
+  // create 5 threads and wait for their
+  // return. the OS will create 5 individual
+  // thread ids, which means that we will
+  // create 5 individual thread specific
+  // storage locations
+  Threads::ThreadGroup<> tg;
+  for (unsigned int i = 10; i < 15; ++i)
+    tg += Threads::new_thread(execute, i);
+
+  // now make sure the threads all finish
+  tg.join_all();
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test();
+}
diff --git a/tests/multithreading/thread_local_storage_08.output b/tests/multithreading/thread_local_storage_08.output
new file mode 100644 (file)
index 0000000..f66f4ca
--- /dev/null
@@ -0,0 +1,8 @@
+
+DEAL::Creating
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying
+DEAL::Copying

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.