From 36f4ba035c87fc49a2e7149319642785b4ffdbbd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 23 Mar 2018 14:10:20 +1100 Subject: [PATCH] thread: rename thread primitives from job namespace --- CMakeLists.txt | 42 ++++++++++++++--------------- job/queue.hpp | 20 +++++++------- test/parallel/queue.cpp | 6 ++--- test/{job => thread}/event.cpp | 4 +-- test/{job => thread}/flag.cpp | 6 ++--- test/{job => thread}/monitor.cpp | 12 ++++----- test/{job => thread}/semaphore.cpp | 12 ++++----- test/{job => thread}/spinlock.cpp | 10 +++---- test/{job => thread}/ticketlock.cpp | 10 +++---- {job => thread}/event.hpp | 6 ++--- {job => thread}/event_linux.cpp | 2 +- {job => thread}/event_win32.cpp | 0 {job => thread}/flag.hpp | 6 ++--- {job => thread}/flag_linux.cpp | 2 +- {job => thread}/flag_win32.cpp | 0 {job => thread}/monitor.cpp | 2 +- {job => thread}/monitor.hpp | 6 ++--- {job => thread}/semaphore.hpp | 6 ++--- {job => thread}/semaphore_linux.cpp | 2 +- {job => thread}/semaphore_win32.cpp | 0 {job => thread}/spinlock.cpp | 2 +- {job => thread}/spinlock.hpp | 6 ++--- {job => thread}/ticketlock.cpp | 2 +- {job => thread}/ticketlock.hpp | 6 ++--- 24 files changed, 85 insertions(+), 85 deletions(-) rename test/{job => thread}/event.cpp (94%) rename test/{job => thread}/flag.cpp (94%) rename test/{job => thread}/monitor.cpp (89%) rename test/{job => thread}/semaphore.cpp (86%) rename test/{job => thread}/spinlock.cpp (89%) rename test/{job => thread}/ticketlock.cpp (92%) rename {job => thread}/event.hpp (94%) rename {job => thread}/event_linux.cpp (98%) rename {job => thread}/event_win32.cpp (100%) rename {job => thread}/flag.hpp (90%) rename {job => thread}/flag_linux.cpp (98%) rename {job => thread}/flag_win32.cpp (100%) rename {job => thread}/monitor.cpp (95%) rename {job => thread}/monitor.hpp (93%) rename {job => thread}/semaphore.hpp (92%) rename {job => thread}/semaphore_linux.cpp (99%) rename {job => thread}/semaphore_win32.cpp (100%) rename {job => thread}/spinlock.cpp (97%) rename {job => thread}/spinlock.hpp (93%) rename {job => thread}/ticketlock.cpp (97%) rename {job => thread}/ticketlock.hpp (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0af359b0..cb48f133 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,9 +87,9 @@ list ( ##----------------------------------------------------------------------------- if (LINUX) list (APPEND UTIL_FILES - job/event_linux.cpp - job/flag_linux.cpp - job/semaphore_linux.cpp + thread/event_linux.cpp + thread/flag_linux.cpp + thread/semaphore_linux.cpp ) endif () @@ -125,9 +125,9 @@ if (WINDOWS) exe_win32.cpp io_win32.cpp io_win32.hpp - job/event_win32.cpp - job/flag_win32.cpp - job/semaphore_win32.cpp + thread/event_win32.cpp + thread/flag_win32.cpp + thread/semaphore_win32.cpp library_win32.cpp library_win32.hpp time_win32.cpp @@ -277,17 +277,8 @@ list ( io.hpp iterator.hpp job/fwd.hpp - job/event.hpp - job/flag.hpp - job/monitor.cpp - job/monitor.hpp job/queue.cpp job/queue.hpp - job/semaphore.hpp - job/ticketlock.cpp - job/ticketlock.hpp - job/spinlock.cpp - job/spinlock.hpp json/fwd.hpp json/except.cpp json/except.hpp @@ -377,6 +368,15 @@ list ( tap.hpp term.cpp term.hpp + thread/event.hpp + thread/flag.hpp + thread/monitor.cpp + thread/monitor.hpp + thread/semaphore.hpp + thread/ticketlock.cpp + thread/ticketlock.hpp + thread/spinlock.cpp + thread/spinlock.hpp time.cpp time.hpp time/parse.hpp @@ -500,13 +500,7 @@ if (TESTS) hton introspection iterator - job/event - job/flag - job/monitor job/queue - job/semaphore - job/spinlock - job/ticketlock json_types json2/event maths @@ -532,6 +526,12 @@ if (TESTS) string stringid strongdef + thread/event + thread/flag + thread/monitor + thread/semaphore + thread/spinlock + thread/ticketlock time/8601 traits tuple diff --git a/job/queue.hpp b/job/queue.hpp index 95077ec8..0881563e 100644 --- a/job/queue.hpp +++ b/job/queue.hpp @@ -19,10 +19,10 @@ #include "../pool.hpp" -#include "ticketlock.hpp" -#include "semaphore.hpp" -#include "flag.hpp" -#include "monitor.hpp" +#include "../thread/ticketlock.hpp" +#include "../thread/semaphore.hpp" +#include "../thread/flag.hpp" +#include "../thread/monitor.hpp" #include "../parallel/queue.hpp" @@ -185,8 +185,8 @@ namespace util::job { std::array data; std::function function; - semaphore references = 0; - flag done; + thread::semaphore references = 0; + thread::flag done; }; private: @@ -197,9 +197,9 @@ namespace util::job { std::atomic m_running = 0; struct { - monitor< + thread::monitor< std::deque, - ticketlock + thread::ticketlock > pending; pool store; @@ -207,11 +207,11 @@ namespace util::job { std::vector notified; } m_tasks; - semaphore m_pending; + thread::semaphore m_pending; std::vector m_threads; - semaphore m_doomed; + thread::semaphore m_doomed; std::thread m_reaper; }; } diff --git a/test/parallel/queue.cpp b/test/parallel/queue.cpp index d54ae341..7aafcbe9 100644 --- a/test/parallel/queue.cpp +++ b/test/parallel/queue.cpp @@ -1,6 +1,6 @@ #include "parallel/queue.hpp" -#include "job/flag.hpp" -#include "job/semaphore.hpp" +#include "thread/flag.hpp" +#include "thread/semaphore.hpp" #include "debug.hpp" #include "tap.hpp" @@ -12,7 +12,7 @@ static constexpr uint32_t slots = 4; static constexpr int parallelism = 8; static constexpr int chunk_size = 1<<12; -util::job::flag start; +util::thread::flag start; using queue_t = util::parallel::queue; diff --git a/test/job/event.cpp b/test/thread/event.cpp similarity index 94% rename from test/job/event.cpp rename to test/thread/event.cpp index 10f63889..95931f47 100644 --- a/test/job/event.cpp +++ b/test/thread/event.cpp @@ -1,4 +1,4 @@ -#include "job/event.hpp" +#include "thread/event.hpp" #include "tap.hpp" #include @@ -17,7 +17,7 @@ main () // the test isn't 100% deterministic (because we're attempting to create // specific timings by just waiting). but it's a decent first check. std::atomic val = 0; - util::job::event a; + util::thread::event a; std::thread t ([&] () { a.wait (); diff --git a/test/job/flag.cpp b/test/thread/flag.cpp similarity index 94% rename from test/job/flag.cpp rename to test/thread/flag.cpp index 0f4f7ced..ce6319f6 100644 --- a/test/job/flag.cpp +++ b/test/thread/flag.cpp @@ -1,4 +1,4 @@ -#include "job/flag.hpp" +#include "thread/flag.hpp" #include "tap.hpp" #include @@ -9,7 +9,7 @@ main () { util::TAP::logger tap; - util::job::flag f; + util::thread::flag f; std::atomic value = 0; std::thread t1 ([&] () { @@ -45,7 +45,7 @@ main () constexpr int parallelism = 16; std::vector< - std::array + std::array > flags (iterations); const auto func = [&flags] (const int idx) { diff --git a/test/job/monitor.cpp b/test/thread/monitor.cpp similarity index 89% rename from test/job/monitor.cpp rename to test/thread/monitor.cpp index 65da5fcc..06b51dab 100644 --- a/test/job/monitor.cpp +++ b/test/thread/monitor.cpp @@ -15,9 +15,9 @@ */ #include "tap.hpp" -#include "job/monitor.hpp" -#include "job/event.hpp" -#include "job/semaphore.hpp" +#include "thread/monitor.hpp" +#include "thread/event.hpp" +#include "thread/semaphore.hpp" #include #include @@ -40,8 +40,8 @@ struct foo { } std::atomic value = 0; - util::job::semaphore enter; - util::job::semaphore leave; + util::thread::semaphore enter; + util::thread::semaphore leave; }; @@ -51,7 +51,7 @@ main () { util::TAP::logger tap; - util::job::monitor obj; + util::thread::monitor obj; const auto &value = obj->value; auto &enter = obj->enter; auto &leave = obj->leave; diff --git a/test/job/semaphore.cpp b/test/thread/semaphore.cpp similarity index 86% rename from test/job/semaphore.cpp rename to test/thread/semaphore.cpp index 7ed7a598..9545b0ef 100644 --- a/test/job/semaphore.cpp +++ b/test/thread/semaphore.cpp @@ -1,5 +1,5 @@ -#include "job/semaphore.hpp" -#include "job/flag.hpp" +#include "thread/semaphore.hpp" +#include "thread/flag.hpp" #include "tap.hpp" #include @@ -8,7 +8,7 @@ /////////////////////////////////////////////////////////////////////////////// void -fight (util::job::semaphore &sem, const int iterations) +fight (util::thread::semaphore &sem, const int iterations) { for (int i = 0; i < iterations; ++i) std::lock_guard {sem}; @@ -22,14 +22,14 @@ main () util::TAP::logger tap; { - util::job::semaphore sem (0); + util::thread::semaphore sem (0); tap.expect_eq (sem.value (), 0, "initialisation is respected"); tap.expect_eq (sem.unlock (), 1, "bare release increments without blocking"); tap.expect_eq (sem.lock (), 0, "bare acquire decrements without blocking"); } { - util::job::semaphore sem (1); + util::thread::semaphore sem (1); std::atomic test = 0; std::thread t ([&] () { @@ -53,7 +53,7 @@ main () constexpr int iterations = 1 << 16; std::vector threads; - util::job::semaphore sem (0); + util::thread::semaphore sem (0); for (unsigned i = 0; i < parallelism; ++i) threads.emplace_back (fight, std::ref (sem), iterations); diff --git a/test/job/spinlock.cpp b/test/thread/spinlock.cpp similarity index 89% rename from test/job/spinlock.cpp rename to test/thread/spinlock.cpp index af26e263..f953bb94 100644 --- a/test/job/spinlock.cpp +++ b/test/thread/spinlock.cpp @@ -14,8 +14,8 @@ * Copyright 2018 Danny Robson */ -#include "job/flag.hpp" -#include "job/spinlock.hpp" +#include "thread/flag.hpp" +#include "thread/spinlock.hpp" #include "tap.hpp" #include @@ -24,7 +24,7 @@ /////////////////////////////////////////////////////////////////////////////// void -fight (util::job::flag &start, util::job::spinlock &l, int iterations) +fight (util::thread::flag &start, util::thread::spinlock &l, int iterations) { start.wait (); for (int count = 0; count < iterations; ++count) @@ -38,7 +38,7 @@ main () { util::TAP::logger tap; - util::job::spinlock l; + util::thread::spinlock l; l.lock (); tap.expect (true, "locked without contention"); @@ -49,7 +49,7 @@ main () tap.skip ("n-way fight"); } else { constexpr int iterations = 1 << 12; - util::job::flag start_flag; + util::thread::flag start_flag; std::vector contestants; for (unsigned i = 0; i < std::thread::hardware_concurrency (); ++i) diff --git a/test/job/ticketlock.cpp b/test/thread/ticketlock.cpp similarity index 92% rename from test/job/ticketlock.cpp rename to test/thread/ticketlock.cpp index 5c6fd6f1..037b014a 100644 --- a/test/job/ticketlock.cpp +++ b/test/thread/ticketlock.cpp @@ -14,8 +14,8 @@ * Copyright 2018 Danny Robson */ -#include "job/flag.hpp" -#include "job/ticketlock.hpp" +#include "thread/flag.hpp" +#include "thread/ticketlock.hpp" #include "tap.hpp" #include @@ -28,7 +28,7 @@ using ffs_t = std::chrono::high_resolution_clock; //----------------------------------------------------------------------------- void -fight (util::job::flag &start, util::job::ticketlock &l, int total, ffs_t::time_point &finish) { +fight (util::thread::flag &start, util::thread::ticketlock &l, int total, ffs_t::time_point &finish) { start.wait (); for (int count = 0; count < total; ++count) @@ -45,7 +45,7 @@ main () { util::TAP::logger tap; - util::job::ticketlock l; + util::thread::ticketlock l; l.lock (); tap.expect (true, "locked without contention"); @@ -67,7 +67,7 @@ main () // there's liable to be a lot of noise in the measurement with a test // that runs in a short enough time period. constexpr int iterations = 1 << 16; - util::job::flag start_flag; + util::thread::flag start_flag; ffs_t::time_point a_finish, b_finish; diff --git a/job/event.hpp b/thread/event.hpp similarity index 94% rename from job/event.hpp rename to thread/event.hpp index a88d6465..13799353 100644 --- a/job/event.hpp +++ b/thread/event.hpp @@ -14,12 +14,12 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_EVENT_HPP -#define CRUFT_UTIL_JOB_EVENT_HPP +#ifndef CRUFT_UTIL_THREAD_EVENT_HPP +#define CRUFT_UTIL_THREAD_EVENT_HPP #include -namespace util::job { +namespace util::thread { /// a reusable synchronisation object that allows threads to wait until /// notify is called. /// diff --git a/job/event_linux.cpp b/thread/event_linux.cpp similarity index 98% rename from job/event_linux.cpp rename to thread/event_linux.cpp index 3e9447a9..64b832f6 100644 --- a/job/event_linux.cpp +++ b/thread/event_linux.cpp @@ -24,7 +24,7 @@ #include #include -using util::job::event; +using util::thread::event; /////////////////////////////////////////////////////////////////////////////// diff --git a/job/event_win32.cpp b/thread/event_win32.cpp similarity index 100% rename from job/event_win32.cpp rename to thread/event_win32.cpp diff --git a/job/flag.hpp b/thread/flag.hpp similarity index 90% rename from job/flag.hpp rename to thread/flag.hpp index f0a6921a..8320b505 100644 --- a/job/flag.hpp +++ b/thread/flag.hpp @@ -14,12 +14,12 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_FLAG_HPP -#define CRUFT_UTIL_JOB_FLAG_HPP +#ifndef CRUFT_UTIL_THREAD_FLAG_HPP +#define CRUFT_UTIL_THREAD_FLAG_HPP #include -namespace util::job { +namespace util::thread { /// a fire-once event that users can wait upon. if already fired then /// waiting will be a noop. class flag { diff --git a/job/flag_linux.cpp b/thread/flag_linux.cpp similarity index 98% rename from job/flag_linux.cpp rename to thread/flag_linux.cpp index 0b4a9303..21700bda 100644 --- a/job/flag_linux.cpp +++ b/thread/flag_linux.cpp @@ -24,7 +24,7 @@ #include #include -using util::job::flag; +using util::thread::flag; /////////////////////////////////////////////////////////////////////////////// diff --git a/job/flag_win32.cpp b/thread/flag_win32.cpp similarity index 100% rename from job/flag_win32.cpp rename to thread/flag_win32.cpp diff --git a/job/monitor.cpp b/thread/monitor.cpp similarity index 95% rename from job/monitor.cpp rename to thread/monitor.cpp index af925686..cbdaf7d2 100644 --- a/job/monitor.cpp +++ b/thread/monitor.cpp @@ -16,4 +16,4 @@ #include "monitor.hpp" -using util::job::monitor; +using util::thread::monitor; diff --git a/job/monitor.hpp b/thread/monitor.hpp similarity index 93% rename from job/monitor.hpp rename to thread/monitor.hpp index 6d8faf58..98095ca7 100644 --- a/job/monitor.hpp +++ b/thread/monitor.hpp @@ -14,15 +14,15 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_MONITOR_HPP -#define CRUFT_UTIL_JOB_MONITOR_HPP +#ifndef CRUFT_UTIL_THREAD_MONITOR_HPP +#define CRUFT_UTIL_THREAD_MONITOR_HPP #include #include #include -namespace util::job { +namespace util::thread { template class monitor { public: diff --git a/job/semaphore.hpp b/thread/semaphore.hpp similarity index 92% rename from job/semaphore.hpp rename to thread/semaphore.hpp index 8c320810..545661fe 100644 --- a/job/semaphore.hpp +++ b/thread/semaphore.hpp @@ -14,12 +14,12 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_SEMAPHORE_HPP -#define CRUFT_UTIL_JOB_SEMAPHORE_HPP +#ifndef CRUFT_UTIL_THREAD_SEMAPHORE_HPP +#define CRUFT_UTIL_THREAD_SEMAPHORE_HPP #include -namespace util::job { +namespace util::thread { /// Explicitly does not conform to BasicLockable. class semaphore { public: diff --git a/job/semaphore_linux.cpp b/thread/semaphore_linux.cpp similarity index 99% rename from job/semaphore_linux.cpp rename to thread/semaphore_linux.cpp index 33b18199..f434fccc 100644 --- a/job/semaphore_linux.cpp +++ b/thread/semaphore_linux.cpp @@ -25,7 +25,7 @@ #include #include -using util::job::semaphore; +using util::thread::semaphore; /////////////////////////////////////////////////////////////////////////////// diff --git a/job/semaphore_win32.cpp b/thread/semaphore_win32.cpp similarity index 100% rename from job/semaphore_win32.cpp rename to thread/semaphore_win32.cpp diff --git a/job/spinlock.cpp b/thread/spinlock.cpp similarity index 97% rename from job/spinlock.cpp rename to thread/spinlock.cpp index dc56368c..71c88540 100644 --- a/job/spinlock.cpp +++ b/thread/spinlock.cpp @@ -18,7 +18,7 @@ #include "../debug.hpp" -using util::job::spinlock; +using util::thread::spinlock; /////////////////////////////////////////////////////////////////////////////// diff --git a/job/spinlock.hpp b/thread/spinlock.hpp similarity index 93% rename from job/spinlock.hpp rename to thread/spinlock.hpp index 5462c116..8c301cd7 100644 --- a/job/spinlock.hpp +++ b/thread/spinlock.hpp @@ -14,12 +14,12 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_SPINLOCK_HPP -#define CRUFT_UTIL_JOB_SPINLOCK_HPP +#ifndef CRUFT_UTIL_THREAD_SPINLOCK_HPP +#define CRUFT_UTIL_THREAD_SPINLOCK_HPP #include -namespace util::job { +namespace util::thread { /// a CPU intensive, but lower latency, lock. /// /// std::atomic_flag seems like it might have been a good option on which diff --git a/job/ticketlock.cpp b/thread/ticketlock.cpp similarity index 97% rename from job/ticketlock.cpp rename to thread/ticketlock.cpp index 8d2a9a5e..687c468c 100644 --- a/job/ticketlock.cpp +++ b/thread/ticketlock.cpp @@ -16,7 +16,7 @@ #include "ticketlock.hpp" -using util::job::ticketlock; +using util::thread::ticketlock; /////////////////////////////////////////////////////////////////////////////// diff --git a/job/ticketlock.hpp b/thread/ticketlock.hpp similarity index 88% rename from job/ticketlock.hpp rename to thread/ticketlock.hpp index 6a76197f..2283b93b 100644 --- a/job/ticketlock.hpp +++ b/thread/ticketlock.hpp @@ -14,12 +14,12 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_JOB_TICKETLOCK_HPP -#define CRUFT_UTIL_JOB_TICKETLOCK_HPP +#ifndef CRUFT_UTIL_THREAD_TICKETLOCK_HPP +#define CRUFT_UTIL_THREAD_TICKETLOCK_HPP #include -namespace util::job { +namespace util::thread { class ticketlock { public: ticketlock ();