source: mainline/uspace/lib/c/include/futex.h@ 1afa94d

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

Reduce the number of files that include <sys/types.h>

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