1d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com/*
2d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * Copyright 2013 Google Inc.
3d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com *
4d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * Use of this source code is governed by a BSD-style license that can be
5d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * found in the LICENSE file.
6d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com */
7d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
8d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#ifndef SkAtomics_win_DEFINED
9d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#define SkAtomics_win_DEFINED
10d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
11d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com/** Windows Interlocked atomics. */
12d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
13d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#include <intrin.h>
14d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#include <stdint.h>
15d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
16d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com//MSDN says in order to declare an interlocked function for use as an
17d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com//intrinsic, include intrin.h and put the function in a #pragma intrinsic
18d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com//directive.
19d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com//The pragma appears to be unnecessary, but doesn't hurt.
20d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDecrement)
21d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#pragma intrinsic(_InterlockedCompareExchange)
22d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
23d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline int32_t sk_atomic_inc(int32_t* addr) {
24d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com    // InterlockedIncrement returns the new value, we want to return the old.
25d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com    return _InterlockedIncrement(reinterpret_cast<long*>(addr)) - 1;
26d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com}
27d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
2800a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomonstatic inline int64_t sk_atomic_inc(int64_t* addr) {
2900a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon    // InterlockedIncrement returns the new value, we want to return the old.
3000a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon    return InterlockedIncrement64(addr) - 1;
3100a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon}
3200a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon
33d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline int32_t sk_atomic_add(int32_t* addr, int32_t inc) {
34d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com    return _InterlockedExchangeAdd(reinterpret_cast<long*>(addr), static_cast<long>(inc));
35d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com}
36d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
37d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline int32_t sk_atomic_dec(int32_t* addr) {
38d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com    // InterlockedDecrement returns the new value, we want to return the old.
39d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com    return _InterlockedDecrement(reinterpret_cast<long*>(addr)) + 1;
40d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com}
41d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
42d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline void sk_membar_acquire__after_atomic_dec() { }
43d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
444b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.orgstatic inline bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after) {
454b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org    return _InterlockedCompareExchange(reinterpret_cast<long*>(addr), after, before) == before;
464b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org}
474b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org
4881496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.orgstatic inline void* sk_atomic_cas(void** addr, void* before, void* after) {
4981496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org    return InterlockedCompareExchangePointer(addr, after, before);
5081496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org}
5181496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org
52d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline void sk_membar_acquire__after_atomic_conditional_inc() { }
53d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com
54d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#endif
55