lock.pass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // UNSUPPORTED: libcpp-has-no-threads
  10. // FLAKY_TEST.
  11. // <mutex>
  12. // class recursive_timed_mutex;
  13. // void lock();
  14. #include <mutex>
  15. #include <thread>
  16. #include <cstdlib>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. std::recursive_timed_mutex m;
  20. typedef std::chrono::system_clock Clock;
  21. typedef Clock::time_point time_point;
  22. typedef Clock::duration duration;
  23. typedef std::chrono::milliseconds ms;
  24. typedef std::chrono::nanoseconds ns;
  25. void f()
  26. {
  27. time_point t0 = Clock::now();
  28. m.lock();
  29. time_point t1 = Clock::now();
  30. m.lock();
  31. m.unlock();
  32. m.unlock();
  33. ns d = t1 - t0 - ms(250);
  34. assert(d < ms(50)); // within 50ms
  35. }
  36. int main(int, char**)
  37. {
  38. m.lock();
  39. std::thread t(f);
  40. std::this_thread::sleep_for(ms(250));
  41. m.unlock();
  42. t.join();
  43. return 0;
  44. }