OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Defines interface Runnable and class Thread. | |
17 // | |
18 // Thread encapsulates win32 primitives of creating and | |
19 // manipulating win32 threads. | |
20 | |
21 #ifndef OMAHA_COMMON_THREAD_H__ | |
22 #define OMAHA_COMMON_THREAD_H__ | |
23 | |
24 #include "omaha/base/synchronized.h" | |
25 | |
26 namespace omaha { | |
27 | |
28 // Any class which requires part of its execution in a | |
29 // separate thread should be derived from Runnable interface. | |
30 // It can have member variable of type Thread. When the | |
31 // thread needs to be launched one does something like that. | |
32 // A::func() { | |
33 // thread_.start(this); | |
34 // } | |
35 | |
36 class Runnable { | |
37 friend class Thread; | |
38 protected: | |
39 Runnable() {} | |
40 virtual ~Runnable() {} | |
41 virtual void Run() = 0; | |
42 private: | |
43 DISALLOW_EVIL_CONSTRUCTORS(Runnable); | |
44 }; | |
45 | |
46 // Any class devived from this one will be able to call | |
47 // Thread function QueueApc and have the function OnApc get | |
48 // executed in context of this thread. Thread must be in alertable | |
49 // state to be able to execute the apc function. | |
50 class ApcReceiver { | |
51 friend class Thread; | |
52 protected: | |
53 ApcReceiver() {} | |
54 virtual ~ApcReceiver() {} | |
55 virtual void OnApc(ULONG_PTR param) = 0; | |
56 private: | |
57 DISALLOW_EVIL_CONSTRUCTORS(ApcReceiver); | |
58 }; | |
59 | |
60 // This class encapsulates win32 thread management functions. | |
61 class Thread { | |
62 public: | |
63 Thread(); | |
64 ~Thread(); | |
65 | |
66 bool Start(Runnable* runner); | |
67 bool Suspend(); | |
68 bool Resume(); | |
69 bool Terminate(int exit_code); | |
70 bool SetPriority(int priority); | |
71 bool GetPriority(int* priority) const; | |
72 DWORD GetThreadId() const; | |
73 HANDLE GetThreadHandle() const; | |
74 | |
75 // Checks if the thread is running. | |
76 bool Running() const; | |
77 | |
78 // Waits until thread exits. | |
79 bool WaitTillExit(DWORD msec) const; | |
80 | |
81 // Queues an APC to the ApcReceiver. | |
82 bool QueueApc(ApcReceiver* receiver, ULONG_PTR param); | |
83 | |
84 // Posts message to a thread. | |
85 bool PostMessage(UINT msg, WPARAM wparam, LPARAM lparam); | |
86 private: | |
87 static DWORD __stdcall Prepare(void* thisPointer); // Thread proc. | |
88 static void __stdcall APCProc(ULONG_PTR dwParam); | |
89 | |
90 Runnable* runner_; // Interface to work with. | |
91 HANDLE thread_; | |
92 DWORD thread_id_; | |
93 Gate start_gate_; // Synchronizes the thread start. | |
94 | |
95 struct ApcInfo { | |
96 ApcReceiver* receiver_; | |
97 ULONG_PTR param_; | |
98 }; | |
99 | |
100 DISALLOW_EVIL_CONSTRUCTORS(Thread); | |
101 }; | |
102 | |
103 } // namespace omaha | |
104 | |
105 #endif // OMAHA_COMMON_THREAD_H__ | |
OLD | NEW |