[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * 4 * This file is part of the phpBB Forum Software package. 5 * 6 * @copyright (c) phpBB Limited <https://www.phpbb.com> 7 * @license GNU General Public License, version 2 (GPL-2.0) 8 * 9 * For full copyright and license information, please see 10 * the docs/CREDITS.txt file. 11 * 12 */ 13 14 namespace phpbb\lock; 15 16 /** 17 * Database locking class 18 */ 19 class db 20 { 21 /** 22 * Name of the config variable this lock uses 23 * @var string 24 */ 25 private $config_name; 26 27 /** 28 * Unique identifier for this lock. 29 * 30 * @var string 31 */ 32 private $unique_id; 33 34 /** 35 * Stores the state of this lock 36 * @var bool 37 */ 38 private $locked; 39 40 /** 41 * The phpBB configuration 42 * @var \phpbb\config\config 43 */ 44 private $config; 45 46 /** 47 * A database connection 48 * @var \phpbb\db\driver\driver_interface 49 */ 50 private $db; 51 52 /** 53 * Creates a named released instance of the lock. 54 * 55 * You have to call acquire() to actually create the lock. 56 * 57 * @param string $config_name A config variable to be used for locking 58 * @param \phpbb\config\config $config The phpBB configuration 59 * @param \phpbb\db\driver\driver_interface $db A database connection 60 */ 61 public function __construct($config_name, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db) 62 { 63 $this->config_name = $config_name; 64 $this->config = $config; 65 $this->db = $db; 66 } 67 68 /** 69 * Tries to acquire the lock by updating 70 * the configuration variable in the database. 71 * 72 * As a lock may only be held by one process at a time, lock 73 * acquisition may fail if another process is holding the lock 74 * or if another process obtained the lock but never released it. 75 * Locks are forcibly released after a timeout of 1 hour. 76 * 77 * @return bool true if lock was acquired 78 * false otherwise 79 */ 80 public function acquire() 81 { 82 if ($this->locked) 83 { 84 return false; 85 } 86 87 if (!isset($this->config[$this->config_name])) 88 { 89 $this->config->set($this->config_name, '0', false); 90 } 91 $lock_value = $this->config[$this->config_name]; 92 93 // make sure lock cannot be acquired by multiple processes 94 if ($lock_value) 95 { 96 // if the other process is running more than an hour already we have to assume it 97 // aborted without cleaning the lock 98 $time = explode(' ', $lock_value); 99 $time = $time[0]; 100 101 if ($time + 3600 >= time()) 102 { 103 return false; 104 } 105 } 106 107 $this->unique_id = time() . ' ' . unique_id(); 108 109 // try to update the config value, if it was already modified by another 110 // process we failed to acquire the lock. 111 $this->locked = $this->config->set_atomic($this->config_name, $lock_value, $this->unique_id, false); 112 113 return $this->locked; 114 } 115 116 /** 117 * Does this process own the lock? 118 * 119 * @return bool true if lock is owned 120 * false otherwise 121 */ 122 public function owns_lock() 123 { 124 return (bool) $this->locked; 125 } 126 127 /** 128 * Releases the lock. 129 * 130 * The lock must have been previously obtained, that is, acquire() call 131 * was issued and returned true. 132 * 133 * Note: Attempting to release a lock that is already released, 134 * that is, calling release() multiple times, is harmless. 135 * 136 * @return null 137 */ 138 public function release() 139 { 140 if ($this->locked) 141 { 142 $this->config->set_atomic($this->config_name, $this->unique_id, '0', false); 143 $this->locked = false; 144 } 145 } 146 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |