1//
2// Thread_STD.h
3//
4// Library: Foundation
5// Package: Threading
6// Module: Thread
7//
8// Definition of the ThreadImpl class for STD Threads.
9//
10// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_Thread_STD_INCLUDED
18#define Foundation_Thread_STD_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/Runnable.h"
23#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
24#include "Poco/SignalHandler.h"
25#endif
26#include "Poco/Event.h"
27#include "Poco/RefCountedObject.h"
28#include "Poco/AutoPtr.h"
29#include "Poco/SharedPtr.h"
30#include <thread>
31
32
33#ifdef __APPLE__
34#if (__cplusplus < 201103L)
35#define thread_local __thread
36#endif
37#elif defined(_MSC_VER)
38#include "Poco/UnWindows.h"
39#endif
40
41
42namespace Poco {
43
44
45class Foundation_API ThreadImpl
46{
47public:
48
49 typedef std::thread::native_handle_type TIDImpl;
50
51 typedef void (*Callable)(void*);
52
53 enum Priority
54 {
55 PRIO_LOWEST_IMPL,
56 PRIO_LOW_IMPL,
57 PRIO_NORMAL_IMPL,
58 PRIO_HIGH_IMPL,
59 PRIO_HIGHEST_IMPL
60 };
61
62 enum Policy
63 {
64 POLICY_DEFAULT_IMPL = 0
65 };
66
67 ThreadImpl();
68 ~ThreadImpl();
69
70 TIDImpl tidImpl() const;
71 void setPriorityImpl(int prio);
72 int getPriorityImpl() const;
73 void setOSPriorityImpl(int prio, int policy = 0);
74 int getOSPriorityImpl() const;
75 static int getMinOSPriorityImpl(int policy);
76 static int getMaxOSPriorityImpl(int policy);
77 void setStackSizeImpl(int size);
78 int getStackSizeImpl() const;
79 void setAffinityImpl(int cpu);
80 int getAffinityImpl() const;
81 void startImpl(SharedPtr<Runnable> pTarget);
82 void joinImpl();
83 bool joinImpl(long milliseconds);
84 bool isRunningImpl() const;
85 static void sleepImpl(long milliseconds);
86 static void yieldImpl();
87 static ThreadImpl* currentImpl();
88 static TIDImpl currentTidImpl();
89
90protected:
91 static void* runnableEntry(void* pThread);
92
93private:
94 class CurrentThreadHolder
95 {
96 public:
97 CurrentThreadHolder()
98 {
99 }
100 ~CurrentThreadHolder()
101 {
102 }
103 ThreadImpl* get() const
104 {
105 return _pThread;
106 }
107 void set(ThreadImpl* pThread)
108 {
109 _pThread = pThread;
110 }
111
112 private:
113 static thread_local ThreadImpl* _pThread;
114 };
115
116 struct ThreadData: public RefCountedObject
117 {
118 ThreadData():
119 thread(),
120 prio(PRIO_NORMAL_IMPL),
121 policy(0),
122 task(0),
123 done(Event::EVENT_MANUALRESET),
124 stackSize(POCO_THREAD_STACK_SIZE),
125 cpu(-1),
126 started(false),
127 joined(false)
128 {
129 }
130
131 SharedPtr<Runnable> pRunnableTarget;
132 std::unique_ptr<std::thread> thread;
133 TIDImpl tid;
134 int prio;
135 int osPrio;
136 int policy;
137 int task;
138 Event done;
139 std::size_t stackSize;
140 int cpu;
141 bool started;
142 bool joined;
143 };
144
145 AutoPtr<ThreadData> _pData;
146
147 static CurrentThreadHolder _currentThreadHolder;
148
149#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
150 SignalHandler::JumpBufferVec _jumpBufferVec;
151 friend class SignalHandler;
152#endif
153};
154
155
156//
157// inlines
158//
159inline int ThreadImpl::getPriorityImpl() const
160{
161 return _pData->prio;
162}
163
164
165inline int ThreadImpl::getOSPriorityImpl() const
166{
167 return _pData->osPrio;
168}
169
170
171inline bool ThreadImpl::isRunningImpl() const
172{
173 return !_pData->pRunnableTarget.isNull();
174}
175
176
177inline void ThreadImpl::yieldImpl()
178{
179 std::this_thread::yield();
180}
181
182
183inline int ThreadImpl::getStackSizeImpl() const
184{
185 return static_cast<int>(_pData->stackSize);
186}
187
188
189inline ThreadImpl::TIDImpl ThreadImpl::tidImpl() const
190{
191 return _pData->tid;
192}
193
194
195} // namespace Poco
196
197
198#endif // Foundation_Thread_STD_INCLUDED
199