brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · 1a635c3 Raw
81 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef TEST_SUPPORT_CHECKING_MUTEX_H10#define TEST_SUPPORT_CHECKING_MUTEX_H11 12#include <cassert>13#include <chrono>14 15struct checking_mutex {16  enum state {17    locked_via_lock,18    locked_via_try_lock,19    locked_via_try_lock_for,20    locked_via_try_lock_until,21    unlocked,22    none,23  };24 25  state current_state = unlocked;26  state last_try      = none;27  bool reject         = false;28 29  checking_mutex()                      = default;30  checking_mutex(const checking_mutex&) = delete;31  ~checking_mutex() { assert(current_state == unlocked); }32 33  void lock() {34    assert(current_state == unlocked);35    assert(!reject);36    current_state = locked_via_lock;37    last_try      = locked_via_lock;38    reject        = true;39  }40 41  void unlock() {42    assert(current_state != unlocked && current_state != none);43    last_try      = unlocked;44    current_state = unlocked;45    reject        = false;46  }47 48  bool try_lock() {49    last_try = locked_via_try_lock;50    if (reject)51      return false;52    current_state = locked_via_try_lock;53    return true;54  }55 56  template <class Rep, class Period>57  bool try_lock_for(const std::chrono::duration<Rep, Period>&) {58    last_try = locked_via_try_lock_for;59    if (reject)60      return false;61    current_state = locked_via_try_lock_for;62    return true;63  }64 65  template <class Clock, class Duration>66  bool try_lock_until(const std::chrono::time_point<Clock, Duration>&) {67    last_try = locked_via_try_lock_until;68    if (reject)69      return false;70    current_state = locked_via_try_lock_until;71    return true;72  }73 74  checking_mutex* operator&() = delete;75 76  template <class T>77  void operator,(const T&) = delete;78};79 80#endif // TEST_SUPPORT_CHECKING_MUTEX_H81