nsNamedPipeService.h (2197B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef mozilla_netwerk_socket_nsNamedPipeService_h 7 #define mozilla_netwerk_socket_nsNamedPipeService_h 8 9 #include <windows.h> 10 #include "mozilla/Atomics.h" 11 #include "mozilla/Mutex.h" 12 #include "nsINamedPipeService.h" 13 #include "nsIObserver.h" 14 #include "nsIRunnable.h" 15 #include "nsIThread.h" 16 #include "nsTArray.h" 17 #include "mozilla/StaticPtr.h" 18 19 namespace mozilla { 20 namespace net { 21 22 class NamedPipeService final : public nsINamedPipeService, 23 public nsIObserver, 24 public nsIRunnable { 25 public: 26 NS_DECL_THREADSAFE_ISUPPORTS 27 NS_DECL_NSINAMEDPIPESERVICE 28 NS_DECL_NSIOBSERVER 29 NS_DECL_NSIRUNNABLE 30 31 static already_AddRefed<nsINamedPipeService> GetOrCreate(); 32 33 private: 34 explicit NamedPipeService(); 35 virtual ~NamedPipeService() = default; 36 37 nsresult Init(); 38 39 void Shutdown(); 40 void RemoveRetiredObjects(); 41 42 HANDLE mIocp; // native handle to the I/O completion port. 43 Atomic<bool> 44 mIsShutdown; // set to true to stop the event loop running by mThread. 45 nsCOMPtr<nsIThread> mThread; // worker thread to get I/O events. 46 47 /** 48 * The observers is maintained in |mObservers| to ensure valid life-cycle. 49 * We don't remove the handle and corresponding observer directly, instead 50 * the handle and observer into a "retired" list and close/remove them in 51 * the worker thread to avoid a race condition that might happen between 52 * |CloseHandle()| and |GetQueuedCompletionStatus()|. 53 */ 54 Mutex mLock MOZ_UNANNOTATED; 55 nsTArray<nsCOMPtr<nsINamedPipeDataObserver>> 56 mObservers; // protected by mLock 57 nsTArray<nsCOMPtr<nsINamedPipeDataObserver>> 58 mRetiredObservers; // protected by mLock 59 nsTArray<HANDLE> mRetiredHandles; // protected by mLock 60 61 static StaticRefPtr<NamedPipeService> gSingleton; 62 }; 63 64 } // namespace net 65 } // namespace mozilla 66 67 #endif // mozilla_netwerk_socket_nsNamedPipeService_h