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