source: mainline/uspace/lib/c/generic/futex.c@ 8dab988

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8dab988 was 6e569bf, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Fix accidentally introduced build issue.

  • Property mode set to 100644
File size: 4.4 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 <futex.h>
36
37#include <assert.h>
38#include <atomic.h>
39#include <fibril.h>
40#include <io/kio.h>
41
42#include "private/fibril.h"
43
44//#define DPRINTF(...) kio_printf(__VA_ARGS__)
45#define DPRINTF(...) ((void)0)
46
47/** Initialize futex counter.
48 *
49 * @param futex Futex.
50 * @param val Initialization value.
51 *
52 */
53void futex_initialize(futex_t *futex, int val)
54{
55 atomic_set(&futex->val, val);
56}
57
58#ifdef CONFIG_DEBUG_FUTEX
59
60void __futex_assert_is_locked(futex_t *futex, const char *name)
61{
62 void *owner = __atomic_load_n(&futex->owner, __ATOMIC_RELAXED);
63 fibril_t *self = (fibril_t *) fibril_get_id();
64 if (owner != self) {
65 DPRINTF("Assertion failed: %s (%p) is not locked by fibril %p (instead locked by fibril %p).\n", name, futex, self, owner);
66 }
67 assert(owner == self);
68}
69
70void __futex_assert_is_not_locked(futex_t *futex, const char *name)
71{
72 void *owner = __atomic_load_n(&futex->owner, __ATOMIC_RELAXED);
73 fibril_t *self = (fibril_t *) fibril_get_id();
74 if (owner == self) {
75 DPRINTF("Assertion failed: %s (%p) is already locked by fibril %p.\n", name, futex, self);
76 }
77 assert(owner != self);
78}
79
80void __futex_lock(futex_t *futex, const char *name)
81{
82 /*
83 * We use relaxed atomics to avoid violating C11 memory model.
84 * They should compile to regular load/stores, but simple assignments
85 * would be UB by definition.
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_n(&futex->owner, __ATOMIC_RELAXED);
94 assert(prev_owner == NULL);
95 __atomic_store_n(&futex->owner, self, __ATOMIC_RELAXED);
96
97 atomic_inc(&self->futex_locks);
98}
99
100void __futex_unlock(futex_t *futex, const char *name)
101{
102 fibril_t *self = (fibril_t *) fibril_get_id();
103 DPRINTF("Unlocking futex %s (%p) by fibril %p.\n", name, futex, self);
104 __futex_assert_is_locked(futex, name);
105 __atomic_store_n(&futex->owner, NULL, __ATOMIC_RELAXED);
106 atomic_dec(&self->futex_locks);
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_n(&futex->owner, __ATOMIC_RELAXED);
116 assert(owner == NULL);
117
118 __atomic_store_n(&futex->owner, self, __ATOMIC_RELAXED);
119
120 atomic_inc(&self->futex_locks);
121
122 DPRINTF("Trylock on futex %s (%p) by fibril %p succeeded.\n", name, futex, self);
123 } else {
124 DPRINTF("Trylock on futex %s (%p) by fibril %p failed.\n", name, futex, self);
125 }
126
127 return success;
128}
129
130void __futex_give_to(futex_t *futex, void *new_owner, const char *name)
131{
132 fibril_t *self = fibril_self();
133 fibril_t *no = new_owner;
134 DPRINTF("Passing futex %s (%p) from fibril %p to fibril %p.\n", name, futex, self, no);
135
136 __futex_assert_is_locked(futex, name);
137 atomic_dec(&self->futex_locks);
138 atomic_inc(&no->futex_locks);
139 __atomic_store_n(&futex->owner, new_owner, __ATOMIC_RELAXED);
140}
141
142#endif
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.