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

Last change on this file since eec201d was 508b0df1, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Remove uspace <atomic.h>, use <stdatomic.h> instead

  • 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}
56
57#ifdef CONFIG_DEBUG_FUTEX
58
59void __futex_assert_is_locked(futex_t *futex, const char *name)
60{
61 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
62 fibril_t *self = (fibril_t *) fibril_get_id();
63 if (owner != self) {
64 DPRINTF("Assertion failed: %s (%p) is not locked by fibril %p (instead locked by fibril %p).\n", name, futex, self, owner);
65 }
66 assert(owner == self);
67}
68
69void __futex_assert_is_not_locked(futex_t *futex, const char *name)
70{
71 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
72 fibril_t *self = (fibril_t *) fibril_get_id();
73 if (owner == self) {
74 DPRINTF("Assertion failed: %s (%p) is already locked by fibril %p.\n", name, futex, self);
75 }
76 assert(owner != self);
77}
78
79void __futex_lock(futex_t *futex, const char *name)
80{
81 /*
82 * We use relaxed atomics to avoid violating C11 memory model.
83 * They should compile to regular load/stores, but simple assignments
84 * would be UB by definition.
85 * The proper ordering is ensured by the surrounding futex operation.
86 */
87
88 fibril_t *self = (fibril_t *) fibril_get_id();
89 DPRINTF("Locking futex %s (%p) by fibril %p.\n", name, futex, self);
90 __futex_assert_is_not_locked(futex, name);
91 futex_down(futex);
92
93 void *prev_owner = atomic_load_explicit(&futex->owner,
94 memory_order_relaxed);
95 assert(prev_owner == NULL);
96 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
97}
98
99void __futex_unlock(futex_t *futex, const char *name)
100{
101 fibril_t *self = (fibril_t *) fibril_get_id();
102 DPRINTF("Unlocking futex %s (%p) by fibril %p.\n", name, futex, self);
103 __futex_assert_is_locked(futex, name);
104 atomic_store_explicit(&futex->owner, NULL, memory_order_relaxed);
105 futex_up(futex);
106}
107
108bool __futex_trylock(futex_t *futex, const char *name)
109{
110 fibril_t *self = (fibril_t *) fibril_get_id();
111 bool success = futex_trydown(futex);
112 if (success) {
113 void *owner = atomic_load_explicit(&futex->owner,
114 memory_order_relaxed);
115 assert(owner == NULL);
116
117 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
118
119 DPRINTF("Trylock on futex %s (%p) by fibril %p succeeded.\n", name, futex, self);
120 } else {
121 DPRINTF("Trylock on futex %s (%p) by fibril %p failed.\n", name, futex, self);
122 }
123
124 return success;
125}
126
127void __futex_give_to(futex_t *futex, void *new_owner, const char *name)
128{
129 fibril_t *self = fibril_self();
130 fibril_t *no = new_owner;
131 DPRINTF("Passing futex %s (%p) from fibril %p to fibril %p.\n", name, futex, self, no);
132
133 __futex_assert_is_locked(futex, name);
134 atomic_store_explicit(&futex->owner, new_owner, memory_order_relaxed);
135}
136
137#endif
138
139/** @}
140 */
Note: See TracBrowser for help on using the repository browser.