Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/threads/nsThreadManager.h
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsThreadManager_h__
8
#define nsThreadManager_h__
9
10
#include "mozilla/Mutex.h"
11
#include "nsIThreadManager.h"
12
#include "nsRefPtrHashtable.h"
13
#include "nsThread.h"
14
15
class nsIRunnable;
16
17
class nsThreadManager : public nsIThreadManager
18
{
19
public:
20
  NS_DECL_ISUPPORTS
21
  NS_DECL_NSITHREADMANAGER
22
23
  static nsThreadManager& get();
24
25
  static void InitializeShutdownObserver();
26
27
  nsresult Init();
28
29
  // Shutdown all threads.  This function should only be called on the main
30
  // thread of the application process.
31
  void Shutdown();
32
33
  // Called by nsThread to inform the ThreadManager it exists.  This method
34
  // must be called when the given thread is the current thread.
35
  void RegisterCurrentThread(nsThread& aThread);
36
37
  // Called by nsThread to inform the ThreadManager it is going away.  This
38
  // method must be called when the given thread is the current thread.
39
  void UnregisterCurrentThread(nsThread& aThread, bool aIfExists = false);
40
41
  // Returns the current thread.  Returns null if OOM or if ThreadManager isn't
42
  // initialized.  Creates the nsThread if one does not exist yet.
43
  nsThread* GetCurrentThread();
44
45
  // Returns true iff the currently running thread has an nsThread associated
46
  // with it (ie; whether this is a thread that we can dispatch runnables to).
47
  bool IsNSThread() const;
48
49
  // CreateCurrentThread sets up an nsThread for the current thread. It uses the
50
  // event queue and main thread flags passed in. It should only be called once
51
  // for the current thread. After it returns, GetCurrentThread() will return
52
  // the thread that was created. GetCurrentThread() will also create a thread
53
  // (lazily), but it doesn't allow the queue or main-thread attributes to be
54
  // specified.
55
  nsThread* CreateCurrentThread(mozilla::SynchronizedEventQueue* aQueue,
56
                                nsThread::MainThreadFlag aMainThread);
57
58
  // Returns the maximal number of threads that have been in existence
59
  // simultaneously during the execution of the thread manager.
60
  uint32_t GetHighestNumberOfThreads();
61
62
  // This needs to be public in order to support static instantiation of this
63
  // class with older compilers (e.g., egcs-2.91.66).
64
  ~nsThreadManager()
65
3
  {
66
3
  }
67
68
  void EnableMainThreadEventPrioritization();
69
  void FlushInputEventPrioritization();
70
  void SuspendInputEventPrioritization();
71
  void ResumeInputEventPrioritization();
72
73
private:
74
  nsThreadManager()
75
    : mCurThreadIndex(0)
76
    , mMainPRThread(nullptr)
77
    , mLock("nsThreadManager.mLock")
78
    , mInitialized(false)
79
    , mCurrentNumberOfThreads(1)
80
    , mHighestNumberOfThreads(1)
81
3
  {
82
3
  }
83
84
  nsresult
85
  SpinEventLoopUntilInternal(nsINestedEventLoopCondition* aCondition,
86
                             bool aCheckingShutdown);
87
88
  static void ReleaseThread(void* aData);
89
90
  nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;
91
  unsigned            mCurThreadIndex;  // thread-local-storage index
92
  RefPtr<nsThread>  mMainThread;
93
  PRThread*         mMainPRThread;
94
  mozilla::OffTheBooksMutex mLock;  // protects tables
95
  mozilla::Atomic<bool,
96
                  mozilla::SequentiallyConsistent,
97
                  mozilla::recordreplay::Behavior::DontPreserve> mInitialized;
98
99
  // The current number of threads
100
  uint32_t            mCurrentNumberOfThreads;
101
  // The highest number of threads encountered so far during the session
102
  uint32_t            mHighestNumberOfThreads;
103
};
104
105
#define NS_THREADMANAGER_CID                       \
106
{ /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */       \
107
  0x7a4204c6,                                      \
108
  0xe45a,                                          \
109
  0x4c37,                                          \
110
  {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \
111
}
112
113
#endif  // nsThreadManager_h__