<commit_before><?php namespace Semaphore;

use Adapter\AdapterInterface;
use Adapter\Filesystem;
use LogicException;

/**
 * Semaphore wrapper
 * @class
 */
class Semaphore
{
	/**
 	 * List of active keys due to unlock everything for the rescue
	 * @var array
	 */
	protected $keys = array();

	/**
	 * Automaticall unlock all semaphores locked by the given semaphore object
	 * @var bool
	 */
	protected $autoUnlock = true;

	/**
	 * Create semaphore instance with given adapter
	 * If adapter is not given - use default one
	 * @param AdapterInterface $adapter
	 */
	public function __construct(AdapterInterface $adapter = null) {
		$this->adapter = $adapter ?: $this->getDefaultAdapter();
	}

	/**
	 * Set autounlock flag
	 * @param bool
	 */
	public function setAutoUnlock($autoUnlock) {
		$this->autoUnlock = $this->autoUnlock;
	}

	/**
	 * Unlock all locked semaphores
	 */
	public function __destruct() {
		if ($this->autoUnlock) {
			$this->unlockAll();
		}
	}

	/**
	 * Unlock all previously locked semaphores
	 */
	public function unlockAll() {
		foreach ($this->keys as $key) {
			$this->unlock($key);
		}
	}

	/**
	 * Attempt to lock semaphore with given key
	 * @param string $key
	 * @return true if successfully locked semaphore, otherwise false
	 */
	public function lock($key) {
		$locked = $this->adapter->lock($key);

		if ($locked) {
			$this->storeKey($key);
		}

		return $locked;
	}

	/**
	 * Unlock semaphore with given key
	 * @param string $key
	 * @return bool true if successfully unlocked, otherwise false
	 */
	public function unlock($key) {
		$unlocked = $this->adapter->unlock($key);

		if ($unlocked) {
			$this->destroyKey($key);
		}

		return $unlocked;
	}

	/**
	 * Check if semaphore with given key is locked
	 * @param string $key
	 * @return bool
	 */
	public function locked($key) {
		return $this->adapter->locked($locked);
	}

	/**
	 * Store key to active keys list
	 * @param string $key
	 */
	protected function storeKey($key) {
		if (empty($this->keys)) {
			$this->keys[$key] = true;
		}
	}

	/**
	 * Remove key from active key list
	 * @param string $key
	 */
	protected function destroyKey($key) {
		if (!empty($this->keys[$key])) {
			unset($this->keys[$key]);
		}
	}
}<commit_msg>Change semaphore default adapter<commit_after><?php namespace Semaphore;

use Adapter\AdapterInterface;
use Adapter\SharedMemory;
use LogicException;

/**
 * Semaphore wrapper
 * @class
 */
class Semaphore
{
	/**
	 * List of active keys (created in scope of current object) due to unlock everything for the rescue
	 * @var array
	 */
	protected $keys = array();

	/**
	 * Automaticall unlock all semaphores locked by the given semaphore object
	 * @var bool
	 */
	protected $autoUnlock = true;

	/**
	 * Create semaphore instance with given adapter
	 * If adapter is not given - use default one
	 * @param AdapterInterface $adapter
	 */
	public function __construct(AdapterInterface $adapter = null) {
		$this->adapter = $adapter ?: $this->getDefaultAdapter();
	}

	/**
	 * Set autounlock flag
	 * @param bool
	 */
	public function setAutoUnlock($autoUnlock) {
		$this->autoUnlock = $this->autoUnlock;
	}


	/**
	 * Unlock all previously locked semaphores
	 */
	public function __destruct() {
		if ($this->autoUnlock) {
			$this->unlockAll();
		}
	}

	/**
	 * Unlock all previously locked semaphores
	 */
	public function unlockAll() {
		foreach ($this->keys as $key) {
			$this->unlock($key);
		}
	}

	/**
	 * Attempt to lock semaphore with given key
	 * @param string $key
	 * @return true if successfully locked semaphore, otherwise false
	 */
	public function lock($key) {
		$locked = $this->adapter->lock($key);

		if ($locked) {
			$this->storeKey($key);
		}

		return $locked;
	}

	/**
	 * Unlock semaphore with given key
	 * @param string $key
	 * @return bool true if successfully unlocked, otherwise false
	 */
	public function unlock($key) {
		$unlocked = $this->adapter->unlock($key);

		if ($unlocked) {
			$this->destroyKey($key);
		}

		return $unlocked;
	}

	/**
	 * Check if semaphore with given key is locked
	 * @param string $key
	 * @return bool
	 */
	public function locked($key) {
		return $this->adapter->locked($locked);
	}

	/**
	 * Store key to active keys list
	 * @param string $key
	 */
	protected function storeKey($key) {
		if (empty($this->keys)) {
			$this->keys[$key] = true;
		}
	}

	/**
	 * Remove key from active key list
	 * @param string $key
	 */
	protected function destroyKey($key) {
		if (!empty($this->keys[$key])) {
			unset($this->keys[$key]);
		}
	}

	protected function getDefaultAdapter() {
		return new SharedMemory();
	}
}
{'fraction_non_alphanumeric': 0.14096437880104257, 'fraction_numerical': 0.0, 'mean_word_length': 3.319887429643527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4010643', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1415, 'n_words': 428}