487eaf0a8d674d0da7622ac3f76eefc1a519570d
[platform/upstream/syncevolution.git] / src / synthesis / src / platform_adapters / platform_thread.h
1 /*
2  *  File:         platform_thread.h
3  *
4  *  Author:                       Lukas Zeller (luz@synthesis.ch)
5  *
6  *  Platform specific thread object implementation
7  *
8  *  Copyright (c) 2004-2009 by Synthesis AG (www.synthesis.ch)
9  *
10  *  2004-04-15 : luz : created
11  *
12  */
13
14 #ifndef PLATFORM_THREAD_H
15 #define PLATFORM_THREAD_H
16
17 #include <generic_types.h>
18
19 #ifdef _WIN32
20   #include <windows.h>
21 #endif
22
23 #ifdef ANDROID
24   #include <pthread.h>
25 #endif
26
27 /*
28 #ifndef _MSC_VER
29   using namespace sysync;
30 #endif
31 */
32
33 namespace sysync {
34
35
36 // get id of the running process
37 uIntArch myProcessID();
38 // get id of the running thread
39 uIntArch myThreadID();
40
41
42 class TThreadObject; // forward
43
44 // function executed by thread
45 typedef uInt32 (*TThreadFunc)(TThreadObject *aThreadObject, uInt32 aParam);
46
47
48 // wrapper class for thread
49 class TThreadObject {
50 public:
51   // creates thread object. Thread ist not started yet, must use launch() for this
52   TThreadObject();
53   // destroys the thread object
54   virtual ~TThreadObject();
55   // starts thread (or re-starts it again after termination)
56   bool launch(
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
61   );
62   // get thread ID
63   uIntArch getid(void);
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)
67   void kill(void);
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; };
79 private:
80   // thread options
81   uInt32 fStackSize;
82   // the thread function
83   TThreadFunc fThreadFunc;
84   uInt32 fThreadFuncParam;
85   // the termination request flag
86   bool fTerminationRequested;
87   // the exit code
88   uInt32 fExitCode;
89 public:
90   // auto disposal of the thread object when thread exits
91   bool fAutoDisposeThreadObj;
92 private:
93
94   #ifdef _WIN32
95   // the windows thread
96   HANDLE fWinThreadHandle;
97   DWORD  fWinThreadId;
98   #endif
99
100   #if defined LINUX || defined MACOSX
101   // the linux POSIX thread
102 public:
103   pthread_t       fPosixThread;
104   pthread_mutex_t fDoneCondMutex;
105   pthread_cond_t  fDoneCond;
106   bool            fTerminated; // really finished
107   #endif
108 };
109
110
111 } // namespace sysync
112
113 #endif // PLATFORM_THREAD_H
114 /* eof */