Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle 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 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\lock;
18
 
19
use coding_exception;
20
 
21
/**
22
 * Class representing a lock
23
 *
24
 * The methods available for a specific lock type are only known by it's factory.
25
 *
26
 * @package   core
27
 * @category  lock
28
 * @copyright Damyon Wiese 2013
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class lock {
32
 
33
    /** @var string|int $key A unique key representing a held lock */
34
    protected $key = '';
35
 
36
    /** @var lock_factory $factory The factory that generated this lock */
37
    protected $factory;
38
 
39
    /** @var bool $released Has this lock been released? If a lock falls out of scope without being released - show a warning. */
40
    protected $released;
41
 
42
    /** @var string $caller Where was this called from? Stored for when a warning is shown */
43
    protected $caller = 'unknown';
44
 
45
    /**
46
     * Construct a lock containing the unique key required to release it.
47
     * @param mixed $key - The lock key. The type of this is up to the lock_factory being used.
48
     *      For file locks this is a file handle. For MySQL this is a string.
49
     * @param lock_factory $factory - The factory that generated this lock.
50
     */
51
    public function __construct($key, $factory) {
52
        $this->factory = $factory;
53
        $this->key = $key;
54
        $this->released = false;
1441 ariadna 55
 
56
        // Track where the lock was raised, so we can report un-released locks in a helpful way.
57
        $this->caller = format_backtrace(debug_backtrace(), true);
1 efrain 58
    }
59
 
60
    /**
61
     * Sets the lock factory that owns a lock. This function should not be called under normal use.
62
     * It is intended only for cases like {@see timing_wrapper_lock_factory} where we wrap a lock
63
     * factory.
64
     *
65
     * When used, it should be called immediately after constructing the lock.
66
     *
67
     * @param lock_factory $factory New lock factory that owns this lock
68
     */
69
    public function init_factory(lock_factory $factory): void {
70
        $this->factory = $factory;
71
    }
72
 
73
    /**
74
     * Return the unique key representing this lock.
75
     * @return string|int lock key.
76
     */
77
    public function get_key() {
78
        return $this->key;
79
    }
80
 
81
    /**
82
     * Release this lock
83
     * @return bool
84
     */
85
    public function release() {
86
        $this->released = true;
87
        if (empty($this->factory)) {
88
            return false;
89
        }
90
        $result = $this->factory->release_lock($this);
91
        // Release any held references to the factory.
92
        unset($this->factory);
93
        $this->factory = null;
94
        $this->key = '';
95
        return $result;
96
    }
97
 
98
    /**
1441 ariadna 99
     * Release a lock if it has not already been released.
100
     *
101
     * @param bool $withexception If true, throw an exception if the lock has not been released.
102
     * @throws \coding_exception
1 efrain 103
     */
1441 ariadna 104
    public function release_if_not_released(bool $withexception = false): void {
105
        if (!$this->released) {
1 efrain 106
            $key = $this->key;
1441 ariadna 107
 
1 efrain 108
            $this->release();
1441 ariadna 109
 
110
            if ($withexception) {
111
                throw new \core\exception\coding_exception(<<<EOF
112
                    A lock was created but not released at: {$this->caller}
113
                    Code should look like:
114
 
115
                        \$factory = \core\lock\lock_config::get_lock_factory('type');
116
                        \$lock = \$factory->get_lock($key);
117
                        \$lock->release();  // Locks must ALWAYS be released like this.
118
                EOF);
119
            }
1 efrain 120
        }
121
    }
122
 
1441 ariadna 123
    /**
124
     * Print debugging if this lock falls out of scope before being released.
125
     */
126
    public function __destruct() {
127
        global $CFG;
128
        $this->release_if_not_released(defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST || $CFG->debugdeveloper);
129
    }
1 efrain 130
}