1 | /* $Id: ClientWatcher.h 60061 2016-03-16 14:47:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox API client session watcher
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ____H_CLIENTWATCHER
|
---|
19 | #define ____H_CLIENTWATCHER
|
---|
20 |
|
---|
21 |
|
---|
22 | #include <list>
|
---|
23 | #include <VBox/com/ptr.h>
|
---|
24 | #include <VBox/com/AutoLock.h>
|
---|
25 |
|
---|
26 | #include "VirtualBoxImpl.h"
|
---|
27 |
|
---|
28 | #if defined(RT_OS_WINDOWS)
|
---|
29 | # define CWUPDATEREQARG NULL
|
---|
30 | # define CWUPDATEREQTYPE HANDLE
|
---|
31 | # ifdef CW_WITH_BIRD_WATCHING
|
---|
32 | # define CW_MAX_CLIENTS _16K /**< Max number of clients we can watch (windows). */
|
---|
33 | # ifndef DEBUG /* The debug version triggers worker thread code much much earlier. */
|
---|
34 | # define CW_MAX_CLIENTS_PER_THREAD 63 /**< Max clients per watcher thread (windows). */
|
---|
35 | # else
|
---|
36 | # define CW_MAX_CLIENTS_PER_THREAD 3 /**< Max clients per watcher thread (windows). */
|
---|
37 | # endif
|
---|
38 | # define CW_MAX_HANDLES_PER_THREAD (CW_MAX_CLIENTS_PER_THREAD + 1) /**< Max handles per thread. */
|
---|
39 | # endif /* CW_WITH_BIRD_WATCHING */
|
---|
40 | #elif defined(RT_OS_OS2)
|
---|
41 | # define CWUPDATEREQARG NIL_RTSEMEVENT
|
---|
42 | # define CWUPDATEREQTYPE RTSEMEVENT
|
---|
43 | #elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER) || defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
|
---|
44 | # define CWUPDATEREQARG NIL_RTSEMEVENT
|
---|
45 | # define CWUPDATEREQTYPE RTSEMEVENT
|
---|
46 | #else
|
---|
47 | # error "Port me!"
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Class which checks for API clients which have crashed/exited, and takes
|
---|
52 | * the necessary cleanup actions. Singleton.
|
---|
53 | */
|
---|
54 | class VirtualBox::ClientWatcher
|
---|
55 | {
|
---|
56 | public:
|
---|
57 | /**
|
---|
58 | * Constructor which creates a usable instance
|
---|
59 | *
|
---|
60 | * @param pVirtualBox Reference to VirtualBox object
|
---|
61 | */
|
---|
62 | ClientWatcher(const ComObjPtr<VirtualBox> &pVirtualBox);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Default destructor. Cleans everything up.
|
---|
66 | */
|
---|
67 | ~ClientWatcher();
|
---|
68 |
|
---|
69 | bool isReady();
|
---|
70 |
|
---|
71 | void update();
|
---|
72 | void addProcess(RTPROCESS pid);
|
---|
73 |
|
---|
74 | private:
|
---|
75 | /**
|
---|
76 | * Default constructor. Don't use, will not create a sensible instance.
|
---|
77 | */
|
---|
78 | ClientWatcher();
|
---|
79 |
|
---|
80 | static DECLCALLBACK(int) worker(RTTHREAD hThreadSelf, void *pvUser);
|
---|
81 |
|
---|
82 | VirtualBox *mVirtualBox;
|
---|
83 | RTTHREAD mThread;
|
---|
84 | CWUPDATEREQTYPE mUpdateReq;
|
---|
85 | util::RWLockHandle mLock;
|
---|
86 |
|
---|
87 | typedef std::list<RTPROCESS> ProcessList;
|
---|
88 | ProcessList mProcesses;
|
---|
89 |
|
---|
90 | #if defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER) || defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
|
---|
91 | uint8_t mUpdateAdaptCtr;
|
---|
92 | #endif
|
---|
93 | #ifdef CW_WITH_BIRD_WATCHING
|
---|
94 | #ifdef RT_OS_WINDOWS
|
---|
95 | /** Indicate a real update request is pending.
|
---|
96 | * To avoid race conditions this must be set before mUpdateReq is signalled and
|
---|
97 | * read after resetting mUpdateReq. */
|
---|
98 | volatile bool mfUpdateReq;
|
---|
99 | /** Set when the worker threads are supposed to shut down. */
|
---|
100 | volatile bool mfTerminate;
|
---|
101 | /** Number of active subworkers.
|
---|
102 | * When decremented to 0, subworker zero is signalled. */
|
---|
103 | uint32_t volatile mcActiveSubworkers;
|
---|
104 | /** Number of valid handles in mahWaitHandles. */
|
---|
105 | uint32_t mcWaitHandles;
|
---|
106 | /** The wait interval (usually INFINITE). */
|
---|
107 | uint32_t mcMsWait;
|
---|
108 | /** Per subworker data. Subworker 0 is the main worker and does not have a
|
---|
109 | * pReq pointer since. */
|
---|
110 | struct PerSubworker
|
---|
111 | {
|
---|
112 | /** The wait result. */
|
---|
113 | DWORD dwWait;
|
---|
114 | /** The subworker index. */
|
---|
115 | uint32_t iSubworker;
|
---|
116 | /** The subworker thread handle. */
|
---|
117 | RTTHREAD hThread;
|
---|
118 | /** Self pointer (for worker thread). */
|
---|
119 | VirtualBox::ClientWatcher *pSelf;
|
---|
120 | } maSubworkers[(CW_MAX_CLIENTS + CW_MAX_CLIENTS_PER_THREAD - 1) / CW_MAX_CLIENTS_PER_THREAD];
|
---|
121 | /** Wait handle array. The mUpdateReq manual reset event handle is inserted
|
---|
122 | * every 64 entries, first entry being 0. */
|
---|
123 | HANDLE mahWaitHandles[CW_MAX_CLIENTS + (CW_MAX_CLIENTS + CW_MAX_CLIENTS_PER_THREAD - 1) / CW_MAX_CLIENTS_PER_THREAD];
|
---|
124 |
|
---|
125 | void subworkerWait(VirtualBox::ClientWatcher::PerSubworker *pSubworker, uint32_t cMsWait);
|
---|
126 | static DECLCALLBACK(int) subworkerThread(RTTHREAD hThreadSelf, void *pvUser);
|
---|
127 | void winResetHandleArray(uint32_t cProcHandles);
|
---|
128 | #endif
|
---|
129 | #endif /* CW_WITH_BIRD_WATCHING */
|
---|
130 | };
|
---|
131 |
|
---|
132 | #endif /* !____H_CLIENTWATCHER */
|
---|
133 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|