| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2022 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_mutex_h |
| 16 |
#define dealii_mutex_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <mutex> |
| 22 |
|
| 23 |
DEAL_II_NAMESPACE_OPEN |
| 24 |
|
| 25 |
/** |
| 26 |
* @addtogroup threads |
| 27 |
* @{ |
| 28 |
*/ |
| 29 |
|
| 30 |
namespace Threads |
| 31 |
{ |
| 32 |
/** |
| 33 |
* A class implementing a <a |
| 34 |
* href="https://en.wikipedia.org/wiki/Lock_(computer_science)">mutex</a>. |
| 35 |
* Mutexes are used to lock data structures to ensure that only a |
| 36 |
* single thread of execution can access them at the same time. |
| 37 |
* |
| 38 |
* This class is a thin wrapper around `std::mutex`. The only difference |
| 39 |
* is that this class is copyable when `std::mutex` is not. Indeed, when |
| 40 |
* copied, the receiving object does not copy any state from the object |
| 41 |
* being copied, i.e. an entirely new mutex is created. These semantics |
| 42 |
* are consistent with the common use case if a mutex is used as a member |
| 43 |
* variable to lock the other member variables of a class: in that case, |
| 44 |
* the mutex of the copied-to object should only guard the members of the |
| 45 |
* copied-to object, not the members of both the copied-to and |
| 46 |
* copied-from object. Since at the time when the class is copied, the |
| 47 |
* destination's member variable is not used yet, its corresponding mutex |
| 48 |
* should also remain in its original state. |
| 49 |
*/ |
| 50 |
class Mutex : public std::mutex |
| 51 |
{ |
| 52 |
public: |
| 53 |
/** |
| 54 |
* Default constructor. |
| 55 |
*/ |
| 56 |
Mutex() = default; |
| 57 |
|
| 58 |
/** |
| 59 |
* Copy constructor. As discussed in this class's documentation, no state |
| 60 |
* is copied from the object given as argument. |
| 61 |
*/ |
| 62 |
Mutex(const Mutex &) |
| 63 |
: std::mutex() |
| 64 |
{} |
| 65 |
|
| 66 |
/** |
| 67 |
* Copy operators. As discussed in this class's documentation, no state |
| 68 |
* is copied from the object given as argument. |
| 69 |
*/ |
| 70 |
Mutex & |
| 71 |
operator=(const Mutex &) |
| 72 |
{ |
| 73 |
return *this; |
| 74 |
} |
| 75 |
}; |
| 76 |
} // namespace Threads |
| 77 |
|
| 78 |
/** |
| 79 |
* @} |
| 80 |
*/ |
| 81 |
|
| 82 |
DEAL_II_NAMESPACE_CLOSE |
| 83 |
#endif |