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

Last change on this file was 45c8eea, checked in by Jakub Jermar <jakub@…>, 7 years ago

Preallocate waitq handle during initialization

Do not clutter futex_down_composable() with the preallocation of the
wait queue handle and do it single-threadedly in futex_initialize().

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[1cef26f]1/*
[4db6eaf]2 * Copyright (c) 2008 Jakub Jermar
[1cef26f]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.
[b2951e2]27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[596d65c]33 */
[1cef26f]34
[f6372be9]35#include <assert.h>
[508b0df1]36#include <stdatomic.h>
[f6372be9]37#include <fibril.h>
38#include <io/kio.h>
39
[6340b4d2]40#include "../private/fibril.h"
[f787c8e]41#include "../private/futex.h"
[f6372be9]42
43//#define DPRINTF(...) kio_printf(__VA_ARGS__)
[2965d18]44#define DPRINTF(...) dummy_printf(__VA_ARGS__)
[17242c6e]45
46/** Initialize futex counter.
47 *
[596d65c]48 * @param futex Futex.
49 * @param val Initialization value.
50 *
[45c8eea]51 * @return Error code.
[17242c6e]52 */
[45c8eea]53errno_t futex_initialize(futex_t *futex, int val)
[1cef26f]54{
[508b0df1]55 atomic_store_explicit(&futex->val, val, memory_order_relaxed);
[9e889f6]56 futex->whandle = CAP_NIL;
[45c8eea]57 return futex_allocate_waitq(futex);
[1cef26f]58}
59
[f6372be9]60#ifdef CONFIG_DEBUG_FUTEX
61
62void __futex_assert_is_locked(futex_t *futex, const char *name)
63{
[508b0df1]64 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
[f6372be9]65 fibril_t *self = (fibril_t *) fibril_get_id();
66 if (owner != self) {
67 DPRINTF("Assertion failed: %s (%p) is not locked by fibril %p (instead locked by fibril %p).\n", name, futex, self, owner);
68 }
69 assert(owner == self);
70}
71
72void __futex_assert_is_not_locked(futex_t *futex, const char *name)
73{
[508b0df1]74 void *owner = atomic_load_explicit(&futex->owner, memory_order_relaxed);
[f6372be9]75 fibril_t *self = (fibril_t *) fibril_get_id();
76 if (owner == self) {
77 DPRINTF("Assertion failed: %s (%p) is already locked by fibril %p.\n", name, futex, self);
78 }
79 assert(owner != self);
80}
81
82void __futex_lock(futex_t *futex, const char *name)
83{
[6e569bf]84 /*
85 * We use relaxed atomics to avoid violating C11 memory model.
[f6372be9]86 * They should compile to regular load/stores, but simple assignments
87 * would be UB by definition.
[2965d18]88 * The proper ordering is ensured by the surrounding futex operation.
[f6372be9]89 */
90
91 fibril_t *self = (fibril_t *) fibril_get_id();
92 DPRINTF("Locking futex %s (%p) by fibril %p.\n", name, futex, self);
93 __futex_assert_is_not_locked(futex, name);
94 futex_down(futex);
95
[508b0df1]96 void *prev_owner = atomic_load_explicit(&futex->owner,
97 memory_order_relaxed);
[f6372be9]98 assert(prev_owner == NULL);
[508b0df1]99 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
[f6372be9]100}
101
102void __futex_unlock(futex_t *futex, const char *name)
103{
104 fibril_t *self = (fibril_t *) fibril_get_id();
105 DPRINTF("Unlocking futex %s (%p) by fibril %p.\n", name, futex, self);
106 __futex_assert_is_locked(futex, name);
[508b0df1]107 atomic_store_explicit(&futex->owner, NULL, memory_order_relaxed);
[f6372be9]108 futex_up(futex);
109}
110
111bool __futex_trylock(futex_t *futex, const char *name)
112{
113 fibril_t *self = (fibril_t *) fibril_get_id();
114 bool success = futex_trydown(futex);
115 if (success) {
[508b0df1]116 void *owner = atomic_load_explicit(&futex->owner,
117 memory_order_relaxed);
[f6372be9]118 assert(owner == NULL);
119
[508b0df1]120 atomic_store_explicit(&futex->owner, self, memory_order_relaxed);
[f6372be9]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);
[508b0df1]137 atomic_store_explicit(&futex->owner, new_owner, memory_order_relaxed);
[f6372be9]138}
139
140#endif
141
[fadd381]142/** @}
[b2951e2]143 */
Note: See TracBrowser for help on using the repository browser.