1 /* 2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU) 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 #ifndef COMMON_LOCK_H 13 #define COMMON_LOCK_H 14 15 #include <common/types.h> 16 17 /* Simple/Compact ticket/rwlock impl */ 18 19 struct lock { 20 volatile u64 slock; 21 }; 22 23 struct rwlock { 24 volatile u32 lock; 25 }; 26 27 int lock_init(struct lock *lock); 28 void lock(struct lock *lock); 29 /* returns 0 on success, -1 otherwise */ 30 int try_lock(struct lock *lock); 31 void unlock(struct lock *lock); 32 int is_locked(struct lock *lock); 33 34 /* Global locks */ 35 extern struct lock big_kernel_lock; 36 37 int rwlock_init(struct rwlock *rwlock); 38 void read_lock(struct rwlock *rwlock); 39 void write_lock(struct rwlock *rwlock); 40 /* read/write_try_lock return 0 on success, -1 otherwise */ 41 int read_try_lock(struct rwlock *rwlock); 42 int write_try_lock(struct rwlock *rwlock); 43 void read_unlock(struct rwlock *rwlock); 44 void write_unlock(struct rwlock *rwlock); 45 void rw_downgrade(struct rwlock *rwlock); 46 47 #endif /* COMMON_LOCK_H */