Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:37

0001 // Created on: 2006-03-10
0002 // Created by: data exchange team
0003 // Copyright (c) 2006-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _OSD_Thread_HeaderFile
0017 #define _OSD_Thread_HeaderFile
0018 
0019 #include <Standard.hxx>
0020 #include <Standard_DefineAlloc.hxx>
0021 #include <Standard_Handle.hxx>
0022 
0023 #include <OSD_ThreadFunction.hxx>
0024 #include <OSD_PThread.hxx>
0025 #include <Standard_ThreadId.hxx>
0026 #include <Standard_Boolean.hxx>
0027 
0028 
0029 //! A simple platform-intependent interface to execute
0030 //! and control threads.
0031 class OSD_Thread
0032 {
0033 public:
0034 
0035   DEFINE_STANDARD_ALLOC
0036 
0037   //! Empty constructor
0038   Standard_EXPORT OSD_Thread();
0039 
0040   //! Initialize the tool by the thread function
0041   //!
0042   //! Note: On Windows, you might have to take an address of the thread
0043   //! function explicitly to pass it to this constructor without compiler error
0044   Standard_EXPORT OSD_Thread(const OSD_ThreadFunction& func);
0045 
0046   //! Copy constructor
0047   Standard_EXPORT OSD_Thread(const OSD_Thread& other);
0048 
0049   //! Copy thread handle from other OSD_Thread object.
0050   Standard_EXPORT void Assign (const OSD_Thread& other);
0051 void operator = (const OSD_Thread& other)
0052 {
0053   Assign(other);
0054 }
0055 
0056   //! Destructor. Detaches the thread if it wasn't done already.
0057   Standard_EXPORT ~OSD_Thread();
0058 
0059   Standard_EXPORT void SetPriority (const Standard_Integer thePriority);
0060 
0061   //! Initialize the tool by the thread function.
0062   //! If the current thread handle is not null, nullifies it.
0063   //!
0064   //! Note: On Windows, you might have to take an address of the thread
0065   //! function explicitly to pass it to this method without compiler error
0066   Standard_EXPORT void SetFunction (const OSD_ThreadFunction& func);
0067 
0068   //! Starts a thread with thread function given in constructor,
0069   //! passing the specified input data (as void *) to it.
0070   //! The parameter \a WNTStackSize (on Windows only)
0071   //! specifies size of the stack to be allocated for the thread
0072   //! (by default - the same as for the current executable).
0073   //! Returns True if thread started successfully
0074   Standard_EXPORT Standard_Boolean Run (const Standard_Address data = 0, const Standard_Integer WNTStackSize = 0);
0075 
0076   //! Detaches the execution thread from this Thread object,
0077   //! so that it cannot be waited.
0078   //! Note that mechanics of this operation is different on
0079   //! UNIX/Linux (the thread is put to detached state) and Windows
0080   //! (the handle is closed).
0081   //! However, the purpose is the same: to instruct the system to
0082   //! release all thread data upon its completion.
0083   Standard_EXPORT void Detach();
0084 
0085   //! Waits till the thread finishes execution.
0086   Standard_Boolean Wait()
0087   {
0088     Standard_Address aRes = 0;
0089     return Wait (aRes);
0090   }
0091 
0092   //! Wait till the thread finishes execution.
0093   //! Returns True if wait was successful, False in case of error.
0094   //!
0095   //! If successful and \a result argument is provided, saves the pointer
0096   //! (void*) returned by the thread function in \a result.
0097   //!
0098   //! Note however that it is advisable not to rely upon returned result
0099   //! value, as it is not always the value actually returned by the thread
0100   //! function. In addition, on Windows it is converted via DWORD.
0101   Standard_EXPORT Standard_Boolean Wait (Standard_Address& theResult);
0102 
0103   //! Waits for some time and if the thread is finished,
0104   //! it returns the result.
0105   //! The function returns false if the thread is not finished yet.
0106   Standard_EXPORT Standard_Boolean Wait (const Standard_Integer time, Standard_Address& theResult);
0107 
0108   //! Returns ID of the currently controlled thread ID,
0109   //! or 0 if no thread is run
0110   Standard_EXPORT Standard_ThreadId GetId() const;
0111 
0112   //! Auxiliary: returns ID of the current thread
0113   Standard_EXPORT static Standard_ThreadId Current();
0114 
0115 private:
0116 
0117   OSD_ThreadFunction myFunc;
0118   OSD_PThread myThread;
0119   Standard_ThreadId myThreadId;
0120   Standard_Integer myPriority;
0121 
0122 };
0123 
0124 #endif // _OSD_Thread_HeaderFile