xpcom/threads/nsThreadManager.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/threads/nsThreadManager.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef nsThreadManager_h__
    1.11 +#define nsThreadManager_h__
    1.12 +
    1.13 +#include "mozilla/Mutex.h"
    1.14 +#include "nsIThreadManager.h"
    1.15 +#include "nsRefPtrHashtable.h"
    1.16 +#include "nsThread.h"
    1.17 +
    1.18 +class nsIRunnable;
    1.19 +
    1.20 +class nsThreadManager : public nsIThreadManager
    1.21 +{
    1.22 +public:
    1.23 +  NS_DECL_ISUPPORTS
    1.24 +  NS_DECL_NSITHREADMANAGER
    1.25 +
    1.26 +  static nsThreadManager *get() {
    1.27 +    static nsThreadManager sInstance;
    1.28 +    return &sInstance;
    1.29 +  }
    1.30 +
    1.31 +  nsresult Init();
    1.32 +
    1.33 +  // Shutdown all threads.  This function should only be called on the main
    1.34 +  // thread of the application process.
    1.35 +  void Shutdown();
    1.36 +
    1.37 +  // Called by nsThread to inform the ThreadManager it exists.  This method
    1.38 +  // must be called when the given thread is the current thread.
    1.39 +  void RegisterCurrentThread(nsThread *thread);
    1.40 +
    1.41 +  // Called by nsThread to inform the ThreadManager it is going away.  This
    1.42 +  // method must be called when the given thread is the current thread.
    1.43 +  void UnregisterCurrentThread(nsThread *thread);
    1.44 +
    1.45 +  // Returns the current thread.  Returns null if OOM or if ThreadManager isn't
    1.46 +  // initialized.
    1.47 +  nsThread *GetCurrentThread();
    1.48 +
    1.49 +  // Returns the maximal number of threads that have been in existence
    1.50 +  // simultaneously during the execution of the thread manager.
    1.51 +  uint32_t GetHighestNumberOfThreads();
    1.52 +
    1.53 +  // This needs to be public in order to support static instantiation of this
    1.54 +  // class with older compilers (e.g., egcs-2.91.66).
    1.55 +  ~nsThreadManager() {}
    1.56 +
    1.57 +private:
    1.58 +  nsThreadManager()
    1.59 +    : mCurThreadIndex(0)
    1.60 +    , mMainPRThread(nullptr)
    1.61 +    , mLock(nullptr)
    1.62 +    , mInitialized(false)
    1.63 +    , mCurrentNumberOfThreads(1)
    1.64 +    , mHighestNumberOfThreads(1) {
    1.65 +  }
    1.66 +
    1.67 +  nsRefPtrHashtable<nsPtrHashKey<PRThread>, nsThread> mThreadsByPRThread;
    1.68 +  unsigned             mCurThreadIndex;  // thread-local-storage index
    1.69 +  nsRefPtr<nsThread>  mMainThread;
    1.70 +  PRThread           *mMainPRThread;
    1.71 +  // This is a pointer in order to allow creating nsThreadManager from
    1.72 +  // the static context in debug builds.
    1.73 +  nsAutoPtr<mozilla::Mutex> mLock;  // protects tables
    1.74 +  bool                mInitialized;
    1.75 +
    1.76 +   // The current number of threads
    1.77 +   uint32_t           mCurrentNumberOfThreads;
    1.78 +   // The highest number of threads encountered so far during the session
    1.79 +   uint32_t           mHighestNumberOfThreads;
    1.80 +};
    1.81 +
    1.82 +#define NS_THREADMANAGER_CID                       \
    1.83 +{ /* 7a4204c6-e45a-4c37-8ebb-6709a22c917c */       \
    1.84 +  0x7a4204c6,                                      \
    1.85 +  0xe45a,                                          \
    1.86 +  0x4c37,                                          \
    1.87 +  {0x8e, 0xbb, 0x67, 0x09, 0xa2, 0x2c, 0x91, 0x7c} \
    1.88 +}
    1.89 +
    1.90 +#endif  // nsThreadManager_h__

mercurial