38 typedef int ut_thread_id_t;
39 #elif defined(USE_PTHREADS)
42 typedef pthread_t ut_thread_id_t;
44 #error Unsupported Platform for UT_Thread
47 #define UT_INVALID_THREAD_ID ((ut_thread_id_t)0)
50 #define UT_THREAD_DEFAULT_STACK_SIZE (8U*1024U*1024U)
51 #define UT_THREAD_SMALL_STACK_SIZE (1U*1024U*1024U)
53 typedef void *(*UTthreadFunc)(
void*);
96 static UT_Thread *allocThread(SpinMode spin_mode,
bool uses_tbb=
true);
98 static int getNumProcessors();
101 static int activeThreadCount();
107 static void resetNumProcessors();
110 #if defined(USE_PTHREADS)
111 static ut_thread_id_t getMyThreadId() {
return pthread_self(); }
113 static ut_thread_id_t getMyThreadId();
116 static ut_thread_id_t getMainThreadId();
117 static int getMainSequentialThreadId();
120 return getMyThreadId() == getMainThreadId();
126 static bool isUTThreadCurrent();
133 static bool isThreadingEnabled();
139 static bool setThreadingEnabled(
bool will_be_enabled);
145 : myPreviouslyEnabled(setThreadingEnabled(false))
149 if (myPreviouslyEnabled)
150 setThreadingEnabled(
true);
153 const bool myPreviouslyEnabled;
159 for(
uint i = 0; i < cycles; i++)
160 #
if defined(USE_PTHREADS)
162 __asm__ __volatile__(
"yield;");
164 __asm__ __volatile__(
"pause;");
171 #if defined(USE_PTHREADS)
172 static inline void yield(
bool higher_only=
false)
183 struct timespec ts = {0,100};
188 static void yield(
bool higher_only=
false);
204 static void configureMaxThreads(
int maxthreads = 0);
211 static void configureThreadStackSize(
int stacksize);
214 static bool isMaxThreadsConfigured();
220 static bool minimizeThisThreadPriority();
228 void setQoS(qos_class_t qos);
233 qos_class_t getQoS()
const;
257 virtual void threadStarted();
261 virtual void threadEnded();
269 virtual int isResourceHog()
const;
272 virtual State getState();
274 virtual void waitForState(
State desired) = 0;
275 virtual void setSpinMode(
SpinMode spin_mode);
278 virtual void killThread() = 0;
282 virtual int suspendThread() = 0;
283 virtual int restartThread() = 0;
286 {
return waitThread(0); }
289 virtual int64 getMemoryUsage(
bool inclusive)
const = 0;
296 virtual int waitThread(
int block=1) = 0;
299 virtual int isValid();
304 static void *threadWrapper(
void *
data);
307 virtual void setState(
State state) = 0;
327 static void onExit_();
342 int stacksize)
override;
352 int64 mem = inclusive ?
sizeof(*this) : 0;
366 UT_ThreadSet(
int nthreads=-1,
int null_thread_if_1_cpu = 0);
378 myUserData = user_data_array;
379 myUserDataInc = structlen;
383 myUserData = user_data;
389 int wait(
int block=1);
396 return myThreads[which];
437 UT_Thread *nextThread(
int *thread_index =0,
438 AssignmentStyle style = BLOCKING);
442 int wait(
int block = 1);
446 void cleanup(
int kill = 0);
452 return myThreads[
index];
456 void addThreads(
int thread_count);
468 static const uint cycles_for_noop = 4;
469 static const uint cycles_for_pause = cycles_for_noop * 4;
470 static const uint cycles_for_yield_higher = cycles_for_pause * 2;
471 static const uint cycles_for_yield_all = cycles_for_yield_higher * 2;
476 if (myCycles > cycles_for_yield_all)
483 if (myCycles <= cycles_for_noop)
487 else if (myCycles <= cycles_for_pause)
491 else if (myCycles <= cycles_for_yield_higher)
495 myCycles += (myCycles+1)>>1;
527 template <
bool UseTBB = true>
532 template <
typename Func,
typename... Args>
535 WrapFunctor<Func, Args...>(std::forward<Func>(
func)),
536 std::forward<Args>(
args)...)
546 template <
typename Func,
typename... Args>
550 WrapFunctor(Func&&
func)
551 : myFunc(std::move(
func))
555 decltype(
auto) operator()(Args&&...
args)
const
558 return myFunc(std::forward<Args>(args)...);
565 template <
bool UseTBB = true>
576 myThreads.setSize(nthreads);
584 return myThreads(idx);
588 return myThreads(idx);
592 return myThreads[idx];
596 return myThreads[idx];
600 for (
auto&&
t : myThreads)
613 for (
auto&&
t : myThreads)
int getNumThreads() const
void setUserData(void *user_data)
#define SYS_DEPRECATED(__V__)
int64 getMemoryUsage(bool inclusive) const override
NOTE: This level doesn't own any data apart from itself.
The subsystem to initialize and cleanup UT.
virtual int restartThread()=0
void *(* UTthreadFunc)(void *)
UT_StdThreadGroup & operator=(const UT_StdThreadGroup &)=delete
UT_StdThread & operator=(const UT_StdThread &)=delete
UT_Thread * operator[](int index)
thread_t & operator[](int idx)
UT_StdThread(Func &&func, Args &&...args)
UT_Thread * operator[](int which)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
virtual void setState(State state)=0
static int getNumProcessors()
virtual void waitForState(State desired)=0
void setFunc(UTthreadFunc func)
virtual bool startThread(UTthreadFunc func, void *data, int stacksize)=0
UT_Thread & operator=(const UT_Thread &)=delete
virtual void killThread()=0
UT_StdThreadGroup(int nthreads=-1)
const UT_TaskScope * myTaskScope
virtual int suspendThread()=0
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
static int isMainThread()
~ThreadingDisabledScope()
LeafData & operator=(const LeafData &)=delete
const thread_t & operator[](int idx) const
static void pause(uint cycles)
**If you just want to fire and args
static void yield(bool higher_only=false)
virtual int waitThread(int block=1)=0
bool joinable(int idx) const
void setUserData(void *user_data_array, size_t structlen)