source: mainline/uspace/lib/c/generic/thread/futex.c@ 9e889f6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9e889f6 was 9e889f6, checked in by Jakub Jermar <jakub@…>, 7 years ago

Switch userspace futexes to using waitq kobjects

This replaces futexes entirely when they are not shared across address
spaces.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <assert.h>
36#include <stdatomic.h>
37#include <fibril.h>
38#include <io/kio.h>
39
40#include "../private/fibril.h"
41#include "../private/futex.h"
42
43//#define DPRINTF(...) kio_printf(__VA_ARGS__)
44#define DPRINTF(...) dummy_printf(__VA_ARGS__)
45
46/** Initialize futex counter.
47 *
48 * @param futex Futex.
49 * @param val Initialization value.
50 *
51 */
52void futex_initialize(futex_t *futex, int val)
53{
54 atomic_store_explicit(&futex->val, val, memory_order_relaxed);
55 atomic_store_explicit(&futex->alloc_lock, 0, memory_order_relaxed);
56 futex->whandle = CAP_NIL;
57}
58
59#ifdef CONFIG_DEBUG_FUTEX
60
61void __futex_assert_is_locked(futex_t *futex, const char *name)
62{
63 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
64 fibril_t *self = (fibril_t *) fibril_get_id();
65 if (owner != self) {
66 DPRINTF("Assertion failed: %s (%p) is not locked by fibril %p (instead locked by fibril %p).\n", name, futex, self, owner);
67 }
68 assert(owner == self);
69}
70
71void __futex_assert_is_not_locked(futex_t *futex, const char *name)
72{
73 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
74 fibril_t *self = (fibril_t *) fibril_get_id();
75 if (owner == self) {
76 DPRINTF("Assertion failed: %s (%p) is already locked by fibril %p.\n", name, futex, self);
77 }
78 assert(owner != self);
79}
80
81void __futex_lock(futex_t *futex, const char *name)
82{
83 /*
84 * We use relaxed atomics to avoid violating C11 memory model.
85 * They should compile to regular load/stores, but simple assignments
86 * would be UB by definition.
87 * The proper ordering is ensured by the surrounding futex operation.
88 */
89
90 fibril_t *self = (fibril_t *) fibril_get_id();
91 DPRINTF("Locking futex %s (%p) by fibril %p.\n", name, futex, self);
92 __futex_assert_is_not_locked(futex, name);
93 futex_down(futex);
94
95 void *prev_owner = atomic_load_explicit(&futex->owner,
96 memory_order_relaxed);
97 assert(prev_owner == NULL);
98 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
99}
100
101void __futex_unlock(futex_t *futex, const char *name)
102{
103 fibril_t *self = (fibril_t *) fibril_get_id();
104 DPRINTF("Unlocking futex %s (%p) by fibril %p.\n", name, futex, self);
105 __futex_assert_is_locked(futex, name);
106 atomic_store_explicit(&futex->owner, NULL, memory_order_relaxed);
107 futex_up(futex);
108}
109
110bool __futex_trylock(futex_t *futex, const char *name)
111{
112 fibril_t *self = (fibril_t *) fibril_get_id();
113 bool success = futex_trydown(futex);
114 if (success) {
115 void *owner = atomic_load_explicit(&futex->owner,
116 memory_order_relaxed);
117 assert(owner == NULL);
118
119 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
120
121 DPRINTF("Trylock on futex %s (%p) by fibril %p succeeded.\n", name, futex, self);
122 } else {
123 DPRINTF("Trylock on futex %s (%p) by fibril %p failed.\n", name, futex, self);
124 }
125
126 return success;
127}
128
129void __futex_give_to(futex_t *futex, void *new_owner, const char *name)
130{
131 fibril_t *self = fibril_self();
132 fibril_t *no = new_owner;
133 DPRINTF("Passing futex %s (%p) from fibril %p to fibril %p.\n", name, futex, self, no);
134
135 __futex_assert_is_locked(futex, name);
136 atomic_store_explicit(&futex->owner, new_owner, memory_order_relaxed);
137}
138
139#endif
140
141/** @}
142 */
Note: See TracBrowser for help on using the repository browser.