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 10f03ac381SLouis Dionne // ALLOW_RETRIES: 2 11ea982ed3SEric Fiselier 125a83710eSEric Fiselier // <mutex> 135a83710eSEric Fiselier 145a83710eSEric Fiselier // class timed_mutex; 155a83710eSEric Fiselier 165a83710eSEric Fiselier // template <class Rep, class Period> 175a83710eSEric Fiselier // unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); 185a83710eSEric Fiselier 195a83710eSEric Fiselier #include <mutex> 205a83710eSEric Fiselier #include <thread> 215a83710eSEric Fiselier #include <cstdlib> 225a83710eSEric Fiselier #include <cassert> 235a83710eSEric Fiselier 2456462801SLouis Dionne #include "make_test_thread.h" 257fc6a556SMarshall Clow #include "test_macros.h" 267fc6a556SMarshall Clow 275a83710eSEric Fiselier std::timed_mutex m; 285a83710eSEric Fiselier 295a83710eSEric Fiselier typedef std::chrono::steady_clock Clock; 305a83710eSEric Fiselier typedef Clock::time_point time_point; 315a83710eSEric Fiselier typedef Clock::duration duration; 325a83710eSEric Fiselier typedef std::chrono::milliseconds ms; 335a83710eSEric Fiselier typedef std::chrono::nanoseconds ns; 345a83710eSEric Fiselier f1()355a83710eSEric Fiseliervoid f1() 365a83710eSEric Fiselier { 375a83710eSEric Fiselier time_point t0 = Clock::now(); 385a83710eSEric Fiselier std::unique_lock<std::timed_mutex> lk(m, ms(300)); 395a83710eSEric Fiselier assert(lk.owns_lock() == true); 405a83710eSEric Fiselier time_point t1 = Clock::now(); 415a83710eSEric Fiselier ns d = t1 - t0 - ms(250); 425a83710eSEric Fiselier assert(d < ms(50)); // within 50ms 435a83710eSEric Fiselier } 445a83710eSEric Fiselier f2()455a83710eSEric Fiseliervoid f2() 465a83710eSEric Fiselier { 475a83710eSEric Fiselier time_point t0 = Clock::now(); 485a83710eSEric Fiselier std::unique_lock<std::timed_mutex> lk(m, ms(250)); 495a83710eSEric Fiselier assert(lk.owns_lock() == false); 505a83710eSEric Fiselier time_point t1 = Clock::now(); 515a83710eSEric Fiselier ns d = t1 - t0 - ms(250); 525a83710eSEric Fiselier assert(d < ms(50)); // within 50ms 535a83710eSEric Fiselier } 545a83710eSEric Fiselier main(int,char **)552df59c50SJF Bastienint main(int, char**) 565a83710eSEric Fiselier { 575a83710eSEric Fiselier { 585a83710eSEric Fiselier m.lock(); 5956462801SLouis Dionne std::thread t = support::make_test_thread(f1); 605a83710eSEric Fiselier std::this_thread::sleep_for(ms(250)); 615a83710eSEric Fiselier m.unlock(); 625a83710eSEric Fiselier t.join(); 635a83710eSEric Fiselier } 645a83710eSEric Fiselier { 655a83710eSEric Fiselier m.lock(); 6656462801SLouis Dionne std::thread t = support::make_test_thread(f2); 675a83710eSEric Fiselier std::this_thread::sleep_for(ms(300)); 685a83710eSEric Fiselier m.unlock(); 695a83710eSEric Fiselier t.join(); 705a83710eSEric Fiselier } 712df59c50SJF Bastien 722df59c50SJF Bastien return 0; 735a83710eSEric Fiselier } 74