1 /*
   2  * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef _REAPER_
  26 #define _REAPER_
  27 
  28 #include <vector>
  29 #include <windows.h>
  30 
  31 typedef void ReaperCB(void* userData);
  32 
  33 /** A Reaper maintains a thread which waits for child processes to
  34     terminate; upon termination it calls a user-specified ReaperCB to
  35     clean up resources associated with those child processes. */
  36 
  37 class Reaper {
  38 private:
  39   Reaper& operator=(const Reaper&);
  40   Reaper(const Reaper&);
  41 
  42 public:
  43   Reaper(ReaperCB*);
  44   ~Reaper();
  45 
  46   // Start the reaper thread.
  47   bool start();
  48 
  49   // Stop the reaper thread. This is called automatically in the
  50   // reaper's destructor. It is not thread safe and should be called
  51   // by at most one thread at a time.
  52   bool stop();
  53 
  54   // Register a given child process with the reaper. This should be
  55   // called by the application's main thread. When that process
  56   // terminates, the cleanup callback will be called with the
  57   // specified userData in the context of the reaper thread. Callbacks
  58   // are guaranteed to be called serially, so they can safely refer to
  59   // static data as well as the given user data.
  60   void registerProcess(HANDLE processHandle, void* userData);
  61 
  62 private:
  63   // For thread safety of register()
  64   CRITICAL_SECTION crit;
  65 
  66   ReaperCB* cb;
  67 
  68   // State variables
  69   volatile bool active;
  70   volatile bool shouldShutDown;
  71 
  72   struct ProcessInfo {
  73     HANDLE handle;
  74     void* userData;
  75   };
  76 
  77   // Bookkeeping
  78   std::vector<ProcessInfo> procInfo;
  79 
  80   // Synchronization between application thread and reaper thread
  81   HANDLE event;
  82 
  83   // Entry point for reaper thread
  84   void reaperThread();
  85 
  86   // Static function which is actual thread entry point
  87   static DWORD WINAPI reaperThreadEntry(LPVOID data);
  88 };
  89 
  90 #endif  // #defined _REAPER_