xref: /freebsd/lib/libthr/thread/thr_pshared.c (revision 80a87461087caa71c74d891c5b7cddf0156ad58a)
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  *
4  * This software was developed by Konstantin Belousov
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/queue.h>
35 #include "namespace.h"
36 #include <stdlib.h>
37 #include "un-namespace.h"
38 
39 #include "thr_private.h"
40 
41 struct psh {
42 	LIST_ENTRY(psh) link;
43 	void *key;
44 	void *val;
45 };
46 
47 LIST_HEAD(pshared_hash_head, psh);
48 #define	HASH_SIZE	128
49 static struct pshared_hash_head pshared_hash[HASH_SIZE];
50 #define	PSHARED_KEY_HASH(key)	(((unsigned long)(key) >> 8) % HASH_SIZE)
51 /* XXXKIB: lock could be split to per-hash chain, if appears contested */
52 static struct urwlock pshared_lock = DEFAULT_URWLOCK;
53 
54 void
55 __thr_pshared_init(void)
56 {
57 	int i;
58 
59 	_thr_urwlock_init(&pshared_lock);
60 	for (i = 0; i < HASH_SIZE; i++)
61 		LIST_INIT(&pshared_hash[i]);
62 }
63 
64 static void
65 pshared_rlock(struct pthread *curthread)
66 {
67 
68 	curthread->locklevel++;
69 	_thr_rwl_rdlock(&pshared_lock);
70 }
71 
72 static void
73 pshared_wlock(struct pthread *curthread)
74 {
75 
76 	curthread->locklevel++;
77 	_thr_rwl_wrlock(&pshared_lock);
78 }
79 
80 static void
81 pshared_unlock(struct pthread *curthread)
82 {
83 
84 	_thr_rwl_unlock(&pshared_lock);
85 	curthread->locklevel--;
86 	_thr_ast(curthread);
87 }
88 
89 static void
90 pshared_gc(struct pthread *curthread)
91 {
92 	struct pshared_hash_head *hd;
93 	struct psh *h, *h1;
94 	int error, i;
95 
96 	pshared_wlock(curthread);
97 	for (i = 0; i < HASH_SIZE; i++) {
98 		hd = &pshared_hash[i];
99 		LIST_FOREACH_SAFE(h, hd, link, h1) {
100 			error = _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_ALIVE,
101 			    h->val, NULL);
102 			if (error == 0)
103 				continue;
104 			LIST_REMOVE(h, link);
105 			munmap(h->val, PAGE_SIZE);
106 			free(h);
107 		}
108 	}
109 	pshared_unlock(curthread);
110 }
111 
112 static void *
113 pshared_lookup(void *key)
114 {
115 	struct pshared_hash_head *hd;
116 	struct psh *h;
117 
118 	hd = &pshared_hash[PSHARED_KEY_HASH(key)];
119 	LIST_FOREACH(h, hd, link) {
120 		if (h->key == key)
121 			return (h->val);
122 	}
123 	return (NULL);
124 }
125 
126 static int
127 pshared_insert(void *key, void **val)
128 {
129 	struct pshared_hash_head *hd;
130 	struct psh *h;
131 
132 	hd = &pshared_hash[PSHARED_KEY_HASH(key)];
133 	LIST_FOREACH(h, hd, link) {
134 		if (h->key == key) {
135 			if (h->val != *val) {
136 				munmap(*val, PAGE_SIZE);
137 				*val = h->val;
138 			}
139 			return (1);
140 		}
141 	}
142 
143 	h = malloc(sizeof(*h));
144 	if (h == NULL)
145 		return (0);
146 	h->key = key;
147 	h->val = *val;
148 	LIST_INSERT_HEAD(hd, h, link);
149 	return (1);
150 }
151 
152 static void *
153 pshared_remove(void *key)
154 {
155 	struct pshared_hash_head *hd;
156 	struct psh *h;
157 	void *val;
158 
159 	hd = &pshared_hash[PSHARED_KEY_HASH(key)];
160 	LIST_FOREACH(h, hd, link) {
161 		if (h->key == key) {
162 			LIST_REMOVE(h, link);
163 			val = h->val;
164 			free(h);
165 			return (val);
166 		}
167 	}
168 	return (NULL);
169 }
170 
171 static void
172 pshared_clean(void *key, void *val)
173 {
174 
175 	if (val != NULL)
176 		munmap(val, PAGE_SIZE);
177 	_umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_DESTROY, key, NULL);
178 }
179 
180 void *
181 __thr_pshared_offpage(void *key, int doalloc)
182 {
183 	struct pthread *curthread;
184 	void *res;
185 	int fd, ins_done;
186 
187 	curthread = _get_curthread();
188 	pshared_rlock(curthread);
189 	res = pshared_lookup(key);
190 	pshared_unlock(curthread);
191 	if (res != NULL)
192 		return (res);
193 	fd = _umtx_op(NULL, UMTX_OP_SHM, doalloc ? UMTX_SHM_CREAT :
194 	    UMTX_SHM_LOOKUP, key, NULL);
195 	if (fd == -1)
196 		return (NULL);
197 	res = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
198 	close(fd);
199 	if (res == MAP_FAILED)
200 		return (NULL);
201 	pshared_wlock(curthread);
202 	ins_done = pshared_insert(key, &res);
203 	pshared_unlock(curthread);
204 	if (!ins_done) {
205 		pshared_clean(key, res);
206 		res = NULL;
207 	}
208 	return (res);
209 }
210 
211 void
212 __thr_pshared_destroy(void *key)
213 {
214 	struct pthread *curthread;
215 	void *val;
216 
217 	curthread = _get_curthread();
218 	pshared_wlock(curthread);
219 	val = pshared_remove(key);
220 	pshared_unlock(curthread);
221 	pshared_clean(key, val);
222 	pshared_gc(curthread);
223 }
224