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