1e41f4b71Sopenharmony_ci# @arkts.utils (ArkTS Utils) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe Utils module provides a variety of ArkTS utility functions. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { ArkTSUtils } from '@kit.ArkTS' 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## ArkTSUtils.locks 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciTo avoid data races between concurrent instances, the ArkTS common library introduces **AsyncLock**. Passing **AsyncLock** objects by reference across concurrent instances is supported. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciArkTS supports asynchronous operations, and blocking locks are prone to deadlocks. Therefore, only asynchronous locks (non-blocking locks) are used in ArkTS. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciThe method that uses an asynchronous lock must be marked as **async**, and the caller must use **await** in the call to ensure the correct call sequence. As a result, all outer functions are marked as **async**. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci### AsyncLockCallback 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_citype AsyncLockCallback\<T> = () => T | Promise\<T> 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciA supplementary type alias that represents the callback in all the overloads of [lockAsync](#lockasync). 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci### AsyncLock 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciA class that implements an asynchronous lock and allows asynchronous operations to be performed under a lock. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci#### Attributes 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci| Name| Type | Readable| Writable| Description | 44e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | ---------- | 45e41f4b71Sopenharmony_ci| name | string | Yes | No | Name of the lock.| 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Example** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```ts 50e41f4b71Sopenharmony_ci// Example 1 51e41f4b71Sopenharmony_ci@Sendable 52e41f4b71Sopenharmony_ciclass A { 53e41f4b71Sopenharmony_ci count_: number = 0; 54e41f4b71Sopenharmony_ci async getCount(): Promise<number> { 55e41f4b71Sopenharmony_ci let lock: ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request("lock_1"); 56e41f4b71Sopenharmony_ci return lock.lockAsync(() => { 57e41f4b71Sopenharmony_ci return this.count_; 58e41f4b71Sopenharmony_ci }) 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci async setCount(count: number) { 61e41f4b71Sopenharmony_ci let lock: ArkTSUtils.locks.AsyncLock = ArkTSUtils.locks.AsyncLock.request("lock_1"); 62e41f4b71Sopenharmony_ci await lock.lockAsync(() => { 63e41f4b71Sopenharmony_ci this.count_ = count; 64e41f4b71Sopenharmony_ci }) 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci// Example 2 69e41f4b71Sopenharmony_ci@Sendable 70e41f4b71Sopenharmony_ciclass A { 71e41f4b71Sopenharmony_ci count_: number = 0; 72e41f4b71Sopenharmony_ci lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock(); 73e41f4b71Sopenharmony_ci async getCount(): Promise<number> { 74e41f4b71Sopenharmony_ci return this.lock_.lockAsync(() => { 75e41f4b71Sopenharmony_ci return this.count_; 76e41f4b71Sopenharmony_ci }) 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci async setCount(count: number) { 79e41f4b71Sopenharmony_ci await this.lock_.lockAsync(() => { 80e41f4b71Sopenharmony_ci this.count_ = count; 81e41f4b71Sopenharmony_ci }) 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci} 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci@Concurrent 86e41f4b71Sopenharmony_ciasync function foo(a: A) { 87e41f4b71Sopenharmony_ci await a.setCount(10) 88e41f4b71Sopenharmony_ci} 89e41f4b71Sopenharmony_ci``` 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci#### constructor 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ciconstructor() 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ciDefault constructor used to create an asynchronous lock. 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**Return value** 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci| Type | Description | 104e41f4b71Sopenharmony_ci| ----------------------- | ------------------ | 105e41f4b71Sopenharmony_ci| [AsyncLock](#asynclock) | **AsyncLock** instance created.| 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**Example** 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci```ts 110e41f4b71Sopenharmony_cilet lock = new ArkTSUtils.locks.AsyncLock(); 111e41f4b71Sopenharmony_ci``` 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci#### request 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_cistatic request(name: string): AsyncLock 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciFinds or creates (if not found) an **AsyncLock** instance with the specified name. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Parameters** 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 126e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------------------------- | 127e41f4b71Sopenharmony_ci| name | string | Yes | Name of the lock.| 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**Return value** 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| Type | Description | 132e41f4b71Sopenharmony_ci| ----------------------- | -------------------------------- | 133e41f4b71Sopenharmony_ci| [AsyncLock](#asynclock) | **AsyncLock** instance found or created.| 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci**Example** 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci```ts 138e41f4b71Sopenharmony_cilet lockName = 'isAvailableLock'; 139e41f4b71Sopenharmony_cilet lock = ArkTSUtils.locks.AsyncLock.request(lockName); 140e41f4b71Sopenharmony_ci``` 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci#### query 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_cistatic query(name: string): AsyncLockState 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciQueries information about an asynchronous lock. 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**Parameters** 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 155e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------- | 156e41f4b71Sopenharmony_ci| name | string | Yes | Name of the lock.| 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**Return value** 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci| Type | Description | 161e41f4b71Sopenharmony_ci| --------------------------------- | ---------------------------------- | 162e41f4b71Sopenharmony_ci| [AsyncLockState](#asynclockstate) | **AsyncLockState** instance that contains the lock state information.| 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci**Error codes** 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci| ID| Error Message | 169e41f4b71Sopenharmony_ci| -------- | ------------- | 170e41f4b71Sopenharmony_ci| 10200030 | The lock does not exist. | 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**Example** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci```ts 175e41f4b71Sopenharmony_ci// You have created a lock somewhere else. 176e41f4b71Sopenharmony_ci// let lock = ArkTSUtils.locks.AsyncLock.request("queryTestLock"); 177e41f4b71Sopenharmony_cilet state = ArkTSUtils.locks.AsyncLock.query('queryTestLock'); 178e41f4b71Sopenharmony_ciif (!state) { 179e41f4b71Sopenharmony_ci throw new Error('Test failed: A valid state is expected, but the obtained state is '+ state); 180e41f4b71Sopenharmony_ci} 181e41f4b71Sopenharmony_cilet pending: ArkTSUtils.locks.AsyncLockInfo[] = state.pending; 182e41f4b71Sopenharmony_cilet held: ArkTSUtils.locks.AsyncLockInfo[] = state.held; 183e41f4b71Sopenharmony_ci``` 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci#### queryAll 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_cistatic queryAll(): AsyncLockState[] 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciQueries information about all existing asynchronous locks. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**Return value** 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci| Type | Description | 198e41f4b71Sopenharmony_ci| ----------------------------------- | -------------------------------- | 199e41f4b71Sopenharmony_ci| [AsyncLockState](#asynclockstate)[] | **AsyncLockState** array that contains the lock state information.| 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Example** 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci```ts 204e41f4b71Sopenharmony_cilet states: ArkTSUtils.locks.AsyncLockState[] = ArkTSUtils.locks.AsyncLock.queryAll(); 205e41f4b71Sopenharmony_ciif (states.length == 0) { 206e41f4b71Sopenharmony_ci throw new Error ('Test failed: At least one state is expected, but what got is ' + states.length); 207e41f4b71Sopenharmony_ci} 208e41f4b71Sopenharmony_ci``` 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci#### lockAsync 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_cilockAsync\<T>(callback: AsyncLockCallback\<T>): Promise\<T> 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ciPerforms an operation exclusively under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci**Parameters** 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 223e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ---------------------- | 224e41f4b71Sopenharmony_ci| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci**Return value** 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci| Type | Description | 229e41f4b71Sopenharmony_ci| ----------- | --------------------------- | 230e41f4b71Sopenharmony_ci| Promise\<T> | Promise that will be resolved after the callback is executed.| 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**Error codes** 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci| ID| Error Message | 237e41f4b71Sopenharmony_ci| -------- | ------------- | 238e41f4b71Sopenharmony_ci| 10200030 | The lock does not exist. | 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci**Example** 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci```ts 243e41f4b71Sopenharmony_cilet lock = new ArkTSUtils.locks.AsyncLock(); 244e41f4b71Sopenharmony_cilet p1 = lock.lockAsync<void>(() => { 245e41f4b71Sopenharmony_ci // Perform an operation. 246e41f4b71Sopenharmony_ci}); 247e41f4b71Sopenharmony_ci``` 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci#### lockAsync 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_cilockAsync\<T>(callback: AsyncLockCallback\<T>, mode: AsyncLockMode): Promise\<T> 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ciPerforms an operation under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**Parameters** 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 262e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ---------------------- | 263e41f4b71Sopenharmony_ci| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 264e41f4b71Sopenharmony_ci| mode | [AsyncLockMode](#asynclockmode) | Yes | Mode of the lock. | 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**Return value** 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci| Type | Description | 269e41f4b71Sopenharmony_ci| ----------- | --------------------------- | 270e41f4b71Sopenharmony_ci| Promise\<T> | Promise that will be resolved after the callback is executed.| 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci**Error codes** 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci| ID| Error Message | 277e41f4b71Sopenharmony_ci| -------- | ------------- | 278e41f4b71Sopenharmony_ci| 10200030 | The lock does not exist. | 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci**Example** 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci```ts 283e41f4b71Sopenharmony_cilet lock = new ArkTSUtils.locks.AsyncLock(); 284e41f4b71Sopenharmony_cilet p1 = lock.lockAsync<void>(() => { 285e41f4b71Sopenharmony_ci // Perform an operation. 286e41f4b71Sopenharmony_ci}, ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE); 287e41f4b71Sopenharmony_ci``` 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci#### lockAsync 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_cilockAsync\<T, U>(callback: AsyncLockCallback\<T>, mode: AsyncLockMode, options: AsyncLockOptions\<U>): Promise\<T | U> 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ciPerforms an operation under a lock. This API acquires the lock, executes the callback, and releases the lock. The callback is executed asynchronously in the same thread where [lockAsync](#lockasync) was called. An optional timeout value can be provided in [AsyncLockOptions](#asynclockoptions). If a lock is not acquired before timeout, **lockAsync** returns a projected Promise with a **BusinessError** instance. In this instance, the error message contains information about the locks being held and in the waiting state, as well as possible deadlock warnings. 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**Parameters** 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 302e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ---------------------- | 303e41f4b71Sopenharmony_ci| callback | [AsyncLockCallback](#asynclockcallback) | Yes | Callback to be executed after a lock is acquired.| 304e41f4b71Sopenharmony_ci| mode | [AsyncLockMode](#asynclockmode) | Yes | Mode of the lock. | 305e41f4b71Sopenharmony_ci| options | [AsyncLockOptions\<U>](#asynclockoptions) | Yes | Options of the lock. | 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci**Return value** 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci| Type | Description | 310e41f4b71Sopenharmony_ci| ---------------- | -------------------------------------------------- | 311e41f4b71Sopenharmony_ci| Promise\<T \| U> | Promise that will be resolved after the callback is executed, or rejected in the case of timeout.| 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci**Error codes** 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci| ID| Error Message | 318e41f4b71Sopenharmony_ci| -------- | ----------------- | 319e41f4b71Sopenharmony_ci| 10200030 | The lock does not exist. | 320e41f4b71Sopenharmony_ci| 10200031 | Timeout exceeded. | 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci**Example** 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci```ts 325e41f4b71Sopenharmony_cilet lock = new ArkTSUtils.locks.AsyncLock(); 326e41f4b71Sopenharmony_cilet options = new ArkTSUtils.locks.AsyncLockOptions<void>(); 327e41f4b71Sopenharmony_cioptions.timeout = 1000; 328e41f4b71Sopenharmony_cilet p: Promise<void> = lock.lockAsync<void, void>( 329e41f4b71Sopenharmony_ci () => { 330e41f4b71Sopenharmony_ci // Perform an operation. 331e41f4b71Sopenharmony_ci }, 332e41f4b71Sopenharmony_ci ArkTSUtils.locks.AsyncLockMode.EXCLUSIVE, 333e41f4b71Sopenharmony_ci options 334e41f4b71Sopenharmony_ci); 335e41f4b71Sopenharmony_ci``` 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci### AsyncLockMode 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ciEnumerates the modes of an asynchronous lock. 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci| Name | Value | Description | 346e41f4b71Sopenharmony_ci| --------- | --- | -------------------------------------------------------- | 347e41f4b71Sopenharmony_ci| SHARED | 1 | Shared lock. An operation can be reentrant in the same thread.| 348e41f4b71Sopenharmony_ci| EXCLUSIVE | 2 | Exclusive lock. An operation is performed only when the lock is exclusively acquired.| 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci### AsyncLockOptions 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ciclass AsyncLockOptions\<T> 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ciClass that implements the asynchronous lock options. 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci#### constructor 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ciconstructor() 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ciDefault constructor used to create an **AsyncLockOptions** instance with the default values for all attributes. 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci**Return value** 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci| Type | Description | 373e41f4b71Sopenharmony_ci| ------------------------------------- | ---------------------- | 374e41f4b71Sopenharmony_ci| [AsyncLockOptions](#asynclockoptions) | **AsyncLockOptions** instance created.| 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**Example** 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci```ts 379e41f4b71Sopenharmony_cilet s: ArkTSUtils.locks.AbortSignal<string> = { aborted: false, reason: 'Aborted' }; 380e41f4b71Sopenharmony_cilet options = new ArkTSUtils.locks.AsyncLockOptions<string>(); 381e41f4b71Sopenharmony_cioptions.isAvailable = false; 382e41f4b71Sopenharmony_cioptions.signal = s; 383e41f4b71Sopenharmony_ci``` 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci#### Attributes 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 388e41f4b71Sopenharmony_ci| ----------- | ------------------------------------- | ---- | ---- | ------------------------------------------------------------------------------------------------------------------------- | 389e41f4b71Sopenharmony_ci| isAvailable | boolean | Yes | Yes | Whether the lock is available. If the value is **true**, a lock is granted only when it is not held. If the value is **false**, a lock is granted once it is released. The default value is **false**.| 390e41f4b71Sopenharmony_ci| signal | [AbortSignal\<T>](#abortsignal)\|null | Yes | Yes | Signal used to abort an asynchronous operation. If **signal.aborted** is **true**, the lock request is discarded. If **signal.aborted** is **null**, the request is queued normally. The default value is **null**. | 391e41f4b71Sopenharmony_ci| timeout | number | Yes | Yes | Timeout interval of the lock request, in milliseconds. If the value is greater than zero and a lock is not acquired before time, [lockAsync](#lockasync) returns a rejected Promise. The default value is **0**. | 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci### AsyncLockState 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ciA class used to store information about all lock operations currently performed on an **AsyncLock** instance. 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci#### Attributes 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 404e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ---- | ---------------- | 405e41f4b71Sopenharmony_ci| held | [AsyncLockInfo[]](#asynclockinfo) | Yes | Yes | Information about the lock being held. | 406e41f4b71Sopenharmony_ci| pending | [AsyncLockInfo[]](#asynclockinfo) | Yes | Yes | Information about the lock in the waiting state.| 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci### AsyncLockInfo 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciDescribes the information about a lock. 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci#### Attributes 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 419e41f4b71Sopenharmony_ci| --------- | ------------------------------- | ---- | ---- | --------------------------------------------------------- | 420e41f4b71Sopenharmony_ci| name | string | Yes | Yes | Name of the lock. | 421e41f4b71Sopenharmony_ci| mode | [AsyncLockMode](#asynclockmode) | Yes | Yes | Mode of the lock. | 422e41f4b71Sopenharmony_ci| contextId | number | Yes | Yes | Context identifier of the caller of [AsyncLockMode](#asynclockmode).| 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci### AbortSignal 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ciA class that implements a signal used to abort an asynchronous operation. An instance of this class must be accessed in the same thread it creates. Otherwise, undefined behavior occurs. 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci#### Attributes 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 435e41f4b71Sopenharmony_ci| ------- | ------- | ---- | ---- | ---------------------------------------------------------------- | 436e41f4b71Sopenharmony_ci| aborted | boolean | Yes | Yes | Whether to abort the operation. The value **true** means to abort the operation, and **false** means the opposite. | 437e41f4b71Sopenharmony_ci| reason | \<T> | Yes | Yes | Reason for abort. This value will be used in the rejected Promise returned by [lockAsync](#lockasync).| 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci## ArkTSUtils.ASON 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ciA utility class used to parse JSON strings into sendable data. ASON allows you to parse JSON strings and generate data that can be passed across concurrency domains. It also supports conversion from sendable data into JSON strings. 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci### ISendable 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_citype ISendable = lang.ISendable 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci**ISendable** is the parent type of all sendable types except null and undefined. It does not have any necessary methods or properties. 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci| Type| Description | 454e41f4b71Sopenharmony_ci| ------ | ------ | 455e41f4b71Sopenharmony_ci| [lang.ISendable](js-apis-arkts-lang.md#langisendable) | Parent type of all sendable types.| 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci### Transformer 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_citype Transformer = (this: ISendable, key: string, value: ISendable | undefined | null) => ISendable | undefined | null 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ciDefines the type of the conversion result function. 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**Parameters** 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 470e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- | 471e41f4b71Sopenharmony_ci| this | [ISendable](#isendable) | Yes| Object to which the key-value pair to parse belongs.| 472e41f4b71Sopenharmony_ci| key | string | Yes| Key to parse.| 473e41f4b71Sopenharmony_ci| value | [ISendable](#isendable) | Yes| Value of the key.| 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci**Return value** 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci| Type| Description| 478e41f4b71Sopenharmony_ci| -------- | -------- | 479e41f4b71Sopenharmony_ci| [ISendable](#isendable) \| undefined \| null | **ISendable** object, undefined, or null.| 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci### BigIntMode 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ciEnumerates the modes for processing BigInt. 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci| Name| Value| Description | 490e41f4b71Sopenharmony_ci| ------ | ------ | --------------- | 491e41f4b71Sopenharmony_ci| DEFAULT | 0 |BigInt is not supported.| 492e41f4b71Sopenharmony_ci| PARSE_AS_BIGINT | 1 |Parses an integer that is less than -(2^53-1) or greater than (2^53-1) as BigInt.| 493e41f4b71Sopenharmony_ci| ALWAYS_PARSE_AS_BIGINT | 2 |Parses all integers as BigInt.| 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci### ParseReturnType 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ciEnumerates the return types of the parsing result. 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci| Name| Value| Description | 502e41f4b71Sopenharmony_ci| ------ | ------ | --------------- | 503e41f4b71Sopenharmony_ci| OBJECT | 0 |Returns a **SendableObject** object.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 504e41f4b71Sopenharmony_ci| MAP<sup>13+</sup> | 1 |Returns a **SendableMap** object.<br>**Atomic service API**: This API can be used in atomic services since API version 13.| 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci### ParseOptions 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ciDescribes the parsing options, which defines the BigInt processing mode and the return type of the parsing result. 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 515e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- | 516e41f4b71Sopenharmony_ci| bigIntMode | [BigIntMode](#bigintmode) | Yes|Mode for processing BigInt.| 517e41f4b71Sopenharmony_ci| parseReturnType | [ParseReturnType](#parsereturntype) | Yes|Return type of the parsing result.| 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci### parse 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ciparse(text: string, reviver?: Transformer, options?: ParseOptions): ISendable | null 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ciParses a JSON string to generate ISendable data or null. 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci**Parameters** 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 532e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------- | 533e41f4b71Sopenharmony_ci| text | string | Yes| Valid JSON string.| 534e41f4b71Sopenharmony_ci| reviver | [Transformer](#transformer) | No| Conversion function. This parameter can be used to modify the value generated after parsing. The default value is undefined. Currently, only undefined can be passed in.| 535e41f4b71Sopenharmony_ci| options | [ParseOptions](#parseoptions) | No| Parsing options. This parameter is used to control the type of the parsing result. The default value is undefined.| 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci**Return value** 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci| Type| Description| 540e41f4b71Sopenharmony_ci| -------- | -------- | 541e41f4b71Sopenharmony_ci| [ISendable](#isendable) \| null | ISendable data or **null** (if **null** is passed in).| 542e41f4b71Sopenharmony_ci 543e41f4b71Sopenharmony_ci**Example** 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci```ts 546e41f4b71Sopenharmony_ciimport { lang } from '@kit.ArkTS'; 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_citype ISendable = lang.ISendable; 549e41f4b71Sopenharmony_cilet jsonText = '{"name": "John", "age": 30, "city": "ChongQing"}'; 550e41f4b71Sopenharmony_cilet obj = ArkTSUtils.ASON.parse(jsonText) as ISendable; 551e41f4b71Sopenharmony_ciconsole.info((obj as object)?.["name"]); 552e41f4b71Sopenharmony_ci// Expected output: 'John' 553e41f4b71Sopenharmony_ciconsole.info((obj as object)?.["age"]); 554e41f4b71Sopenharmony_ci// Expected output: 30 555e41f4b71Sopenharmony_ciconsole.info((obj as object)?.["city"]); 556e41f4b71Sopenharmony_ci// Expected output: 'ChongQing' 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_cilet options: ArkTSUtils.ASON.ParseOptions = { 559e41f4b71Sopenharmony_ci bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT, 560e41f4b71Sopenharmony_ci parseReturnType: ArkTSUtils.ASON.ParseReturnType.OBJECT, 561e41f4b71Sopenharmony_ci} 562e41f4b71Sopenharmony_cilet numberText = '{"largeNumber":112233445566778899}'; 563e41f4b71Sopenharmony_cilet numberObj = ArkTSUtils.ASON.parse(numberText,undefined,options) as ISendable; 564e41f4b71Sopenharmony_ci 565e41f4b71Sopenharmony_ciconsole.info((numberObj as object)?.["largeNumber"]); 566e41f4b71Sopenharmony_ci// Expected output: 112233445566778899 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_cilet options2: ArkTSUtils.ASON.ParseOptions = { 569e41f4b71Sopenharmony_ci bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT, 570e41f4b71Sopenharmony_ci parseReturnType: ArkTSUtils.ASON.ParseReturnType.MAP, 571e41f4b71Sopenharmony_ci } 572e41f4b71Sopenharmony_cilet mapText = '{"largeNumber":112233445566778899}'; 573e41f4b71Sopenharmony_cilet map = ArkTSUtils.ASON.parse(mapText,undefined,options2); 574e41f4b71Sopenharmony_ciconsole.info("map is " + map); 575e41f4b71Sopenharmony_ci// Expected output: map is [object SendableMap] 576e41f4b71Sopenharmony_ciconsole.info("largeNumber is " + (map as collections.Map<string,bigint>).get("largeNumber")); 577e41f4b71Sopenharmony_ci// Expected output: largeNumber is 112233445566778899 578e41f4b71Sopenharmony_ci``` 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci### stringify 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_cistringify(value: ISendable | null | undefined): string 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ciConverts ISendable data into a JSON string. 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci**Parameters** 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 593e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 594e41f4b71Sopenharmony_ci| value | [ISendable](#isendable) \| null \| undefined | Yes| ISendable data.| 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci**Return value** 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ci| Type| Description| 599e41f4b71Sopenharmony_ci| -------- | -------- | 600e41f4b71Sopenharmony_ci| string | JSON string.| 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci**Example** 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci```ts 605e41f4b71Sopenharmony_ciimport { collections } from '@kit.ArkTS'; 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_cilet arr = new collections.Array(1, 2, 3); 608e41f4b71Sopenharmony_cilet str = ArkTSUtils.ASON.stringify(arr); 609e41f4b71Sopenharmony_ciconsole.info(str); 610e41f4b71Sopenharmony_ci// Expected output: '[1,2,3]' 611e41f4b71Sopenharmony_ci``` 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci### isSendable 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ciisSendable(value: Object | null | undefined): boolean 616e41f4b71Sopenharmony_ci 617e41f4b71Sopenharmony_ciChecks whether the passed-in value is of the sendable data type. 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**Parameters** 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 626e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 627e41f4b71Sopenharmony_ci| value | Object \| null \| undefined | Yes| Object to check.| 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci**Return value** 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci| Type| Description| 632e41f4b71Sopenharmony_ci| -------- | -------- | 633e41f4b71Sopenharmony_ci| boolean | Check result. The value **true** means that the passed-in value is of the sendable data type, and **false** means the opposite.| 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci**Example** 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci```ts 638e41f4b71Sopenharmony_ciimport { ArkTSUtils } from '@kit.ArkTS' 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci@Sendable 641e41f4b71Sopenharmony_cifunction sendableFunc() 642e41f4b71Sopenharmony_ci{ 643e41f4b71Sopenharmony_ci console.info("sendableFunc") 644e41f4b71Sopenharmony_ci} 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ciif (ArkTSUtils.isSendable(sendableFunc)) { 647e41f4b71Sopenharmony_ci console.info("sendableFunc is Sendable"); 648e41f4b71Sopenharmony_ci} else { 649e41f4b71Sopenharmony_ci console.info("sendableFunc is not Sendable"); 650e41f4b71Sopenharmony_ci} 651e41f4b71Sopenharmony_ci// Expected output: 'SendableFunc is Sendable' 652e41f4b71Sopenharmony_ci``` 653