summaryrefslogtreecommitdiffstats
path: root/system/libraries/Session/Session_driver.php
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2014-08-27 21:14:36 +0200
committerAndrey Andreev <narf@devilix.net>2014-08-27 21:14:36 +0200
commit93d9fa77732b2538417b934a9c23293ee465a23d (patch)
tree6f291be13151562ae28b75d540d4c77632cb1337 /system/libraries/Session/Session_driver.php
parent85f0c558ca2f47453ce7e8ae767451f5c0045479 (diff)
feature/session (#3073): Rework locking mechanism & add Redis driver
Diffstat (limited to 'system/libraries/Session/Session_driver.php')
-rw-r--r--system/libraries/Session/Session_driver.php64
1 files changed, 63 insertions, 1 deletions
diff --git a/system/libraries/Session/Session_driver.php b/system/libraries/Session/Session_driver.php
index cc35b66d1..a3bc392ad 100644
--- a/system/libraries/Session/Session_driver.php
+++ b/system/libraries/Session/Session_driver.php
@@ -90,12 +90,19 @@ abstract class CI_Session_driver implements SessionHandlerInterface {
protected $_match_ip;
/**
- * Data dash
+ * Data fingerprint
*
* @var bool
*/
protected $_fingerprint;
+ /**
+ * Lock placeholder
+ *
+ * @var mixed
+ */
+ protected $_lock = FALSE;
+
// ------------------------------------------------------------------------
/**
@@ -202,6 +209,61 @@ abstract class CI_Session_driver implements SessionHandlerInterface {
);
}
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get lock
+ *
+ * A default locking mechanism via semaphores, if ext/sysvsem is available.
+ *
+ * Drivers will usually override this and only fallback to it if no other
+ * locking mechanism is available.
+ *
+ * @param string $session_id
+ * @return bool
+ */
+ protected function _get_lock($session_id)
+ {
+ if ( ! extension_loaded('sysvsem'))
+ {
+ $this->_lock = TRUE;
+ return TRUE;
+ }
+
+ if (($this->_lock = sem_get($session_id.($this->_match_ip ? '_'.$_SERVER['REMOTE_ADDR'] : ''), 1, 0644)) === FALSE)
+ {
+ return FALSE;
+ }
+
+ if ( ! sem_acquire($this->_lock))
+ {
+ sem_remove($this->_lock);
+ $this->_lock = FALSE;
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Release lock
+ *
+ * @return bool
+ */
+ protected function _release_lock()
+ {
+ if (extension_loaded('sysvsem') && $this->_lock)
+ {
+ sem_release($this->_lock);
+ sem_remove($this->_lock);
+ $this->_lock = FALSE;
+ }
+
+ return TRUE;
+ }
+
}
/* End of file Session_driver.php */