public function get_access_write(&$auto_unlocker_reference)
{
if ($this->writers_count <= 0) {
if ($this->readers_count > 0) {
$this->readers_count = 0;
$this->release_access_read($auto_unlocker_reference);
}
//acquire mutex for writing
if (!$this->acquire_writers_mutex()) {
return false;
}
//only 1 writer can send message to writers queue, so if in queue more than 1 message - it's somebody's error, and we will fix it now:
$this->clean_queue(self::writers);
//tell to readers, that they should wait while we will write
//this action should be made before writer will wait for readers
$sent = $this->increment(self::writers);
//after this command, readers will wait, until we will leave the queue
if (!$sent) {
$this->release_writers_mutex();
return false;
}
//but if readers has come before us - wait, until they finish
if (!$this->wait(self::readers)) {
$this->decrement(self::writers);
$this->release_writers_mutex();
return false;
}
}
$auto_unlocker_reference = new \Jamm\Memory\KeyAutoUnlocker(array($this, 'release_access_write'));
$this->writers_count++;
//and now we can write :)
return true;
}