⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atomic.cpp

📁 C/C++ 多任务下的数据结构与算法 (周伟明)华中科技大学出版社
💻 CPP
字号:
/*
 * Copyright (c) 2006-2008
 * Author: Weiming Zhou
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  
 */
#if _WIN32 || _WIN64
#include <windows.h>
#endif

#include "CapiGlobal.h"

#if _WIN32 

extern "C" BOOL AtomicCAS(LONG volatile *dest, LONG newvalue, LONG oldvalue)
{
    LONG 	ret;
    ret = InterlockedCompareExchange(dest, newvalue, oldvalue);
    if ( ret == oldvalue )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

extern "C" BOOL AtomicCAS64(LONGLONG volatile *dest, LONGLONG newvalue, LONGLONG oldvalue)
{
    // not implement yet
    return FALSE;
}



#if 0
extern "C" BOOL AtomicCAS64(LONGLONG volatile *dest, LONGLONG newvalue, LONGLONG oldvalue)
{
    LONGLONG 	ret;
    ret = InterlockedCompareExchange64(dest, newvalue, oldvalue);
    if ( ret == oldvalue )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
#endif


#else

LONG AtomicWrite(LONG volatile *Target, LONG Value)
{
    LONG	Old;

    do {
        Old = *Target;

    }while (!AtomicCAS(Target, Value, Old));

    return Old;
}

LONG AtomicIncrement(LONG volatile *Target)
{
    LONG	Old;

    do {
        Old = *Target;

    }while (!AtomicCAS(Target, Old + 1, Old));

    return Old;
}


LONG AtomicDecrement(LONG volatile *Target)
{
    LONG	Old;

    do {
        Old = *Target;

    }while (!AtomicCAS(Target, Old - 1, Old));

    return Old;
}

BOOL TAS(LONG volatile *value)
{
    LONG	ret;
    ret = AtomicCAS(value, 1, 0);
    if ( ret == 0 )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -