-
Notifications
You must be signed in to change notification settings - Fork 261
/
kern_mutex_obj.c
152 lines (134 loc) · 4.28 KB
/
kern_mutex_obj.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* $NetBSD: kern_mutex_obj.c,v 1.15 2023/10/02 21:03:55 ad Exp $ */
/*-
* Copyright (c) 2008, 2019, 2023 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.15 2023/10/02 21:03:55 ad Exp $");
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/mutex.h>
#include <sys/kmem.h>
/* Mutex cache */
#define MUTEX_OBJ_MAGIC 0x5aa3c85d
struct kmutexobj {
kmutex_t mo_lock;
u_int mo_magic;
u_int mo_refcnt;
uint8_t mo_pad[COHERENCY_UNIT - sizeof(kmutex_t) -
sizeof(u_int) * 2];
};
/*
* mutex_obj_alloc:
*
* Allocate a single lock object, waiting for memory if needed.
*/
kmutex_t *
mutex_obj_alloc(kmutex_type_t type, int ipl)
{
struct kmutexobj *mo;
mo = kmem_intr_alloc(sizeof(*mo), KM_SLEEP);
KASSERT(ALIGNED_POINTER(mo, coherency_unit));
_mutex_init(&mo->mo_lock, type, ipl,
(uintptr_t)__builtin_return_address(0));
mo->mo_magic = MUTEX_OBJ_MAGIC;
mo->mo_refcnt = 1;
return (kmutex_t *)mo;
}
/*
* mutex_obj_alloc:
*
* Allocate a single lock object, failing if no memory available.
*/
kmutex_t *
mutex_obj_tryalloc(kmutex_type_t type, int ipl)
{
struct kmutexobj *mo;
mo = kmem_intr_alloc(sizeof(*mo), KM_NOSLEEP);
KASSERT(ALIGNED_POINTER(mo, coherency_unit));
if (__predict_true(mo != NULL)) {
_mutex_init(&mo->mo_lock, type, ipl,
(uintptr_t)__builtin_return_address(0));
mo->mo_magic = MUTEX_OBJ_MAGIC;
mo->mo_refcnt = 1;
}
return (kmutex_t *)mo;
}
/*
* mutex_obj_hold:
*
* Add a single reference to a lock object. A reference to the object
* must already be held, and must be held across this call.
*/
void
mutex_obj_hold(kmutex_t *lock)
{
struct kmutexobj *mo = (struct kmutexobj *)lock;
KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
"%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
__func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
KASSERTMSG(mo->mo_refcnt > 0,
"%s: lock %p: mo->mo_refcnt (%#x) == 0",
__func__, mo, mo->mo_refcnt);
atomic_inc_uint(&mo->mo_refcnt);
}
/*
* mutex_obj_free:
*
* Drop a reference from a lock object. If the last reference is being
* dropped, free the object and return true. Otherwise, return false.
*/
bool
mutex_obj_free(kmutex_t *lock)
{
struct kmutexobj *mo = (struct kmutexobj *)lock;
KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
"%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
__func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
KASSERTMSG(mo->mo_refcnt > 0,
"%s: lock %p: mo->mo_refcnt (%#x) == 0",
__func__, mo, mo->mo_refcnt);
membar_release();
if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) {
return false;
}
membar_acquire();
mutex_destroy(&mo->mo_lock);
kmem_intr_free(mo, sizeof(*mo));
return true;
}
/*
* mutex_obj_refcnt:
*
* Return the reference count on a lock object.
*/
u_int
mutex_obj_refcnt(kmutex_t *lock)
{
struct kmutexobj *mo = (struct kmutexobj *)lock;
return mo->mo_refcnt;
}