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.
26 * A Zero Reference Lock (ZRL) is a reference count that can lock out new
27 * references only when the count is zero and only without waiting if the count
28 * is not already zero. It is similar to a read-write lock in that it allows
29 * multiple readers and only a single writer, but it does not allow a writer to
30 * block while waiting for readers to exit, and therefore the question of
31 * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
32 * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
33 * is perfectly safe for the same reader to acquire the same lock multiple
34 * times. The fact that a ZRL is reentrant for readers (through multiple calls
35 * to zrl_add()) makes it convenient for determining whether something is
36 * actively referenced without the fuss of flagging lock ownership across
39 #include <sys/zrlock.h>
42 * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
43 * treated as zero references.
45 #define ZRL_LOCKED ((uint32_t)-1)
46 #define ZRL_DESTROYED -2
49 zrl_init(zrlock_t *zrl)
51 mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
53 cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
56 zrl->zr_caller = NULL;
61 zrl_destroy(zrlock_t *zrl)
63 ASSERT(zrl->zr_refcount == 0);
65 mutex_destroy(&zrl->zr_mtx);
66 zrl->zr_refcount = ZRL_DESTROYED;
67 cv_destroy(&zrl->zr_cv);
72 zrl_add_debug(zrlock_t *zrl, const char *zc)
74 zrl_add(zrlock_t *zrl)
77 uint32_t n = (uint32_t)zrl->zr_refcount;
79 while (n != ZRL_LOCKED) {
80 uint32_t cas = atomic_cas_32(
81 (uint32_t *)&zrl->zr_refcount, n, n + 1);
83 ASSERT((int32_t)n >= 0);
85 if (zrl->zr_owner == curthread) {
86 DTRACE_PROBE2(zrlock__reentry,
87 zrlock_t *, zrl, uint32_t, n);
89 zrl->zr_owner = curthread;
97 mutex_enter(&zrl->zr_mtx);
98 while (zrl->zr_refcount == ZRL_LOCKED) {
99 cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
101 ASSERT(zrl->zr_refcount >= 0);
104 zrl->zr_owner = curthread;
107 mutex_exit(&zrl->zr_mtx);
111 zrl_remove(zrlock_t *zrl)
115 n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
116 ASSERT((int32_t)n >= 0);
118 if (zrl->zr_owner == curthread) {
119 zrl->zr_owner = NULL;
120 zrl->zr_caller = NULL;
126 zrl_tryenter(zrlock_t *zrl)
128 uint32_t n = (uint32_t)zrl->zr_refcount;
131 uint32_t cas = atomic_cas_32(
132 (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
135 ASSERT(zrl->zr_owner == NULL);
136 zrl->zr_owner = curthread;
142 ASSERT((int32_t)n > ZRL_DESTROYED);
148 zrl_exit(zrlock_t *zrl)
150 ASSERT(zrl->zr_refcount == ZRL_LOCKED);
152 mutex_enter(&zrl->zr_mtx);
154 ASSERT(zrl->zr_owner == curthread);
155 zrl->zr_owner = NULL;
156 membar_producer(); /* make sure the owner store happens first */
158 zrl->zr_refcount = 0;
159 cv_broadcast(&zrl->zr_cv);
160 mutex_exit(&zrl->zr_mtx);
164 zrl_refcount(zrlock_t *zrl)
168 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
170 n = (int)zrl->zr_refcount;
171 return (n <= 0 ? 0 : n);
175 zrl_is_zero(zrlock_t *zrl)
177 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
179 return (zrl->zr_refcount <= 0);
183 zrl_is_locked(zrlock_t *zrl)
185 ASSERT(zrl->zr_refcount > ZRL_DESTROYED);
187 return (zrl->zr_refcount == ZRL_LOCKED);
192 zrl_owner(zrlock_t *zrl)
194 return (zrl->zr_owner);
198 #if defined(_KERNEL) && defined(HAVE_SPL)
201 EXPORT_SYMBOL(zrl_add_debug);
203 EXPORT_SYMBOL(zrl_add);
205 EXPORT_SYMBOL(zrl_remove);