// -*-C++-*-
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#ifndef API_SMP_UTILS_HEADER
#define API_SMP_UTILS_HEADER

#include <arch.hpp>

/// x86-related locking stuff ///
#if defined(ARCH_x86)
// Intel 3a  8.10.6.7: 128-byte boundary
typedef unsigned int spinlock_t __attribute__((aligned(128)));

#ifdef INCLUDEOS_SINGLE_THREADED
inline void lock(spinlock_t&) {}
inline void unlock(spinlock_t&) {}
#else
inline void lock(spinlock_t& lock) {
  while (!__sync_bool_compare_and_swap(&lock, 0, 1)) {
    while (lock) asm("pause");
  }
}
inline void unlock(spinlock_t& lock) {
  __sync_lock_release(&lock, 0); // barrier
}
#endif

struct scoped_spinlock
{
  scoped_spinlock(spinlock_t& ref) noexcept : spinlock(ref) {
    //asm("" : : : "memory");
    lock(this->spinlock);
  }
  ~scoped_spinlock() noexcept {
    __sync_lock_release(&spinlock, 0); // barrier
  }
private:
    spinlock_t& spinlock;
};

struct minimal_barrier_t
{
  void inc()
  {
    __sync_fetch_and_add(&val, 1);
  }

  void spin_wait(int max)
  {
    asm("mfence");
    while (this->val < max) {
      asm("pause; nop;");
    }
  }

  void reset(int val)
  {
    asm volatile("mfence");
    this->val = val;
  }

private:
  volatile int val = 0;
};

#endif // arch

#endif // hdr
