aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/include/kstd/mutex
blob: ae91970f7fe2efd2e3d699fd3f2d75bb6eea50ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef KSTD_MUTEX_HPP
#define KSTD_MUTEX_HPP

#include <atomic>

namespace kstd
{

  //! A non-recursive mutex.
  struct mutex
  {
    mutex();
    ~mutex();

    mutex(mutex const &) = delete;
    mutex(mutex &&) = delete;

    auto operator=(mutex const &) -> mutex & = delete;
    auto operator=(mutex &&) -> mutex & = delete;

    //! Lock the mutex.
    //!
    //! @note This function blocks for as long as the mutex is not available.
    auto lock() -> void;

    //! Try to lock the mutex.
    //!
    //! @note This function never blocks.
    //! @return @p true iff. the mutex was successfully locked, @p false otherwise.
    auto try_lock() -> bool;

    //! Unlock the mutex.
    //!
    //! @note The behavior is undefined if the mutex is not currently held by the thread unlocking it.
    auto unlock() -> void;

  private:
    std::atomic_flag m_locked{};
  };

}  // namespace kstd

#endif