OLD | NEW |
| (Empty) |
1 // Copyright 2003-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 // Single Instance of a process running (plus some synchronization functions | |
16 // that should be moved elsewhere) | |
17 // | |
18 // synchronization functions | |
19 | |
20 #ifndef OMAHA_COMMON_SINGLE_INSTANCE_H_ | |
21 #define OMAHA_COMMON_SINGLE_INSTANCE_H_ | |
22 | |
23 #include <windows.h> | |
24 | |
25 #include "base/basictypes.h" | |
26 #include "omaha/base/debug.h" | |
27 | |
28 namespace omaha { | |
29 | |
30 // Used to ensure only a single instance of a process per machine | |
31 // (e.g., even in terminal server sessions) | |
32 class SingleInstance { | |
33 public: | |
34 // Constructor | |
35 SingleInstance() : user_mutex_handle_(NULL), global_mutex_handle_(NULL) {} | |
36 | |
37 // Destructor | |
38 ~SingleInstance() { Shutdown(); } | |
39 | |
40 // Check to see whether an instance is already running across all sessions. | |
41 // If not, enter single instance protection. The user must call Shutdown() | |
42 // on that SingleInstance once single instance protection is no longer needed. | |
43 bool StartupSingleInstance(const TCHAR* id); | |
44 | |
45 // Check to see whether an instance is already running for this user. If not, | |
46 // enter user-only single instance protection. The user must call Shutdown() | |
47 // on that SingleInstance once single instance protection is no longer needed. | |
48 bool StartupSingleSessionInstance(const TCHAR* id); | |
49 | |
50 // Startup a single instance protection. The user must call Shutdown() on | |
51 // that SingleInstance once the single instance protection is no longer needed
. | |
52 HRESULT Startup(const TCHAR* id, | |
53 bool* already_running, | |
54 bool* already_running_in_different_session); | |
55 | |
56 // Shutdown a single instance protection | |
57 HRESULT Shutdown(); | |
58 | |
59 // Check to see whether an instance is already running | |
60 static HRESULT CheckAlreadyRunning( | |
61 const TCHAR* id, | |
62 bool* already_running, | |
63 bool* already_running_in_different_session); | |
64 | |
65 private: | |
66 // Create a mutex | |
67 static HRESULT CreateInstanceMutex(const TCHAR* mutex_id, | |
68 HANDLE* mutex_handle, | |
69 bool* already_running); | |
70 | |
71 // Open a mutex | |
72 static HRESULT OpenInstanceMutex(const TCHAR* mutex_id, | |
73 bool* already_running); | |
74 | |
75 HANDLE user_mutex_handle_; | |
76 HANDLE global_mutex_handle_; | |
77 | |
78 DISALLOW_EVIL_CONSTRUCTORS(SingleInstance); | |
79 }; | |
80 | |
81 } // namespace omaha | |
82 | |
83 #endif // OMAHA_COMMON_SINGLE_INSTANCE_H_ | |
OLD | NEW |