u_atomic.c revision b4219da8
11.39Sperry/*
21.23Ssimonb * Copyright © 2017 Gražvydas Ignotas
31.23Ssimonb *
41.23Ssimonb * Permission is hereby granted, free of charge, to any person obtaining a
51.23Ssimonb * copy of this software and associated documentation files (the "Software"),
61.23Ssimonb * to deal in the Software without restriction, including without limitation
71.23Ssimonb * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81.23Ssimonb * and/or sell copies of the Software, and to permit persons to whom the
91.23Ssimonb * Software is furnished to do so, subject to the following conditions:
101.23Ssimonb *
111.23Ssimonb * The above copyright notice and this permission notice (including the next
121.23Ssimonb * paragraph) shall be included in all copies or substantial portions of the
131.23Ssimonb * Software.
141.23Ssimonb *
151.23Ssimonb * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161.23Ssimonb * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171.23Ssimonb * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181.23Ssimonb * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191.23Ssimonb * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201.23Ssimonb * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
211.23Ssimonb * IN THE SOFTWARE.
221.23Ssimonb */
231.23Ssimonb
241.23Ssimonb/* #if defined(MISSING_64BIT_ATOMICS) && defined(HAVE_PTHREAD) */
251.23Ssimonb#include <sys/types.h>
261.23Ssimonb#ifndef __HAVE_ATOMIC64_OPS
271.23Ssimonb
281.23Ssimonb#include <stdint.h>
291.23Ssimonb#include <pthread.h>
301.23Ssimonb
311.23Ssimonb#if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__)
321.23Ssimonb#define WEAK __attribute__((weak))
331.23Ssimonb#else
341.23Ssimonb#define WEAK
351.23Ssimonb#endif
361.23Ssimonb
371.23Ssimonbstatic pthread_mutex_t sync_mutex = PTHREAD_MUTEX_INITIALIZER;
381.8Scgd
391.1ScgdWEAK long long
401.6Scgd__atomic_fetch_add_8(volatile long long *ptr, long long val, int memorder)
411.6Scgd{
421.1Scgd   long long r;
431.6Scgd
441.6Scgd   pthread_mutex_lock(&sync_mutex);
451.6Scgd   *ptr += val;
461.6Scgd   r = *ptr;
471.6Scgd   pthread_mutex_unlock(&sync_mutex);
481.6Scgd
491.6Scgd   return r;
501.6Scgd}
511.6Scgd
521.6ScgdWEAK long long
531.6Scgd__atomic_fetch_sub_8(volatile long long *ptr, long long val, int memorder)
541.6Scgd{
551.6Scgd   long long r;
561.6Scgd
571.6Scgd   pthread_mutex_lock(&sync_mutex);
581.6Scgd   *ptr -= val;
591.6Scgd   r = *ptr;
601.6Scgd   pthread_mutex_unlock(&sync_mutex);
611.6Scgd
621.1Scgd   return r;
631.1Scgd}
641.38Sperry
651.1ScgdWEAK long long
661.23Ssimonb__sync_val_compare_and_swap_8(volatile long long *ptr, long long oldval, long long newval)
671.1Scgd{
681.1Scgd   long long r;
691.1Scgd
701.1Scgd   pthread_mutex_lock(&sync_mutex);
711.1Scgd   r = *ptr;
721.11Sthorpej   if (*ptr == oldval)
731.11Sthorpej      *ptr = newval;
741.14Slukem   pthread_mutex_unlock(&sync_mutex);
751.11Sthorpej
761.11Sthorpej   return r;
771.11Sthorpej}
781.11Sthorpej
791.14Slukem#endif
801.11Sthorpej