1# shared_mutex.h 2 3<!--Kit: Function Flow Runtime Kit--> 4<!--Subsystem: Resourceschedule--> 5<!--Owner: @chuchihtung; @yanleo--> 6<!--Designer: @geoffrey_guo; @huangyouzhong--> 7<!--Tester: @lotsof; @sunxuhao--> 8<!--Adviser: @foryourself--> 9 10## Overview 11 12The **shared_mutex.h** file declares read-write lock APIs in C. 13 14**File to include**: <ffrt/shared_mutex.h> 15 16**Library**: libffrt.z.so 17 18**System capability**: SystemCapability.Resourceschedule.Ffrt.Core 19 20**Since**: 18 21 22**Related module**: [FFRT](capi-ffrt.md) 23 24## Summary 25 26### Function 27 28| Name| Description| 29| -- | -- | 30| [FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr)](#ffrt_rwlock_init) | Initializes a read-write lock.| 31| [FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_wrlock) | Obtains a write lock.| 32| [FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_trywrlock) | Attempts to obtain a write lock.| 33| [FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_rdlock) | Obtains a read lock.| 34| [FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_tryrdlock) | Attempts to obtain a read lock.| 35| [FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_unlock) | Releases the read-write lock.| 36| [FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock)](#ffrt_rwlock_destroy) | Destroys the read-write lock.| 37 38## Function Description 39 40### ffrt_rwlock_init() 41 42``` 43FFRT_C_API int ffrt_rwlock_init(ffrt_rwlock_t* rwlock, const ffrt_rwlockattr_t* attr) 44``` 45 46**Description** 47 48Initializes a read-write lock. 49 50**Since**: 18 51 52 53**Parameters** 54 55| Name| Description| 56| -- | -- | 57| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 58| [const ffrt_rwlockattr_t](capi-ffrt-ffrt-rwlockattr-t.md)* attr | Pointer to the read-write lock attribute.| 59 60**Returns** 61 62| Type| Description| 63| -- | -- | 64| FFRT_C_API int | Returns **ffrt_success** if the read-write lock is successfully initialized;<br> returns **ffrt_error_inval** otherwise.| 65 66### ffrt_rwlock_wrlock() 67 68``` 69FFRT_C_API int ffrt_rwlock_wrlock(ffrt_rwlock_t* rwlock) 70``` 71 72**Description** 73 74Obtains a write lock. 75 76**Since**: 18 77 78 79**Parameters** 80 81| Name| Description| 82| -- | -- | 83| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 84 85**Returns** 86 87| Type| Description| 88| -- | -- | 89| FFRT_C_API int | Returns **ffrt_success** if the write lock is successfully obtained;<br> returns **ffrt_error_inval** or blocks the task otherwise.| 90 91### ffrt_rwlock_trywrlock() 92 93``` 94FFRT_C_API int ffrt_rwlock_trywrlock(ffrt_rwlock_t* rwlock) 95``` 96 97**Description** 98 99Attempts to obtain a write lock. 100 101**Since**: 18 102 103 104**Parameters** 105 106| Name| Description| 107| -- | -- | 108| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 109 110**Returns** 111 112| Type| Description| 113| -- | -- | 114| FFRT_C_API int | Returns **ffrt_success** if the write lock is successfully obtained;<br> returns **ffrt_error_inval** or **ffrt_error_busy** otherwise.| 115 116### ffrt_rwlock_rdlock() 117 118``` 119FFRT_C_API int ffrt_rwlock_rdlock(ffrt_rwlock_t* rwlock) 120``` 121 122**Description** 123 124Obtains a read lock. 125 126**Since**: 18 127 128 129**Parameters** 130 131| Name| Description| 132| -- | -- | 133| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 134 135**Returns** 136 137| Type| Description| 138| -- | -- | 139| FFRT_C_API int | Returns **ffrt_success** if the read lock is successfully obtained;<br> returns **ffrt_error_inval** or blocks the task otherwise.| 140 141### ffrt_rwlock_tryrdlock() 142 143``` 144FFRT_C_API int ffrt_rwlock_tryrdlock(ffrt_rwlock_t* rwlock) 145``` 146 147**Description** 148 149Attempts to obtain a read lock. 150 151**Since**: 18 152 153 154**Parameters** 155 156| Name| Description| 157| -- | -- | 158| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 159 160**Returns** 161 162| Type| Description| 163| -- | -- | 164| FFRT_C_API int | Returns **ffrt_success** if the read lock is successfully obtained;<br> returns **ffrt_error_inval** or **ffrt_error_busy** otherwise.| 165 166### ffrt_rwlock_unlock() 167 168``` 169FFRT_C_API int ffrt_rwlock_unlock(ffrt_rwlock_t* rwlock) 170``` 171 172**Description** 173 174Releases the read-write lock. 175 176**Since**: 18 177 178 179**Parameters** 180 181| Name| Description| 182| -- | -- | 183| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 184 185**Returns** 186 187| Type| Description| 188| -- | -- | 189| FFRT_C_API int | Returns **ffrt_success** if the read-write lock is successfully released;<br> returns **ffrt_error_inval** otherwise.| 190 191### ffrt_rwlock_destroy() 192 193``` 194FFRT_C_API int ffrt_rwlock_destroy(ffrt_rwlock_t* rwlock) 195``` 196 197**Description** 198 199Destroys the read-write lock. 200 201**Since**: 18 202 203 204**Parameters** 205 206| Name| Description| 207| -- | -- | 208| [ffrt_rwlock_t](capi-ffrt-ffrt-rwlock-t.md)* rwlock | Pointer to the read-write lock.| 209 210**Returns** 211 212| Type| Description| 213| -- | -- | 214| FFRT_C_API int | Returns **ffrt_success** if the read-write lock is destroyed successfully;<br>returns **ffrt_error_inval** otherwise.| 215