目录

shared_mutex.h

Overview

Declares the C APIs of the read-write lock (rwlock).

File to include: <ffrt/shared_mutex.h>

Library: libffrt.z.so

System capability: SystemCapability.Resourceschedule.Ffrt.Core

Since: 18

Related module: FFRT

Summary

[h2]Function

Name Description
FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr) Initializes an rwlock. When the rwlock is no longer used, it must be destroyed by ffrt_rwlock_destroy.
FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock) Acquires a write lock. Blocks the current thread when the lock is unavailable. On success, the calling thread holds an exclusive write lock until it is released by ffrt_rwlock_unlock. A write lock is exclusive and cannot be held simultaneously with any read lock.
FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock) Attempts to acquire a write lock. Does not block the current thread. On success, the calling thread holds an exclusive write lock until it is released by ffrt_rwlock_unlock.
FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock) Acquires a read lock. Blocks the current thread when the lock is unavailable. On success, the calling thread holds a read lock until it is released by ffrt_rwlock_unlock. Multiple readers can hold the lock simultaneously, but it cannot be held simultaneously with a write lock.
FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock) Attempts to acquire a read lock. Does not block the current thread. On success, the calling thread holds a read lock until it is released by ffrt_rwlock_unlock.
FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock) Unlocks an rwlock. The calling thread must already hold the rwlock, and the lock must have been acquired by ffrt_rwlock_rdlock, ffrt_rwlock_tryrdlock, ffrt_rwlock_wrlock, or ffrt_rwlock_trywrlock.
FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock) Destroys an rwlock. The rwlock must have been initialized by ffrt_rwlock_init, and it must not be held by any thread as a read lock or write lock when this API is called.

Function Description

[h2]ffrt_rwlock_init()

FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr)

Description

Initializes an rwlock. When the rwlock is no longer used, it must be destroyed by ffrt_rwlock_destroy.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.
const ffrt_rwlockattr_t* attr Pointer to the rwlock attribute. Currently only the default mode is supported, and it must be set to a null pointer.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is initialized successfully and attr is a null pointer.

Returns ffrt_error_inval otherwise.

[h2]ffrt_rwlock_wrlock()

FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock)

Description

Acquires a write lock. Blocks the current thread when the lock is unavailable. On success, the calling thread holds an exclusive write lock until it is released by ffrt_rwlock_unlock. A write lock is exclusive and cannot be held simultaneously with any read lock.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is locked successfully.

Returns ffrt_error_inval if rwlock is a null pointer.

Reference

ffrt_rwlock_rdlock

ffrt_rwlock_trywrlock

[h2]ffrt_rwlock_trywrlock()

FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock)

Description

Tries to acquire a write lock. Does not block the current thread. On success, the calling thread holds an exclusive write lock until it is released by ffrt_rwlock_unlock.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is locked successfully.

Returns ffrt_error_inval or ffrt_error_busy otherwise.

Reference

ffrt_rwlock_wrlock

[h2]ffrt_rwlock_rdlock()

FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock)

Description

Obtains a read lock. Blocks the current thread when the lock is unavailable. On success, the calling thread holds a read lock until it is released by ffrt_rwlock_unlock. Multiple readers can hold the lock simultaneously, but it cannot be held simultaneously with a write lock.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is locked successfully;

Returns ffrt_error_inval if rwlock is a null pointer.

Reference

ffrt_rwlock_wrlock

ffrt_rwlock_tryrdlock

[h2]ffrt_rwlock_tryrdlock()

FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock)

Description

Attempts to acquire a read lock. Does not block the current thread. On success, the calling thread holds a read lock until it is released by ffrt_rwlock_unlock.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is locked successfully.

Returns ffrt_error_inval or ffrt_error_busy otherwise.

Reference

ffrt_rwlock_rdlock

[h2]ffrt_rwlock_unlock()

FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock)

Description

Unlocks an rwlock. The calling thread must already hold the rwlock, and the lock must have been acquired by ffrt_rwlock_rdlock, ffrt_rwlock_tryrdlock, ffrt_rwlock_wrlock, or ffrt_rwlock_trywrlock.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is unlocked successfully.

Returns ffrt_error_inval otherwise.

[h2]ffrt_rwlock_destroy()

FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock)

Description

Destroys an rwlock. The rwlock must have been initialized by ffrt_rwlock_init, and it must not be held by any thread as a read lock or write lock when this API is called.

Since: 18

Parameters

Name Description
ffrt_rwlock_t* rwlock Pointer to the rwlock.

Returns

Type Description
FFRT_C_API int

Returns ffrt_success if the rwlock is destroyed successfully.

Returns ffrt_error_inval otherwise.