Proyectos de Subversion Moodle

Rev

| 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;
55
        $caller = debug_backtrace(true, 2)[1];
56
        if ($caller && array_key_exists('file', $caller ) ) {
57
            $this->caller = $caller['file'] . ' on line ' . $caller['line'];
58
        } else if ($caller && array_key_exists('class', $caller)) {
59
            $this->caller = $caller['class'] . $caller['type'] . $caller['function'];
60
        }
61
    }
62
 
63
    /**
64
     * Sets the lock factory that owns a lock. This function should not be called under normal use.
65
     * It is intended only for cases like {@see timing_wrapper_lock_factory} where we wrap a lock
66
     * factory.
67
     *
68
     * When used, it should be called immediately after constructing the lock.
69
     *
70
     * @param lock_factory $factory New lock factory that owns this lock
71
     */
72
    public function init_factory(lock_factory $factory): void {
73
        $this->factory = $factory;
74
    }
75
 
76
    /**
77
     * Return the unique key representing this lock.
78
     * @return string|int lock key.
79
     */
80
    public function get_key() {
81
        return $this->key;
82
    }
83
 
84
    /**
85
     * @deprecated since Moodle 3.10.
86
     */
87
    public function extend() {
88
        throw new coding_exception('The function extend() has been removed, please do not use it anymore.');
89
    }
90
 
91
    /**
92
     * Release this lock
93
     * @return bool
94
     */
95
    public function release() {
96
        $this->released = true;
97
        if (empty($this->factory)) {
98
            return false;
99
        }
100
        $result = $this->factory->release_lock($this);
101
        // Release any held references to the factory.
102
        unset($this->factory);
103
        $this->factory = null;
104
        $this->key = '';
105
        return $result;
106
    }
107
 
108
    /**
109
     * Print debugging if this lock falls out of scope before being released.
110
     */
111
    public function __destruct() {
112
        if (!$this->released && defined('PHPUNIT_TEST')) {
113
            $key = $this->key;
114
            $this->release();
115
            throw new \coding_exception("A lock was created but not released at:\n" .
116
                                        $this->caller . "\n\n" .
117
                                        " Code should look like:\n\n" .
118
                                        " \$factory = \core\lock\lock_config::get_lock_factory('type');\n" .
119
                                        " \$lock = \$factory->get_lock($key);\n" .
120
                                        " \$lock->release();  // Locks must ALWAYS be released like this.\n\n");
121
        }
122
    }
123
 
124
}