source: mainline/uspace/lib/c/include/futex.h@ 3d95c9d

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

"Obviously harmless" error handling tweaks.

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[1cef26f]1/*
[df4ed85]2 * Copyright (c) 2006 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.
27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
33 */
34
[fadd381]35#ifndef LIBC_FUTEX_H_
36#define LIBC_FUTEX_H_
[1cef26f]37
38#include <atomic.h>
[897fd8f1]39#include <errno.h>
[cb10bc9]40#include <libc.h>
41
[927a181e]42typedef struct futex {
43 atomic_t val;
[d54b303]44#ifdef FUTEX_UPGRADABLE
45 int upgraded;
46#endif
[927a181e]47} futex_t;
[4db6eaf]48
[cb10bc9]49
[4db6eaf]50extern void futex_initialize(futex_t *futex, int value);
[cb10bc9]51
[d54b303]52#ifdef FUTEX_UPGRADABLE
53#include <rcu.h>
54
[32d2e60]55#define FUTEX_INITIALIZE(val) {{ (val) }, 0}
[d54b303]56
[156b6406]57#define futex_lock(fut) \
[d54b303]58({ \
59 rcu_read_lock(); \
60 (fut)->upgraded = rcu_access(_upgrade_futexes); \
61 if ((fut)->upgraded) \
[156b6406]62 (void) futex_down((fut)); \
[d54b303]63})
64
[156b6406]65#define futex_trylock(fut) \
[d54b303]66({ \
67 rcu_read_lock(); \
68 int _upgraded = rcu_access(_upgrade_futexes); \
69 if (_upgraded) { \
[156b6406]70 int _acquired = futex_trydown((fut)); \
[d54b303]71 if (!_acquired) { \
72 rcu_read_unlock(); \
73 } else { \
74 (fut)->upgraded = true; \
75 } \
76 _acquired; \
77 } else { \
78 (fut)->upgraded = false; \
79 1; \
80 } \
81})
82
[156b6406]83#define futex_unlock(fut) \
[d54b303]84({ \
85 if ((fut)->upgraded) \
[156b6406]86 (void) futex_up((fut)); \
[d54b303]87 rcu_read_unlock(); \
88})
89
90extern int _upgrade_futexes;
91
92extern void futex_upgrade_all_and_wait(void);
93
94#else
95
[32d2e60]96#define FUTEX_INITIALIZE(val) {{ (val) }}
[d54b303]97
[156b6406]98#define futex_lock(fut) (void) futex_down((fut))
99#define futex_trylock(fut) futex_trydown((fut))
100#define futex_unlock(fut) (void) futex_up((fut))
[d54b303]101
102#endif
[cb10bc9]103
[d54b303]104#define FUTEX_INITIALIZER FUTEX_INITIALIZE(1)
[cb10bc9]105
106/** Try to down the futex.
107 *
108 * @param futex Futex.
109 *
[d5c1051]110 * @return true if the futex was acquired.
111 * @return false if the futex was not acquired.
[cb10bc9]112 *
113 */
[d5c1051]114static inline bool futex_trydown(futex_t *futex)
[cb10bc9]115{
116 return cas(&futex->val, 1, 0);
117}
118
119/** Down the futex.
120 *
121 * @param futex Futex.
122 *
123 * @return ENOENT if there is no such virtual address.
[897fd8f1]124 * @return EOK on success.
125 * @return Error code from <errno.h> otherwise.
[cb10bc9]126 *
127 */
[156b6406]128static inline int futex_down(futex_t *futex)
[cb10bc9]129{
130 if ((atomic_signed_t) atomic_predec(&futex->val) < 0)
[d5c1051]131 return (int) __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->val.count);
[cb10bc9]132
[897fd8f1]133 return EOK;
[cb10bc9]134}
135
136/** Up the futex.
137 *
138 * @param futex Futex.
139 *
140 * @return ENOENT if there is no such virtual address.
[897fd8f1]141 * @return EOK on success.
142 * @return Error code from <errno.h> otherwise.
[cb10bc9]143 *
144 */
[156b6406]145static inline int futex_up(futex_t *futex)
[cb10bc9]146{
147 if ((atomic_signed_t) atomic_postinc(&futex->val) < 0)
[d5c1051]148 return (int) __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->val.count);
[cb10bc9]149
[897fd8f1]150 return EOK;
[cb10bc9]151}
[1cef26f]152
153#endif
[b2951e2]154
[fadd381]155/** @}
[b2951e2]156 */
Note: See TracBrowser for help on using the repository browser.