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 //===----------------------------------------------------------------------===// 8d7da36c6SLouis Dionne 9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads 10f03ac381SLouis Dionne // ALLOW_RETRIES: 2 115a83710eSEric Fiselier 125a83710eSEric Fiselier // <mutex> 135a83710eSEric Fiselier 145a83710eSEric Fiselier // class recursive_timed_mutex; 155a83710eSEric Fiselier 165a83710eSEric Fiselier // bool try_lock(); 175a83710eSEric Fiselier 185a83710eSEric Fiselier #include <mutex> 195a83710eSEric Fiselier #include <thread> 205a83710eSEric Fiselier #include <cstdlib> 215a83710eSEric Fiselier #include <cassert> 225a83710eSEric Fiselier 2356462801SLouis Dionne #include "make_test_thread.h" 247fc6a556SMarshall Clow #include "test_macros.h" 257fc6a556SMarshall Clow 265a83710eSEric Fiselier std::recursive_timed_mutex m; 275a83710eSEric Fiselier 285a83710eSEric Fiselier typedef std::chrono::system_clock Clock; 295a83710eSEric Fiselier typedef Clock::time_point time_point; 305a83710eSEric Fiselier typedef Clock::duration duration; 315a83710eSEric Fiselier typedef std::chrono::milliseconds ms; 325a83710eSEric Fiselier typedef std::chrono::nanoseconds ns; 335a83710eSEric Fiselier f()345a83710eSEric Fiseliervoid f() 355a83710eSEric Fiselier { 365a83710eSEric Fiselier time_point t0 = Clock::now(); 375a83710eSEric Fiselier assert(!m.try_lock()); 385a83710eSEric Fiselier assert(!m.try_lock()); 395a83710eSEric Fiselier assert(!m.try_lock()); 405a83710eSEric Fiselier while(!m.try_lock()) 415a83710eSEric Fiselier ; 425a83710eSEric Fiselier time_point t1 = Clock::now(); 435a83710eSEric Fiselier assert(m.try_lock()); 445a83710eSEric Fiselier m.unlock(); 455a83710eSEric Fiselier m.unlock(); 465a83710eSEric Fiselier ns d = t1 - t0 - ms(250); 475a83710eSEric Fiselier assert(d < ms(200)); // within 200ms 485a83710eSEric Fiselier } 495a83710eSEric Fiselier main(int,char **)502df59c50SJF Bastienint main(int, char**) 515a83710eSEric Fiselier { 525a83710eSEric Fiselier m.lock(); 5356462801SLouis Dionne std::thread t = support::make_test_thread(f); 545a83710eSEric Fiselier std::this_thread::sleep_for(ms(250)); 555a83710eSEric Fiselier m.unlock(); 565a83710eSEric Fiselier t.join(); 572df59c50SJF Bastien 582df59c50SJF Bastien return 0; 595a83710eSEric Fiselier } 60