Commit 6563707a authored by jselbie's avatar jselbie

Fix for compiler target i386

parent 198791c0
include ../common.inc
PROJECT_TARGET := libcommon.a
PROJECT_SRCS := cmdlineparser.cpp common.cpp fasthash.cpp getconsolewidth.cpp getmillisecondcounter.cpp logger.cpp prettyprint.cpp refcountobject.cpp stringhelper.cpp
PROJECT_SRCS := atomichelpers.cpp cmdlineparser.cpp common.cpp fasthash.cpp getconsolewidth.cpp getmillisecondcounter.cpp logger.cpp prettyprint.cpp refcountobject.cpp stringhelper.cpp
PROJECT_OBJS := $(subst .cpp,.o,$(PROJECT_SRCS))
INCLUDES := $(BOOST_INCLUDE)
PRECOMP_H_GCH := commonincludes.hpp.gch
......
/*
Copyright 2011 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "commonincludes.hpp"
#ifdef ATOMICS_ARE_DEFINED
extern "C" unsigned int xadd_4(volatile void* pVal, unsigned int inc)
{
unsigned int result;
unsigned int* pValInt = (unsigned int*)pVal;
asm volatile(
"lock; xaddl %%eax, %2;"
:"=a" (result)
: "a" (inc), "m" (*pValInt)
:"memory" );
return (result);
}
extern "C" unsigned int __sync_add_and_fetch_4(volatile void* pVal, unsigned int inc)
{
return (xadd_4(pVal, inc) + inc);
}
extern "C" unsigned int __sync_sub_and_fetch_4(volatile void* pVal, unsigned int inc)
{
return (xadd_4(pVal, -inc) - inc);
}
extern "C" unsigned int __sync_fetch_and_add_4(volatile void* pVal, unsigned int inc)
{
return xadd_4(pVal, inc);
}
extern "C" unsigned int __sync_fetch_and_sub_4(volatile void* pVal, unsigned int inc)
{
return xadd_4(pVal, -inc);
}
#ifdef __GNUC__
#pragma message "atomichelpers.cpp: Defining sync_add_and_fetch helpers for i386 compile"
#endif
#endif
int AtomicIncrement(int* pInt)
{
COMPILE_TIME_ASSERT(sizeof(int)==4);
// InterlockedIncrement
return __sync_add_and_fetch(pInt, 1);
}
int AtomicDecrement(int* pInt)
{
// InterlockedDecrement
return __sync_sub_and_fetch(pInt, 1);
}
/*
Copyright 2013 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef ATOMICHELPERS_H
#define ATOMICHELPERS_H
typedef unsigned int (*xaddFunctionType)(volatile void*, unsigned int);
#if defined(i386) || defined(__i386__) || defined(__i386)
#define ATOMICS_ARE_DEFINED
extern "C" unsigned int xadd_4(volatile void* pVal, unsigned int inc);
extern "C" unsigned int __sync_add_and_fetch_4(volatile void* pVal, unsigned int inc);
extern "C" unsigned int __sync_sub_and_fetch_4(volatile void* pVal, unsigned int inc);
extern "C" unsigned int __sync_fetch_and_add_4(volatile void* pVal, unsigned int inc);
extern "C" unsigned int __sync_fetch_and_sub_4(volatile void* pVal, unsigned int inc);
#endif
int AtomicIncrement(int* pInt);
int AtomicDecrement(int* pInt);
#endif /* ATOMICHELPERS_H */
......@@ -18,17 +18,8 @@
#include "commonincludes.hpp"
#include "refcountobject.h"
int AtomicIncrement(int* pInt)
{
// InterlockedIncrement
return __sync_add_and_fetch(pInt, 1);
}
int AtomicDecrement(int* pInt)
{
// InterlockedDecrement
return __sync_sub_and_fetch(pInt, 1);
}
#include "atomichelpers.h"
CBasicRefCount::CBasicRefCount()
......
......@@ -21,8 +21,6 @@
int AtomicIncrement(int* pInt);
int AtomicDecrement(int* pInt);
class IRefCounted
......
......@@ -19,6 +19,7 @@
#include "commonincludes.hpp"
#include "stringhelper.h"
#include "atomichelpers.h"
#include "stunbuilder.h"
#include <boost/crc.hpp>
......@@ -118,7 +119,7 @@ HRESULT CStunMessageBuilder::AddRandomTransactionId(StunTransactionId* pTransId)
entropy ^= getpid();
entropy ^= reinterpret_cast<uintptr_t>(this);
entropy ^= time(NULL);
entropy ^= __sync_fetch_and_add(&g_sequence_number, 1);
entropy ^= AtomicIncrement(&g_sequence_number);
}
#endif
......
include ../common.inc
PROJECT_TARGET := stuntestcode
PROJECT_OBJS := testbuilder.o testclientlogic.o testcmdline.o testcode.o testdatastream.o testfasthash.o testintegrity.o testmessagehandler.o testpolling.o testreader.o testrecvfromex.o
PROJECT_OBJS := testatomichelpers.o testbuilder.o testclientlogic.o testcmdline.o testcode.o testdatastream.o testfasthash.o testintegrity.o testmessagehandler.o testpolling.o testreader.o testrecvfromex.o
INCLUDES := $(BOOST_INCLUDE) $(OPENSSL_INCLUDE) -I../common -I../stuncore -I../networkutils
LIB_PATH := -L../networkutils -L../stuncore -L../common
......
/*
Copyright 2013 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "commonincludes.hpp"
#include "testatomichelpers.h"
#include "atomichelpers.h"
HRESULT CTestAtomicHelpers::Run()
{
HRESULT hr = S_OK;
int value = -2000;
int nextexpected = -2000;
int result = 0;
while (value < 2000)
{
nextexpected++;
result = AtomicIncrement(&value);
ChkIf(result != nextexpected, E_UNEXPECTED);
ChkIf(result != value, E_UNEXPECTED);
}
value = 2000;
nextexpected = 2000;
while (value > -2000)
{
nextexpected--;
result = AtomicDecrement(&value);
ChkIf(result != nextexpected, E_UNEXPECTED);
ChkIf(result != value, E_UNEXPECTED);
}
Cleanup:
return hr;
}
\ No newline at end of file
/*
Copyright 2013 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef TESTATOMICHELPERS_H
#define TESTATOMICHELPERS_H
#include "unittest.h"
class CTestAtomicHelpers : public IUnitTest
{
public:
virtual HRESULT Run();
UT_DECLARE_TEST_NAME("CTestAtomicHelpers");
};
#endif /* TESTATOMICHELPERS_H */
......@@ -32,6 +32,7 @@
#include "prettyprint.h"
#include "polling.h"
#include "testpolling.h"
#include "testatomichelpers.h"
void ReaderFuzzTest()
{
......@@ -82,6 +83,7 @@ void RunUnitTests()
boost::shared_ptr<CTestRecvFromExIPV6> spTestRecvFromEx6(new CTestRecvFromExIPV6);
boost::shared_ptr<CTestFastHash> spTestFastHash(new CTestFastHash);
boost::shared_ptr<CTestPolling> spTestPolling(new CTestPolling);
boost::shared_ptr<CTestAtomicHelpers> spTestAtomicHelpers(new CTestAtomicHelpers);
vecTests.push_back(spTestDataStream.get());
vecTests.push_back(spTestReader.get());
......@@ -94,6 +96,7 @@ void RunUnitTests()
vecTests.push_back(spTestRecvFromEx6.get());
vecTests.push_back(spTestFastHash.get());
vecTests.push_back(spTestPolling.get());
vecTests.push_back(spTestAtomicHelpers.get());
for (size_t index = 0; index < vecTests.size(); index++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment