~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/counted_lock.py

  • Committer: Ian Clatworthy
  • Date: 2008-03-27 07:51:10 UTC
  • mto: (3311.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3312.
  • Revision ID: ian.clatworthy@canonical.com-20080327075110-afgd7x03ybju06ez
Reduce evangelism in the User Guide

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Counted lock class"""
18
18
 
19
19
 
20
 
from bzrlib import (
21
 
    errors,
 
20
from bzrlib.errors import (
 
21
    LockError,
 
22
    ReadOnlyError,
22
23
    )
23
24
 
24
25
 
 
26
# TODO: Pass through lock tokens on lock_write and read, and return them...
 
27
#
 
28
# TODO: Allow upgrading read locks to write?  Conceptually difficult.
 
29
 
 
30
 
25
31
class CountedLock(object):
26
32
    """Decorator around a lock that makes it reentrant.
27
33
 
34
40
        self._lock_mode = None
35
41
        self._lock_count = 0
36
42
 
37
 
    def __repr__(self):
38
 
        return "%s(%r)" % (self.__class__.__name__,
39
 
            self._real_lock)
40
 
 
41
43
    def break_lock(self):
42
44
        self._real_lock.break_lock()
43
45
        self._lock_mode = None
54
56
        it is taken in read mode.
55
57
        """
56
58
        if self._lock_mode:
 
59
            assert self._lock_mode in ('r', 'w'), \
 
60
                   "invalid lock mode %r" % self._lock_mode
57
61
            self._lock_count += 1
58
62
        else:
 
63
            assert self._lock_count == 0
59
64
            self._real_lock.lock_read()
60
65
            self._lock_count = 1
61
66
            self._lock_mode = 'r'
62
67
 
63
 
    def lock_write(self, token=None):
 
68
    def lock_write(self):
64
69
        """Acquire the lock in write mode.
65
70
 
66
71
        If the lock was originally acquired in read mode this will fail.
67
 
 
68
 
        :param token: If non-None, reacquire the lock using this token.
69
72
        """
70
73
        if self._lock_count == 0:
71
 
            return_token = self._real_lock.lock_write(token)
 
74
            assert self._lock_mode is None
 
75
            self._real_lock.lock_write()
72
76
            self._lock_mode = 'w'
73
 
            self._lock_count += 1
74
 
            return return_token
75
77
        elif self._lock_mode != 'w':
76
 
            raise errors.ReadOnlyError(self)
77
 
        else:
78
 
            self._real_lock.validate_token(token)
79
 
            self._lock_count += 1
80
 
            return token
 
78
            raise ReadOnlyError(self)
 
79
        self._lock_count += 1
81
80
 
82
81
    def unlock(self):
83
82
        if self._lock_count == 0:
84
 
            raise errors.LockNotHeld(self)
 
83
            raise LockError("%s not locked" % (self,))
85
84
        elif self._lock_count == 1:
86
 
            # these are decremented first; if we fail to unlock the most
87
 
            # reasonable assumption is that we still don't have the lock
88
 
            # anymore
 
85
            self._real_lock.unlock()
89
86
            self._lock_mode = None
90
 
            self._lock_count -= 1
91
 
            self._real_lock.unlock()
92
 
        else:
93
 
            self._lock_count -= 1
 
87
        self._lock_count -= 1