<commit_before>/* $Id$
 *
 * OpenMAMA: The open middleware agnostic messaging API
 * Copyright (C) 2011 NYSE Technologies, Inc.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#ifndef _WOMBAT_WINTERLOCKED_H
#define _WOMBAT_WINTERLOCKED_H

/*
 * This file contains a set of interlock functions for atomic access on scalar
 * variables.
 */

#include "wCommon.h"

/* *************************************************** */
/* Linux implementation. */
/* *************************************************** */

/* 32-bit atomic exchange. Returns previous value. */
static __inline__ uint32_t
axchg32 (uint32_t* ptr, uint32_t newVal)
{
    __asm__ volatile ("xchgl %k0, %1"
                      : "=r" (newVal)
                      : "m" (*ptr), "0" (newVal)
                      : "memory");
    return newVal;
}

/* 32-bit atomic increment. */
static __inline__ void
ainc32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; incl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

/* 32-bit atomic decrement. */
static __inline__ void
adec32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; decl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

/* *************************************************** */
/* Type Defines. */
/* *************************************************** */
/* The structure on linux. */
typedef uint32_t wInterlockedInt;

/**
 * This function will initialise a wInterlockedInt.
 *
 * @param[in] value Pointer to the item to be initialized.
 * @return 0 on success.
 */

WCOMMONINLINE int wInterlocked_initialize(wInterlockedInt *value)
{
    return 0;
}<commit_msg>COMMON: Support atomic operations on additional CPUs (#201)<commit_after>/* $Id$
 *
 * OpenMAMA: The open middleware agnostic messaging API
 * Copyright (C) 2011 NYSE Technologies, Inc.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#ifndef _WOMBAT_WINTERLOCKED_H
#define _WOMBAT_WINTERLOCKED_H

/*
 * This file contains a set of interlock functions for atomic access on scalar
 * variables.
 */

#include "wCommon.h"

/* *************************************************** */
/* Linux implementation. */
/* *************************************************** */

#if defined(__i386__) || defined(__x86_64__)

/* 32-bit atomic exchange. Returns previous value. */
static __inline__ uint32_t
axchg32 (uint32_t* ptr, uint32_t newVal)
{
    __asm__ volatile ("xchgl %k0, %1"
                      : "=r" (newVal)
                      : "m" (*ptr), "0" (newVal)
                      : "memory");
    return newVal;
}

/* 32-bit atomic increment. */
static __inline__ void
ainc32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; incl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

/* 32-bit atomic decrement. */
static __inline__ void
adec32 (uint32_t* ptr)
{
    __asm__ volatile ("lock; decl %0"
                      : "=m" (*ptr)
                      : "m" (*ptr));
}

#else

#ifdef __GNUC__

/* 32-bit atomic exchange. Returns previous value. */
static __inline__ uint32_t
axchg32 (uint32_t* ptr, uint32_t newVal)
{
    /* Warning: behavior not guaranteed to be consistent
       on all platforms.  Unit tests may be required to validate
       on each build.
       http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Atomic-Builtins.html
    */
    return __sync_lock_test_and_set(ptr, newVal);
}

/* 32-bit atomic increment. */
static __inline__ void
ainc32 (uint32_t* ptr)
{
    __sync_fetch_and_add(ptr, 1);
}

/* 32-bit atomic decrement. */
static __inline__ void
adec32 (uint32_t* ptr)
{
   __sync_fetch_and_sub(ptr, 1);
}

#else
#error "Unsupported CPU / compiler combination, please implement atomic functions"
#endif


#endif

/* *************************************************** */
/* Type Defines. */
/* *************************************************** */
/* The structure on linux. */
typedef uint32_t wInterlockedInt;

/**
 * This function will initialise a wInterlockedInt.
 *
 * @param[in] value Pointer to the item to be initialized.
 * @return 0 on success.
 */

WCOMMONINLINE int wInterlocked_initialize(wInterlockedInt *value)
{
    return 0;
}
{'fraction_non_alphanumeric': 0.1704852967707018, 'fraction_numerical': 0.023994226952913585, 'mean_word_length': 3.134228187919463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '658285', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1589, 'n_words': 587}