1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <algorithm>
10#include <random>
11
12#if _LIBCPP_HAS_THREADS
13# include <mutex>
14# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
15# pragma comment(lib, "pthread")
16# endif
17#endif
18
19_LIBCPP_BEGIN_NAMESPACE_STD
20_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
21
22#if _LIBCPP_HAS_THREADS
23static constinit __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
24#endif
25unsigned __rs_default::__c_ = 0;
26
27__rs_default::__rs_default() {
28#if _LIBCPP_HAS_THREADS
29 __libcpp_mutex_lock(m: &__rs_mut);
30#endif
31 __c_ = 1;
32}
33
34__rs_default::__rs_default(const __rs_default&) { ++__c_; }
35
36__rs_default::~__rs_default() {
37#if _LIBCPP_HAS_THREADS
38 if (--__c_ == 0)
39 __libcpp_mutex_unlock(m: &__rs_mut);
40#else
41 --__c_;
42#endif
43}
44
45__rs_default::result_type __rs_default::operator()() {
46 static mt19937 __rs_g;
47 return __rs_g();
48}
49
50__rs_default __rs_get() { return __rs_default(); }
51
52_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
53_LIBCPP_END_NAMESPACE_STD
54