# FileMutex

> FileMutex implements mutex "lock" mechanism via local file system files.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [mutex](/docs/api/yii/mutex/index.md) › FileMutex

| class | `yii\mutex\FileMutex` |
| --- | --- |
| Extends | [`Mutex`](/docs/api/yii/mutex/mutex/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Uses traits | [`RetryAcquireTrait`](/docs/api/yii/mutex/retryacquiretrait/index.md) |
| Since | 2.0 |
| Source | [`framework/mutex/FileMutex.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/mutex/FileMutex.php#L43) |

FileMutex implements mutex "lock" mechanism via local file system files.

This component relies on PHP `flock()` function.

Application configuration example:

```
[
    'components' => [
        'mutex' => [
            'class' => 'yii\mutex\FileMutex'
        ],
    ],
]
```

> Note: this component can maintain the locks only for the single web server,
> it probably will not suffice in case you are using cloud server solution.

> Warning: due to `flock()` function nature this component is unreliable when
> using a multithreaded server API like ISAPI.

See also: [`Mutex`](/docs/api/yii/mutex/mutex/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-autoRelease"></span>[`$autoRelease`](/docs/api/yii/mutex/mutex/index.md#property-autoRelease) | `bool` | Whether all locks acquired in this process (i.e. local locks) must be released automatically before finishing script execution. Default: `true`. | [`Mutex`](/docs/api/yii/mutex/mutex/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$dirMode`](#property-dirMode) | `int` | The permission to be set for newly created directories. Default: `0775`. | `FileMutex` |
| [`$fileMode`](#property-fileMode) | `int\|null` | The permission to be set for newly created mutex files. Default: `null`. | `FileMutex` |
| [`$isWindows`](#property-isWindows) | `bool\|null` | Whether file handling should assume a Windows file system. Default: `null`. Available since 2.0.16. | `FileMutex` |
| [`$mutexPath`](#property-mutexPath) | `string` | The directory to store mutex files. Default: `'@runtime/mutex'`. | `FileMutex` |
| <span id="property-retryDelay"></span>[`$retryDelay`](/docs/api/yii/mutex/retryacquiretrait/index.md#property-retryDelay) | `int` | Number of milliseconds between each try in `acquire()` until specified timeout times out. Default: `50`. Available since 2.0.16. | [`RetryAcquireTrait`](/docs/api/yii/mutex/retryacquiretrait/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-acquire"></span>[`acquire()`](/docs/api/yii/mutex/mutex/index.md#method-acquire) | Acquires a lock by name. | [`Mutex`](/docs/api/yii/mutex/mutex/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like operating systems. | `FileMutex` |
| <span id="method-isAcquired"></span>[`isAcquired()`](/docs/api/yii/mutex/mutex/index.md#method-isAcquired) | Checks if a lock is acquired by the current process. | [`Mutex`](/docs/api/yii/mutex/mutex/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-release"></span>[`release()`](/docs/api/yii/mutex/mutex/index.md#method-release) | Releases acquired lock. This method will return false in case the lock was not found. | [`Mutex`](/docs/api/yii/mutex/mutex/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`acquireLock()`](#method-acquireLock) protected | Acquires lock by given name. | `FileMutex` |
| [`getLockFilePath()`](#method-getLockFilePath) protected | Generate path for lock file. | `FileMutex` |
| [`releaseLock()`](#method-releaseLock) protected | Releases lock by given name. | `FileMutex` |

## Property details

### $dirMode <span id="property-dirMode"></span>

```php
public int $dirMode = 0775
```

The permission to be set for newly created directories.

This value will be used by PHP chmod() function. No umask will be applied.
Defaults to 0775, meaning the directory is read-writable by owner and group,
but read-only for other users.

### $fileMode <span id="property-fileMode"></span>

```php
public int|null $fileMode = null
```

The permission to be set for newly created mutex files.

This value will be used by PHP chmod() function. No umask will be applied.
If not set, the permission will be determined by the current environment.

### $isWindows <span id="property-isWindows"></span>

```php
public bool|null $isWindows = null
```

Whether file handling should assume a Windows file system.

This value will determine how [`releaseLock()`](/docs/api/yii/mutex/filemutex/index.md#method-releaseLock) goes about deleting the lock file.
If not set, it will be determined by checking the DIRECTORY_SEPARATOR constant.

**Since**

2.0.16

### $mutexPath <span id="property-mutexPath"></span>

```php
public string $mutexPath = '@runtime/mutex'
```

The directory to store mutex files.

You may use [path alias](/docs/guide/concept-aliases/index.md) here.
Defaults to the "mutex" subdirectory under the application runtime path.

## Method details

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes mutex component implementation dedicated for UNIX, GNU/Linux, Mac OS X, and other UNIX-like
operating systems.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md)

### acquireLock() <span id="method-acquireLock"></span>

```php
protected function acquireLock(string $name, int $timeout = 0): bool
```

Acquires lock by given name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Of the lock to be acquired. |
| `$timeout` | `int` | Time (in seconds) to wait for lock to become released. |

**Returns**

`bool`: Acquiring result.

### getLockFilePath() <span id="method-getLockFilePath"></span>

```php
protected function getLockFilePath(string $name): string
```

Generate path for lock file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` |  |

**Returns**

`string`

**Since**

2.0.10

### releaseLock() <span id="method-releaseLock"></span>

```php
protected function releaseLock(string $name): bool
```

Releases lock by given name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Of the lock to be released. |

**Returns**

`bool`: Release result.