annotate agent/src/os/win32/Reaper.cpp @ 1552:c18cbe5936b8

6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
author trims
date Thu, 27 May 2010 19:08:38 -0700
parents a61af66fc99e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include <iostream>
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include "Reaper.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 using namespace std;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 Reaper::Reaper(ReaperCB* cb) {
a61af66fc99e Initial load
duke
parents:
diff changeset
31 InitializeCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
32 event = CreateEvent(NULL, TRUE, FALSE, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
33 this->cb = cb;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 active = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 shouldShutDown = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 }
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
40 Reaper::start() {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 bool result = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
42
a61af66fc99e Initial load
duke
parents:
diff changeset
43 EnterCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 if (!active) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 DWORD id;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 HANDLE reaper = CreateThread(NULL, 0, &Reaper::reaperThreadEntry,
a61af66fc99e Initial load
duke
parents:
diff changeset
48 this, 0, &id);
a61af66fc99e Initial load
duke
parents:
diff changeset
49 if (reaper != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 result = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 }
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 LeaveCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 }
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 bool
a61af66fc99e Initial load
duke
parents:
diff changeset
60 Reaper::stop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 bool result = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 EnterCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if (active) {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 shouldShutDown = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 SetEvent(event);
a61af66fc99e Initial load
duke
parents:
diff changeset
68 while (active) {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 Sleep(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71 shouldShutDown = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 result = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 LeaveCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 void
a61af66fc99e Initial load
duke
parents:
diff changeset
81 Reaper::registerProcess(HANDLE processHandle, void* userData) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 ProcessInfo info;
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 info.handle = processHandle;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 info.userData = userData;
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 EnterCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 procInfo.push_back(info);
a61af66fc99e Initial load
duke
parents:
diff changeset
90 SetEvent(event);
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 LeaveCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 void
a61af66fc99e Initial load
duke
parents:
diff changeset
96 Reaper::reaperThread() {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 while (!shouldShutDown) {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // Take atomic snapshot of the current process list and user data
a61af66fc99e Initial load
duke
parents:
diff changeset
99 EnterCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 int num = procInfo.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
102 HANDLE* handleList = new HANDLE[1 + num];
a61af66fc99e Initial load
duke
parents:
diff changeset
103 void** dataList = new void*[num];
a61af66fc99e Initial load
duke
parents:
diff changeset
104 for (int i = 0; i < num; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 handleList[i] = procInfo[i].handle;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 dataList[i] = procInfo[i].userData;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 LeaveCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 // Topmost handle becomes the event object, so other threads can
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // signal this one to notice differences in the above list (or
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // shut down)
a61af66fc99e Initial load
duke
parents:
diff changeset
114 handleList[num] = event;
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 // Wait for these objects
a61af66fc99e Initial load
duke
parents:
diff changeset
117 DWORD idx = WaitForMultipleObjects(1 + num, handleList,
a61af66fc99e Initial load
duke
parents:
diff changeset
118 FALSE, INFINITE);
a61af66fc99e Initial load
duke
parents:
diff changeset
119 if ((idx >= WAIT_OBJECT_0) && (idx <= WAIT_OBJECT_0 + num)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 idx -= WAIT_OBJECT_0;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 if (idx < num) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 // A process exited (i.e., it wasn't that we were woken up
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // just because the event went off)
a61af66fc99e Initial load
duke
parents:
diff changeset
124 (*cb)(dataList[idx]);
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // Remove this process from the list (NOTE: requires that
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // ordering does not change, i.e., that all additions are to
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // the back of the process list)
a61af66fc99e Initial load
duke
parents:
diff changeset
128 EnterCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 std::vector<ProcessInfo>::iterator iter = procInfo.begin();
a61af66fc99e Initial load
duke
parents:
diff changeset
131 iter += idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 procInfo.erase(iter);
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 LeaveCriticalSection(&crit);
a61af66fc99e Initial load
duke
parents:
diff changeset
135 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // Notification from other thread
a61af66fc99e Initial load
duke
parents:
diff changeset
137 ResetEvent(event);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // Unexpected return value. For now, warn.
a61af66fc99e Initial load
duke
parents:
diff changeset
141 cerr << "Reaper::reaperThread(): unexpected return value "
a61af66fc99e Initial load
duke
parents:
diff changeset
142 << idx << " from WaitForMultipleObjects" << endl;
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // Clean up these lists
a61af66fc99e Initial load
duke
parents:
diff changeset
146 delete[] handleList;
a61af66fc99e Initial load
duke
parents:
diff changeset
147 delete[] dataList;
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 // Time to shut down
a61af66fc99e Initial load
duke
parents:
diff changeset
151 active = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 DWORD WINAPI
a61af66fc99e Initial load
duke
parents:
diff changeset
155 Reaper::reaperThreadEntry(LPVOID data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 Reaper* reaper = (Reaper*) data;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 reaper->reaperThread();
a61af66fc99e Initial load
duke
parents:
diff changeset
158 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }