| 1 |
// { dg-do run } |
| 2 |
// { dg-additional-options "-pthread" { target pthread } } |
| 3 |
// { dg-require-effective-target c++11 } |
| 4 |
// { dg-require-gthreads "" } |
| 5 |
|
| 6 |
// Copyright (C) 2010-2025 Free Software Foundation, Inc. |
| 7 |
// |
| 8 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 9 |
// software; you can redistribute it and/or modify it under the |
| 10 |
// terms of the GNU General Public License as published by the |
| 11 |
// Free Software Foundation; either version 3, or (at your option) |
| 12 |
// any later version. |
| 13 |
|
| 14 |
// This library is distributed in the hope that it will be useful, |
| 15 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
// GNU General Public License for more details. |
| 18 |
|
| 19 |
// You should have received a copy of the GNU General Public License along |
| 20 |
// with this library; see the file COPYING3. If not see |
| 21 |
// <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
#include <chrono> |
| 24 |
#include <condition_variable> |
| 25 |
#include <system_error> |
| 26 |
#include <testsuite_hooks.h> |
| 27 |
|
| 28 |
struct Mutex |
| 29 |
{ |
| 30 |
Mutex() : locked(false) { } |
| 31 |
|
| 32 |
void lock() |
| 33 |
{ |
| 34 |
if (locked) |
| 35 |
throw locked; |
| 36 |
mtx.lock(); |
| 37 |
locked = true; |
| 38 |
} |
| 39 |
|
| 40 |
void unlock() |
| 41 |
{ |
| 42 |
if (!locked) |
| 43 |
throw locked; |
| 44 |
mtx.unlock(); |
| 45 |
locked = false; |
| 46 |
} |
| 47 |
|
| 48 |
std::mutex mtx; |
| 49 |
bool locked; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
template <typename ClockType> |
| 54 |
void test01() |
| 55 |
{ |
| 56 |
try |
| 57 |
{ |
| 58 |
std::chrono::microseconds ms(500); |
| 59 |
std::condition_variable_any c1; |
| 60 |
Mutex m; |
| 61 |
m.lock(); |
| 62 |
|
| 63 |
auto then = ClockType::now(); |
| 64 |
std::cv_status result = c1.wait_until(m, then + ms); |
| 65 |
VERIFY( result == std::cv_status::timeout ); |
| 66 |
VERIFY( (ClockType::now() - then) >= ms ); |
| 67 |
VERIFY( m.locked ); |
| 68 |
} |
| 69 |
catch (const std::system_error& e) |
| 70 |
{ |
| 71 |
VERIFY( false ); |
| 72 |
} |
| 73 |
catch (...) |
| 74 |
{ |
| 75 |
VERIFY( false ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/* User defined clock that ticks in two-thousandths of a second |
| 80 |
forty-two minutes ahead of steady_clock. */ |
| 81 |
struct user_defined_clock |
| 82 |
{ |
| 83 |
typedef std::chrono::steady_clock::rep rep; |
| 84 |
typedef std::ratio<1, 2000> period; |
| 85 |
typedef std::chrono::duration<rep, period> duration; |
| 86 |
typedef std::chrono::time_point<user_defined_clock> time_point; |
| 87 |
|
| 88 |
static constexpr bool is_steady = true; |
| 89 |
|
| 90 |
static time_point now() noexcept |
| 91 |
{ |
| 92 |
using namespace std::chrono; |
| 93 |
const auto steady_since_epoch = steady_clock::now().time_since_epoch(); |
| 94 |
const auto user_since_epoch = duration_cast<duration>(steady_since_epoch); |
| 95 |
return time_point(user_since_epoch + minutes(42)); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
int main() |
| 100 |
{ |
| 101 |
test01<std::chrono::steady_clock>(); |
| 102 |
test01<std::chrono::system_clock>(); |
| 103 |
test01<user_defined_clock>(); |
| 104 |
} |