2 * File: platform_thread.h
4 * Author: Lukas Zeller (luz@synthesis.ch)
6 * Platform specific thread object implementation
8 * Copyright (c) 2004-2009 by Synthesis AG (www.synthesis.ch)
10 * 2004-04-15 : luz : created
14 #ifndef PLATFORM_THREAD_H
15 #define PLATFORM_THREAD_H
17 #include <generic_types.h>
29 using namespace sysync;
36 // get id of the running process
37 uIntArch myProcessID();
38 // get id of the running thread
39 uIntArch myThreadID();
42 class TThreadObject; // forward
44 // function executed by thread
45 typedef uInt32 (*TThreadFunc)(TThreadObject *aThreadObject, uInt32 aParam);
48 // wrapper class for thread
51 // creates thread object. Thread ist not started yet, must use launch() for this
53 // destroys the thread object
54 virtual ~TThreadObject();
55 // starts thread (or re-starts it again after termination)
57 TThreadFunc aThreadFunc=NULL, // the function to execute in the thread
58 uIntArch aThreadFuncParam=0, // a parameter to pass to the thread
59 size_t aStackSize=0, // if 0, default stack size is used
60 bool aAutoDispose=false // if true, the thread object will dispose itself when thread has finished running
64 // soft-terminates thread (sets a flag which requests execute() to terminate
65 void terminate(void) { fTerminationRequested=true; };
66 // hard (emergency) terminate (aborts processing on the OS level)
68 // wait for termination of the thread, returns true if so within specified time
69 // negative wait time means waiting infinitely.
70 bool waitfor(sInt32 aMilliSecondsToWait=0);
71 // get exit code of the thread (valid only if thread has already terminated
72 uInt32 exitcode(void) { return fExitCode; };
73 // This method is the thread function itself
74 // - can be derived to create special threads
75 // default behaviour is to call the fThreadFunc with this and fThreadFuncParam
76 virtual uInt32 execute(void);
77 // checks for termination request
78 bool terminationRequested(void) { return fTerminationRequested; };
82 // the thread function
83 TThreadFunc fThreadFunc;
84 uInt32 fThreadFuncParam;
85 // the termination request flag
86 bool fTerminationRequested;
90 // auto disposal of the thread object when thread exits
91 bool fAutoDisposeThreadObj;
96 HANDLE fWinThreadHandle;
100 #if defined LINUX || defined MACOSX
101 // the linux POSIX thread
103 pthread_t fPosixThread;
104 pthread_mutex_t fDoneCondMutex;
105 pthread_cond_t fDoneCond;
106 bool fTerminated; // really finished
111 } // namespace sysync
113 #endif // PLATFORM_THREAD_H