1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved. 3 * Description: Lock Dependency Check. 4 * Author: Huawei LiteOS Team 5 * Create: 2018-10-18 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without specific prior written 15 * permission. 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * --------------------------------------------------------------------------- */ 28 29 /** 30 * @defgroup los_lockdep lockdep 31 * @ingroup kernel 32 */ 33 34 #ifndef _LOS_LOCKDEP_H 35 #define _LOS_LOCKDEP_H 36 37 #include "los_config.h" 38 #include "los_typedef.h" 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif /* __cplusplus */ 43 44 /* lockdep supported lock type */ 45 enum LockType { 46 #ifdef LOSCFG_KERNEL_SPINDEP 47 LOCK_SPIN, 48 #endif 49 #ifdef LOSCFG_KERNEL_MUXDEP 50 LOCK_MUTEX, 51 #endif 52 #ifdef LOSCFG_KERNEL_SEMDEP 53 LOCK_SEM, 54 #endif 55 #ifdef LOSCFG_PTHREAD_MUXDEP 56 LOCK_PTHREAD, 57 #endif 58 LOCK_TYPE_MAX, 59 }; 60 61 /* Define the structure of deadlock detection entity */ 62 typedef struct { 63 enum LockType type; /* lock type */ 64 UINT32 id; /* semaphore or mutex id */ 65 CHAR *name; /* spinlock name */ 66 UINT32 cpuId; /* the cpu id when the lock is obtained. */ 67 VOID *owner; /* the pointer to the lock owner's task control block. */ 68 } LosLockCheck; 69 70 #ifdef LOSCFG_KERNEL_LOCKDEP 71 72 /* This Macro is used to obtain the pointer to a lock check entity using a corresponding parameter */ 73 #define LOCKDEP_GET_ADDR(lockDep, index) ((lockDep)->heldLocks[(index)].lockAddr) 74 75 /* The max num that a task holding lock */ 76 #define MAX_LOCK_DEPTH 16U 77 78 enum LockDepErrType { 79 LOCKDEP_SUCCESS = 0, 80 LOCKDEP_ERR_DOUBLE_LOCK, 81 LOCKDEP_ERR_DEAD_LOCK, 82 LOCKDEP_ERR_UNLOCK_WITHOUT_LOCK, 83 LOCKDEP_ERR_OVERFLOW, 84 LOCKDEP_ERR_MAX, 85 }; 86 87 typedef struct { 88 VOID *lockPtr; 89 VOID *lockAddr; 90 UINT64 waitTime; 91 UINT64 holdTime; 92 } HeldLocks; 93 94 typedef struct { 95 VOID *waitLock; 96 INT32 lockDepth; 97 HeldLocks heldLocks[MAX_LOCK_DEPTH]; 98 } LockDep; 99 #endif 100 101 /* Note: The following functions are for internal use only. */ 102 103 /* This API is used to init dead lock */ 104 extern VOID OsLockDepCheckInit(enum LockType type, LosLockCheck *lock, UINT32 id); 105 106 /* This API is used to deinitialize dead lock */ 107 extern VOID OsLockDepCheckDeInit(enum LockType type, LosLockCheck *lock, UINT32 id); 108 109 /* This API is used to check dead lock */ 110 extern VOID OsLockDepCheckIn(enum LockType type, LosLockCheck *lock, UINT32 id); 111 112 /* This API is used to trace when a lock locked */ 113 extern VOID OsLockDepRecord(enum LockType type, LosLockCheck *lock, UINT32 id); 114 115 /* This API is used to trace when a lock unlocked */ 116 extern VOID OsLockDepCheckOut(enum LockType type, LosLockCheck *lock, UINT32 id); 117 118 #ifdef LOSCFG_KERNEL_SPINDEP 119 extern VOID OsLockdepClearSpinlocks(VOID); 120 #define LOCKDEP_CHECK_INIT(lock) do { \ 121 OsLockDepCheckInit(LOCK_SPIN, &lock->lockCheck, 0); \ 122 lock->lockCheck.name = "spinlock"; \ 123 } while (0) 124 #define LOCKDEP_CHECK_IN(lock) OsLockDepCheckIn(LOCK_SPIN, &lock->lockCheck, 0) 125 #define LOCKDEP_RECORD(lock) OsLockDepRecord(LOCK_SPIN, &lock->lockCheck, 0) 126 #define LOCKDEP_CHECK_OUT(lock) OsLockDepCheckOut(LOCK_SPIN, &lock->lockCheck, 0) 127 #define LOCKDEP_CLEAR_LOCKS() OsLockdepClearSpinlocks() 128 #else 129 #define LOCKDEP_CHECK_INIT(lock) 130 #define LOCKDEP_CHECK_IN(lock) 131 #define LOCKDEP_RECORD(lock) 132 #define LOCKDEP_CHECK_OUT(lock) 133 #define LOCKDEP_CLEAR_LOCKS() 134 #endif 135 136 #ifdef __cplusplus 137 } 138 #endif /* __cplusplus */ 139 #endif /* _LOS_LOCKDEP_H */ 140