fork download
#include <stdint.h>

#define CLI __asm__ __volatile__( "cli" )
#define STI __asm__ __volatile__( "sti" )

class spin_lock
{
public:
	spin_lock() : _isr_state{0}, _lock{0} {};

	void lock( void )
	{
		/* remember the current IF flag and disable interrupts */
		_isr_state = ( processor::core::read_eflags() & ( 1 << 9 ) );
		CLI;

		/* go for the lock */
		do {} while( __sync_lock_test_and_set( &_lock, 1 ) );
	}

	void unlock( void )
	{
		/* release the lock */
		__sync_lock_release( &_lock );

		if( _isr_state )
		{
			/* if IF was set re-enable interrupts */
			STI;
		}
	}

private:
	bool             _isr_state = 0;
	volatile uint8_t _lock = 0;
};
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘void spin_lock::lock()’:
prog.cpp:11:18: error: ‘processor’ has not been declared
   _isr_state = ( processor::core::read_flags() & ( 1 << 9 ) );
                  ^
prog.cpp:12:3: error: ‘processor’ has not been declared
   processor::core::disable_interrupts();
   ^
prog.cpp: In member function ‘void spin_lock::unlock()’:
prog.cpp:26:4: error: ‘processor’ has not been declared
    processor::core::enable_interrupts();
    ^
stdout
Standard output is empty