aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/GrSingleOwner.h
blob: 64e63d3b19f8bbfb9a5af32e4d9447ad5f301462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * 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