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