[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Task](https://evolu.dev/docs/api-reference/common/Task) › MutexRef

Defined in: [packages/common/src/Task.ts:6149](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6149)

[Ref](https://evolu.dev/docs/api-reference/common/Ref/interfaces/Ref) protected by a [Mutex](https://evolu.dev/docs/api-reference/common/Task/interfaces/Mutex).

`MutexRef` serializes reads, writes, and updates through an internal Mutex.
Reads see a stable value, while writes and updates commit one transition at a
time. When an update fails or is aborted, the previous value is preserved.

`MutexRef` is non-reentrant. Updaters and modifiers run while holding the
internal Mutex, so calling another method on the same MutexRef from inside
one of them waits on itself and will not progress.

Use it for state whose transitions are [Task](https://evolu.dev/docs/api-reference/common/Task/type-aliases/Task)s: atomic async
read-modify-write. Plain Ref cannot express that — between a sync read and a
later write, a concurrent transition can interleave and get lost.

`MutexRef` reads, writes, and updates are Tasks and incur normal [Run](https://evolu.dev/docs/api-reference/common/Task/interfaces/Run)
lifecycle overhead. Use [Ref](https://evolu.dev/docs/api-reference/common/Ref/interfaces/Ref) instead for synchronous state transitions,
especially on allocation-sensitive hot paths.

## See

[createMutexRef](https://evolu.dev/docs/api-reference/common/Task/functions/createMutexRef)

## Properties

<a id="get"></a>

### get

```ts
readonly get: Task<T>;
```

Defined in: [packages/common/src/Task.ts:6151](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6151)

Returns the current value.

---

<a id="getandset"></a>

### getAndSet

```ts
readonly getAndSet: (value: T) => Task<T>;
```

Defined in: [packages/common/src/Task.ts:6157](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6157)

Sets the current value and returns the previous value.

### getAndUpdate

```ts
readonly getAndUpdate: <E, D>(updater: (current: T) => Task<T, E, D>) => Task<T, E, D>;
```

Defined in: [packages/common/src/Task.ts:6168](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6168)

Updates the current value and returns the previous value.

### modify

```ts
readonly modify: <R, E, D>(modifier: (current: T) => Task<readonly [R, T], E, D>) => Task<R, E, D>;
```

Defined in: [packages/common/src/Task.ts:6178](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6178)

Modifies the current value and returns a computed result.

### set

```ts
readonly set: (value: T) => Task<void>;
```

Defined in: [packages/common/src/Task.ts:6154](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6154)

Sets the current value.

### setAndGet

```ts
readonly setAndGet: (value: T) => Task<T>;
```

Defined in: [packages/common/src/Task.ts:6160](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6160)

Sets the current value and returns it.

### snapshot

```ts
readonly snapshot: () => SemaphoreSnapshot;
```

Defined in: [packages/common/src/Task.ts:6183](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6183)

Returns the current lock state for monitoring and debugging.

### update

```ts
readonly update: <E, D>(updater: (current: T) => Task<T, E, D>) => Task<void, E, D>;
```

Defined in: [packages/common/src/Task.ts:6163](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6163)

Updates the current value.

### updateAndGet

```ts
readonly updateAndGet: <E, D>(updater: (current: T) => Task<T, E, D>) => Task<T, E, D>;
```

Defined in: [packages/common/src/Task.ts:6173](https://github.com/evoluhq/evolu/blob/037c390af081e9944d616ff298a729515c5c5ab7/packages/common/src/Task.ts#L6173)

Updates the current value and returns it.