Commit 12fd3358 authored by Nika Layzell's avatar Nika Layzell
Browse files

Bug 1834640 - Part 1: Add threadsafety annotations to...

Bug 1834640 - Part 1: Add threadsafety annotations to nsSocketTransportService, r=jesup,necko-reviewers

While looking at the relevant code, I noticed the unannotated mutex, and added
some annotations to it. The minor changes I had to do to keep the tree
building after the annotations shouldn't be actual threadsafety issues, as
most of it is either dead code, or exclusively called from the only mutating
thread.

Differential Revision: https://phabricator.services.mozilla.com/D178867
parent c3709f29
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -775,11 +775,10 @@ nsSocketTransportService::Init() {
    // Install our mThread, protecting against concurrent readers
    thread.swap(mThread);
    mDirectTaskDispatcher = do_QueryInterface(mThread);
  }

  MOZ_DIAGNOSTIC_ASSERT(
      mDirectTaskDispatcher,
      "Underlying thread must support direct task dispatching");
  }

  Preferences::RegisterCallbacks(UpdatePrefs, gCallbackPrefs, this);
  UpdatePrefs();
@@ -852,7 +851,8 @@ nsresult nsSocketTransportService::ShutdownThread() {
  }

  // join with thread
  mThread->Shutdown();
  nsCOMPtr<nsIThread> thread = GetThreadSafely();
  thread->Shutdown();
  {
    MutexAutoLock lock(mLock);
    // Drop our reference to mThread and make sure that any concurrent readers
@@ -888,6 +888,7 @@ nsresult nsSocketTransportService::ShutdownThread() {

NS_IMETHODIMP
nsSocketTransportService::GetOffline(bool* offline) {
  MutexAutoLock lock(mLock);
  *offline = mOffline;
  return NS_OK;
}
+7 −7
Original line number Diff line number Diff line
@@ -164,20 +164,20 @@ class nsSocketTransportService final : public nsPISocketTransportService,
  // Effectively owned by the SocketThread
  RefPtr<nsSocketTransportService> mSelf;

  Mutex mLock MOZ_UNANNOTATED{"nsSocketTransportService::mLock"};
  Mutex mLock{"nsSocketTransportService::mLock"};
  // Variables in the next section protected by mLock

  // mThread and mDirectTaskDispatcher are only ever modified on the main
  // thread. Will be set on Init and set to null after shutdown. You must access
  // mThread and mDirectTaskDispatcher outside the main thread via respectively
  // GetThreadSafely and GetDirectTaskDispatchedSafely().
  nsCOMPtr<nsIThread> mThread;
  nsCOMPtr<nsIThread> mThread MOZ_GUARDED_BY(mLock);
  // We store a pointer to mThread as a direct task dispatcher to avoid having
  // to do do_QueryInterface whenever we need to access the interface.
  nsCOMPtr<nsIDirectTaskDispatcher> mDirectTaskDispatcher;
  UniquePtr<PollableEvent> mPollableEvent;
  bool mOffline{false};
  bool mGoingOffline{false};
  nsCOMPtr<nsIDirectTaskDispatcher> mDirectTaskDispatcher MOZ_GUARDED_BY(mLock);
  UniquePtr<PollableEvent> mPollableEvent MOZ_GUARDED_BY(mLock);
  bool mOffline MOZ_GUARDED_BY(mLock) = false;
  bool mGoingOffline MOZ_GUARDED_BY(mLock) = false;

  // Detaches all sockets.
  void Reset(bool aGuardLocals);
@@ -288,7 +288,7 @@ class nsSocketTransportService final : public nsPISocketTransportService,
  // True if TCP keepalive is enabled globally.
  bool mKeepaliveEnabledPref{false};
  // Timeout of pollable event signalling.
  TimeDuration mPollableEventTimeout;
  TimeDuration mPollableEventTimeout MOZ_GUARDED_BY(mLock);

  Atomic<bool> mServingPendingQueue{false};
  Atomic<int32_t, Relaxed> mMaxTimePerPollIter{100};