~stolowski/thumbnailer/more-debug

« back to all changes in this revision

Viewing changes to include/internal/file_lock.h

  • Committer: CI Train Bot
  • Author(s): Michi Henning
  • Date: 2016-01-07 04:36:36 UTC
  • mfrom: (129.1.22 landing-12.15)
  • Revision ID: ci-train-bot@canonical.com-20160107043636-xbao3rktj4uf3u0f
Merge changes from devel to trunk for landing.
Approved by: Michi Henning, PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <chrono>
 
22
#include <string>
 
23
 
 
24
namespace unity
 
25
{
 
26
 
 
27
namespace thumbnailer
 
28
{
 
29
 
 
30
namespace internal
 
31
{
 
32
 
 
33
class AdvisoryFileLock final
 
34
{
 
35
public:
 
36
    AdvisoryFileLock(std::string const& path);
 
37
    AdvisoryFileLock(AdvisoryFileLock const&) = delete;
 
38
    AdvisoryFileLock& operator=(AdvisoryFileLock const&) = delete;
 
39
    AdvisoryFileLock(AdvisoryFileLock&&) = default;
 
40
    AdvisoryFileLock& operator=(AdvisoryFileLock&&) = default;
 
41
    ~AdvisoryFileLock();
 
42
 
 
43
    static int const sleep_interval = 100;
 
44
 
 
45
    // Returns true if lock can be acquired within msecs, false otherwise.
 
46
    // Pass milliseconds::zero() to wait indefinitely.
 
47
    // Throws if lock is already held by the calling process.
 
48
    // Wait is implemented as a busy-wait loop, retrying every sleep_interval milliseconds.
 
49
    bool lock(std::chrono::milliseconds msecs = std::chrono::milliseconds::zero());
 
50
 
 
51
    // Throws if lock is not held by the calling process.
 
52
    void unlock();
 
53
 
 
54
private:
 
55
    std::string path_;
 
56
    bool locked_;
 
57
    int fd_;
 
58
};
 
59
 
 
60
}  // namespace internal
 
61
 
 
62
}  // namespace thumbnailer
 
63
 
 
64
}  // namespace service