更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Rwlock
Rwlock 的协作图:

结构体

struct  OsRwlock
 

类型定义

typedef struct OsRwlock LosRwlock
 

函数

UINT32 LOS_RwlockInit (LosRwlock *rwlock)
 Init a rwlock. 更多...
 
UINT32 LOS_RwlockDestroy (LosRwlock *rwlock)
 Destroy a rwlock. 更多...
 
UINT32 LOS_RwlockRdLock (LosRwlock *rwlock, UINT32 timeout)
 Wait to lock a read lock. 更多...
 
UINT32 LOS_RwlockTryRdLock (LosRwlock *rwlock)
 Try wait to lock a read lock. 更多...
 
UINT32 LOS_RwlockWrLock (LosRwlock *rwlock, UINT32 timeout)
 Wait to lock a write lock. 更多...
 
UINT32 LOS_RwlockTryWrLock (LosRwlock *rwlock)
 Try wait to lock a write lock. 更多...
 
UINT32 LOS_RwlockUnLock (LosRwlock *rwlock)
 Release a rwlock. 更多...
 

详细描述

类型定义说明

◆ LosRwlock

typedef struct OsRwlock LosRwlock

Rwlock object.

函数说明

◆ LOS_RwlockDestroy()

UINT32 LOS_RwlockDestroy ( LosRwlock rwlock)

Destroy a rwlock.

Description:
This API is used to delete a specified rwlock. Return LOS_OK on deleting successfully, return specific error code otherwise.
注意
  • The specific rwlock should be created firstly.
  • The rwlock can be deleted successfully only if no other tasks pend on it.
参数
rwlock[IN] Handle of the rwlock to be deleted.
返回值
#LOS_EINVALThe rwlock pointer is NULL.
#LOS_EBUSYTasks pended on this rwlock.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_OKThe rwlock is successfully deleted.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_RwlockInit

Destroy a rwlock.

在文件 los_rwlock.c102 行定义.

103{
104 UINT32 intSave;
105
106 if (rwlock == NULL) {
107 return LOS_EINVAL;
108 }
109
110 SCHEDULER_LOCK(intSave);
111 if ((rwlock->magic & RWLOCK_COUNT_MASK) != OS_RWLOCK_MAGIC) {
112 SCHEDULER_UNLOCK(intSave);
113 return LOS_EBADF;
114 }
115
116 if (rwlock->rwCount != 0) {
117 SCHEDULER_UNLOCK(intSave);
118 return LOS_EBUSY;
119 }
120
121 (VOID)memset_s(rwlock, sizeof(LosRwlock), 0, sizeof(LosRwlock));
122 SCHEDULER_UNLOCK(intSave);
123 return LOS_OK;
124}
struct OsRwlock LosRwlock
unsigned int UINT32
Definition: los_typedef.h:57
INT32 magic
Definition: los_rwlock.h:53
INT32 rwCount
Definition: los_rwlock.h:54

◆ LOS_RwlockInit()

UINT32 LOS_RwlockInit ( LosRwlock rwlock)

Init a rwlock.

Description:
This API is used to Init a rwlock. A rwlock handle is assigned to rwlockHandle when the rwlock is init successfully. Return LOS_OK on creating successful, return specific error code otherwise.
参数
rwlock[IN] Handle pointer of the successfully init rwlock.
返回值
#LOS_EINVALThe rwlock pointer is NULL.
#LOS_EPERMMultiply initialization.
#LOS_OKThe rwlock is successfully created.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_RwlockDestroy

Init a rwlock.

在文件 los_rwlock.c79 行定义.

80{
81 UINT32 intSave;
82
83 if (rwlock == NULL) {
84 return LOS_EINVAL;
85 }
86
87 SCHEDULER_LOCK(intSave);
88 if ((rwlock->magic & RWLOCK_COUNT_MASK) == OS_RWLOCK_MAGIC) {
89 SCHEDULER_UNLOCK(intSave);
90 return LOS_EPERM;
91 }
92
93 rwlock->rwCount = 0;
94 rwlock->writeOwner = NULL;
95 LOS_ListInit(&(rwlock->readList));
96 LOS_ListInit(&(rwlock->writeList));
97 rwlock->magic = OS_RWLOCK_MAGIC;
98 SCHEDULER_UNLOCK(intSave);
99 return LOS_OK;
100}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LOS_DL_LIST readList
Definition: los_rwlock.h:59
VOID * writeOwner
Definition: los_rwlock.h:58
LOS_DL_LIST writeList
Definition: los_rwlock.h:60
函数调用图:

◆ LOS_RwlockRdLock()

UINT32 LOS_RwlockRdLock ( LosRwlock rwlock,
UINT32  timeout 
)

Wait to lock a read lock.

Description:
This API is used to wait for a specified period of time to lock a read lock.
注意
  • The specific rwlock should be created firstly.
  • The function fails if the rwlock that is waited on is already locked by another thread when the task scheduling is disabled.
  • Do not wait on a rwlock during an interrupt.
  • The function fails when other tasks have the write lock or there are some task pending on the write list with the higher priority.
  • A recursive rwlock can be locked more than once by the same thread.
  • Do not call this API in software timer callback.
参数
rwlock[IN] Handle of the rwlock to be waited on.
timeout[IN] Waiting time. The value range is [0, LOS_WAIT_FOREVER](unit: Tick).
返回值
#LOS_EINVALThe rwlock pointer is NULL, The timeout is zero or Lock status error.
#LOS_EINTRThe rwlock is being locked during an interrupt.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_EDEADLKRwlock error check failed or System locked task scheduling.
#LOS_ETIMEDOUTThe rwlock waiting times out.
#LOS_EPERMThe rwlock is used in system tasks.
#LOS_OKThe rwlock is successfully locked.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_RwlockInit | LOS_RwlockUnLock

Wait to lock a read lock.

在文件 los_rwlock.c321 行定义.

322{
323 UINT32 intSave;
324
325 UINT32 ret = OsRwlockCheck(rwlock);
326 if (ret != LOS_OK) {
327 return ret;
328 }
329
330 SCHEDULER_LOCK(intSave);
331 ret = OsRwlockRdUnsafe(rwlock, timeout);
332 SCHEDULER_UNLOCK(intSave);
333 return ret;
334}
UINT32 OsRwlockRdUnsafe(LosRwlock *rwlock, UINT32 timeout)
Definition: los_rwlock.c:252
STATIC UINT32 OsRwlockCheck(LosRwlock *rwlock)
读写锁检查
Definition: los_rwlock.c:126
函数调用图:

◆ LOS_RwlockTryRdLock()

UINT32 LOS_RwlockTryRdLock ( LosRwlock rwlock)

Try wait to lock a read lock.

Description:
This API is used to wait for a specified period of time to lock a read lock.
注意
  • The specific rwlock should be created firstly.
  • The function fails if the rwlock that is waited on is already locked by another thread when the task scheduling is disabled.
  • Do not wait on a rwlock during an interrupt.
  • The function fails when other tasks have the write lock or there are some task pending on the write list with the higher priority.
  • A recursive rwlock can be locked more than once by the same thread.
  • Do not call this API in software timer callback.
参数
rwlock[IN] Handle of the rwlock to be waited on.
返回值
#LOS_EINVALThe rwlock pointer is NULL or Lock status error.
#LOS_EINTRThe rwlock is being locked during an interrupt.
#LOS_EBUSYFail to get the rwlock, the rwlock has been used.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_EDEADLKrwlock error check failed or System locked task scheduling.
#LOS_ETIMEDOUTThe rwlock waiting times out.
#LOS_EPERMThe rwlock is used in system tasks.
#LOS_OKThe rwlock is successfully locked.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_RwlockInit | LOS_RwlockUnLock

Try wait to lock a read lock.

在文件 los_rwlock.c336 行定义.

337{
338 UINT32 intSave;
339
340 UINT32 ret = OsRwlockCheck(rwlock);
341 if (ret != LOS_OK) {
342 return ret;
343 }
344
345 SCHEDULER_LOCK(intSave);
346 ret = OsRwlockTryRdUnsafe(rwlock, 0);//所谓尝试就是没锁爷就返回,不等待,不纠结.当前任务也不会被挂起
347 SCHEDULER_UNLOCK(intSave);
348 return ret;
349}
UINT32 OsRwlockTryRdUnsafe(LosRwlock *rwlock, UINT32 timeout)
Definition: los_rwlock.c:261
函数调用图:

◆ LOS_RwlockTryWrLock()

UINT32 LOS_RwlockTryWrLock ( LosRwlock rwlock)

Try wait to lock a write lock.

Description:
This API is used to wait for a specified period of time to lock a write lock.
注意
  • The specific rwlock should be created firstly.
  • The function fails if the rwlock that is waited on is already locked by another thread when the task scheduling. is disabled.
  • Do not wait on a rwlock during an interrupt.
  • The funtion fails when other tasks have the read or write lock.
  • A recursive rwlock can be locked more than once by the same thread.
  • Do not call this API in software timer callback.
参数
rwlock[IN] Handle of the rwlock to be waited on.
返回值
#LOS_EINVALThe rwlock pointer is NULL or Lock status error.
#LOS_EINTRThe rwlock is being locked during an interrupt.
#LOS_EBUSYFail to get the rwlock, the rwlock has been used.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_EDEADLKrwlock error check failed or System locked task scheduling.
#LOS_ETIMEDOUTThe rwlock waiting times out.
#LOS_EPERMThe rwlock is used in system tasks.
#LOS_OKThe rwlock is successfully locked.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_RwlockInit | LOS_RwlockUnLock

Try wait to lock a write lock.

在文件 los_rwlock.c366 行定义.

367{
368 UINT32 intSave;
369
370 UINT32 ret = OsRwlockCheck(rwlock);
371 if (ret != LOS_OK) {
372 return ret;
373 }
374
375 SCHEDULER_LOCK(intSave);
376 ret = OsRwlockTryWrUnsafe(rwlock, 0);//所谓尝试就是没锁爷就返回,不等待,不纠结.当前任务也不会被挂起
377 SCHEDULER_UNLOCK(intSave);
378 return ret;
379}
UINT32 OsRwlockTryWrUnsafe(LosRwlock *rwlock, UINT32 timeout)
Definition: los_rwlock.c:299
函数调用图:

◆ LOS_RwlockUnLock()

UINT32 LOS_RwlockUnLock ( LosRwlock rwlock)

Release a rwlock.

Description:
This API is used to release a specified rwlock.
注意
  • The specific rwlock should be created firstly.
  • Do not release a rwlock during an interrupt.
  • When the write list is null and the read list is not null, all the task pending on the read list will be waken.
  • When the write list is not null and the read list is null, the task pending on the read list will be waken by the priority.
  • When the write list and the read list are not null, all the task pending on the both list will be waken by the priority.
  • If the task on the write list has the same priority as the task on the read list, the forth will be waken.
  • If a recursive rwlock is locked for many times, it must be unlocked for the same times to be released.
参数
rwlock[IN] Handle of the rwlock to be released.
返回值
#LOS_EINVALThe rwlock pointer is NULL, The timeout is zero or Lock status error.
#LOS_EINTRThe rwlock is being locked during an interrupt.
#LOS_EPERMThe rwlock is not locked or has been used.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_EDEADLKrwlock error check failed or System locked task scheduling.
#LOS_ETIMEDOUTThe rwlock waiting times out.
#LOS_EPERMThe rwlock is used in system tasks.
#LOS_OKThe rwlock is successfully locked.
Dependency:
  • los_mux.h: the header file that contains the API declaration.
参见
LOS_RwlockInit | LOS_ReadLock | LOS_WriteLock

Release a rwlock.

在文件 los_rwlock.c482 行定义.

483{
484 UINT32 intSave;
485 BOOL needSched = FALSE;
486
487 UINT32 ret = OsRwlockCheck(rwlock);
488 if (ret != LOS_OK) {
489 return ret;
490 }
491
492 SCHEDULER_LOCK(intSave);
493 ret = OsRwlockUnlockUnsafe(rwlock, &needSched);
494 SCHEDULER_UNLOCK(intSave);
495 LOS_MpSchedule(OS_MP_CPU_ALL);//设置调度CPU的方式,所有CPU参与调度
496 if (needSched == TRUE) {//是否需要调度
497 LOS_Schedule();//产生调度,切换任务执行
498 }
499 return ret;
500}
VOID LOS_Schedule(VOID)
Trigger active task scheduling.
Definition: los_sched.c:469
VOID LOS_MpSchedule(UINT32 target)
Definition: los_mp.c:76
UINT32 OsRwlockUnlockUnsafe(LosRwlock *rwlock, BOOL *needSched)
释放锁,唤醒任务
Definition: los_rwlock.c:449
size_t BOOL
Definition: los_typedef.h:88
函数调用图:

◆ LOS_RwlockWrLock()

UINT32 LOS_RwlockWrLock ( LosRwlock rwlock,
UINT32  timeout 
)

Wait to lock a write lock.

Description:
This API is used to wait for a specified period of time to lock a write lock.
注意
  • The specific rwlock should be created firstly.
  • The function fails if the rwlock that is waited on is already locked by another thread when the task scheduling. is disabled.
  • Do not wait on a rwlock during an interrupt.
  • The funtion fails when other tasks have the read or write lock.
  • A recursive rwlock can be locked more than once by the same thread.
  • Do not call this API in software timer callback.
参数
rwlock[IN] Handle of the rwlock to be waited on.
timeout[IN] Waiting time. The value range is [0, LOS_WAIT_FOREVER](unit: Tick).
返回值
#LOS_EINVALThe rwlock pointer is NULL, The timeout is zero or Lock status error.
#LOS_EINTRThe rwlock is being locked during an interrupt.
#LOS_EBADFThe lock has been destroyed or broken.
#LOS_EDEADLKRwlock error check failed or System locked task scheduling.
#LOS_ETIMEDOUTThe rwlock waiting times out.
#LOS_EPERMThe rwlock is used in system tasks.
#LOS_OKThe rwlock is successfully locked.
Dependency:
  • los_rwlock.h: the header file that contains the API declaration.
参见
LOS_MuxInit | LOS_MuxUnlock

Wait to lock a write lock.

在文件 los_rwlock.c351 行定义.

352{
353 UINT32 intSave;
354
355 UINT32 ret = OsRwlockCheck(rwlock);
356 if (ret != LOS_OK) {
357 return ret;
358 }
359
360 SCHEDULER_LOCK(intSave);
361 ret = OsRwlockWrUnsafe(rwlock, timeout);
362 SCHEDULER_UNLOCK(intSave);
363 return ret;
364}
UINT32 OsRwlockWrUnsafe(LosRwlock *rwlock, UINT32 timeout)
Definition: los_rwlock.c:290
函数调用图: