Home | History | Annotate | Download | only in thread.lock.shared.locking
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // UNSUPPORTED: libcpp-has-no-threads
     11 // UNSUPPORTED: c++98, c++03, c++11
     12 
     13 // <shared_mutex>
     14 
     15 // template <class Mutex> class shared_lock;
     16 
     17 // template <class Clock, class Duration>
     18 //   bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
     19 
     20 #include <shared_mutex>
     21 #include <cassert>
     22 
     23 #include "test_macros.h"
     24 
     25 bool try_lock_until_called = false;
     26 
     27 struct mutex
     28 {
     29     template <class Clock, class Duration>
     30         bool try_lock_shared_until(const std::chrono::time_point<Clock, Duration>& abs_time)
     31     {
     32         typedef std::chrono::milliseconds ms;
     33         assert(Clock::now() - abs_time < ms(5));
     34         try_lock_until_called = !try_lock_until_called;
     35         return try_lock_until_called;
     36     }
     37     void unlock_shared() {}
     38 };
     39 
     40 mutex m;
     41 
     42 int main()
     43 {
     44     typedef std::chrono::steady_clock Clock;
     45     std::shared_lock<mutex> lk(m, std::defer_lock);
     46     assert(lk.try_lock_until(Clock::now()) == true);
     47     assert(try_lock_until_called == true);
     48     assert(lk.owns_lock() == true);
     49 #ifndef TEST_HAS_NO_EXCEPTIONS
     50     try
     51     {
     52         lk.try_lock_until(Clock::now());
     53         assert(false);
     54     }
     55     catch (std::system_error& e)
     56     {
     57         assert(e.code().value() == EDEADLK);
     58     }
     59 #endif
     60     lk.unlock();
     61     assert(lk.try_lock_until(Clock::now()) == false);
     62     assert(try_lock_until_called == false);
     63     assert(lk.owns_lock() == false);
     64     lk.release();
     65 #ifndef TEST_HAS_NO_EXCEPTIONS
     66     try
     67     {
     68         lk.try_lock_until(Clock::now());
     69         assert(false);
     70     }
     71     catch (std::system_error& e)
     72     {
     73         assert(e.code().value() == EPERM);
     74     }
     75 #endif
     76 }
     77