| 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 | |
| 21 | #if _LIBCPP_HAS_THREADS |
| 22 | static constinit __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER; |
| 23 | #endif |
| 24 | unsigned __rs_default::__c_ = 0; |
| 25 | |
| 26 | __rs_default::__rs_default() { |
| 27 | #if _LIBCPP_HAS_THREADS |
| 28 | __libcpp_mutex_lock(m: &__rs_mut); |
| 29 | #endif |
| 30 | __c_ = 1; |
| 31 | } |
| 32 | |
| 33 | __rs_default::__rs_default(const __rs_default&) { ++__c_; } |
| 34 | |
| 35 | __rs_default::~__rs_default() { |
| 36 | #if _LIBCPP_HAS_THREADS |
| 37 | if (--__c_ == 0) |
| 38 | __libcpp_mutex_unlock(m: &__rs_mut); |
| 39 | #else |
| 40 | --__c_; |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | __rs_default::result_type __rs_default::operator()() { |
| 45 | static mt19937 __rs_g; |
| 46 | return __rs_g(); |
| 47 | } |
| 48 | |
| 49 | __rs_default __rs_get() { return __rs_default(); } |
| 50 | |
| 51 | _LIBCPP_END_NAMESPACE_STD |
| 52 |