ProcessPriorityManager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* -*- Mode: C++; tab-width: 8; 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 file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_ProcessPriorityManager_h_
  6. #define mozilla_ProcessPriorityManager_h_
  7. #include "mozilla/HalTypes.h"
  8. namespace mozilla {
  9. namespace dom {
  10. class ContentParent;
  11. } // namespace dom
  12. /**
  13. * This class sets the priority of subprocesses in response to explicit
  14. * requests and events in the system.
  15. *
  16. * A process's priority changes e.g. when it goes into the background via
  17. * mozbrowser's setVisible(false). Process priority affects CPU scheduling and
  18. * also which processes get killed when we run out of memory.
  19. *
  20. * After you call Initialize(), the only thing you probably have to do is call
  21. * SetProcessPriority on processes immediately after creating them in order to
  22. * set their initial priority. The ProcessPriorityManager takes care of the
  23. * rest.
  24. */
  25. class ProcessPriorityManager final
  26. {
  27. public:
  28. /**
  29. * Initialize the ProcessPriorityManager machinery, causing the
  30. * ProcessPriorityManager to actively manage the priorities of all
  31. * subprocesses. You should call this before creating any subprocesses.
  32. *
  33. * You should also call this function even if you're in a child process,
  34. * since it will initialize ProcessPriorityManagerChild.
  35. */
  36. static void Init();
  37. /**
  38. * Set the process priority of a given ContentParent's process.
  39. *
  40. * Note that because this method takes a ContentParent*, you can only set the
  41. * priority of your subprocesses. In fact, because we don't support nested
  42. * content processes (bug 761935), you can only call this method from the
  43. * main process.
  44. *
  45. * It probably only makes sense to call this function immediately after a
  46. * process is created. At this point, the process priority manager doesn't
  47. * have enough context about the processs to know what its priority should
  48. * be.
  49. *
  50. * Eventually whatever priority you set here can and probably will be
  51. * overwritten by the process priority manager.
  52. */
  53. static void SetProcessPriority(dom::ContentParent* aContentParent,
  54. hal::ProcessPriority aPriority);
  55. /**
  56. * Returns true iff this process's priority is FOREGROUND*.
  57. *
  58. * Note that because process priorities are set in the main process, it's
  59. * possible for this method to return a stale value. So be careful about
  60. * what you use this for.
  61. */
  62. static bool CurrentProcessIsForeground();
  63. /**
  64. * Returns true if one or more processes with FOREGROUND_HIGH priority are
  65. * present, false otherwise.
  66. */
  67. static bool AnyProcessHasHighPriority();
  68. private:
  69. ProcessPriorityManager();
  70. DISALLOW_EVIL_CONSTRUCTORS(ProcessPriorityManager);
  71. };
  72. } // namespace mozilla
  73. #endif