/*! \file pispinlock.h
 * \brief PISpinlock
*/
/*
	PIP - Platform Independent Primitives
	PISpinlock
	Ivan Pelipenko peri4ko@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PISPINLOCK_H
#define PISPINLOCK_H

#include "piinit.h"
#include <atomic>


class PIP_EXPORT PISpinlock
{
public:
	NO_COPY_CLASS(PISpinlock)

	//! Constructs unlocked spinlock
	explicit PISpinlock() {flag.clear();}

	//! Destroy spinlock
	~PISpinlock() {}


	//! \brief Lock spinlock
	//! \details If spinlock is unlocked it set to locked state and returns immediate.
	//! If spinlock is already locked function blocks until spinlock will be unlocked
	void lock() {while (flag.test_and_set(std::memory_order_acquire));}

	//! \brief Unlock spinlock
	//! \details In any case this function returns immediate
	void unlock() {flag.clear(std::memory_order_release);}

private:
	std::atomic_flag flag;

};


//! \brief PISpinlockLocker
//! \details
//! When a PISpinlockLocker object is created, it attempts to lock the spinlock it is given, if "condition" true.
//! When control leaves the scope in which the PISpinlockLocker object was created,
//! the PISpinlockLocker is destructed and the spinlock is released, if "condition" true.
//! If "condition" false this class do nothing.
//! The PISpinlockLocker class is non-copyable.
class PIP_EXPORT PISpinlockLocker
{
public:
	NO_COPY_CLASS(PISpinlockLocker)
	PISpinlockLocker(PISpinlock & s, bool condition = true): spinlock(s), cond(condition) {if (cond) spinlock.lock();}
	~PISpinlockLocker() {if (cond) spinlock.unlock();}
private:
	PISpinlock & spinlock;
	bool cond;
};

#endif // PISPINLOCK_H
