PCL
Thread.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/Thread.h - Released 2024-12-28T16:53:48Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Thread_h
53 #define __PCL_Thread_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/String.h>
60 #include <pcl/UIObject.h>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
71 // ----------------------------------------------------------------------------
72 
99 namespace ThreadPriority
100 {
101  enum value_type
102  {
103  Inherit, // Inherit caller's thread priority.
104  Idle, // Schedule when no other threads are busy.
105  Lowest,
106  Low,
107  Normal, // Standard thread priority
108  High,
109  Highest,
110  TimeCritical, // Schedule as often as possible, taking precedence over any other threads.
111  DefaultMax = Highest
112  };
113 }
114 
115 // ----------------------------------------------------------------------------
116 
125 class PCL_CLASS Thread : public UIObject
126 {
127 public:
128 
132  using priority = ThreadPriority::value_type;
133 
138 
142  ~Thread() override
143  {
144  }
145 
153  void EnsureUnique() override
154  {
155  // Threads are unique objects by definition.
156  }
157 
164  static Thread& Null();
165 
199  void Start( priority = ThreadPriority::Inherit, int processor = -1 );
200 
221 
246  bool SetAffinity( const Array<int>& processors );
247 
272  bool SetAffinity( int processor );
273 
286  void Kill(); // ### Dangerous - Use at your own risk! ###
287 
291  bool IsActive() const;
292 
296  priority Priority() const;
297 
301  void SetPriority( priority );
302 
308  {
309  SetPriority( ThreadPriority::Inherit );
310  }
311 
321  void Wait();
322 
338  bool Wait( unsigned ms );
339 
344  void Sleep( unsigned ms );
345 
346  /*
347  * Returns a reference to the current thread (the thread from which this
348  * member function is invoked), or Thread::Null() if the current thread is
349  * either (a) a thread not being controlled by the PixInsight API, or (b) a
350  * thread that has been created by another module.
351  static Thread& CurrentThread();
352  */
353 
365  static bool IsRootThread();
366 
375 
392  virtual void Run()
393  {
394  // Reimplement this function to provide your thread's functionality.
395  }
396 
414  uint32 Status() const;
415 
433  bool TryGetStatus( uint32& status ) const;
434 
449  void SetStatus( uint32 status );
450 
472  void Abort()
473  {
474  SetStatus( 0x80000000 );
475  }
476 
489  bool IsAborted() const
490  {
491  return (Status() & 0x80000000) != 0;
492  }
493 
505  bool TryIsAborted() const
506  {
507  uint32 status;
508  return TryGetStatus( status ) && (status & 0x80000000) != 0;
509  }
510 
526 
534  bool HasConsoleOutputText() const
535  {
536  return !ConsoleOutputText().IsEmpty();
537  }
538 
546 
560 
620  static int NumberOfThreads( size_type count, size_type overheadLimit = 1u );
621 
672  size_type overheadLimit = 1u,
673  int maxThreads = PCL_MAX_PROCESSORS );
674 
708  int align = 16,
709  size_type overheadLimit = 1u,
710  int maxThreads = PCL_MAX_PROCESSORS );
711 private:
712 
713  int m_processorIndex = -1;
714 
715  Thread( void* h ) : UIObject( h )
716  {
717  }
718 
719  void* CloneHandle() const override;
720 
721 protected:
722 
723  virtual bool IsStealth() const
724  {
725  return false;
726  }
727 
728  friend class ThreadDispatcher;
729 };
730 
731 // ----------------------------------------------------------------------------
732 
740 void PCL_FUNC Sleep( unsigned ms );
741 
742 // ----------------------------------------------------------------------------
743 
744 } // pcl
745 
746 #endif // __PCL_Thread_h
747 
748 // ----------------------------------------------------------------------------
749 // EOF pcl/Thread.h - Released 2024-12-28T16:53:48Z
Generic dynamic array.
Definition: Array.h:100
Unicode (UTF-16) string.
Definition: String.h:8146
Client-side interface to a PixInsight thread.
Definition: Thread.h:126
String ConsoleOutputText() const
bool IsAborted() const
Definition: Thread.h:489
static Array< size_type > OptimalThreadLoadsAligned(size_type count, int align=16, size_type overheadLimit=1u, int maxThreads=PCL_MAX_PROCESSORS)
uint32 Status() const
void ResetPriority()
Definition: Thread.h:307
static int NumberOfThreads(size_type count, size_type overheadLimit=1u)
bool TryIsAborted() const
Definition: Thread.h:505
void SetPriority(priority)
bool TryGetStatus(uint32 &status) const
bool IsActive() const
void FlushConsoleOutputText()
void ClearConsoleOutputText()
virtual void Run()
Definition: Thread.h:392
bool Wait(unsigned ms)
void EnsureUnique() override
Definition: Thread.h:153
static Thread & Null()
Array< int > Affinity() const
void Start(priority=ThreadPriority::Inherit, int processor=-1)
void SetStatus(uint32 status)
static bool IsRootThread()
priority Priority() const
bool HasConsoleOutputText() const
Definition: Thread.h:534
bool SetAffinity(int processor)
void Sleep(unsigned ms)
void Abort()
Definition: Thread.h:472
~Thread() override
Definition: Thread.h:142
static int NumberOfRunningThreads()
static Array< size_type > OptimalThreadLoads(size_type count, size_type overheadLimit=1u, int maxThreads=PCL_MAX_PROCESSORS)
bool SetAffinity(const Array< int > &processors)
Root base class for all user interface objects.
Definition: UIObject.h:95
unsigned int uint32
Definition: Defs.h:666
size_t size_type
Definition: Defs.h:609
void PCL_FUNC Sleep(unsigned ms)
PCL root namespace.
Definition: AbstractImage.h:77