# RwLock

`import { RwLock } from "destack:sync";`

Reader-writer lock.

```ds title="RwLock"
export class RwLock<T> {
    state: Atomic<uint32>;
    value: T;
    constructor(value: T);
    read(): RwLockReadGuard<T>;
    tryRead(): RwLockReadGuard<T> | undefined;
    write(): RwLockWriteGuard<T>;
    tryWrite(): RwLockWriteGuard<T> | undefined;
    getExclusive(): &exclusive T;
    intoInner(): T;
}
```

[language/library/src/sync/rwlock.ds:8:49](https://github.com/destack-sh/destack/blob/main/language/library/src/sync/rwlock.ds#L8-L49)

## Members

### `state: Atomic<uint32>`

The lock state.

### `value: T`

The protected value.

### `constructor(value: T)`

Create a reader-writer lock protecting `value`.

### `read(): RwLockReadGuard<T>`

Lock this value for shared reading.

### `tryRead(): RwLockReadGuard<T> | undefined`

Try to lock this value for shared reading.

### `write(): RwLockWriteGuard<T>`

Lock this value for exclusive writing.

### `tryWrite(): RwLockWriteGuard<T> | undefined`

Try to lock this value for exclusive writing.

### `getExclusive(): &exclusive T`

Borrow the protected value without locking.

### `intoInner(): T`

Consume this lock and return the protected value.
