~stolowski/thumbnailer/more-debug

« back to all changes in this revision

Viewing changes to src/file_lock.cpp

  • 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
#include <internal/file_lock.h>
 
20
 
 
21
#include <unity/UnityExceptions.h>
 
22
 
 
23
#include <thread>
 
24
 
 
25
#include <sys/file.h>
 
26
#include <unistd.h>
 
27
 
 
28
using namespace std;
 
29
 
 
30
namespace unity
 
31
{
 
32
 
 
33
namespace thumbnailer
 
34
{
 
35
 
 
36
namespace internal
 
37
{
 
38
 
 
39
int const AdvisoryFileLock::sleep_interval;
 
40
 
 
41
AdvisoryFileLock::AdvisoryFileLock(string const& path)
 
42
    : path_(path)
 
43
    , locked_(false)
 
44
{
 
45
    if ((fd_ = open(path_.c_str(), O_CREAT, 0444)) == -1)
 
46
    {
 
47
        throw FileException("AdvisoryFileLock::lock(): cannot open " + path_, errno);
 
48
    }
 
49
}
 
50
 
 
51
bool AdvisoryFileLock::lock(chrono::milliseconds msecs)
 
52
{
 
53
    if (locked_)
 
54
    {
 
55
        throw LogicException("AdvisoryFileLock::lock(): locked already: " + path_);
 
56
    }
 
57
 
 
58
    auto mode = LOCK_EX;
 
59
    if (msecs != chrono::milliseconds::zero())
 
60
    {
 
61
        mode |= LOCK_NB;
 
62
    }
 
63
    int64_t remaining_time = msecs.count();
 
64
    int rc;
 
65
    do
 
66
    {
 
67
        if ((rc = flock(fd_, mode)) == -1)
 
68
        {
 
69
            if (errno == EWOULDBLOCK)
 
70
            {
 
71
                this_thread::sleep_for(chrono::milliseconds(sleep_interval));
 
72
                remaining_time -= sleep_interval;
 
73
            }
 
74
            else
 
75
            {
 
76
                throw FileException("AdvisoryFileLock::lock(): flock failed", errno);  // LCOV_EXCL_LINE
 
77
            }
 
78
        }
 
79
    }
 
80
    while (rc == -1 && remaining_time > 0);
 
81
    locked_ = rc != -1;
 
82
    return locked_;
 
83
}
 
84
 
 
85
void AdvisoryFileLock::unlock()
 
86
{
 
87
    if (!locked_)
 
88
    {
 
89
        throw LogicException("AdvisoryFileLock::unlock(): unlocked already: " + path_);
 
90
    }
 
91
    if (flock(fd_, LOCK_UN) == -1)
 
92
    {
 
93
        throw FileException("AdvisoryFileLock::unlock(): cannot unlock " + path_, errno);  // LCOV_EXCL_LINE
 
94
    }
 
95
    locked_ = false;
 
96
}
 
97
 
 
98
AdvisoryFileLock::~AdvisoryFileLock()
 
99
{
 
100
    if (fd_ >= 0)
 
101
    {
 
102
        ::close(fd_);
 
103
    }
 
104
}
 
105
 
 
106
}  // namespace internal
 
107
 
 
108
}  // namespace thumbnailer
 
109
 
 
110
}  // namespace unity