Bug 1733593 [wpt PR 31046] - Add a wpt for interaction id., a=testonly
[gecko.git] / netwerk / socket / nsNamedPipeService.h
blob5d6e5f86fe4eeb95fbce9c32baa2f3cd6db73895
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/. */
6 #ifndef mozilla_netwerk_socket_nsNamedPipeService_h
7 #define mozilla_netwerk_socket_nsNamedPipeService_h
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"
19 namespace mozilla {
20 namespace net {
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
31 static already_AddRefed<nsINamedPipeService> GetOrCreate();
33 private:
34 explicit NamedPipeService();
35 virtual ~NamedPipeService() = default;
37 nsresult Init();
39 void Shutdown();
40 void RemoveRetiredObjects();
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.
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()|.
54 Mutex mLock;
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
61 static StaticRefPtr<NamedPipeService> gSingleton;
64 } // namespace net
65 } // namespace mozilla
67 #endif // mozilla_netwerk_socket_nsNamedPipeService_h