Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
Thread.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * Thread.h
17 * A thread object.
18 * Copyright (C) 2010 Simon Newton
19 */
20
21#ifndef INCLUDE_OLA_THREAD_THREAD_H_
22#define INCLUDE_OLA_THREAD_THREAD_H_
23
24#ifdef _WIN32
25// On MinGW, pthread.h pulls in Windows.h, which in turn pollutes the global
26// namespace. We define VC_EXTRALEAN and WIN32_LEAN_AND_MEAN to reduce this.
27#define VC_EXTRALEAN
28#define WIN32_LEAN_AND_MEAN
29#endif // _WIN32
30#include <pthread.h>
31#include <ola/base/Macro.h>
32#include <ola/thread/Mutex.h>
33
34#include <string>
35
36#if defined(_WIN32) && defined(__GNUC__)
37inline std::ostream& operator<<(std::ostream &stream,
38 const ptw32_handle_t &handle) {
39 stream << handle.p;
40 return stream;
41}
42#endif // defined(_WIN32) && defined(__GNUC__)
43
44namespace ola {
45namespace thread {
46
47typedef pthread_t ThreadId;
48
52class Thread {
53 public:
60 struct Options {
61 public:
65 std::string name;
66
73 int policy;
74
81
89
94 explicit Options(const std::string &name = "");
95 };
96
101 explicit Thread(const Options &options = Options());
102
106 virtual ~Thread() {}
107
115 virtual bool Start();
116
124 virtual bool FastStart();
125
131 virtual bool Join(void *ptr = NULL);
132
140 bool IsRunning();
141
146 ThreadId Id() const { return m_thread_id; }
147
155 std::string Name() const { return m_options.name; }
156
161 void* _InternalRun();
162
167 static inline ThreadId Self() { return pthread_self(); }
168
169 protected:
176 virtual void *Run() = 0;
177
178 private:
179 pthread_t m_thread_id;
180 bool m_running;
181 Options m_options;
182 Mutex m_mutex; // protects m_running
183 ConditionVariable m_condition; // use to wait for the thread to start
184
185 Thread(const Thread &) = delete;
186 const Thread &operator=(const Thread &) = delete;
187};
188} // namespace thread
189} // namespace ola
190#endif // INCLUDE_OLA_THREAD_THREAD_H_
Helper macros.
Definition Mutex.h:83
Definition Mutex.h:41
Definition Thread.h:52
Thread(const Options &options=Options())
Create a new thread with the specified thread options.
Definition Thread.cpp:75
virtual ~Thread()
Destructor.
Definition Thread.h:106
virtual bool FastStart()
Start the thread and return immediately.
Definition Thread.cpp:104
std::string Name() const
Return the thread name.
Definition Thread.h:155
virtual bool Start()
Start the thread and wait for the thread to be running.
Definition Thread.cpp:90
bool IsRunning()
Check if the thread is running.
Definition Thread.cpp:169
virtual void * Run()=0
The entry point for the new thread.
ThreadId Id() const
Return the thread id.
Definition Thread.h:146
virtual bool Join(void *ptr=NULL)
Join this thread.
Definition Thread.cpp:158
static ThreadId Self()
Returns the current thread's id.
Definition Thread.h:167
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
Thread options.
Definition Thread.h:60
int policy
The scheduling policy.
Definition Thread.h:73
int priority
The thread priority.
Definition Thread.h:80
int inheritsched
The scheduling mode, either PTHREAD_EXPLICIT_SCHED or PTHREAD_INHERIT_SCHED.
Definition Thread.h:88
std::string name
The name of the thread.
Definition Thread.h:65
Options(const std::string &name="")
Create new thread Options.
Definition Thread.cpp:62