15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Protocol Buffers - Google's data interchange format
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2012 Google Inc.  All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://code.google.com/p/protobuf/
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The compilation of extension_set.cc fails when windows.h is included.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Therefore we move the code depending on windows.h to this separate cc file.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Don't compile this file for people not concerned about thread safety.
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/stubs/atomicops.h>
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_X86_MSVC_H_
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace google {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace protobuf {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline void MemoryBarrier() {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We use MemoryBarrier from WinNT.h
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ::MemoryBarrier();
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic32 old_value,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic32 new_value) {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LONG result = InterlockedCompareExchange(
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<volatile LONG*>(ptr),
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<LONG>(new_value),
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<LONG>(old_value));
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<Atomic32>(result);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic32 new_value) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LONG result = InterlockedExchange(
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<volatile LONG*>(ptr),
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<LONG>(new_value));
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<Atomic32>(result);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 Atomic32 increment) {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return InterlockedExchangeAdd(
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<volatile LONG*>(ptr),
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<LONG>(increment)) + increment;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(_WIN64)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 64-bit low-level operations on 64-bit platform.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic64 old_value,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic64 new_value) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PVOID result = InterlockedCompareExchangePointer(
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reinterpret_cast<volatile PVOID*>(ptr),
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reinterpret_cast<PVOID>(new_value), reinterpret_cast<PVOID>(old_value));
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return reinterpret_cast<Atomic64>(result);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  Atomic64 new_value) {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PVOID result = InterlockedExchangePointer(
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reinterpret_cast<volatile PVOID*>(ptr),
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    reinterpret_cast<PVOID>(new_value));
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return reinterpret_cast<Atomic64>(result);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 Atomic64 increment) {
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return InterlockedExchangeAdd64(
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<volatile LONGLONG*>(ptr),
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<LONGLONG>(increment)) + increment;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // defined(_WIN64)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace internal
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace protobuf
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace google
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_X86_MSVC_H_
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif  // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
113