xref: /third_party/node/deps/uv/src/win/atomicops-inl.h (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the
61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
91cb0ef41Sopenharmony_ci *
101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software.
121cb0ef41Sopenharmony_ci *
131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
191cb0ef41Sopenharmony_ci * IN THE SOFTWARE.
201cb0ef41Sopenharmony_ci */
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef UV_WIN_ATOMICOPS_INL_H_
231cb0ef41Sopenharmony_ci#define UV_WIN_ATOMICOPS_INL_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#include "uv.h"
261cb0ef41Sopenharmony_ci#include "internal.h"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci/* Atomic set operation on char */
301cb0ef41Sopenharmony_ci#ifdef _MSC_VER /* MSVC */
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci/* _InterlockedOr8 is supported by MSVC on x32 and x64. It is slightly less
331cb0ef41Sopenharmony_ci * efficient than InterlockedExchange, but InterlockedExchange8 does not exist,
341cb0ef41Sopenharmony_ci * and interlocked operations on larger targets might require the target to be
351cb0ef41Sopenharmony_ci * aligned. */
361cb0ef41Sopenharmony_ci#pragma intrinsic(_InterlockedOr8)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cistatic char INLINE uv__atomic_exchange_set(char volatile* target) {
391cb0ef41Sopenharmony_ci  return _InterlockedOr8(target, 1);
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci#else /* GCC, Clang in mingw mode */
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cistatic inline char uv__atomic_exchange_set(char volatile* target) {
451cb0ef41Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__)
461cb0ef41Sopenharmony_ci  /* Mingw-32 version, hopefully this works for 64-bit gcc as well. */
471cb0ef41Sopenharmony_ci  const char one = 1;
481cb0ef41Sopenharmony_ci  char old_value;
491cb0ef41Sopenharmony_ci  __asm__ __volatile__ ("lock xchgb %0, %1\n\t"
501cb0ef41Sopenharmony_ci                        : "=r"(old_value), "=m"(*target)
511cb0ef41Sopenharmony_ci                        : "0"(one), "m"(*target)
521cb0ef41Sopenharmony_ci                        : "memory");
531cb0ef41Sopenharmony_ci  return old_value;
541cb0ef41Sopenharmony_ci#else
551cb0ef41Sopenharmony_ci  return __sync_fetch_and_or(target, 1);
561cb0ef41Sopenharmony_ci#endif
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci#endif
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci#endif /* UV_WIN_ATOMICOPS_INL_H_ */
62