aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/shared/mutex.cpp
blob: 65cd095ce109d72de124ced71f7e1d2aa097a32a (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
#include "arch/shared/mutex.hpp"

namespace teachos::arch::shared
{
  auto mutex::lock() -> void
  {
    while (true)
    {
      if (!locked.exchange(true, std::memory_order_acquire))
      {
        return;
      }
    }
  }

  /**
   * @brief Try to lock the mutex (non-blocking)
   *
   * @return true if lock has been acquired and false otherwise
   */
  auto mutex::try_lock() -> bool { return !locked.exchange(true, std::memory_order_acquire); }

  /**
   * @brief Unlock the mutex
   */
  auto mutex::unlock() -> void { locked.store(false, std::memory_order_release); }
}  // namespace teachos::arch::shared