2016-01-06 16:26:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrSingleOwner_DEFINED
|
|
|
|
#define GrSingleOwner_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2016-01-06 16:26:09 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkMutex.h"
|
|
|
|
#include "include/private/SkThreadID.h"
|
2016-01-06 16:26:09 +00:00
|
|
|
|
|
|
|
// This is a debug tool to verify an object is only being used from one thread at a time.
|
|
|
|
class GrSingleOwner {
|
|
|
|
public:
|
|
|
|
GrSingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {}
|
|
|
|
|
|
|
|
struct AutoEnforce {
|
|
|
|
AutoEnforce(GrSingleOwner* so) : fSO(so) { fSO->enter(); }
|
|
|
|
~AutoEnforce() { fSO->exit(); }
|
|
|
|
|
|
|
|
GrSingleOwner* fSO;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
void enter() {
|
2019-05-10 16:16:17 +00:00
|
|
|
SkAutoMutexExclusive lock(fMutex);
|
2016-01-06 16:26:09 +00:00
|
|
|
SkThreadID self = SkGetThreadID();
|
|
|
|
SkASSERT(fOwner == self || fOwner == kIllegalThreadID);
|
|
|
|
fReentranceCount++;
|
|
|
|
fOwner = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exit() {
|
2019-05-10 16:16:17 +00:00
|
|
|
SkAutoMutexExclusive lock(fMutex);
|
2016-01-06 17:25:13 +00:00
|
|
|
SkASSERT(fOwner == SkGetThreadID());
|
2016-01-06 16:26:09 +00:00
|
|
|
fReentranceCount--;
|
|
|
|
if (fReentranceCount == 0) {
|
|
|
|
fOwner = kIllegalThreadID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkMutex fMutex;
|
2019-05-10 16:16:17 +00:00
|
|
|
SkThreadID fOwner SK_GUARDED_BY(fMutex);
|
|
|
|
int fReentranceCount SK_GUARDED_BY(fMutex);
|
2016-01-06 16:26:09 +00:00
|
|
|
};
|
2016-01-08 18:09:13 +00:00
|
|
|
#else
|
|
|
|
class GrSingleOwner {}; // Provide a dummy implementation so we can pass pointers to constructors
|
2016-01-06 16:26:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|