KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_lock_retry.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
27
28#ifndef KIGIT_LOCK_RETRY_H
29#define KIGIT_LOCK_RETRY_H
30
31#include <import_export.h>
32
33#include <chrono>
34#include <filesystem>
35#include <functional>
36#include <memory>
37#include <string>
38
39struct git_index;
40struct git_repository;
41
42namespace KIGIT
43{
44
46constexpr std::chrono::milliseconds BACKEND_LOCK_BUDGET{ 5000 };
47
53bool IsLockContention( int aGitError );
54
59APIEXPORT int WithLockRetry( std::chrono::milliseconds aBudget, const std::function<int()>& aOp );
60
67{
68public:
70 explicit REPO_ACTION_LOCK( const std::filesystem::path& aCommonDir, const char* aName = "kicad-action" );
72
75
77 int TryAcquire();
78
80 int Acquire( std::chrono::milliseconds aBudget = BACKEND_LOCK_BUDGET );
81
82 bool Held() const { return m_held; }
83
85 const std::string& Error() const { return m_error; }
86
87 void Release();
88
89private:
90 std::filesystem::path m_path;
91 int m_fd = -1;
92 bool m_held = false;
93 std::string m_error;
94};
95
102int EditIndexLocked( git_repository* aRepo, const std::function<int( git_index* aIndex )>& aEdit );
103
106APIEXPORT std::unique_ptr<REPO_ACTION_LOCK> RepoActionLock( git_repository* aRepo, const char* aName = "kicad-action" );
107
108} // namespace KIGIT
109
110#endif // KIGIT_LOCK_RETRY_H
REPO_ACTION_LOCK(const std::filesystem::path &aCommonDir, const char *aName="kicad-action")
aCommonDir is as git_repository_commondir gives it. aName picks an independent lock in that directory...
std::filesystem::path m_path
REPO_ACTION_LOCK & operator=(const REPO_ACTION_LOCK &)=delete
int TryAcquire()
Try once without waiting. 0 once held, GIT_ELOCKED while another holder has it, GIT_ERROR otherwise.
REPO_ACTION_LOCK(const REPO_ACTION_LOCK &)=delete
int Acquire(std::chrono::milliseconds aBudget=BACKEND_LOCK_BUDGET)
TryAcquire again after each contention until it is held, fails otherwise, or aBudget is spent.
const std::string & Error() const
Why the last TryAcquire could not use the lock file, or empty when it could.
#define APIEXPORT
Macros which export functions from a DLL/DSO.
bool IsLockContention(int aGitError)
True when aGitError says another writer held a lock or moved a ref first.
constexpr std::chrono::milliseconds BACKEND_LOCK_BUDGET
Budget for a backend call, which never runs on the UI thread.
int EditIndexLocked(git_repository *aRepo, const std::function< int(git_index *aIndex)> &aEdit)
Rewrite aRepo's index the way git does, holding index.lock from a fresh read of the file to the renam...
int WithLockRetry(std::chrono::milliseconds aBudget, const std::function< int()> &aOp)
Run aOp, which returns 0 or a libgit2 error code, again after each lock contention until it succeeds,...
std::unique_ptr< REPO_ACTION_LOCK > RepoActionLock(git_repository *aRepo, const char *aName)
The lock aName in aRepo's common git directory, which every worktree of the repository shares.