thread: rename thread primitives from job namespace

This commit is contained in:
Danny Robson 2018-03-23 14:10:20 +11:00
parent 6cd2bef17d
commit 36f4ba035c
24 changed files with 85 additions and 85 deletions

View File

@ -87,9 +87,9 @@ list (
##----------------------------------------------------------------------------- ##-----------------------------------------------------------------------------
if (LINUX) if (LINUX)
list (APPEND UTIL_FILES list (APPEND UTIL_FILES
job/event_linux.cpp thread/event_linux.cpp
job/flag_linux.cpp thread/flag_linux.cpp
job/semaphore_linux.cpp thread/semaphore_linux.cpp
) )
endif () endif ()
@ -125,9 +125,9 @@ if (WINDOWS)
exe_win32.cpp exe_win32.cpp
io_win32.cpp io_win32.cpp
io_win32.hpp io_win32.hpp
job/event_win32.cpp thread/event_win32.cpp
job/flag_win32.cpp thread/flag_win32.cpp
job/semaphore_win32.cpp thread/semaphore_win32.cpp
library_win32.cpp library_win32.cpp
library_win32.hpp library_win32.hpp
time_win32.cpp time_win32.cpp
@ -277,17 +277,8 @@ list (
io.hpp io.hpp
iterator.hpp iterator.hpp
job/fwd.hpp job/fwd.hpp
job/event.hpp
job/flag.hpp
job/monitor.cpp
job/monitor.hpp
job/queue.cpp job/queue.cpp
job/queue.hpp job/queue.hpp
job/semaphore.hpp
job/ticketlock.cpp
job/ticketlock.hpp
job/spinlock.cpp
job/spinlock.hpp
json/fwd.hpp json/fwd.hpp
json/except.cpp json/except.cpp
json/except.hpp json/except.hpp
@ -377,6 +368,15 @@ list (
tap.hpp tap.hpp
term.cpp term.cpp
term.hpp 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.cpp
time.hpp time.hpp
time/parse.hpp time/parse.hpp
@ -500,13 +500,7 @@ if (TESTS)
hton hton
introspection introspection
iterator iterator
job/event
job/flag
job/monitor
job/queue job/queue
job/semaphore
job/spinlock
job/ticketlock
json_types json_types
json2/event json2/event
maths maths
@ -532,6 +526,12 @@ if (TESTS)
string string
stringid stringid
strongdef strongdef
thread/event
thread/flag
thread/monitor
thread/semaphore
thread/spinlock
thread/ticketlock
time/8601 time/8601
traits traits
tuple tuple

View File

@ -19,10 +19,10 @@
#include "../pool.hpp" #include "../pool.hpp"
#include "ticketlock.hpp" #include "../thread/ticketlock.hpp"
#include "semaphore.hpp" #include "../thread/semaphore.hpp"
#include "flag.hpp" #include "../thread/flag.hpp"
#include "monitor.hpp" #include "../thread/monitor.hpp"
#include "../parallel/queue.hpp" #include "../parallel/queue.hpp"
@ -185,8 +185,8 @@ namespace util::job {
std::array<char,64> data; std::array<char,64> data;
std::function<void(task&)> function; std::function<void(task&)> function;
semaphore references = 0; thread::semaphore references = 0;
flag done; thread::flag done;
}; };
private: private:
@ -197,9 +197,9 @@ namespace util::job {
std::atomic<int> m_running = 0; std::atomic<int> m_running = 0;
struct { struct {
monitor< thread::monitor<
std::deque<task*>, std::deque<task*>,
ticketlock thread::ticketlock
> pending; > pending;
pool<task> store; pool<task> store;
@ -207,11 +207,11 @@ namespace util::job {
std::vector<task*> notified; std::vector<task*> notified;
} m_tasks; } m_tasks;
semaphore m_pending; thread::semaphore m_pending;
std::vector<std::thread> m_threads; std::vector<std::thread> m_threads;
semaphore m_doomed; thread::semaphore m_doomed;
std::thread m_reaper; std::thread m_reaper;
}; };
} }

View File

@ -1,6 +1,6 @@
#include "parallel/queue.hpp" #include "parallel/queue.hpp"
#include "job/flag.hpp" #include "thread/flag.hpp"
#include "job/semaphore.hpp" #include "thread/semaphore.hpp"
#include "debug.hpp" #include "debug.hpp"
#include "tap.hpp" #include "tap.hpp"
@ -12,7 +12,7 @@ static constexpr uint32_t slots = 4;
static constexpr int parallelism = 8; static constexpr int parallelism = 8;
static constexpr int chunk_size = 1<<12; static constexpr int chunk_size = 1<<12;
util::job::flag start; util::thread::flag start;
using queue_t = util::parallel::queue<int,slots>; using queue_t = util::parallel::queue<int,slots>;

View File

@ -1,4 +1,4 @@
#include "job/event.hpp" #include "thread/event.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <atomic> #include <atomic>
@ -17,7 +17,7 @@ main ()
// the test isn't 100% deterministic (because we're attempting to create // the test isn't 100% deterministic (because we're attempting to create
// specific timings by just waiting). but it's a decent first check. // specific timings by just waiting). but it's a decent first check.
std::atomic<int> val = 0; std::atomic<int> val = 0;
util::job::event a; util::thread::event a;
std::thread t ([&] () { std::thread t ([&] () {
a.wait (); a.wait ();

View File

@ -1,4 +1,4 @@
#include "job/flag.hpp" #include "thread/flag.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <thread> #include <thread>
@ -9,7 +9,7 @@ main ()
{ {
util::TAP::logger tap; util::TAP::logger tap;
util::job::flag f; util::thread::flag f;
std::atomic<int> value = 0; std::atomic<int> value = 0;
std::thread t1 ([&] () { std::thread t1 ([&] () {
@ -45,7 +45,7 @@ main ()
constexpr int parallelism = 16; constexpr int parallelism = 16;
std::vector< std::vector<
std::array<util::job::flag,parallelism> std::array<util::thread::flag,parallelism>
> flags (iterations); > flags (iterations);
const auto func = [&flags] (const int idx) { const auto func = [&flags] (const int idx) {

View File

@ -15,9 +15,9 @@
*/ */
#include "tap.hpp" #include "tap.hpp"
#include "job/monitor.hpp" #include "thread/monitor.hpp"
#include "job/event.hpp" #include "thread/event.hpp"
#include "job/semaphore.hpp" #include "thread/semaphore.hpp"
#include <atomic> #include <atomic>
#include <thread> #include <thread>
@ -40,8 +40,8 @@ struct foo {
} }
std::atomic<int> value = 0; std::atomic<int> value = 0;
util::job::semaphore enter; util::thread::semaphore enter;
util::job::semaphore leave; util::thread::semaphore leave;
}; };
@ -51,7 +51,7 @@ main ()
{ {
util::TAP::logger tap; util::TAP::logger tap;
util::job::monitor<foo> obj; util::thread::monitor<foo> obj;
const auto &value = obj->value; const auto &value = obj->value;
auto &enter = obj->enter; auto &enter = obj->enter;
auto &leave = obj->leave; auto &leave = obj->leave;

View File

@ -1,5 +1,5 @@
#include "job/semaphore.hpp" #include "thread/semaphore.hpp"
#include "job/flag.hpp" #include "thread/flag.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <thread> #include <thread>
@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void
fight (util::job::semaphore &sem, const int iterations) fight (util::thread::semaphore &sem, const int iterations)
{ {
for (int i = 0; i < iterations; ++i) for (int i = 0; i < iterations; ++i)
std::lock_guard {sem}; std::lock_guard {sem};
@ -22,14 +22,14 @@ main ()
util::TAP::logger tap; 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.value (), 0, "initialisation is respected");
tap.expect_eq (sem.unlock (), 1, "bare release increments without blocking"); tap.expect_eq (sem.unlock (), 1, "bare release increments without blocking");
tap.expect_eq (sem.lock (), 0, "bare acquire decrements 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<int> test = 0; std::atomic<int> test = 0;
std::thread t ([&] () { std::thread t ([&] () {
@ -53,7 +53,7 @@ main ()
constexpr int iterations = 1 << 16; constexpr int iterations = 1 << 16;
std::vector<std::thread> threads; std::vector<std::thread> threads;
util::job::semaphore sem (0); util::thread::semaphore sem (0);
for (unsigned i = 0; i < parallelism; ++i) for (unsigned i = 0; i < parallelism; ++i)
threads.emplace_back (fight, std::ref (sem), iterations); threads.emplace_back (fight, std::ref (sem), iterations);

View File

@ -14,8 +14,8 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#include "job/flag.hpp" #include "thread/flag.hpp"
#include "job/spinlock.hpp" #include "thread/spinlock.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <thread> #include <thread>
@ -24,7 +24,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void 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 (); start.wait ();
for (int count = 0; count < iterations; ++count) for (int count = 0; count < iterations; ++count)
@ -38,7 +38,7 @@ main ()
{ {
util::TAP::logger tap; util::TAP::logger tap;
util::job::spinlock l; util::thread::spinlock l;
l.lock (); l.lock ();
tap.expect (true, "locked without contention"); tap.expect (true, "locked without contention");
@ -49,7 +49,7 @@ main ()
tap.skip ("n-way fight"); tap.skip ("n-way fight");
} else { } else {
constexpr int iterations = 1 << 12; constexpr int iterations = 1 << 12;
util::job::flag start_flag; util::thread::flag start_flag;
std::vector<std::thread> contestants; std::vector<std::thread> contestants;
for (unsigned i = 0; i < std::thread::hardware_concurrency (); ++i) for (unsigned i = 0; i < std::thread::hardware_concurrency (); ++i)

View File

@ -14,8 +14,8 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#include "job/flag.hpp" #include "thread/flag.hpp"
#include "job/ticketlock.hpp" #include "thread/ticketlock.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <thread> #include <thread>
@ -28,7 +28,7 @@ using ffs_t = std::chrono::high_resolution_clock;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void 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 (); start.wait ();
for (int count = 0; count < total; ++count) for (int count = 0; count < total; ++count)
@ -45,7 +45,7 @@ main ()
{ {
util::TAP::logger tap; util::TAP::logger tap;
util::job::ticketlock l; util::thread::ticketlock l;
l.lock (); l.lock ();
tap.expect (true, "locked without contention"); 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 // there's liable to be a lot of noise in the measurement with a test
// that runs in a short enough time period. // that runs in a short enough time period.
constexpr int iterations = 1 << 16; constexpr int iterations = 1 << 16;
util::job::flag start_flag; util::thread::flag start_flag;
ffs_t::time_point a_finish, b_finish; ffs_t::time_point a_finish, b_finish;

View File

@ -14,12 +14,12 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_EVENT_HPP #ifndef CRUFT_UTIL_THREAD_EVENT_HPP
#define CRUFT_UTIL_JOB_EVENT_HPP #define CRUFT_UTIL_THREAD_EVENT_HPP
#include <atomic> #include <atomic>
namespace util::job { namespace util::thread {
/// a reusable synchronisation object that allows threads to wait until /// a reusable synchronisation object that allows threads to wait until
/// notify is called. /// notify is called.
/// ///

View File

@ -24,7 +24,7 @@
#include <cruft/util/posix/except.hpp> #include <cruft/util/posix/except.hpp>
#include <limits> #include <limits>
using util::job::event; using util::thread::event;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -14,12 +14,12 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_FLAG_HPP #ifndef CRUFT_UTIL_THREAD_FLAG_HPP
#define CRUFT_UTIL_JOB_FLAG_HPP #define CRUFT_UTIL_THREAD_FLAG_HPP
#include <atomic> #include <atomic>
namespace util::job { namespace util::thread {
/// a fire-once event that users can wait upon. if already fired then /// a fire-once event that users can wait upon. if already fired then
/// waiting will be a noop. /// waiting will be a noop.
class flag { class flag {

View File

@ -24,7 +24,7 @@
#include <cruft/util/posix/except.hpp> #include <cruft/util/posix/except.hpp>
#include <limits> #include <limits>
using util::job::flag; using util::thread::flag;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -16,4 +16,4 @@
#include "monitor.hpp" #include "monitor.hpp"
using util::job::monitor; using util::thread::monitor;

View File

@ -14,15 +14,15 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_MONITOR_HPP #ifndef CRUFT_UTIL_THREAD_MONITOR_HPP
#define CRUFT_UTIL_JOB_MONITOR_HPP #define CRUFT_UTIL_THREAD_MONITOR_HPP
#include <mutex> #include <mutex>
#include <utility> #include <utility>
#include <functional> #include <functional>
namespace util::job { namespace util::thread {
template <typename ValueT, typename MutexT = std::mutex> template <typename ValueT, typename MutexT = std::mutex>
class monitor { class monitor {
public: public:

View File

@ -14,12 +14,12 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_SEMAPHORE_HPP #ifndef CRUFT_UTIL_THREAD_SEMAPHORE_HPP
#define CRUFT_UTIL_JOB_SEMAPHORE_HPP #define CRUFT_UTIL_THREAD_SEMAPHORE_HPP
#include <atomic> #include <atomic>
namespace util::job { namespace util::thread {
/// Explicitly does not conform to BasicLockable. /// Explicitly does not conform to BasicLockable.
class semaphore { class semaphore {
public: public:

View File

@ -25,7 +25,7 @@
#include <cruft/util/posix/except.hpp> #include <cruft/util/posix/except.hpp>
#include <limits> #include <limits>
using util::job::semaphore; using util::thread::semaphore;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -18,7 +18,7 @@
#include "../debug.hpp" #include "../debug.hpp"
using util::job::spinlock; using util::thread::spinlock;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -14,12 +14,12 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_SPINLOCK_HPP #ifndef CRUFT_UTIL_THREAD_SPINLOCK_HPP
#define CRUFT_UTIL_JOB_SPINLOCK_HPP #define CRUFT_UTIL_THREAD_SPINLOCK_HPP
#include <atomic> #include <atomic>
namespace util::job { namespace util::thread {
/// a CPU intensive, but lower latency, lock. /// a CPU intensive, but lower latency, lock.
/// ///
/// std::atomic_flag seems like it might have been a good option on which /// std::atomic_flag seems like it might have been a good option on which

View File

@ -16,7 +16,7 @@
#include "ticketlock.hpp" #include "ticketlock.hpp"
using util::job::ticketlock; using util::thread::ticketlock;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -14,12 +14,12 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_JOB_TICKETLOCK_HPP #ifndef CRUFT_UTIL_THREAD_TICKETLOCK_HPP
#define CRUFT_UTIL_JOB_TICKETLOCK_HPP #define CRUFT_UTIL_THREAD_TICKETLOCK_HPP
#include <atomic> #include <atomic>
namespace util::job { namespace util::thread {
class ticketlock { class ticketlock {
public: public:
ticketlock (); ticketlock ();