neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1// thread.hpp v3.0
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <stdexcept>
40#include <memory>
41#include <thread>
42#include <atomic>
43#include <functional>
48
49namespace neolib
50{
51 class NEOLIB_EXPORT thread : public i_thread, public waitable, private noncopyable
52 {
53 // types
54 public:
55 typedef std::thread::id id_type;
56 typedef std::thread thread_object_type;
57 // exceptions
58 public:
59 struct thread_not_started : public std::logic_error { thread_not_started() : std::logic_error("neolib::thread::thread_not_started") {} };
60 struct thread_already_started : public std::logic_error { thread_already_started() : std::logic_error("neolib::thread::thread_already_started") {} };
61 struct cannot_wait_on_self : public std::logic_error { cannot_wait_on_self() : std::logic_error("neolib::thread::cannot_wait_on_self") {} };
62 struct no_thread_object : public std::logic_error { no_thread_object() : std::logic_error("neolib::thread::no_thread_object") {} };
63 struct not_in_thread : public std::logic_error { not_in_thread() : std::logic_error("neolib::thread::not_in_thread") {} };
64 private:
65 typedef std::unique_ptr<thread_object_type> thread_object_pointer;
66 protected:
67 struct cancellation {};
68 // construction
69 public:
70 thread(const std::string& aName = "", bool aAttachToCurrentThread = false);
71 thread(std::function<void()> aExecFunction, const std::string& aName = "");
72 virtual ~thread();
73 // operations
74 public:
75 const std::string& name() const noexcept override;
76 bool using_existing_thread() const noexcept;
77 void start();
78 void cancel();
79 void abort(bool aWait = true) override;
80 void wait() const;
81 wait_result wait(const waitable_event_list& aEventList) const;
82 bool msg_wait(const i_message_queue& aMessageQueue) const;
83 wait_result msg_wait(const i_message_queue& aMessageQueue, const waitable_event_list& aEventList) const;
84 void block();
85 void unblock();
86 thread_state state() const noexcept override;
87 bool started() const noexcept;
88 bool running() const noexcept;
89 bool finished() const noexcept override;
90 bool aborted() const noexcept;
91 bool cancelled() const noexcept;
92 bool error() const noexcept;
93 id_type id() const noexcept;
94 bool in() const;
95 bool blocked() const noexcept;
96 bool has_thread_object() const noexcept;
97 thread_object_type& thread_object() const;
98 static void sleep(const std::chrono::duration<double, std::milli>& aDuration);
99 static void yield();
100 static uint64_t elapsed_ms() noexcept;
101 static uint64_t elapsed_us() noexcept;
102 static uint64_t elapsed_ns() noexcept;
103 static uint64_t program_elapsed_ms() noexcept;
104 static uint64_t program_elapsed_us() noexcept;
105 static uint64_t program_elapsed_ns() noexcept;
106 // implementation
107 private:
108 // from waitable
109 bool waitable_ready() const noexcept override;
110 // own
111 void exec_preamble() override;
112 void exec(yield_type aYieldType = yield_type::NoYield) override;
113 void entry_point();
114 // attributes
115 private:
116 mutable std::recursive_mutex iMutex;
117 const std::string iName;
118 bool iUsingExistingThread;
119 std::optional<std::function<void()>> iExecFunction;
120 std::atomic<thread_state> iState;
121 thread_object_pointer iThreadObject;
122 id_type iId;
123 std::atomic<std::size_t> iBlockedCount;
124 };
125}
const std::string & name() const noexcept override
thread(std::function< void()> aExecFunction, const std::string &aName="")
thread(const std::string &aName="", bool aAttachToCurrentThread=false)
std::thread thread_object_type
Definition thread.hpp:56
virtual ~thread()
std::thread::id id_type
Definition thread.hpp:55
std::variant< wait_result_event, wait_result_message, wait_result_waitable > wait_result
Definition plf_hive.h:79