Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: base/thread_pool.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/thread_pool.h ('k') | base/thread_pool_callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "omaha/base/thread_pool.h"
17
18 #include "base/scoped_ptr.h"
19 #include "omaha/base/debug.h"
20 #include "omaha/base/error.h"
21 #include "omaha/base/exception_barrier.h"
22 #include "omaha/base/logging.h"
23
24 namespace omaha {
25
26 namespace {
27
28 // Context keeps track the information necessary to execute a work item
29 // inside a thread pool thread.
30 class Context {
31 public:
32 Context(ThreadPool* pool, UserWorkItem* work_item)
33 : pool_(pool),
34 work_item_(work_item) {
35 ASSERT1(pool);
36 ASSERT1(work_item);
37 }
38
39 ThreadPool* pool() const { return pool_; }
40 UserWorkItem* work_item() const { return work_item_; }
41
42 private:
43 ThreadPool* pool_;
44 UserWorkItem* work_item_;
45
46 DISALLOW_EVIL_CONSTRUCTORS(Context);
47 };
48
49 // Returns true if delta time since 'baseline' is greater or equal than
50 // 'milisecs'. Note: GetTickCount wraps around every ~48 days.
51 bool TimeHasElapsed(DWORD baseline, DWORD milisecs) {
52 DWORD current = ::GetTickCount();
53 DWORD wrap_bias = 0;
54 if (current < baseline) {
55 wrap_bias = static_cast<DWORD>(0xFFFFFFFF);
56 }
57 return (current - baseline + wrap_bias) >= milisecs ? true : false;
58 }
59
60 } // namespace
61
62
63 DWORD WINAPI ThreadPool::ThreadProc(void* param) {
64 ExceptionBarrier eb;
65 UTIL_LOG(L4, (_T("[ThreadPool::ThreadProc]")));
66 ASSERT1(param);
67 Context* context = static_cast<Context*>(param);
68 context->pool()->ProcessWorkItem(context->work_item());
69 delete context;
70 return 0;
71 }
72
73 ThreadPool::ThreadPool()
74 : work_item_count_(0),
75 shutdown_delay_(0) {
76 UTIL_LOG(L2, (_T("[ThreadPool::ThreadPool]")));
77 }
78
79 ThreadPool::~ThreadPool() {
80 UTIL_LOG(L2, (_T("[ThreadPool::~ThreadPool]")));
81
82 if (!shutdown_event_) {
83 return;
84 }
85
86 DWORD baseline_tick_count = ::GetTickCount();
87 if (::SetEvent(get(shutdown_event_))) {
88 while (work_item_count_ != 0) {
89 ::Sleep(1);
90 if (TimeHasElapsed(baseline_tick_count, shutdown_delay_)) {
91 UTIL_LOG(LE, (_T("[ThreadPool::~ThreadPool][timeout elapsed]")));
92 break;
93 }
94 }
95 }
96 }
97
98 HRESULT ThreadPool::Initialize(int shutdown_delay) {
99 shutdown_delay_ = shutdown_delay;
100 reset(shutdown_event_, ::CreateEvent(NULL, true, false, NULL));
101 return shutdown_event_ ? S_OK : HRESULTFromLastError();
102 }
103
104 void ThreadPool::ProcessWorkItem(UserWorkItem* work_item) {
105 ASSERT1(work_item);
106 work_item->Process();
107 delete work_item;
108 ::InterlockedDecrement(&work_item_count_);
109 }
110
111 HRESULT ThreadPool::QueueUserWorkItem(UserWorkItem* work_item, uint32 flags) {
112 UTIL_LOG(L4, (_T("[ThreadPool::QueueUserWorkItem]")));
113 ASSERT1(work_item);
114
115 scoped_ptr<Context> context(new Context(this, work_item));
116 work_item->set_shutdown_event(get(shutdown_event_));
117 ::InterlockedIncrement(&work_item_count_);
118 if (!::QueueUserWorkItem(&ThreadPool::ThreadProc, context.get(), flags)) {
119 ::InterlockedDecrement(&work_item_count_);
120 return HRESULTFromLastError();
121 }
122
123 // The thread pool has the ownership of the work item thereon.
124 context.release();
125 return S_OK;
126 }
127
128 } // namespace omaha
129
OLDNEW
« no previous file with comments | « base/thread_pool.h ('k') | base/thread_pool_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698