Changeset 9283830 in mainline
- Timestamp:
- 2018-07-05T21:41:20Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7e7c1aac
- Parents:
- c4049e6
- git-author:
- Dzejrou <dzejrou@…> (2018-03-28 14:21:53)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:20)
- Location:
- uspace/lib/cpp
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/mutex.hpp
rc4049e6 r9283830 31 31 32 32 #include <internal/common.hpp> 33 #include <internal/thread.hpp> 33 34 #include <thread> 34 35 35 36 namespace std 36 37 { 37 extern "C" {38 #include <fibril.h>39 #include <fibril_synch.h>40 }41 42 38 /** 43 39 * 20.4.1.2.1, class mutex: … … 47 43 { 48 44 public: 49 constexpr mutex() noexcept; 45 constexpr mutex() noexcept 46 : mtx_{} 47 { 48 aux::threading::mutex::init(mtx_); 49 } 50 50 51 ~mutex(); 51 52 … … 57 58 void unlock(); 58 59 59 using native_handle_type = fibril_mutex_t*;60 using native_handle_type = aux::mutex_t*; 60 61 native_handle_type native_handle(); 61 62 62 63 private: 63 native_handle_typemtx_;64 aux::mutex_t mtx_; 64 65 }; 65 66 … … 81 82 void unlock(); 82 83 83 using native_handle_type = fibril_mutex_t*;84 using native_handle_type = aux::mutex_t*; 84 85 native_handle_type native_handle(); 85 86 86 87 private: 87 native_handle_typemtx_;88 aux::mutex_t mtx_; 88 89 size_t lock_level_; 89 90 thread::id owner_; -
uspace/lib/cpp/include/impl/ratio.hpp
rc4049e6 r9283830 89 89 { 90 90 public: 91 static_assert(D != 0, "ratio with denominator == 0");91 /* static_assert(D != 0, "ratio with denominator == 0"); */ 92 92 93 93 static constexpr intmax_t num = aux::sign_v<N> * aux::sign_v<D> -
uspace/lib/cpp/include/impl/thread.hpp
rc4049e6 r9283830 32 32 #include <chrono> 33 33 #include <internal/common.hpp> 34 #include <internal/thread.hpp> 34 35 #include <ostream> 35 36 36 37 namespace std 37 38 { 38 extern "C" {39 #include <fibril.h>40 #include <fibril_synch.h>41 }42 43 39 namespace aux 44 40 { … … 59 55 finished_{false}, detached_{false} 60 56 { 61 fibril_mutex_initialize(&join_mtx_);62 fibril_condvar_initialize(&join_cv_);57 aux::threading::mutex::init(join_mtx_); 58 aux::threading::condvar::init(join_cv_); 63 59 } 64 60 65 61 void join() 66 62 { 67 fibril_mutex_lock(&join_mtx_);63 aux::threading::mutex::lock(join_mtx_); 68 64 while (!finished_) 69 fibril_condvar_wait(&join_cv_, &join_mtx_);70 fibril_mutex_unlock(&join_mtx_);65 aux::threading::condvar::wait(join_cv_, join_mtx_); 66 aux::threading::mutex::unlock(join_mtx_); 71 67 } 72 68 … … 87 83 88 84 protected: 89 fibril_mutex_t join_mtx_;90 fibril_condvar_t join_cv_;85 aux::mutex_t join_mtx_; 86 aux::condvar_t join_cv_; 91 87 bool finished_; 92 88 bool detached_; … … 105 101 callable_(); 106 102 107 fibril_mutex_lock(&join_mtx_);103 aux::threading::mutex::lock(join_mtx_); 108 104 finished_ = true; 109 fibril_mutex_unlock(&join_mtx_);110 111 fibril_condvar_broadcast(&join_cv_);105 aux::threading::mutex::unlock(join_mtx_); 106 107 aux::threading::condvar::broadcast(join_cv_); 112 108 } 113 109 … … 126 122 class id; 127 123 128 using native_handle_type = fibril_t*;124 using native_handle_type = aux::thread_t*; 129 125 130 126 /** … … 150 146 joinable_wrapper_ = static_cast<aux::joinable_wrapper*>(callable_wrapper); 151 147 152 id_ = fibril_create(153 154 static_cast<void*>(callable_wrapper)148 id_ = aux::threading::thread::create( 149 aux::thread_main<decltype(callable_wrapper)>, 150 *callable_wrapper 155 151 ); 156 fibril_add_ready(id_); 152 153 aux::threading::thread::start(id_); 154 // TODO: fibrils are weird here, 2 returns with same thread ids 157 155 } 158 156 … … 182 180 183 181 private: 184 fid_t id_;182 aux::thread_t id_; 185 183 aux::joinable_wrapper* joinable_wrapper_{nullptr}; 186 184 … … 223 221 { 224 222 auto now = Clock::now(); 225 auto usecs = chrono::duration_cast<chrono::duration<typename Duration::rep, micro>>(abs_time - now); 226 227 fibril_usleep(usecs.count());223 224 auto time = aux::threading::time::convert(abs_time - now); 225 aux::threading::time::sleep(time); 228 226 } 229 227 … … 235 233 236 234 // TODO: timeouts? 237 auto usecs = chrono::duration_cast<chrono::duration<Rep, micro>>(rel_time);238 fibril_usleep(usecs.count());235 auto time = aux::threading::time::convert(rel_time); 236 aux::threading::time::sleep(time); 239 237 } 240 238 } -
uspace/lib/cpp/src/mutex.cpp
rc4049e6 r9283830 31 31 namespace std 32 32 { 33 constexpr mutex::mutex() noexcept34 : mtx_{}35 {36 fibril_mutex_initialize(&mtx_);37 }38 39 33 mutex::~mutex() 40 { 41 if (fibril_mutex_is_locked(&mtx_)) 42 { 43 /** 44 * According to the standard, this is 45 * undefined behavior, we could unlock the 46 * mutex, but that could cause issues if we 47 * are not the current owner. 48 */ 49 // fibril_mutex_unlock(&mtx_); 50 } 51 } 34 { /* DUMMY BODY */ } 52 35 53 36 void mutex::lock() 54 37 { 55 fibril_mutex_lock(&mtx_);38 aux::threading::mutex::lock(mtx_); 56 39 } 57 40 58 41 bool mutex::try_lock() 59 42 { 60 return fibril_mutex_trylock(&mtx_);43 return aux::threading::mutex::try_lock(mtx_); 61 44 } 62 45 63 46 void mutex::unlock() 64 47 { 65 fibril_mutex_unlock(&mtx_);48 aux::threading::mutex::unlock(mtx_); 66 49 } 67 50 … … 74 57 : mtx_{}, lock_level_{}, owner_{} 75 58 { 76 fibril_mutex_initialize(&mtx_);59 aux::threading::mutex::init(mtx_); 77 60 } 78 61 … … 84 67 if (owner_ != this_thread::get_id()) 85 68 { 86 fibril_mutex_lock(&mtx_);69 aux::threading::mutex::lock(mtx_); 87 70 owner_ = this_thread::get_id(); 88 71 lock_level_ = 1; … … 96 79 if (owner_ != this_thread::get_id()) 97 80 { 98 bool res = fibril_mutex_trylock(&mtx_);81 bool res = aux::threading::mutex::try_lock(mtx_); 99 82 if (res) 100 83 { … … 116 99 return; 117 100 else if (--lock_level_ == 0) 118 fibril_mutex_unlock(&mtx_);101 aux::threading::mutex::unlock(mtx_); 119 102 } 120 103 -
uspace/lib/cpp/src/thread.cpp
rc4049e6 r9283830 28 28 29 29 #include <cstdlib> 30 #include <exception> 30 31 #include <thread> 31 32 #include <utility> 33 34 #include <iostream> 32 35 33 36 namespace std … … 39 42 thread::~thread() 40 43 { 41 if (joinable())42 {43 // TODO: call std::terminate44 }44 // TODO: investigate joinable() in detail 45 // + std::terminate behaves weirdly on HelenOS 46 if (joinable() && false) 47 std::terminate(); 45 48 49 // TODO: check for finished too? 50 // TODO: WAIT! if it's not detached, then 51 // we are joinable and std::terminate was called? 52 // TODO: review this entire thing 46 53 if (joinable_wrapper_ && !joinable_wrapper_->detached()) 47 54 delete joinable_wrapper_; … … 49 56 50 57 thread::thread(thread&& other) noexcept 51 : id_{other.id_} 58 : id_{other.id_}, joinable_wrapper_{other.joinable_wrapper_} 52 59 { 53 other.id_ = fid_t{}; 60 other.id_ = aux::thread_t{}; 61 other.joinable_wrapper_ = nullptr; 54 62 } 55 63 … … 57 65 { 58 66 if (joinable()) 59 { 60 // TODO: call std::terminate 61 } 67 std::terminate(); 62 68 63 69 id_ = other.id_; 64 other.id_ = fid_t{}; 70 other.id_ = aux::thread_t{}; 71 72 joinable_wrapper_ = other.joinable_wrapper_; 73 other.joinable_wrapper_ = nullptr; 65 74 66 75 return *this; … … 70 79 { 71 80 std::swap(id_, other.id_); 81 std::swap(joinable_wrapper_, other.joinable_wrapper_); 72 82 } 73 83 74 84 bool thread::joinable() const noexcept 75 85 { 76 return id_ != fid_t{};86 return id_ != aux::thread_t{}; 77 87 } 78 88 … … 85 95 void thread::detach() 86 96 { 87 id_ = fid_t{};97 id_ = aux::thread_t{}; 88 98 89 99 if (joinable_wrapper_) … … 124 134 thread::id get_id() noexcept 125 135 { 126 return thread::id{ fibril_get_id()};136 return thread::id{aux::threading::thread::this_thread()}; 127 137 } 128 138 129 139 void yield() noexcept 130 140 { 131 fibril_yield();141 aux::threading::thread::yield(); 132 142 } 133 143 }
Note:
See TracChangeset
for help on using the changeset viewer.