2 * Copyright (c) 2015 The FreeBSD Foundation
4 * This software was developed by Konstantin Belousov
5 * under sponsorship from the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include "namespace.h"
34 #include "un-namespace.h"
36 #include "thr_private.h"
44 LIST_HEAD(pshared_hash_head, psh);
46 static struct pshared_hash_head pshared_hash[HASH_SIZE];
47 #define PSHARED_KEY_HASH(key) (((unsigned long)(key) >> 8) % HASH_SIZE)
48 /* XXXKIB: lock could be split to per-hash chain, if appears contested */
49 static struct urwlock pshared_lock = DEFAULT_URWLOCK;
53 __thr_pshared_init(void)
57 page_size = getpagesize();
58 THR_ASSERT(page_size >= THR_PAGE_SIZE_MIN,
59 "THR_PAGE_SIZE_MIN is too large");
61 _thr_urwlock_init(&pshared_lock);
62 for (i = 0; i < HASH_SIZE; i++)
63 LIST_INIT(&pshared_hash[i]);
67 pshared_rlock(struct pthread *curthread)
70 curthread->locklevel++;
71 _thr_rwl_rdlock(&pshared_lock);
75 pshared_wlock(struct pthread *curthread)
78 curthread->locklevel++;
79 _thr_rwl_wrlock(&pshared_lock);
83 pshared_unlock(struct pthread *curthread)
86 _thr_rwl_unlock(&pshared_lock);
87 curthread->locklevel--;
92 * Among all processes sharing a lock only one executes
93 * pthread_lock_destroy(). Other processes still have the hash and
96 * Mitigate the problem by checking the liveness of all hashed keys
97 * periodically. Right now this is executed on each
98 * pthread_lock_destroy(), but may be done less often if found to be
102 pshared_gc(struct pthread *curthread)
104 struct pshared_hash_head *hd;
108 pshared_wlock(curthread);
109 for (i = 0; i < HASH_SIZE; i++) {
110 hd = &pshared_hash[i];
111 LIST_FOREACH_SAFE(h, hd, link, h1) {
112 error = _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_ALIVE,
116 LIST_REMOVE(h, link);
117 munmap(h->val, page_size);
121 pshared_unlock(curthread);
125 pshared_lookup(void *key)
127 struct pshared_hash_head *hd;
130 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
131 LIST_FOREACH(h, hd, link) {
139 pshared_insert(void *key, void **val)
141 struct pshared_hash_head *hd;
144 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
145 LIST_FOREACH(h, hd, link) {
147 * When the key already exists in the hash, we should
148 * return either the new (just mapped) or old (hashed)
149 * val, and the other val should be unmapped to avoid
150 * address space leak.
152 * If two threads perform lock of the same object
153 * which is not yet stored in the pshared_hash, then
154 * the val already inserted by the first thread should
155 * be returned, and the second val freed (order is by
156 * the pshared_lock()). Otherwise, if we unmap the
157 * value obtained from the hash, the first thread
158 * might operate on an unmapped off-page object.
160 * There is still an issue: if hashed key was unmapped
161 * and then other page is mapped at the same key
162 * address, the hash would return the old val. I
163 * decided to handle the race of simultaneous hash
164 * insertion, leaving the unlikely remap problem
168 if (h->val != *val) {
169 munmap(*val, page_size);
176 h = malloc(sizeof(*h));
181 LIST_INSERT_HEAD(hd, h, link);
186 pshared_remove(void *key)
188 struct pshared_hash_head *hd;
192 hd = &pshared_hash[PSHARED_KEY_HASH(key)];
193 LIST_FOREACH(h, hd, link) {
195 LIST_REMOVE(h, link);
205 pshared_clean(void *key, void *val)
209 munmap(val, page_size);
210 _umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_DESTROY, key, NULL);
214 pshared_destroy(struct pthread *curthread, void *key)
218 pshared_wlock(curthread);
219 val = pshared_remove(key);
220 pshared_unlock(curthread);
221 pshared_clean(key, val);
225 __thr_pshared_offpage(void *key, int doalloc)
227 struct pthread *curthread;
231 curthread = _get_curthread();
233 pshared_destroy(curthread, key);
236 pshared_rlock(curthread);
237 res = pshared_lookup(key);
238 pshared_unlock(curthread);
242 fd = _umtx_op(NULL, UMTX_OP_SHM, doalloc ? UMTX_SHM_CREAT :
243 UMTX_SHM_LOOKUP, key, NULL);
246 res = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
248 if (res == MAP_FAILED)
250 pshared_wlock(curthread);
251 ins_done = pshared_insert(key, &res);
252 pshared_unlock(curthread);
254 pshared_clean(key, res);
261 __thr_pshared_destroy(void *key)
263 struct pthread *curthread;
265 curthread = _get_curthread();
266 pshared_destroy(curthread, key);
267 pshared_gc(curthread);
271 __thr_pshared_atfork_pre(void)
274 _thr_rwl_rdlock(&pshared_lock);
278 __thr_pshared_atfork_post(void)
281 _thr_rwl_unlock(&pshared_lock);