mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 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 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 8 | #include "SkTLS.h" |
| 9 | |
reed@google.com | 7b57892 | 2012-05-21 15:29:27 +0000 | [diff] [blame] | 10 | // enable to help debug TLS storage |
| 11 | //#define SK_TRACE_TLS_LIFETIME |
| 12 | |
| 13 | |
| 14 | #ifdef SK_TRACE_TLS_LIFETIME |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame^] | 15 | #include "SkAtomics.h" |
reed@google.com | 7b57892 | 2012-05-21 15:29:27 +0000 | [diff] [blame] | 16 | static int32_t gTLSRecCount; |
| 17 | #endif |
| 18 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 19 | struct SkTLSRec { |
| 20 | SkTLSRec* fNext; |
| 21 | void* fData; |
| 22 | SkTLS::CreateProc fCreateProc; |
| 23 | SkTLS::DeleteProc fDeleteProc; |
| 24 | |
reed@google.com | 7b57892 | 2012-05-21 15:29:27 +0000 | [diff] [blame] | 25 | #ifdef SK_TRACE_TLS_LIFETIME |
| 26 | SkTLSRec() { |
| 27 | int n = sk_atomic_inc(&gTLSRecCount); |
| 28 | SkDebugf(" SkTLSRec[%d]\n", n); |
| 29 | } |
| 30 | #endif |
| 31 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 32 | ~SkTLSRec() { |
| 33 | if (fDeleteProc) { |
| 34 | fDeleteProc(fData); |
| 35 | } |
| 36 | // else we leak fData, or it will be managed by the caller |
reed@google.com | 7b57892 | 2012-05-21 15:29:27 +0000 | [diff] [blame] | 37 | |
| 38 | #ifdef SK_TRACE_TLS_LIFETIME |
| 39 | int n = sk_atomic_dec(&gTLSRecCount); |
| 40 | SkDebugf("~SkTLSRec[%d]\n", n - 1); |
| 41 | #endif |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 42 | } |
| 43 | }; |
| 44 | |
| 45 | void SkTLS::Destructor(void* ptr) { |
reed@google.com | 7b57892 | 2012-05-21 15:29:27 +0000 | [diff] [blame] | 46 | #ifdef SK_TRACE_TLS_LIFETIME |
| 47 | SkDebugf("SkTLS::Destructor(%p)\n", ptr); |
| 48 | #endif |
| 49 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 50 | SkTLSRec* rec = (SkTLSRec*)ptr; |
| 51 | do { |
| 52 | SkTLSRec* next = rec->fNext; |
| 53 | SkDELETE(rec); |
| 54 | rec = next; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 55 | } while (rec); |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void* SkTLS::Get(CreateProc createProc, DeleteProc deleteProc) { |
| 59 | if (NULL == createProc) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
reed@google.com | 331e2dc | 2012-05-08 21:45:03 +0000 | [diff] [blame] | 63 | void* ptr = SkTLS::PlatformGetSpecific(true); |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 64 | |
| 65 | if (ptr) { |
| 66 | const SkTLSRec* rec = (const SkTLSRec*)ptr; |
| 67 | do { |
| 68 | if (rec->fCreateProc == createProc) { |
| 69 | SkASSERT(rec->fDeleteProc == deleteProc); |
| 70 | return rec->fData; |
| 71 | } |
| 72 | } while ((rec = rec->fNext) != NULL); |
| 73 | // not found, so create a new one |
| 74 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 75 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 76 | // add a new head of our change |
| 77 | SkTLSRec* rec = new SkTLSRec; |
| 78 | rec->fNext = (SkTLSRec*)ptr; |
| 79 | |
| 80 | SkTLS::PlatformSetSpecific(rec); |
| 81 | |
| 82 | rec->fData = createProc(); |
| 83 | rec->fCreateProc = createProc; |
| 84 | rec->fDeleteProc = deleteProc; |
| 85 | return rec->fData; |
| 86 | } |
| 87 | |
| 88 | void* SkTLS::Find(CreateProc createProc) { |
| 89 | if (NULL == createProc) { |
| 90 | return NULL; |
| 91 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 92 | |
reed@google.com | 331e2dc | 2012-05-08 21:45:03 +0000 | [diff] [blame] | 93 | void* ptr = SkTLS::PlatformGetSpecific(false); |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 94 | |
| 95 | if (ptr) { |
| 96 | const SkTLSRec* rec = (const SkTLSRec*)ptr; |
| 97 | do { |
| 98 | if (rec->fCreateProc == createProc) { |
| 99 | return rec->fData; |
| 100 | } |
| 101 | } while ((rec = rec->fNext) != NULL); |
| 102 | } |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | void SkTLS::Delete(CreateProc createProc) { |
| 107 | if (NULL == createProc) { |
| 108 | return; |
| 109 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 110 | |
reed@google.com | 331e2dc | 2012-05-08 21:45:03 +0000 | [diff] [blame] | 111 | void* ptr = SkTLS::PlatformGetSpecific(false); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 112 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 113 | SkTLSRec* curr = (SkTLSRec*)ptr; |
| 114 | SkTLSRec* prev = NULL; |
| 115 | while (curr) { |
| 116 | SkTLSRec* next = curr->fNext; |
| 117 | if (curr->fCreateProc == createProc) { |
| 118 | if (prev) { |
| 119 | prev->fNext = next; |
| 120 | } else { |
| 121 | // we have a new head of our chain |
| 122 | SkTLS::PlatformSetSpecific(next); |
| 123 | } |
| 124 | SkDELETE(curr); |
| 125 | break; |
| 126 | } |
| 127 | prev = curr; |
| 128 | curr = next; |
| 129 | } |
| 130 | } |