15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier //
9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11
11f17eb4ecSLouis Dionne 
12f03ac381SLouis Dionne // ALLOW_RETRIES: 2
135a83710eSEric Fiselier 
14f17eb4ecSLouis Dionne // shared_timed_mutex was introduced in macosx10.12
15c360553cSLouis Dionne // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
16f17eb4ecSLouis Dionne 
175a83710eSEric Fiselier // <shared_mutex>
185a83710eSEric Fiselier 
195a83710eSEric Fiselier // class shared_timed_mutex;
205a83710eSEric Fiselier 
215a83710eSEric Fiselier // bool try_lock();
225a83710eSEric Fiselier 
235a83710eSEric Fiselier #include <shared_mutex>
245a83710eSEric Fiselier #include <thread>
255a83710eSEric Fiselier #include <cstdlib>
265a83710eSEric Fiselier #include <cassert>
275a83710eSEric Fiselier 
2856462801SLouis Dionne #include "make_test_thread.h"
297fc6a556SMarshall Clow #include "test_macros.h"
307fc6a556SMarshall Clow 
315a83710eSEric Fiselier std::shared_timed_mutex m;
325a83710eSEric Fiselier 
335a83710eSEric Fiselier typedef std::chrono::system_clock Clock;
345a83710eSEric Fiselier typedef Clock::time_point time_point;
355a83710eSEric Fiselier typedef Clock::duration duration;
365a83710eSEric Fiselier typedef std::chrono::milliseconds ms;
375a83710eSEric Fiselier typedef std::chrono::nanoseconds ns;
385a83710eSEric Fiselier 
f()395a83710eSEric Fiselier void f()
405a83710eSEric Fiselier {
415a83710eSEric Fiselier     time_point t0 = Clock::now();
425a83710eSEric Fiselier     assert(!m.try_lock());
435a83710eSEric Fiselier     assert(!m.try_lock());
445a83710eSEric Fiselier     assert(!m.try_lock());
455a83710eSEric Fiselier     while(!m.try_lock())
465a83710eSEric Fiselier         ;
475a83710eSEric Fiselier     time_point t1 = Clock::now();
485a83710eSEric Fiselier     m.unlock();
495a83710eSEric Fiselier     ns d = t1 - t0 - ms(250);
505a83710eSEric Fiselier     assert(d < ms(200));  // within 200ms
515a83710eSEric Fiselier }
525a83710eSEric Fiselier 
main(int,char **)532df59c50SJF Bastien int main(int, char**)
545a83710eSEric Fiselier {
555a83710eSEric Fiselier     m.lock();
5656462801SLouis Dionne     std::thread t = support::make_test_thread(f);
575a83710eSEric Fiselier     std::this_thread::sleep_for(ms(250));
585a83710eSEric Fiselier     m.unlock();
595a83710eSEric Fiselier     t.join();
602df59c50SJF Bastien 
612df59c50SJF Bastien   return 0;
625a83710eSEric Fiselier }
63