aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/libcxx/src/random_shuffle.cpp
blob: 3aaf71d846433aa0fd125b98a7b18a7228f74bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <algorithm>
#include <random>

#ifndef _LIBCPP_HAS_NO_THREADS
#  include <mutex>
#  if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
#    pragma comment(lib, "pthread")
#  endif
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#ifndef _LIBCPP_HAS_NO_THREADS
static constinit __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
#endif
unsigned __rs_default::__c_ = 0;

__rs_default::__rs_default() {
#ifndef _LIBCPP_HAS_NO_THREADS
  __libcpp_mutex_lock(&__rs_mut);
#endif
  __c_ = 1;
}

__rs_default::__rs_default(const __rs_default&) { ++__c_; }

__rs_default::~__rs_default() {
#ifndef _LIBCPP_HAS_NO_THREADS
  if (--__c_ == 0)
    __libcpp_mutex_unlock(&__rs_mut);
#else
  --__c_;
#endif
}

__rs_default::result_type __rs_default::operator()() {
  static mt19937 __rs_g;
  return __rs_g();
}

__rs_default __rs_get() { return __rs_default(); }

_LIBCPP_END_NAMESPACE_STD