summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/lock.py
blob: 3cd02befe535d5fa98475c1f3ce7cab99c56d18f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""Lock implementation for shared filesystems."""
import os
import fcntl
import errno
import time
import socket

# Default timeout for locks.
DEFAULT_TIMEOUT = 60

class _ReadLockContext(object):
    """Context manager that takes and releases a read lock.

    Arguments are lock and timeout (default 5 minutes)
    """
    def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
        self._lock = lock
        self._timeout = timeout

    def __enter__(self):
        self._lock.acquire_read(self._timeout)

    def __exit__(self,type,value,traceback):
        self._lock.release_read()


class _WriteLockContext(object):
    """Context manager that takes and releases a write lock.

    Arguments are lock and timeout (default 5 minutes)
    """
    def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
        self._lock = lock
        self._timeout = timeout

    def __enter__(self):
        self._lock.acquire_write(self._timeout)

    def __exit__(self,type,value,traceback):
        self._lock.release_write()


class Lock(object):
    """Distributed file-based lock using ``flock``."""

    def __init__(self, file_path):
        self._file_path = file_path
        self._fd = os.open(file_path,os.O_RDWR)
        self._reads = 0
        self._writes = 0


    def write_lock(self, timeout=DEFAULT_TIMEOUT):
        """Convenience method that returns a write lock context."""
        return _WriteLockContext(self, timeout)


    def read_lock(self, timeout=DEFAULT_TIMEOUT):
        """Convenience method that returns a read lock context."""
        return _ReadLockContext(self, timeout)


    def acquire_read(self, timeout):
        """
        Implements recursive lock. If held in both read and write mode,
        the write lock will be maintained until all locks are released
        """
        if self._reads == 0 and self._writes == 0:
            self._lock(fcntl.LOCK_SH, timeout)
        self._reads += 1


    def acquire_write(self, timeout):
        """
        Implements recursive lock
        """
        if self._writes == 0:
            self._lock(fcntl.LOCK_EX, timeout)
        self._writes += 1


    def _lock(self, op, timeout):
        """
        The timeout is implemented using nonblocking flock()
        to avoid using signals for timing
        Write locks store pid and host information to the lock file
        Read locks do not store data
        """
        total_time = 0
        while total_time < timeout:
            try:
                fcntl.flock(self._fd, op | fcntl.LOCK_NB)
                if op == fcntl.LOCK_EX:
                    with open(self._file_path, 'w') as f:
                        f.write("pid = " + str(os.getpid()) + ", host = " + socket.getfqdn())
                return
            except IOError as error:
                if error.errno == errno.EAGAIN or error.errno == EACCES:
                    pass
                else:
                    raise
            time.sleep(0.1)
            total_time += 0.1


    def release_read(self):
        """
        Assert there is a lock of the right type to release, recursive lock
        """
        assert self._reads > 0
        if self._reads == 1 and self._writes == 0:
            self._unlock()
        self._reads -= 1


    def release_write(self):
        """
        Assert there is a lock of the right type to release, recursive lock
        """
        assert self._writes > 0
        if self._writes == 1 and self._reads == 0:
            self._unlock()
        self._writes -= 1


    def _unlock(self):
        """
        Releases the lock regardless of mode. Note that read locks may be
        masquerading as write locks at times, but this removes either.
        """
        fcntl.flock(self._fd, fcntl.LOCK_UN)