1219089Spjd/*
2219089Spjd * CDDL HEADER START
3219089Spjd *
4219089Spjd * The contents of this file are subject to the terms of the
5219089Spjd * Common Development and Distribution License (the "License").
6219089Spjd * You may not use this file except in compliance with the License.
7219089Spjd *
8219089Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9219089Spjd * or http://www.opensolaris.org/os/licensing.
10219089Spjd * See the License for the specific language governing permissions
11219089Spjd * and limitations under the License.
12219089Spjd *
13219089Spjd * When distributing Covered Code, include this CDDL HEADER in each
14219089Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15219089Spjd * If applicable, add the following below this CDDL HEADER, with the
16219089Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17219089Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18219089Spjd *
19219089Spjd * CDDL HEADER END
20219089Spjd */
21219089Spjd/*
22219089Spjd * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23290765Smav * Copyright (c) 2014, 2015 by Delphix. All rights reserved.
24308246Savg * Copyright 2016 The MathWorks, Inc. All rights reserved.
25219089Spjd */
26219089Spjd
27219089Spjd/*
28219089Spjd * A Zero Reference Lock (ZRL) is a reference count that can lock out new
29219089Spjd * references only when the count is zero and only without waiting if the count
30219089Spjd * is not already zero. It is similar to a read-write lock in that it allows
31219089Spjd * multiple readers and only a single writer, but it does not allow a writer to
32219089Spjd * block while waiting for readers to exit, and therefore the question of
33219089Spjd * reader/writer priority is moot (no WRWANT bit). Since the equivalent of
34219089Spjd * rw_enter(&lock, RW_WRITER) is disallowed and only tryenter() is allowed, it
35219089Spjd * is perfectly safe for the same reader to acquire the same lock multiple
36219089Spjd * times. The fact that a ZRL is reentrant for readers (through multiple calls
37219089Spjd * to zrl_add()) makes it convenient for determining whether something is
38219089Spjd * actively referenced without the fuss of flagging lock ownership across
39219089Spjd * function calls.
40219089Spjd */
41219089Spjd#include <sys/zrlock.h>
42219089Spjd
43219089Spjd/*
44219089Spjd * A ZRL can be locked only while there are zero references, so ZRL_LOCKED is
45219089Spjd * treated as zero references.
46219089Spjd */
47288560Smav#define	ZRL_LOCKED	-1
48219089Spjd#define	ZRL_DESTROYED	-2
49219089Spjd
50219089Spjdvoid
51219089Spjdzrl_init(zrlock_t *zrl)
52219089Spjd{
53219089Spjd	mutex_init(&zrl->zr_mtx, NULL, MUTEX_DEFAULT, NULL);
54219089Spjd	zrl->zr_refcount = 0;
55219089Spjd	cv_init(&zrl->zr_cv, NULL, CV_DEFAULT, NULL);
56219089Spjd#ifdef	ZFS_DEBUG
57219089Spjd	zrl->zr_owner = NULL;
58219089Spjd	zrl->zr_caller = NULL;
59219089Spjd#endif
60219089Spjd}
61219089Spjd
62219089Spjdvoid
63219089Spjdzrl_destroy(zrlock_t *zrl)
64219089Spjd{
65288560Smav	ASSERT0(zrl->zr_refcount);
66219089Spjd
67219089Spjd	mutex_destroy(&zrl->zr_mtx);
68219089Spjd	zrl->zr_refcount = ZRL_DESTROYED;
69219089Spjd	cv_destroy(&zrl->zr_cv);
70219089Spjd}
71219089Spjd
72219089Spjdvoid
73290765Smavzrl_add_impl(zrlock_t *zrl, const char *zc)
74219089Spjd{
75308246Savg	for (;;) {
76308246Savg		uint32_t n = (uint32_t)zrl->zr_refcount;
77308246Savg		while (n != ZRL_LOCKED) {
78308246Savg			uint32_t cas = atomic_cas_32(
79308246Savg			    (uint32_t *)&zrl->zr_refcount, n, n + 1);
80308246Savg			if (cas == n) {
81308246Savg				ASSERT3S((int32_t)n, >=, 0);
82219089Spjd#ifdef	ZFS_DEBUG
83308246Savg				if (zrl->zr_owner == curthread) {
84308246Savg					DTRACE_PROBE2(zrlock__reentry,
85308246Savg					    zrlock_t *, zrl, uint32_t, n);
86308246Savg				}
87308246Savg				zrl->zr_owner = curthread;
88308246Savg				zrl->zr_caller = zc;
89308246Savg#endif
90308246Savg				return;
91219089Spjd			}
92308246Savg			n = cas;
93219089Spjd		}
94219089Spjd
95308246Savg		mutex_enter(&zrl->zr_mtx);
96308246Savg		while (zrl->zr_refcount == ZRL_LOCKED) {
97308246Savg			cv_wait(&zrl->zr_cv, &zrl->zr_mtx);
98308246Savg		}
99308246Savg		mutex_exit(&zrl->zr_mtx);
100219089Spjd	}
101219089Spjd}
102219089Spjd
103219089Spjdvoid
104219089Spjdzrl_remove(zrlock_t *zrl)
105219089Spjd{
106219089Spjd	uint32_t n;
107219089Spjd
108219089Spjd#ifdef	ZFS_DEBUG
109219089Spjd	if (zrl->zr_owner == curthread) {
110219089Spjd		zrl->zr_owner = NULL;
111219089Spjd		zrl->zr_caller = NULL;
112219089Spjd	}
113219089Spjd#endif
114288560Smav	n = atomic_dec_32_nv((uint32_t *)&zrl->zr_refcount);
115288560Smav	ASSERT3S((int32_t)n, >=, 0);
116219089Spjd}
117219089Spjd
118219089Spjdint
119219089Spjdzrl_tryenter(zrlock_t *zrl)
120219089Spjd{
121219089Spjd	uint32_t n = (uint32_t)zrl->zr_refcount;
122219089Spjd
123219089Spjd	if (n == 0) {
124219089Spjd		uint32_t cas = atomic_cas_32(
125219089Spjd		    (uint32_t *)&zrl->zr_refcount, 0, ZRL_LOCKED);
126219089Spjd		if (cas == 0) {
127219089Spjd#ifdef	ZFS_DEBUG
128288560Smav			ASSERT3P(zrl->zr_owner, ==, NULL);
129219089Spjd			zrl->zr_owner = curthread;
130219089Spjd#endif
131219089Spjd			return (1);
132219089Spjd		}
133219089Spjd	}
134219089Spjd
135288560Smav	ASSERT3S((int32_t)n, >, ZRL_DESTROYED);
136219089Spjd
137219089Spjd	return (0);
138219089Spjd}
139219089Spjd
140219089Spjdvoid
141219089Spjdzrl_exit(zrlock_t *zrl)
142219089Spjd{
143288560Smav	ASSERT3S(zrl->zr_refcount, ==, ZRL_LOCKED);
144219089Spjd
145219089Spjd	mutex_enter(&zrl->zr_mtx);
146219089Spjd#ifdef	ZFS_DEBUG
147288560Smav	ASSERT3P(zrl->zr_owner, ==, curthread);
148219089Spjd	zrl->zr_owner = NULL;
149219089Spjd	membar_producer();	/* make sure the owner store happens first */
150219089Spjd#endif
151219089Spjd	zrl->zr_refcount = 0;
152219089Spjd	cv_broadcast(&zrl->zr_cv);
153219089Spjd	mutex_exit(&zrl->zr_mtx);
154219089Spjd}
155219089Spjd
156219089Spjdint
157219089Spjdzrl_refcount(zrlock_t *zrl)
158219089Spjd{
159288560Smav	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
160219089Spjd
161219089Spjd	int n = (int)zrl->zr_refcount;
162219089Spjd	return (n <= 0 ? 0 : n);
163219089Spjd}
164219089Spjd
165219089Spjdint
166219089Spjdzrl_is_zero(zrlock_t *zrl)
167219089Spjd{
168288560Smav	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
169219089Spjd
170219089Spjd	return (zrl->zr_refcount <= 0);
171219089Spjd}
172219089Spjd
173219089Spjdint
174219089Spjdzrl_is_locked(zrlock_t *zrl)
175219089Spjd{
176288560Smav	ASSERT3S(zrl->zr_refcount, >, ZRL_DESTROYED);
177219089Spjd
178219089Spjd	return (zrl->zr_refcount == ZRL_LOCKED);
179219089Spjd}
180219089Spjd
181219089Spjd#ifdef	ZFS_DEBUG
182219089Spjdkthread_t *
183219089Spjdzrl_owner(zrlock_t *zrl)
184219089Spjd{
185219089Spjd	return (zrl->zr_owner);
186219089Spjd}
187219089Spjd#endif
188