summaryrefslogtreecommitdiff
path: root/usr/src/lib/libfakekernel/common/rwlock.c
blob: 17b4ca604dc7a06ddb3e991408c5982f0a68a27f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * rwlock(9f)
 */

/* This is the API we're emulating */
#include <sys/rwlock.h>

#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/param.h>
#include <sys/synch32.h>
#include <sys/thread.h>

/* avoiding synch.h */
int	rwlock_init(lwp_rwlock_t *, int, void *);
int	rwlock_destroy(lwp_rwlock_t *);
int	rw_rdlock(lwp_rwlock_t *);
int	rw_wrlock(lwp_rwlock_t *);
int	rw_unlock(lwp_rwlock_t *);
int	rw_tryrdlock(lwp_rwlock_t *);
int	rw_trywrlock(lwp_rwlock_t *);
int	_rw_read_held(void *);
int	_rw_write_held(void *);

/*ARGSUSED*/
void
rw_init(krwlock_t *rwlp, char *name, krw_type_t type, void *arg)
{
	(void) rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
	rwlp->rw_owner = _KTHREAD_INVALID;
}

void
rw_destroy(krwlock_t *rwlp)
{
	(void) rwlock_destroy(&rwlp->rw_lock);
	rwlp->rw_owner = _KTHREAD_INVALID;
}

void
rw_enter(krwlock_t *rwlp, krw_t rw)
{
	int rc;

	if (rw == RW_READER) {
		rc = rw_rdlock(&rwlp->rw_lock);
	} else {
		rc = rw_wrlock(&rwlp->rw_lock);
		rwlp->rw_owner = _curthread();
	}
	VERIFY(rc == 0);
}

void
rw_exit(krwlock_t *rwlp)
{
	if (_rw_write_held(&rwlp->rw_lock)) {
		ASSERT(rwlp->rw_owner == _curthread());
		rwlp->rw_owner = _KTHREAD_INVALID;
	}
	(void) rw_unlock(&rwlp->rw_lock);
}

int
rw_tryenter(krwlock_t *rwlp, krw_t rw)
{
	int rv;

	if (rw == RW_WRITER) {
		rv = rw_trywrlock(&rwlp->rw_lock);
		if (rv == 0)
			rwlp->rw_owner = _curthread();
	} else
		rv = rw_tryrdlock(&rwlp->rw_lock);

	return ((rv == 0) ? 1 : 0);
}

/*ARGSUSED*/
int
rw_tryupgrade(krwlock_t *rwlp)
{

	return (0);
}

void
rw_downgrade(krwlock_t *rwlp)
{
	ASSERT(rwlp->rw_owner == _curthread());
	rwlp->rw_owner = _KTHREAD_INVALID;
	VERIFY(rw_unlock(&rwlp->rw_lock) == 0);
	VERIFY(rw_rdlock(&rwlp->rw_lock) == 0);
}

int
rw_read_held(krwlock_t *rwlp)
{
	return (_rw_read_held(rwlp));
}

int
rw_write_held(krwlock_t *rwlp)
{
	return (_rw_write_held(rwlp));
}

int
rw_lock_held(krwlock_t *rwlp)
{
	return (rw_read_held(rwlp) || rw_write_held(rwlp));
}

/*
 * Return the kthread_t * of the lock owner
 */
void *
rw_owner(krwlock_t *rwlp)
{
	return (rwlp->rw_owner);
}