
#ifndef PLS_TTAS_SPIN_LOCK_H
#define PLS_TTAS_SPIN_LOCK_H

#include <atomic>
#include <iostream>

#include "pls/internal/base/thread.h"
#include "pls/internal/base/backoff.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 ttas_spin_lock {
  std::atomic<int> flag_;

 public:
  ttas_spin_lock() : flag_{0} {};
  ttas_spin_lock(const ttas_spin_lock &/*other*/) : flag_{0} {}

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

#endif //PLS_TTAS_SPIN_LOCK_H
