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 
     12 // <shared_mutex>
     13 
     14 // template <class Mutex> class shared_lock;
     15 
     16 // void unlock();
     17 
     18 #include <shared_mutex>
     19 #include <cassert>
     20 
     21 #if _LIBCPP_STD_VER > 11
     22 
     23 bool unlock_called = false;
     24 
     25 struct mutex
     26 {
     27     void lock_shared() {}
     28     void unlock_shared() {unlock_called = true;}
     29 };
     30 
     31 mutex m;
     32 
     33 #endif  // _LIBCPP_STD_VER > 11
     34 
     35 int main()
     36 {
     37 #if _LIBCPP_STD_VER > 11
     38     std::shared_lock<mutex> lk(m);
     39     lk.unlock();
     40     assert(unlock_called == true);
     41     assert(lk.owns_lock() == false);
     42     try
     43     {
     44         lk.unlock();
     45         assert(false);
     46     }
     47     catch (std::system_error& e)
     48     {
     49         assert(e.code().value() == EPERM);
     50     }
     51     lk.release();
     52     try
     53     {
     54         lk.unlock();
     55         assert(false);
     56     }
     57     catch (std::system_error& e)
     58     {
     59         assert(e.code().value() == EPERM);
     60     }
     61 #endif  // _LIBCPP_STD_VER > 11
     62 }
     63