TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkspinlock.h
Go to the documentation of this file.
1 #ifndef TTKSPINLOCK_H
2 #define TTKSPINLOCK_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2025 Greedysky Studio
7 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17 
18  * You should have received a copy of the GNU Lesser General Public License along
19  * with this program; If not, see <http://www.gnu.org/licenses/>.
20  ***************************************************************************/
21 
22 #include "ttkmoduleexport.h"
23 
28 {
29 public:
30  TTKSpinLock() = default;
31 
32  void lock() noexcept
33  {
34  while(m_lock.test_and_set(std::memory_order_acquire))
35  {
36  // wait for spin lock to unlock
37  }
38  }
39 
40  void unlock() noexcept
41  {
42  m_lock.clear(std::memory_order_release);
43  }
44 
45 private:
46  std::atomic_flag m_lock = ATOMIC_FLAG_INIT;
48 
49 };
50 
55 {
56 public:
58  : m_lock(lock)
59  {
60  m_lock.lock();
61  }
62 
64  {
65  m_lock.unlock();
66  }
67 
68 private:
71 
72 };
73 
74 
75 // compatiblity for std spin_lock
76 namespace std
77 {
80 
83 }
84 
85 #endif // TTKSPINLOCK_H
#define TTK_MODULE_EXPORT
The class of the spin lock.
Definition: ttkspinlock.h:27
Definition: ttkcompat.h:39
void unlock() noexcept
Definition: ttkspinlock.h:40
TTKSpinLock spin_lock
Definition: ttkspinlock.h:78
TTKSpinLock & m_lock
Definition: ttkspinlock.h:69
TTKSpinLockGuard spin_lock_guard
Definition: ttkspinlock.h:79
void lock() noexcept
Definition: ttkspinlock.h:32
The class of the spin lock guard.
Definition: ttkspinlock.h:54
TTKSpinLockGuard(TTKSpinLock &lock)
Definition: ttkspinlock.h:57
#define TTK_DISABLE_COPY(Class)
Definition: ttkqtglobal.h:157