Line data Source code
1 : /*
2 :
3 : This file is a part of the JThread package, which contains some object-
4 : oriented thread wrappers for different thread implementations.
5 :
6 : Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
7 :
8 : Permission is hereby granted, free of charge, to any person obtaining a
9 : copy of this software and associated documentation files (the "Software"),
10 : to deal in the Software without restriction, including without limitation
11 : the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 : and/or sell copies of the Software, and to permit persons to whom the
13 : Software is furnished to do so, subject to the following conditions:
14 :
15 : The above copyright notice and this permission notice shall be included in
16 : all copies or substantial portions of the Software.
17 :
18 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 : THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 : FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 : DEALINGS IN THE SOFTWARE.
25 :
26 : */
27 :
28 : #ifndef JTHREAD_H
29 : #define JTHREAD_H
30 :
31 : #if __cplusplus >= 201103L
32 : #include <atomic>
33 : #endif
34 :
35 : #include "jthread/jmutex.h"
36 :
37 : #define ERR_JTHREAD_CANTINITMUTEX -1
38 : #define ERR_JTHREAD_CANTSTARTTHREAD -2
39 : #define ERR_JTHREAD_THREADFUNCNOTSET -3
40 : #define ERR_JTHREAD_NOTRUNNING -4
41 : #define ERR_JTHREAD_ALREADYRUNNING -5
42 :
43 : class JThread
44 : {
45 : public:
46 : JThread();
47 : virtual ~JThread();
48 : int Start();
49 9 : inline void Stop()
50 9 : { requeststop = true; }
51 : int Kill();
52 : virtual void *Thread() = 0;
53 10 : inline bool IsRunning()
54 10 : { return running; }
55 7246 : inline bool StopRequested()
56 7246 : { return requeststop; }
57 : void *GetReturnValue();
58 : bool IsSameThread();
59 :
60 : /*
61 : * Wait for thread to finish
62 : * Note: this does not stop a thread you have to do this on your own
63 : * WARNING: never ever call this on a thread not started or already killed!
64 : */
65 : void Wait();
66 : protected:
67 : void ThreadStarted();
68 : private:
69 :
70 : #if (defined(WIN32) || defined(_WIN32_WCE))
71 : #ifdef _WIN32_WCE
72 : DWORD threadid;
73 : static DWORD WINAPI TheThread(void *param);
74 : #else
75 : static UINT __stdcall TheThread(void *param);
76 : UINT threadid;
77 : #endif // _WIN32_WCE
78 : HANDLE threadhandle;
79 : #else // pthread type threads
80 : static void *TheThread(void *param);
81 :
82 : pthread_t threadid;
83 :
84 : /*
85 : * reading and writing bool values is atomic on all relevant architectures
86 : * ( x86 + arm ). No need to waste time for locking here.
87 : * once C++11 is supported we can tell compiler to handle cpu caches correct
88 : * too. This should cause additional improvement (and silence thread
89 : * concurrency check tools.
90 : */
91 : #if __cplusplus >= 201103L
92 : std::atomic_bool started;
93 : #else
94 : bool started;
95 : #endif
96 : #endif // WIN32
97 : void *retval;
98 : /*
99 : * reading and writing bool values is atomic on all relevant architectures
100 : * ( x86 + arm ). No need to waste time for locking here.
101 : * once C++11 is supported we can tell compiler to handle cpu caches correct
102 : * too. This should cause additional improvement (and silence thread
103 : * concurrency check tools.
104 : */
105 : #if __cplusplus >= 201103L
106 : std::atomic_bool running;
107 : std::atomic_bool requeststop;
108 : #else
109 : bool running;
110 : bool requeststop;
111 : #endif
112 :
113 : JMutex continuemutex,continuemutex2;
114 : };
115 :
116 : #endif // JTHREAD_H
117 :
|