summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
blob: baa10375cf9c916999c5a3a855904f9b960415c4 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// { dg-do run }
// { dg-options "-pthread"  }
// { dg-require-effective-target c++11 }
// { dg-require-effective-target pthread }
// { dg-require-gthreads "" }

// Copyright (C) 2008-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <chrono>
#include <condition_variable>
#include <system_error>
#include <testsuite_hooks.h>
#include <slow_clock.h>

template <typename ClockType>
void test01()
{
  try 
    {
      std::chrono::microseconds ms(500);
      std::condition_variable c1;
      std::mutex m;
      std::unique_lock<std::mutex> l(m);

      auto then = ClockType::now();
      std::cv_status result = c1.wait_until(l, then + ms);
      VERIFY( result == std::cv_status::timeout );
      VERIFY( (ClockType::now() - then) >= ms );
      VERIFY( l.owns_lock() );
    }
  catch (const std::system_error& e)
    {
      VERIFY( false );
    }
  catch (...)
    {
      VERIFY( false );
    }
}

void test01_alternate_clock()
{
  try
    {
      std::condition_variable c1;
      std::mutex m;
      std::unique_lock<std::mutex> l(m);
      auto const expire = slow_clock::now() + std::chrono::seconds(1);

      while (slow_clock::now() < expire)
       {
         auto const result = c1.wait_until(l, expire);

         // If wait_until returns before the timeout has expired when
         // measured against the supplied clock, then wait_until must
         // return no_timeout.
         if (slow_clock::now() < expire)
           VERIFY(result == std::cv_status::no_timeout);

         // If wait_until returns timeout then the timeout must have
         // expired.
         if (result == std::cv_status::timeout)
           VERIFY(slow_clock::now() >= expire);
       }
    }
  catch (const std::system_error& e)
    {
      VERIFY( false );
    }
  catch (...)
    {
      VERIFY( false );
    }
}

/* User defined clock that ticks in two-thousandths of a second
   forty-two minutes ahead of steady_clock. */
struct user_defined_clock
{
  typedef std::chrono::steady_clock::rep rep;
  typedef std::ratio<1, 2000> period;
  typedef std::chrono::duration<rep, period> duration;
  typedef std::chrono::time_point<user_defined_clock> time_point;

  static constexpr bool is_steady = true;

  static time_point now() noexcept
  {
    using namespace std::chrono;
    const auto steady_since_epoch = steady_clock::now().time_since_epoch();
    const auto user_since_epoch = duration_cast<duration>(steady_since_epoch);
    return time_point(user_since_epoch + minutes(42));
  }
};

/*
It's not possible for this test to automatically ensure that the
system_clock test cases result in a wait on CLOCK_REALTIME and steady_clock
test cases result in a wait on CLOCK_MONOTONIC. It's recommended to run the
test under strace(1) and check whether the expected futex calls are made by
glibc. See https://gcc.gnu.org/ml/libstdc++/2019-09/msg00022.html for
instructions.
*/

int main()
{
  test01<std::chrono::steady_clock>();
  test01<std::chrono::system_clock>();
  test01<user_defined_clock>();
  test01_alternate_clock();
}