remove sk_atomic_fetch_add

Change-Id: Ia400db2b73a8061668e62f8e961538a060b216a1
Reviewed-on: https://skia-review.googlesource.com/c/174282
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
Mike Klein 2018-12-04 10:11:16 -05:00 committed by Skia Commit-Bot
parent 497b3680a9
commit 8912a5d240
2 changed files with 13 additions and 34 deletions

View File

@ -11,25 +11,17 @@
#include "SkTypes.h" #include "SkTypes.h"
#include <atomic> #include <atomic>
// ~~~~~~~~ Legacy APIs ~~~~~~~~~
//
// Please use types from <atomic> for any new code.
// That's all this file ends up doing under the hood.
template <typename T>
T sk_atomic_fetch_add(T* ptr, T val, std::memory_order mo = std::memory_order_seq_cst) {
// All values of mo are valid.
std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
return std::atomic_fetch_add_explicit(ap, val, mo);
}
// ~~~~~~~~ Very Legacy APIs ~~~~~~~~~ // ~~~~~~~~ Very Legacy APIs ~~~~~~~~~
// //
// Here are shims for our very old atomics API, to be weaned off of. They use // Here are shims for our very old atomics API, to be weaned off of. They use
// sequentially-consistent memory order to match historical behavior, but most // sequentially-consistent memory order to match historical behavior, but most
// of the callers could perform better with explicit, weaker memory ordering. // of the callers could perform better with explicit, weaker memory ordering.
inline int32_t sk_atomic_inc(int32_t* ptr) { return sk_atomic_fetch_add(ptr, +1); } inline int32_t sk_atomic_inc(int32_t* ptr) {
inline int32_t sk_atomic_dec(int32_t* ptr) { return sk_atomic_fetch_add(ptr, -1); } return reinterpret_cast<std::atomic<int32_t>*>(ptr)->fetch_add(+1);
}
inline int32_t sk_atomic_dec(int32_t* ptr) {
return reinterpret_cast<std::atomic<int32_t>*>(ptr)->fetch_add(-1);
}
#endif//SkAtomics_DEFINED #endif//SkAtomics_DEFINED

View File

@ -5,32 +5,26 @@
* found in the LICENSE file. * found in the LICENSE file.
*/ */
#include "SkAtomics.h"
#include "SkBitmapCache.h" #include "SkBitmapCache.h"
#include "SkMutex.h" #include "SkMutex.h"
#include "SkNextID.h"
#include "SkPixelRef.h" #include "SkPixelRef.h"
#include "SkTraceEvent.h" #include "SkTraceEvent.h"
#include <atomic>
//#define SK_TRACE_PIXELREF_LIFETIME
#include "SkNextID.h"
uint32_t SkNextID::ImageID() { uint32_t SkNextID::ImageID() {
static uint32_t gID = 0; // We never set the low bit.... see SkPixelRef::genIDIsUnique().
static std::atomic<uint32_t> nextID{2};
uint32_t id; uint32_t id;
// Loop in case our global wraps around, as we never want to return a 0.
do { do {
id = sk_atomic_fetch_add(&gID, 2u) + 2; // Never set the low bit. id = nextID.fetch_add(2);
} while (0 == id); } while (id == 0);
return id; return id;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifdef SK_TRACE_PIXELREF_LIFETIME
static int32_t gInstCounter;
#endif
SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes) SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes)
: fWidth(width) : fWidth(width)
, fHeight(height) , fHeight(height)
@ -38,18 +32,11 @@ SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes)
, fRowBytes(rowBytes) , fRowBytes(rowBytes)
, fAddedToCache(false) , fAddedToCache(false)
{ {
#ifdef SK_TRACE_PIXELREF_LIFETIME
SkDebugf(" pixelref %d\n", sk_atomic_inc(&gInstCounter));
#endif
this->needsNewGenID(); this->needsNewGenID();
fMutability = kMutable; fMutability = kMutable;
} }
SkPixelRef::~SkPixelRef() { SkPixelRef::~SkPixelRef() {
#ifdef SK_TRACE_PIXELREF_LIFETIME
SkDebugf("~pixelref %d\n", sk_atomic_dec(&gInstCounter) - 1);
#endif
this->callGenIDChangeListeners(); this->callGenIDChangeListeners();
} }