aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/random
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2017-11-21 23:05:17 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-11-21 23:08:52 -0800
commitf93c48dc061d23495a4425fcad17d55159cb02b1 (patch)
treeee1210f8810e0b0fec9346f762e854b371899919 /tensorflow/core/lib/random
parentef3ee202659a2a49afcd9898451bf9b1256a2757 (diff)
Use LINKER_INITIALIZED for mutexes with static storage class.
This was causing exit-time races as some threads were accessing the mutex as it was being destructed. --------------- It is illegal to use any static type with a constructor/destructor with static storage class in a multithreaded C++ programme that can exit(), even if the constructor is protected by C++11's function-scope static initialization rules, because exit-time destruction is unsafe in the presence of multiple threads. For things that are not function-scope, the construction is also unsafe, because global contruction ordering is undefined in general. The LINKER_INITIALIZED variant constructor for TensorFlow's mutex avoids these problems, at the cost of relying on the linker to zero-initialize the BSS region. PiperOrigin-RevId: 176612772
Diffstat (limited to 'tensorflow/core/lib/random')
-rw-r--r--tensorflow/core/lib/random/random.cc4
1 files changed, 2 insertions, 2 deletions
diff --git a/tensorflow/core/lib/random/random.cc b/tensorflow/core/lib/random/random.cc
index 723c1100f8..82dc829507 100644
--- a/tensorflow/core/lib/random/random.cc
+++ b/tensorflow/core/lib/random/random.cc
@@ -33,14 +33,14 @@ std::mt19937_64 InitRngWithDefaultSeed() { return std::mt19937_64(); }
uint64 New64() {
static std::mt19937_64* rng = InitRngWithRandomSeed();
- static mutex mu;
+ static mutex mu(LINKER_INITIALIZED);
mutex_lock l(mu);
return (*rng)();
}
uint64 New64DefaultSeed() {
static std::mt19937_64 rng = InitRngWithDefaultSeed();
- static mutex mu;
+ static mutex mu(LINKER_INITIALIZED);
mutex_lock l(mu);
return rng();
}