/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/*
 * cas.c
 * Copyright (C) 2015 Alexander Vdolainen <avdolainen@gmail.com>
 *
 * libtdata is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libtdata is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
 */

#include <stdlib.h>
#include <stdint.h>

#include "../config.h"

#if !defined (HAVE__SYNC_BOOL_COMPARE_AND_SWAP_8)
#include <pthread.h>
static pthread_mutex_t __sync_lock = PTHREAD_MUTEX_INITIALIZER;
#endif

#ifndef HAVE__SYNC_BOOL_COMPARE_AND_SWAP_8
_Bool __sync_bool_compare_and_swap_8 (uint64_t*, uint64_t, uint64_t)
__attribute__ ((visibility ("hidden")));

_Bool __sync_bool_compare_and_swap_8 (uint64_t* ptr, uint64_t old, uint64_t new)
{
  int i;
  _Bool ret;

  i = pthread_mutex_lock(&__sync_lock);

  if(*ptr != old)    ret = 0;
  else {
    *ptr = new;
    ret = 1;
  }

  i = pthread_mutex_unlock(&__sync_lock);

  return ret;
}
#endif