| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-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 // | |
| 16 // STA initializes a custom non-COM Single Threaded Apartment to facilitate | |
| 17 // calling of functions and object methods in the thread of the STA. This is | |
| 18 // useful when creating a simple threading model based on a main thread and | |
| 19 // several worker threads, especially for client components that have a UI or | |
| 20 // use the COM STA models. | |
| 21 // | |
| 22 // The current implementation only supports initializing the main STA, which is | |
| 23 // the STA created by the main thread of the process. This is usually the UI | |
| 24 // thread. Having multiple STA in a process is not possible yet. | |
| 25 // | |
| 26 // This custom STA does not interfere with the COM STAs. | |
| 27 // | |
| 28 // In order for the STA to work properly, the STA thread must keep processing | |
| 29 // messages and not block, just like in the COM STA case. | |
| 30 | |
| 31 #ifndef OMAHA_COMMON_STA_H__ | |
| 32 #define OMAHA_COMMON_STA_H__ | |
| 33 | |
| 34 #include <windows.h> | |
| 35 #include "omaha/base/debug.h" | |
| 36 #include "omaha/base/scoped_any.h" | |
| 37 | |
| 38 namespace omaha { | |
| 39 | |
| 40 // Initializes the STA apartment. The 'reserved' parameter must be 0. | |
| 41 // InitializeApartment and UninitializeApartment are reference-counted. | |
| 42 HRESULT InitializeApartment(DWORD reserved); | |
| 43 | |
| 44 // Uninitializes the STA apartment. | |
| 45 HRESULT UninitializeApartment(); | |
| 46 | |
| 47 // A scoped_sta smart pointer is provided to manage the calls to | |
| 48 // InitializeApartment and UninitializeApartment. | |
| 49 inline HRESULT smart_sta_init_helper(DWORD reserved) { | |
| 50 return InitializeApartment(reserved); | |
| 51 } | |
| 52 | |
| 53 inline void smart_uninit_helper(HRESULT result) { | |
| 54 if (result == S_OK) { | |
| 55 VERIFY1(SUCCEEDED(UninitializeApartment())); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 typedef close_fun<void (*)(HRESULT), smart_uninit_helper> close_sta; | |
| 60 | |
| 61 typedef value_const<HRESULT, E_UNEXPECTED> sta_not_init; | |
| 62 | |
| 63 typedef scoped_any<HRESULT, close_sta, sta_not_init> scoped_sta_close; | |
| 64 | |
| 65 struct scoped_sta { | |
| 66 explicit scoped_sta(DWORD reserved) | |
| 67 : result_(smart_sta_init_helper(reserved)) {} | |
| 68 | |
| 69 HRESULT result() const { return get(result_); } | |
| 70 | |
| 71 private: | |
| 72 const scoped_sta_close result_; | |
| 73 }; | |
| 74 | |
| 75 } // namespace omaha | |
| 76 | |
| 77 #endif // OMAHA_COMMON_STA_H__ | |
| 78 | |
| OLD | NEW |