diff options
| author | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-11 18:16:10 -0700 |
|---|---|---|
| committer | Nicole Mazzuca <t-nimaz@microsoft.com> | 2019-07-11 18:21:25 -0700 |
| commit | a0fe40ea5842006c0da901a99ae07d7a25531175 (patch) | |
| tree | 73e7031f04ef399021fe6fe0fd505148d3a5f9a2 /toolsrc/include | |
| parent | 510b0c5cc0233311b6993b89cd5ce488218ed78d (diff) | |
| download | vcpkg-a0fe40ea5842006c0da901a99ae07d7a25531175.tar.gz vcpkg-a0fe40ea5842006c0da901a99ae07d7a25531175.zip | |
add tests!
Also, fix all the bugs I found when I wrote the tests!
Diffstat (limited to 'toolsrc/include')
| -rw-r--r-- | toolsrc/include/vcpkg/base/files.h | 35 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/strings.h | 10 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg/base/work_queue.h | 182 |
3 files changed, 120 insertions, 107 deletions
diff --git a/toolsrc/include/vcpkg/base/files.h b/toolsrc/include/vcpkg/base/files.h index 6cad3d461..a5e04db25 100644 --- a/toolsrc/include/vcpkg/base/files.h +++ b/toolsrc/include/vcpkg/base/files.h @@ -12,8 +12,8 @@ namespace fs using stdfs::copy_options; using stdfs::file_status; using stdfs::file_type; - using stdfs::perms; using stdfs::path; + using stdfs::perms; using stdfs::u8path; /* @@ -26,25 +26,24 @@ namespace fs // we want to poison ADL with these niebloids - namespace detail { - struct symlink_status_t { + namespace detail + { + struct symlink_status_t + { file_status operator()(const path& p, std::error_code& ec) const noexcept; file_status operator()(const path& p, vcpkg::LineInfo li) const noexcept; }; - struct is_symlink_t { - inline bool operator()(file_status s) const { - return stdfs::is_symlink(s); - } + struct is_symlink_t + { + inline bool operator()(file_status s) const { return stdfs::is_symlink(s); } }; - struct is_regular_file_t { - inline bool operator()(file_status s) const { - return stdfs::is_regular_file(s); - } + struct is_regular_file_t + { + inline bool operator()(file_status s) const { return stdfs::is_regular_file(s); } }; - struct is_directory_t { - inline bool operator()(file_status s) const { - return stdfs::is_directory(s); - } + struct is_directory_t + { + inline bool operator()(file_status s) const { return stdfs::is_directory(s); } }; } @@ -63,10 +62,10 @@ namespace fs We also want to poison the ADL on is_regular_file and is_directory, because we don't want people calling these functions on paths */ -using fs::symlink_status; -using fs::is_symlink; -using fs::is_regular_file; using fs::is_directory; +using fs::is_regular_file; +using fs::is_symlink; +using fs::symlink_status; namespace vcpkg::Files { diff --git a/toolsrc/include/vcpkg/base/strings.h b/toolsrc/include/vcpkg/base/strings.h index 625c0240f..a1906790f 100644 --- a/toolsrc/include/vcpkg/base/strings.h +++ b/toolsrc/include/vcpkg/base/strings.h @@ -188,13 +188,5 @@ namespace vcpkg::Strings // base 64 encoding with URL and filesafe alphabet (base64url) // based on IETF RFC 4648 // ignores padding, since one implicitly knows the length from the size of x - namespace detail { - - struct b64url_encode_t { - std::string operator()(std::uint64_t x) const noexcept; - }; - - } - - constexpr detail::b64url_encode_t b64url_encode{}; + std::string b64url_encode(std::uint64_t x) noexcept; } diff --git a/toolsrc/include/vcpkg/base/work_queue.h b/toolsrc/include/vcpkg/base/work_queue.h index 1836404ca..d6666770b 100644 --- a/toolsrc/include/vcpkg/base/work_queue.h +++ b/toolsrc/include/vcpkg/base/work_queue.h @@ -4,78 +4,67 @@ #include <memory> #include <queue> -namespace vcpkg { - template <class Action, class ThreadLocalData> +namespace vcpkg +{ + template<class Action, class ThreadLocalData> struct WorkQueue; - namespace detail { + namespace detail + { // for SFINAE purposes, keep out of the class - template <class Action, class ThreadLocalData> - auto call_moved_action( - Action& action, - const WorkQueue<Action, ThreadLocalData>& work_queue, - ThreadLocalData& tld - ) -> decltype(static_cast<void>(std::move(action)(tld, work_queue))) + template<class Action, class ThreadLocalData> + auto call_moved_action(Action& action, + const WorkQueue<Action, ThreadLocalData>& work_queue, + ThreadLocalData& tld) -> decltype(static_cast<void>(std::move(action)(tld, work_queue))) { std::move(action)(tld, work_queue); } - template <class Action, class ThreadLocalData> - auto call_moved_action( - Action& action, - const WorkQueue<Action, ThreadLocalData>&, - ThreadLocalData& tld - ) -> decltype(static_cast<void>(std::move(action)(tld))) + template<class Action, class ThreadLocalData> + auto call_moved_action(Action& action, const WorkQueue<Action, ThreadLocalData>&, ThreadLocalData& tld) + -> decltype(static_cast<void>(std::move(action)(tld))) { std::move(action)(tld); } - - struct immediately_run_t {}; } - constexpr detail::immediately_run_t immediately_run{}; - - - template <class Action, class ThreadLocalData> - struct WorkQueue { - template <class F> - WorkQueue(std::size_t num_threads, LineInfo li, const F& tld_init) noexcept { + template<class Action, class ThreadLocalData> + struct WorkQueue + { + template<class F> + WorkQueue(std::uint16_t num_threads, LineInfo li, const F& tld_init) noexcept + { m_line_info = li; - m_state = State::BeforeRun; + m_unjoined_workers = num_threads; m_threads.reserve(num_threads); - for (std::size_t i = 0; i < num_threads; ++i) { + for (std::size_t i = 0; i < num_threads; ++i) + { m_threads.push_back(std::thread(Worker{this, tld_init()})); } } - template <class F> - WorkQueue( - detail::immediately_run_t, - std::size_t num_threads, - LineInfo li, - const F& tld_init - ) noexcept : WorkQueue(num_threads, li, tld_init) { - m_state = State::Running; - } - WorkQueue(WorkQueue const&) = delete; WorkQueue(WorkQueue&&) = delete; - ~WorkQueue() { + ~WorkQueue() + { auto lck = std::unique_lock<std::mutex>(m_mutex); - if (m_state == State::Running) { + if (!is_joined(m_state)) + { Checks::exit_with_message(m_line_info, "Failed to call join() on a WorkQueue that was destroyed"); } } // should only be called once; anything else is an error - void run(LineInfo li) { + void run(LineInfo li) + { // this should _not_ be locked before `run()` is called; however, we // want to terminate if someone screws up, rather than cause UB auto lck = std::unique_lock<std::mutex>(m_mutex); - if (m_state != State::BeforeRun) { + if (m_state != State::BeforeRun) + { Checks::exit_with_message(li, "Attempted to run() twice"); } @@ -86,18 +75,40 @@ namespace vcpkg { // if this is called in an existing task, _will block forever_ // DO NOT DO THAT // thread-unsafe - void join(LineInfo li) { + void join(LineInfo li) + { { auto lck = std::unique_lock<std::mutex>(m_mutex); - if (is_joined(m_state)) { + if (is_joined(m_state)) + { Checks::exit_with_message(li, "Attempted to call join() more than once"); - } else if (m_state == State::Terminated) { + } + else if (m_state == State::Terminated) + { m_state = State::TerminatedJoined; - } else { + } + else + { m_state = State::Joined; } } - for (auto& thrd : m_threads) { + + for (;;) + { + auto lck = std::unique_lock<std::mutex>(m_mutex); + if (!m_unjoined_workers) + break; + + else if (!m_running_workers) + { + lck.unlock(); + m_cv.notify_all(); + } + } + + // all threads have returned -- now, it's time to join them + for (auto& thrd : m_threads) + { thrd.join(); } } @@ -105,19 +116,24 @@ namespace vcpkg { // useful in the case of errors // doesn't stop any existing running tasks // returns immediately, so that one can call this in a task - void terminate() const { + void terminate() const + { { auto lck = std::unique_lock<std::mutex>(m_mutex); - if (is_joined(m_state)) { + if (is_joined(m_state)) + { m_state = State::TerminatedJoined; - } else { + } + else + { m_state = State::Terminated; } } m_cv.notify_all(); } - void enqueue_action(Action a) const { + void enqueue_action(Action a) const + { { auto lck = std::unique_lock<std::mutex>(m_mutex); m_actions.push_back(std::move(a)); @@ -127,8 +143,9 @@ namespace vcpkg { m_cv.notify_one(); } - template <class Rng> - void enqueue_all_actions_by_move(Rng&& rng) const { + template<class Rng> + void enqueue_all_actions_by_move(Rng&& rng) const + { { using std::begin; using std::end; @@ -148,8 +165,9 @@ namespace vcpkg { m_cv.notify_all(); } - template <class Rng> - void enqueue_all_actions(Rng&& rng) const { + template<class Rng> + void enqueue_all_actions(Rng&& rng) const + { { using std::begin; using std::end; @@ -170,37 +188,41 @@ namespace vcpkg { } private: - struct Worker { + struct Worker + { const WorkQueue* work_queue; ThreadLocalData tld; - void operator()() { + void operator()() + { // unlocked when waiting, or when in the action // locked otherwise auto lck = std::unique_lock<std::mutex>(work_queue->m_mutex); - work_queue->m_cv.wait(lck, [&] { - return work_queue->m_state != State::BeforeRun; - }); + work_queue->m_cv.wait(lck, [&] { return work_queue->m_state != State::BeforeRun; }); - for (;;) { + for (;;) + { const auto state = work_queue->m_state; - if (is_terminated(state)) { - return; + if (is_terminated(state)) + { + break; } - if (work_queue->m_actions.empty()) { - if (state == State::Running || work_queue->running_workers > 0) { - --work_queue->running_workers; + if (work_queue->m_actions.empty()) + { + if (state == State::Running || work_queue->m_running_workers > 1) + { + --work_queue->m_running_workers; work_queue->m_cv.wait(lck); - ++work_queue->running_workers; + ++work_queue->m_running_workers; continue; } // the queue isn't running, and we are the only worker // no more work! - return; + break; } Action action = std::move(work_queue->m_actions.back()); @@ -210,10 +232,13 @@ namespace vcpkg { detail::call_moved_action(action, *work_queue, tld); lck.lock(); } + + --work_queue->m_unjoined_workers; } }; - enum class State : std::int16_t { + enum class State : std::int16_t + { // can only exist upon construction BeforeRun = -1, @@ -223,22 +248,19 @@ namespace vcpkg { TerminatedJoined, }; - static bool is_terminated(State st) { - return st == State::Terminated || st == State::TerminatedJoined; - } + static bool is_terminated(State st) { return st == State::Terminated || st == State::TerminatedJoined; } - static bool is_joined(State st) { - return st != State::Joined || st == State::TerminatedJoined; - } + static bool is_joined(State st) { return st == State::Joined || st == State::TerminatedJoined; } - mutable std::mutex m_mutex; - // these four are under m_mutex - mutable State m_state; - mutable std::uint16_t running_workers; - mutable std::vector<Action> m_actions; - mutable std::condition_variable m_cv; + mutable std::mutex m_mutex{}; + // these are all under m_mutex + mutable State m_state = State::BeforeRun; + mutable std::uint16_t m_running_workers = 0; + mutable std::uint16_t m_unjoined_workers = 0; // num_threads + mutable std::vector<Action> m_actions{}; + mutable std::condition_variable m_cv{}; - std::vector<std::thread> m_threads; + std::vector<std::thread> m_threads{}; LineInfo m_line_info; }; } |
