skia2/include/private/GrSingleOwner.h
joshualitt de8dc7e920 Reland of Make a single GrSingleOwner in GrContext (patchset #1 id:1 of https://codereview.chromium.org/1565303003/ )
Reason for revert:
trying again

Original issue's description:
> Revert of Make a single GrSingleOwner in GrContext (patchset #3 id:40001 of https://codereview.chromium.org/1563703004/ )
>
> Reason for revert:
> breaking asan
>
> Original issue's description:
> > Make a single GrSingleOwner in GrContext
> >
> > TBR=bsalomon@google.com
> > BUG=skia:
> > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1563703004
> >
> > Committed: https://skia.googlesource.com/skia/+/f9bc796e0dbd31674c22b34761913ee6e8fdd66a
>
> TBR=robertphillips@google.com,joshualitt@chromium.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/94da292e39db0d41da08b1d6055ca5e0d6b498cc

TBR=robertphillips@google.com,joshualitt@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review URL: https://codereview.chromium.org/1572653002
2016-01-08 10:09:13 -08:00

56 lines
1.3 KiB
C++

/*
* 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
#include "SkTypes.h"
#ifdef SK_DEBUG
#include "SkMutex.h"
#include "SkThreadID.h"
// 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() {
SkAutoMutexAcquire lock(fMutex);
SkThreadID self = SkGetThreadID();
SkASSERT(fOwner == self || fOwner == kIllegalThreadID);
fReentranceCount++;
fOwner = self;
}
void exit() {
SkAutoMutexAcquire lock(fMutex);
SkASSERT(fOwner == SkGetThreadID());
fReentranceCount--;
if (fReentranceCount == 0) {
fOwner = kIllegalThreadID;
}
}
SkMutex fMutex;
SkThreadID fOwner; // guarded by fMutex
int fReentranceCount; // guarded by fMutex
};
#else
class GrSingleOwner {}; // Provide a dummy implementation so we can pass pointers to constructors
#endif
#endif