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 #ifndef OMAHA_BASE_THREAD_POOL_H_ | |
17 #define OMAHA_BASE_THREAD_POOL_H_ | |
18 | |
19 #include <windows.h> | |
20 #include "base/basictypes.h" | |
21 #include "omaha/base/scoped_any.h" | |
22 | |
23 namespace omaha { | |
24 | |
25 class UserWorkItem { | |
26 public: | |
27 UserWorkItem() : shutdown_event_(NULL) {} | |
28 virtual ~UserWorkItem() {} | |
29 | |
30 // Template method interface | |
31 void Process() { DoProcess(); } | |
32 | |
33 HANDLE shutdown_event() const { return shutdown_event_; } | |
34 void set_shutdown_event(HANDLE shutdown_event) { | |
35 shutdown_event_ = shutdown_event; | |
36 } | |
37 | |
38 private: | |
39 // Executes the work item. | |
40 virtual void DoProcess() = 0; | |
41 | |
42 // It is the job of implementers to watch for the signaling of this event | |
43 // and shutdown correctly. This event is set when the thread pool is closing. | |
44 // Do not close this event as is owned by the thread pool. | |
45 HANDLE shutdown_event_; | |
46 DISALLOW_EVIL_CONSTRUCTORS(UserWorkItem); | |
47 }; | |
48 | |
49 class ThreadPool { | |
50 public: | |
51 ThreadPool(); | |
52 | |
53 // The destructor might block for 'shutdown_delay'. | |
54 ~ThreadPool(); | |
55 | |
56 HRESULT Initialize(int shutdown_delay); | |
57 | |
58 // Returns true if any work items are still in progress. | |
59 bool HasWorkItems() const { return (0 != work_item_count_); } | |
60 | |
61 // Adds a work item to the queue. If the add fails the ownership of the | |
62 // work items remains with the caller. | |
63 HRESULT QueueUserWorkItem(UserWorkItem* work_item, uint32 flags); | |
64 | |
65 private: | |
66 // Calls UserWorkItem::Process() in the context of the worker thread. | |
67 void ProcessWorkItem(UserWorkItem* work_item); | |
68 | |
69 // This is the thread callback required by the underlying windows API. | |
70 static DWORD WINAPI ThreadProc(void* context); | |
71 | |
72 // Approximate number of work items in the pool. | |
73 volatile LONG work_item_count_; | |
74 | |
75 // This event signals when the thread pool destructor is in progress. | |
76 scoped_event shutdown_event_; | |
77 | |
78 // How many milliseconds to wait for the work items to finish when | |
79 // the thread pool is shutting down. The shutdown delay resolution is ~10ms. | |
80 int shutdown_delay_; | |
81 | |
82 DISALLOW_EVIL_CONSTRUCTORS(ThreadPool); | |
83 }; | |
84 | |
85 } // namespace omaha | |
86 | |
87 #endif // OMAHA_BASE_THREAD_POOL_H_ | |
88 | |
OLD | NEW |