KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_action_lock.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 The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef KIGIT_ACTION_LOCK_H
21#define KIGIT_ACTION_LOCK_H
22
24
25#include <memory>
26#include <mutex>
27
28namespace KIGIT
29{
30
36{
37public:
38 ACTION_LOCK() = default;
39
41 explicit ACTION_LOCK( std::unique_lock<std::mutex>&& aMutex ) :
42 m_mutex( std::move( aMutex ) )
43 {
44 }
45
46 ACTION_LOCK( std::unique_lock<std::mutex>&& aMutex, std::unique_ptr<REPO_ACTION_LOCK> aRepo ) :
47 m_mutex( std::move( aMutex ) ),
48 m_repo( std::move( aRepo ) )
49 {
50 }
51
52 ACTION_LOCK( ACTION_LOCK&& ) noexcept = default;
53
54 ACTION_LOCK& operator=( ACTION_LOCK&& aOther ) noexcept
55 {
56 if( this != &aOther )
57 {
58 unlock();
59 m_mutex = std::move( aOther.m_mutex );
60 m_repo = std::move( aOther.m_repo );
61 }
62
63 return *this;
64 }
65
67
68 bool owns_lock() const { return m_mutex.owns_lock() && ( !m_repo || m_repo->Held() ); }
69
70 explicit operator bool() const { return owns_lock(); }
71
72 void unlock()
73 {
74 m_repo.reset();
75
76 if( m_mutex.owns_lock() )
77 m_mutex.unlock();
78 }
79
80private:
81 std::unique_lock<std::mutex> m_mutex;
82 std::unique_ptr<REPO_ACTION_LOCK> m_repo;
83};
84
85} // namespace KIGIT
86
87#endif // KIGIT_ACTION_LOCK_H
ACTION_LOCK(std::unique_lock< std::mutex > &&aMutex)
The mutex alone, which no other process sees.
ACTION_LOCK()=default
std::unique_lock< std::mutex > m_mutex
ACTION_LOCK(std::unique_lock< std::mutex > &&aMutex, std::unique_ptr< REPO_ACTION_LOCK > aRepo)
std::unique_ptr< REPO_ACTION_LOCK > m_repo
Null when nothing outside this process can share the tree.
ACTION_LOCK(ACTION_LOCK &&) noexcept=default
Retrying short git writes that lost a race with another process.
STL namespace.