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