/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the qmake spec of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the 
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QATOMIC_VXWORKS_H
#define QATOMIC_VXWORKS_H

QT_BEGIN_HEADER

#if defined(__ppc)
#  include <QtCore/qatomic_powerpc.h>
#else

#include "vxWorks.h"
#include "vxAtomicLib.h"

#if defined(QT_OPENGL_ES) && defined(_WRS_KERNEL)
extern "C" int taskLock();
extern "C" int taskUnlock();
#define VX_OPENGL_ES
#endif

QT_BEGIN_NAMESPACE

#define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE

inline bool QBasicAtomicInt::isReferenceCountingNative()
{ return false; }
inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
{ return false; }

#define Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE

inline bool QBasicAtomicInt::isTestAndSetNative()
{ return false; }
inline bool QBasicAtomicInt::isTestAndSetWaitFree()
{ return false; }

#define Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE

inline bool QBasicAtomicInt::isFetchAndStoreNative()
{ return false; }
inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
{ return false; }

#define Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE

inline bool QBasicAtomicInt::isFetchAndAddNative()
{ return false; }
inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
{ return false; }

#define Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
{ return false; }
template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
{ return false; }

#define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_NOT_NATIVE

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
{ return false; }
template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
{ return false; }

#define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
{ return false; }
template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
{ return false; }

// Reference counting

inline bool QBasicAtomicInt::ref()
{
#ifdef VX_OPENGL_ES
    taskLock();
    bool ret = (++_q_value != 0);
    taskUnlock();
    return ret;
#else
    vxAtomicInc((atomic_t *) &_q_value);
    return (_q_value != 0);
#endif
}

inline bool QBasicAtomicInt::deref()
{
#ifdef VX_OPENGL_ES
    taskLock();
    bool ret = (--_q_value != 0);
    taskUnlock();
    return ret;
#else
    vxAtomicDec((atomic_t *) &_q_value);
    return (_q_value != 0);
#endif
}

// Test-and-set for integers

inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
{
#ifdef VX_OPENGL_ES
    if (_q_value == expectedValue) {
        _q_value = newValue;
        taskUnlock();
        return true;
    }
    taskUnlock();
    return false;
#else
    if (vxAtomicCas((atomic_t *) &_q_value, (atomicVal_t)expectedValue, (atomicVal_t)newValue))
        return true;
    return false;
#endif
}

inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

// Fetch-and-store for integers

inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
{
#ifdef VX_OPENGL_ES
    taskLock();
    int returnValue = _q_value;
    _q_value = newValue;
    taskUnlock();
    return returnValue;
#else
    atomicVal_t returnValue = vxAtomicSet((atomic_t *) &_q_value, (atomicVal_t)newValue);
    return (int)returnValue;
#endif
}

inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
{
    return fetchAndStoreOrdered(newValue);
}

inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
{
    return fetchAndStoreOrdered(newValue);
}

inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
{
    return fetchAndStoreOrdered(newValue);
}

// Fetch-and-add for integers

inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
{
#ifdef VX_OPENGL_ES
    taskLock();
    int originalValue = _q_value;
    _q_value += valueToAdd;
    taskUnlock();
    return originalValue;
#else
    atomicVal_t returnValue = vxAtomicAdd((atomic_t *) &_q_value, (atomicVal_t)valueToAdd);
    return (int)returnValue;
#endif
}

inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

// Test and set for pointers

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
{
#ifdef VX_OPENGL_ES
    taskLock();
    if (_q_value == expectedValue) {
        _q_value = newValue;
        taskUnlock();
        return true;
    }
    taskUnlock();
    return false;
#else
    if (vxAtomicCas((atomic_t *) &_q_value, (atomicVal_t)expectedValue, (atomicVal_t)newValue))
        return true;
    return false;
#endif
}

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

template <typename T>
Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
{
    return testAndSetOrdered(expectedValue, newValue);
}

// Fetch and store for pointers

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
{
#ifdef VX_OPENGL_ES
    taskLock();
    T *returnValue = (_q_value);
    _q_value = newValue;
    taskUnlock();
    return returnValue;
#else
    atomicVal_t returnValue = vxAtomicSet((atomic_t *) &_q_value, (atomicVal_t)newValue);
    return reinterpret_cast<T *>(returnValue);
#endif
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
{
    return fetchAndStoreOrdered(newValue);
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
{
    return fetchAndStoreOrdered(newValue);
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
{
    return fetchAndStoreOrdered(newValue);
}

// Fetch and add for pointers

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
{
#ifdef VX_OPENGL_ES
    taskLock();
    T *returnValue = (_q_value);
    _q_value += valueToAdd;
    taskUnlock();
    return returnValue;
#else
    atomicVal_t returnValue = vxAtomicAdd((atomic_t *) &_q_value, (atomicVal_t)valueToAdd * sizeof(T));
    return reinterpret_cast<T *>(returnValue);
#endif
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

template <typename T>
Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
{
    return fetchAndAddOrdered(valueToAdd);
}

QT_END_NAMESPACE

#endif

QT_END_HEADER

#endif // QATOMIC_VXWORKS_H
