
#ifndef PLS_TAS_SPIN_LOCK_H
#define PLS_TAS_SPIN_LOCK_H

#include "tas_spin_lock.h"

#include <atomic>
#include <iostream>

#include "pls/internal/base/thread.h"

namespace pls {
namespace internal {
namespace base {

/**
 * A simple set and test_and_set based spin lock implementation.
 *
 * PORTABILITY:
 * Current implementation is based on C++ 11 atomic_flag.
 */
class tas_spin_lock {
  std::atomic_flag flag_;

 public:
  tas_spin_lock() : flag_{ATOMIC_FLAG_INIT} {};
  tas_spin_lock(const tas_spin_lock &/*other*/) : flag_{ATOMIC_FLAG_INIT} {}

  void lock();
  bool try_lock(unsigned int num_tries = 1);
  void unlock();
};

}
}
}

#endif //PLS_TAS_SPIN_LOCK_H
