]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/core/os_thread.h
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / core / os_thread.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright 2015-2020, Intel Corporation */
3 /*
4 * Copyright (c) 2016, Microsoft Corporation. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * * Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * os_thread.h -- os thread abstraction layer
37 */
38
39 #ifndef OS_THREAD_H
40 #define OS_THREAD_H 1
41
42 #include <stdint.h>
43 #include <time.h>
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 typedef union {
50 long long align;
51 char padding[44]; /* linux: 40 windows: 44 */
52 } os_mutex_t;
53
54 typedef union {
55 long long align;
56 char padding[56]; /* linux: 56 windows: 13 */
57 } os_rwlock_t;
58
59 typedef union {
60 long long align;
61 char padding[48]; /* linux: 48 windows: 12 */
62 } os_cond_t;
63
64 typedef union {
65 long long align;
66 char padding[32]; /* linux: 8 windows: 32 */
67 } os_thread_t;
68
69 typedef union {
70 long long align; /* linux: long windows: 8 FreeBSD: 12 */
71 char padding[16]; /* 16 to be safe */
72 } os_once_t;
73
74 #define OS_ONCE_INIT { .padding = {0} }
75
76 typedef unsigned os_tls_key_t;
77
78 typedef union {
79 long long align;
80 char padding[56]; /* linux: 56 windows: 8 */
81 } os_semaphore_t;
82
83 typedef union {
84 long long align;
85 char padding[56]; /* linux: 56 windows: 8 */
86 } os_thread_attr_t;
87
88 typedef union {
89 long long align;
90 char padding[512];
91 } os_cpu_set_t;
92
93 #ifdef __FreeBSD__
94 #define cpu_set_t cpuset_t
95 typedef uintptr_t os_spinlock_t;
96 #else
97 typedef volatile int os_spinlock_t; /* XXX: not implemented on windows */
98 #endif
99
100 void os_cpu_zero(os_cpu_set_t *set);
101 void os_cpu_set(size_t cpu, os_cpu_set_t *set);
102
103 #ifndef _WIN32
104 #define _When_(...)
105 #endif
106 int os_once(os_once_t *o, void (*func)(void));
107
108 int os_tls_key_create(os_tls_key_t *key, void (*destructor)(void *));
109 int os_tls_key_delete(os_tls_key_t key);
110 int os_tls_set(os_tls_key_t key, const void *value);
111 void *os_tls_get(os_tls_key_t key);
112
113 int os_mutex_init(os_mutex_t *__restrict mutex);
114 int os_mutex_destroy(os_mutex_t *__restrict mutex);
115 _When_(return == 0, _Acquires_lock_(mutex->lock))
116 int os_mutex_lock(os_mutex_t *__restrict mutex);
117 _When_(return == 0, _Acquires_lock_(mutex->lock))
118 int os_mutex_trylock(os_mutex_t *__restrict mutex);
119 int os_mutex_unlock(os_mutex_t *__restrict mutex);
120
121 /* XXX - non POSIX */
122 int os_mutex_timedlock(os_mutex_t *__restrict mutex,
123 const struct timespec *abstime);
124
125 int os_rwlock_init(os_rwlock_t *__restrict rwlock);
126 int os_rwlock_destroy(os_rwlock_t *__restrict rwlock);
127 int os_rwlock_rdlock(os_rwlock_t *__restrict rwlock);
128 int os_rwlock_wrlock(os_rwlock_t *__restrict rwlock);
129 int os_rwlock_tryrdlock(os_rwlock_t *__restrict rwlock);
130 _When_(return == 0, _Acquires_exclusive_lock_(rwlock->lock))
131 int os_rwlock_trywrlock(os_rwlock_t *__restrict rwlock);
132 _When_(rwlock->is_write != 0, _Requires_exclusive_lock_held_(rwlock->lock))
133 _When_(rwlock->is_write == 0, _Requires_shared_lock_held_(rwlock->lock))
134 int os_rwlock_unlock(os_rwlock_t *__restrict rwlock);
135 int os_rwlock_timedrdlock(os_rwlock_t *__restrict rwlock,
136 const struct timespec *abstime);
137 int os_rwlock_timedwrlock(os_rwlock_t *__restrict rwlock,
138 const struct timespec *abstime);
139
140 int os_spin_init(os_spinlock_t *lock, int pshared);
141 int os_spin_destroy(os_spinlock_t *lock);
142 int os_spin_lock(os_spinlock_t *lock);
143 int os_spin_unlock(os_spinlock_t *lock);
144 int os_spin_trylock(os_spinlock_t *lock);
145
146 int os_cond_init(os_cond_t *__restrict cond);
147 int os_cond_destroy(os_cond_t *__restrict cond);
148 int os_cond_broadcast(os_cond_t *__restrict cond);
149 int os_cond_signal(os_cond_t *__restrict cond);
150 int os_cond_timedwait(os_cond_t *__restrict cond,
151 os_mutex_t *__restrict mutex, const struct timespec *abstime);
152 int os_cond_wait(os_cond_t *__restrict cond,
153 os_mutex_t *__restrict mutex);
154
155 /* threading */
156
157 int os_thread_create(os_thread_t *thread, const os_thread_attr_t *attr,
158 void *(*start_routine)(void *), void *arg);
159
160 int os_thread_join(os_thread_t *thread, void **result);
161
162 void os_thread_self(os_thread_t *thread);
163
164 /* thread affinity */
165
166 int os_thread_setaffinity_np(os_thread_t *thread, size_t set_size,
167 const os_cpu_set_t *set);
168
169 int os_thread_atfork(void (*prepare)(void), void (*parent)(void),
170 void (*child)(void));
171
172 int os_semaphore_init(os_semaphore_t *sem, unsigned value);
173 int os_semaphore_destroy(os_semaphore_t *sem);
174 int os_semaphore_wait(os_semaphore_t *sem);
175 int os_semaphore_trywait(os_semaphore_t *sem);
176 int os_semaphore_post(os_semaphore_t *sem);
177
178 #ifdef __cplusplus
179 }
180 #endif
181 #endif /* OS_THREAD_H */