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