4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
24 * Copyright 2016 The MathWorks, Inc. All rights reserved.
28 * A Zero Reference Lock (ZRL) is a reference count that can lock out new
29 * references only when the count is zero and only without waiting if the count
30 * is not already zero. It is similar to a read-write lock in that it allows
31 * multiple readers and only a single writer, but it does not allow a writer to
32 * block while waiting for readers to exit, and therefore the question of
33 * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
34 * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
35 * is perfectly safe for the same reader to acquire the same lock multiple
36 * times. The fact that a ZRL is reentrant for readers (through multiple calls
37 * to zrl_add()) makes it convenient for determining whether something is
38 * actively referenced without the fuss of flagging lock ownership across
41 #include <sys/zrlock.h>
42 #include <sys/trace_zfs.h>
45 * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
46 * treated as zero references.
49 #define ZRL_DESTROYED -2
52 zrl_init(zrlock_t *zrl)
54 mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
56 cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
59 zrl->zr_caller = NULL;
64 zrl_destroy(zrlock_t *zrl)
66 ASSERT0(zrl->zr_refcount);
68 mutex_destroy(&zrl->zr_mtx);
69 zrl->zr_refcount = ZRL_DESTROYED;
70 cv_destroy(&zrl->zr_cv);
74 zrl_add_impl(zrlock_t *zrl, const char *zc)
77 uint32_t n = (uint32_t)zrl->zr_refcount;
78 while (n != ZRL_LOCKED) {
79 uint32_t cas = atomic_cas_32(
80 (uint32_t *)&zrl->zr_refcount, n, n + 1);
82 ASSERT3S((int32_t)n, >=, 0);
84 if (zrl->zr_owner == curthread) {
85 DTRACE_PROBE3(zrlock__reentry,
87 kthread_t *, curthread,
90 zrl->zr_owner = curthread;
98 mutex_enter(&zrl->zr_mtx);
99 while (zrl->zr_refcount == ZRL_LOCKED) {
100 cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
102 mutex_exit(&zrl->zr_mtx);
107 zrl_remove(zrlock_t *zrl)
112 if (zrl->zr_owner == curthread) {
113 zrl->zr_owner = NULL;
114 zrl->zr_caller = NULL;
117 n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
118 ASSERT3S((int32_t)n, >=, 0);
122 zrl_tryenter(zrlock_t *zrl)
124 uint32_t n = (uint32_t)zrl->zr_refcount;
127 uint32_t cas = atomic_cas_32(
128 (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
131 ASSERT3P(zrl->zr_owner, ==, NULL);
132 zrl->zr_owner = curthread;
138 ASSERT3S((int32_t)n, >, ZRL_DESTROYED);
144 zrl_exit(zrlock_t *zrl)
146 ASSERT3S(zrl->zr_refcount, ==, ZRL_LOCKED);
148 mutex_enter(&zrl->zr_mtx);
150 ASSERT3P(zrl->zr_owner, ==, curthread);
151 zrl->zr_owner = NULL;
152 membar_producer(); /* make sure the owner store happens first */
154 zrl->zr_refcount = 0;
155 cv_broadcast(&zrl->zr_cv);
156 mutex_exit(&zrl->zr_mtx);
160 zrl_is_zero(zrlock_t *zrl)
162 ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
164 return (zrl->zr_refcount <= 0);
168 zrl_is_locked(zrlock_t *zrl)
170 ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
172 return (zrl->zr_refcount == ZRL_LOCKED);
177 zrl_owner(zrlock_t *zrl)
179 return (zrl->zr_owner);
185 EXPORT_SYMBOL(zrl_add_impl);
186 EXPORT_SYMBOL(zrl_remove);