~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/lockfile.py

  • Committer: Paul Hummer
  • Date: 2009-03-31 22:24:14 UTC
  • mfrom: (315.1.5 config-cleanup)
  • Revision ID: paul@canonical.com-20090331222414-s0uvie5qiv3nkomv
Cleaned up the config for loggerhead

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (C) 2006  Robey Pointer <robey@lag.net>
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 as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
#
18
 
 
19
 
import os
20
 
import threading
21
 
import time
22
 
 
23
 
from loggerhead import util
24
 
 
25
 
 
26
 
with_lock = util.with_lock('_tlock', 'LockFile')
27
 
 
28
 
 
29
 
class LockFile (object):
30
 
    """
31
 
    simple lockfile implementation that mimics the API of threading.Lock, so
32
 
    it can be used interchangably.  it's actually a reentrant lock, so the
33
 
    lock may be acquired multiple times by the same thread, as long as it's
34
 
    released an equal number of times.  unlike threading.Lock, this lock can
35
 
    be used across processes.
36
 
    
37
 
    this uses os.open(O_CREAT|O_EXCL), which apparently works even on windows,
38
 
    but will not work over NFS, if anyone still uses that.  so don't put the
39
 
    cache folder on an NFS server...
40
 
    """
41
 
 
42
 
    def __init__(self, filename):
43
 
        self._filename = filename
44
 
        # thread lock to maintain internal consistency
45
 
        self._tlock = threading.Lock()
46
 
        self._count = 0
47
 
    
48
 
    @with_lock
49
 
    def _try_acquire(self):
50
 
        if self._count > 0:
51
 
            self._count += 1
52
 
            return True
53
 
        try:
54
 
            fd = os.open(self._filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0600)
55
 
            os.close(fd)
56
 
            self._count += 1
57
 
            return True
58
 
        except OSError:
59
 
            return False
60
 
    
61
 
    def acquire(self):
62
 
        # try over and over, sleeping on exponential backoff with an upper limit of about 5 seconds
63
 
        pause = 0.1
64
 
        max_pause = 5.0
65
 
        while True:
66
 
            if self._try_acquire():
67
 
                return
68
 
            time.sleep(pause)
69
 
            pause = min(pause * 2.0, max_pause)
70
 
 
71
 
    @with_lock
72
 
    def release(self):
73
 
        self._count -= 1
74
 
        if self._count == 0:
75
 
            os.remove(self._filename)