source: mainline/uspace/lib/cpp/include/__bits/thread/threading.hpp@ 57264ac3

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

Unnecessary include

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
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#ifndef LIBCPP_BITS_THREAD_THREADING
30#define LIBCPP_BITS_THREAD_THREADING
31
32namespace std::hel
33{
34 extern "C" {
35 #include <fibril.h>
36 #include <fibril_synch.h>
37 }
38}
39
40#include <chrono>
41
42namespace std::aux
43{
44 struct fibril_tag
45 { /* DUMMY BODY */ };
46
47 struct thread_tag
48 { /* DUMMY BODY */ };
49
50 template<class>
51 struct threading_policy;
52
53 template<>
54 struct threading_policy<fibril_tag>
55 {
56 using mutex_type = hel::fibril_mutex_t;
57 using thread_type = hel::fid_t;
58 using condvar_type = hel::fibril_condvar_t;
59 using time_unit = hel::usec_t;
60 using shared_mutex_type = hel::fibril_rwlock_t;
61
62 struct thread
63 {
64 template<class Callable, class Payload>
65 static thread_type create(Callable clbl, Payload& pld)
66 {
67 return hel::fibril_create(clbl, (void*)&pld);
68 }
69
70 static void start(thread_type thr)
71 {
72 hel::fibril_add_ready(thr);
73 }
74
75 static thread_type this_thread()
76 {
77 return hel::fibril_get_id();
78 }
79
80 static void yield()
81 {
82 hel::fibril_yield();
83 }
84
85 /**
86 * Note: join & detach are performed at the C++
87 * level at the moment, but eventually should
88 * be moved here once joinable fibrils are in libc.
89 */
90 };
91
92 struct mutex
93 {
94 static void init(mutex_type& mtx)
95 {
96 hel::fibril_mutex_initialize(&mtx);
97 }
98
99 static void lock(mutex_type& mtx)
100 {
101 hel::fibril_mutex_lock(&mtx);
102 }
103
104 static void unlock(mutex_type& mtx)
105 {
106 hel::fibril_mutex_unlock(&mtx);
107 }
108
109 static bool try_lock(mutex_type& mtx)
110 {
111 return hel::fibril_mutex_trylock(&mtx);
112 }
113
114 static bool try_lock_for(mutex_type& mtx, time_unit timeout)
115 {
116 // TODO: we need fibril_mutex_trylock_for() :/
117 return try_lock(mtx);
118 }
119 };
120
121 struct condvar
122 {
123 static void init(condvar_type& cv)
124 {
125 hel::fibril_condvar_initialize(&cv);
126 }
127
128 static void wait(condvar_type& cv, mutex_type& mtx)
129 {
130 hel::fibril_condvar_wait(&cv, &mtx);
131 }
132
133 static int wait_for(condvar_type& cv, mutex_type& mtx, time_unit timeout)
134 {
135 return hel::fibril_condvar_wait_timeout(&cv, &mtx, timeout);
136 }
137
138 static void signal(condvar_type& cv)
139 {
140 hel::fibril_condvar_signal(&cv);
141 }
142
143 static void broadcast(condvar_type& cv)
144 {
145 hel::fibril_condvar_broadcast(&cv);
146 }
147 };
148
149 struct time
150 {
151 template<class Rep, class Period>
152 static time_unit convert(const std::chrono::duration<Rep, Period>& dur)
153 {
154 return std::chrono::duration_cast<std::chrono::duration<Rep, micro>>(dur).count();
155 }
156
157 static void sleep(time_unit time)
158 {
159 hel::fibril_usleep(time);
160 }
161 };
162
163 struct shared_mutex
164 {
165 static void init(shared_mutex_type& mtx)
166 {
167 hel::fibril_rwlock_initialize(&mtx);
168 }
169
170 static void lock(shared_mutex_type& mtx)
171 {
172 hel::fibril_rwlock_write_lock(&mtx);
173 }
174
175 static void unlock(shared_mutex_type& mtx)
176 {
177 hel::fibril_rwlock_write_unlock(&mtx);
178 }
179
180 static void lock_shared(shared_mutex_type& mtx)
181 {
182 hel::fibril_rwlock_read_lock(&mtx);
183 }
184
185 static void unlock_shared(shared_mutex_type& mtx)
186 {
187 hel::fibril_rwlock_read_unlock(&mtx);
188 }
189
190 static bool try_lock(shared_mutex_type& mtx)
191 {
192 // TODO: rwlocks don't have try locking capabilities
193 lock(mtx);
194
195 return true;
196 }
197
198 static bool try_lock_shared(shared_mutex_type& mtx)
199 {
200 lock(mtx);
201
202 return true;
203 }
204
205 static bool try_lock_for(shared_mutex_type& mtx, time_unit timeout)
206 {
207 return try_lock(mtx);
208 }
209
210 static bool try_lock_shared_for(shared_mutex_type& mtx, time_unit timeout)
211 {
212 return try_lock(mtx);
213 }
214 };
215 };
216
217 template<>
218 struct threading_policy<thread_tag>
219 {
220 // TODO:
221 };
222
223 using default_tag = fibril_tag;
224 using threading = threading_policy<default_tag>;
225
226 using thread_t = typename threading::thread_type;
227 using mutex_t = typename threading::mutex_type;
228 using condvar_t = typename threading::condvar_type;
229 using time_unit_t = typename threading::time_unit;
230 using shared_mutex_t = typename threading::shared_mutex_type;
231}
232
233#endif
Note: See TracBrowser for help on using the repository browser.