KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1/*
2* This program source code file is part of KiCad, a free EDA CAD application.
3*
4* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
5*
6* This program is free software: you can redistribute it and/or modify it
7* under the terms of the GNU General Public License as published by the
8* Free Software Foundation, either version 3 of the License, or (at your
9* option) any later version.
10*
11* This program is distributed in the hope that it will be useful, but
12* WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License along
17* with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef __KICAD_SPINLOCK_H
21#define __KICAD_SPINLOCK_H
22
23#include <atomic>
24
25
30{
31public:
33 m_lock( false )
34 {}
35
36 void lock()
37 {
38 while( m_lock.exchange( true, std::memory_order_acquire ) );
39 }
40
41 bool try_lock()
42 {
43 return !m_lock.exchange( true, std::memory_order_acquire );
44 }
45
46 void unlock()
47 {
48 m_lock.store( false, std::memory_order_release );
49 }
50
51 bool test()
52 {
53 return m_lock.load( std::memory_order_acquire );
54 }
55
56private:
57 std::atomic<bool> m_lock;
58};
59
60#endif
A trivial spinlock implementation with no optimization.
Definition: spinlock.h:30
void lock()
Definition: spinlock.h:36
void unlock()
Definition: spinlock.h:46
bool test()
Definition: spinlock.h:51
KISPINLOCK()
Definition: spinlock.h:32
std::atomic< bool > m_lock
Definition: spinlock.h:57
bool try_lock()
Definition: spinlock.h:41