#pragma once

#include <euler/syscall.hpp>

namespace std {

  class mutex {

    euler::syscall::stream_handle in_handle;
    euler::syscall::stream_handle out_handle;

  public:
    inline mutex() noexcept {
      euler::syscall::create_private_socket(in_handle, out_handle);
      uint8_t byte = 0;
      euler::syscall::write_to_stream(in_handle, 1, &byte);
    }

    mutex(const mutex &) = delete;

    inline ~mutex() {
      euler::syscall::close_stream(in_handle);
      euler::syscall::close_stream(out_handle);
    }

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

    inline void lock() {
      uint8_t byte;
      euler::syscall::read_from_stream(out_handle, 1, &byte);
    }

    inline void unlock() {
      uint8_t byte = 0;
      euler::syscall::write_to_stream(in_handle, 1, &byte);
    }

  };

}
