GrSingleOwner.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrSingleOwner_DEFINED
  8. #define GrSingleOwner_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #ifdef SK_DEBUG
  11. #include "include/private/SkMutex.h"
  12. #include "include/private/SkThreadID.h"
  13. // This is a debug tool to verify an object is only being used from one thread at a time.
  14. class GrSingleOwner {
  15. public:
  16. GrSingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {}
  17. struct AutoEnforce {
  18. AutoEnforce(GrSingleOwner* so) : fSO(so) { fSO->enter(); }
  19. ~AutoEnforce() { fSO->exit(); }
  20. GrSingleOwner* fSO;
  21. };
  22. private:
  23. void enter() {
  24. SkAutoMutexExclusive lock(fMutex);
  25. SkThreadID self = SkGetThreadID();
  26. SkASSERT(fOwner == self || fOwner == kIllegalThreadID);
  27. fReentranceCount++;
  28. fOwner = self;
  29. }
  30. void exit() {
  31. SkAutoMutexExclusive lock(fMutex);
  32. SkASSERT(fOwner == SkGetThreadID());
  33. fReentranceCount--;
  34. if (fReentranceCount == 0) {
  35. fOwner = kIllegalThreadID;
  36. }
  37. }
  38. SkMutex fMutex;
  39. SkThreadID fOwner SK_GUARDED_BY(fMutex);
  40. int fReentranceCount SK_GUARDED_BY(fMutex);
  41. };
  42. #else
  43. class GrSingleOwner {}; // Provide a dummy implementation so we can pass pointers to constructors
  44. #endif
  45. #endif