12f083884Ss.makeev_local // The MIT License (MIT)
22f083884Ss.makeev_local //
32f083884Ss.makeev_local // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
42f083884Ss.makeev_local //
52f083884Ss.makeev_local // 	Permission is hereby granted, free of charge, to any person obtaining a copy
62f083884Ss.makeev_local // 	of this software and associated documentation files (the "Software"), to deal
72f083884Ss.makeev_local // 	in the Software without restriction, including without limitation the rights
82f083884Ss.makeev_local // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
92f083884Ss.makeev_local // 	copies of the Software, and to permit persons to whom the Software is
102f083884Ss.makeev_local // 	furnished to do so, subject to the following conditions:
112f083884Ss.makeev_local //
122f083884Ss.makeev_local //  The above copyright notice and this permission notice shall be included in
132f083884Ss.makeev_local // 	all copies or substantial portions of the Software.
142f083884Ss.makeev_local //
152f083884Ss.makeev_local // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162f083884Ss.makeev_local // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172f083884Ss.makeev_local // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
182f083884Ss.makeev_local // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192f083884Ss.makeev_local // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
202f083884Ss.makeev_local // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
212f083884Ss.makeev_local // 	THE SOFTWARE.
222f083884Ss.makeev_local 
232f083884Ss.makeev_local #include "Tests.h"
24c307a34bSSergey Makeev #include <math.h>
252f083884Ss.makeev_local #include <UnitTest++.h>
262f083884Ss.makeev_local #include <MTAtomic.h>
27c307a34bSSergey Makeev #include <MTScheduler.h>
282f083884Ss.makeev_local 
292f083884Ss.makeev_local 
SUITE(AtomicTests)302f083884Ss.makeev_local SUITE(AtomicTests)
312f083884Ss.makeev_local {
322f083884Ss.makeev_local 	static const int OLD_VALUE = 1;
332f083884Ss.makeev_local 	static const int VALUE = 13;
342f083884Ss.makeev_local 	static const int NEW_VALUE = 16;
352f083884Ss.makeev_local 	static const int RELAXED_VALUE = 27;
362f083884Ss.makeev_local 
372f083884Ss.makeev_local 	void TestStatics()
382f083884Ss.makeev_local 	{
392f083884Ss.makeev_local 		// This variables must be placed to .data / .bss section
402f083884Ss.makeev_local 		//
412f083884Ss.makeev_local 		// From "Cpp Standard"
422f083884Ss.makeev_local 		//
432f083884Ss.makeev_local 		// 6.7 Declaration statement
442f083884Ss.makeev_local 		//
452f083884Ss.makeev_local 		// 4 The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage
462f083884Ss.makeev_local 		//   duration (3.7.2) is performed before any other initialization takes place.
472f083884Ss.makeev_local 		//   Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable,
482f083884Ss.makeev_local 		//   is performed before its block is first entered.
492f083884Ss.makeev_local 		//
502f083884Ss.makeev_local 		static MT::Atomic32Base<int32> test = { 0 };
512f083884Ss.makeev_local 		static MT::AtomicPtrBase<void> pTest = { nullptr };
522f083884Ss.makeev_local 
532f083884Ss.makeev_local 		test.Store(13);
542f083884Ss.makeev_local 		pTest.Store(nullptr);
552f083884Ss.makeev_local 
562f083884Ss.makeev_local 		CHECK_EQUAL(13, test.Load());
572f083884Ss.makeev_local 		CHECK(pTest.Load() == nullptr);
582f083884Ss.makeev_local 	}
592f083884Ss.makeev_local 
602f083884Ss.makeev_local TEST(AtomicSimpleTest)
612f083884Ss.makeev_local {
622f083884Ss.makeev_local 	TestStatics();
632f083884Ss.makeev_local 
642f083884Ss.makeev_local 	MT::Atomic32<int32> test_relaxed;
652f083884Ss.makeev_local 	test_relaxed.StoreRelaxed(RELAXED_VALUE);
662f083884Ss.makeev_local 	CHECK(test_relaxed.Load() == RELAXED_VALUE);
672f083884Ss.makeev_local 
682f083884Ss.makeev_local 	MT::Atomic32<int32> test;
692f083884Ss.makeev_local 	test.Store(OLD_VALUE);
702f083884Ss.makeev_local 	CHECK(test.Load() == OLD_VALUE);
712f083884Ss.makeev_local 
722f083884Ss.makeev_local 	int prevValue = test.Exchange(VALUE);
732f083884Ss.makeev_local 	CHECK(test.Load() == VALUE);
742f083884Ss.makeev_local 	CHECK(prevValue == OLD_VALUE);
752f083884Ss.makeev_local 
762f083884Ss.makeev_local 	int nowValue = test.IncFetch();
772f083884Ss.makeev_local 	CHECK(nowValue == (VALUE+1));
782f083884Ss.makeev_local 
792f083884Ss.makeev_local 	nowValue = test.DecFetch();
802f083884Ss.makeev_local 	CHECK(nowValue == VALUE);
812f083884Ss.makeev_local 
822f083884Ss.makeev_local 	nowValue = test.AddFetch(VALUE);
832f083884Ss.makeev_local 	CHECK(nowValue == (VALUE+VALUE));
842f083884Ss.makeev_local 
852f083884Ss.makeev_local 	MT::Atomic32<int32> test2(VALUE);
862f083884Ss.makeev_local 	CHECK(test2.Load() == VALUE);
872f083884Ss.makeev_local 
882f083884Ss.makeev_local 	int prevResult = test2.CompareAndSwap(NEW_VALUE, OLD_VALUE);
892f083884Ss.makeev_local 	CHECK(prevResult == VALUE);
902f083884Ss.makeev_local 	CHECK(test2.Load() == VALUE);
912f083884Ss.makeev_local 
922f083884Ss.makeev_local 	prevResult = test2.CompareAndSwap(VALUE, NEW_VALUE);
932f083884Ss.makeev_local 	CHECK(prevResult == VALUE);
942f083884Ss.makeev_local 	CHECK(test2.Load() == NEW_VALUE);
952f083884Ss.makeev_local 
962f083884Ss.makeev_local 	MT::Atomic32<uint32> test3(UINT32_MAX);
972f083884Ss.makeev_local 	CHECK_EQUAL(UINT32_MAX, test3.Load());
982f083884Ss.makeev_local 
992f083884Ss.makeev_local 	//check for wraps
1002f083884Ss.makeev_local 	uint32 uNowValue = test3.IncFetch();
1012f083884Ss.makeev_local 	CHECK(uNowValue == 0);
1022f083884Ss.makeev_local 
1032f083884Ss.makeev_local 	uNowValue = test3.DecFetch();
1042f083884Ss.makeev_local 	CHECK(uNowValue == UINT32_MAX);
1052f083884Ss.makeev_local 
1062f083884Ss.makeev_local 
1072f083884Ss.makeev_local 	char tempObject;
1082f083884Ss.makeev_local 	char* testPtr = &tempObject;
1092f083884Ss.makeev_local 	char* testPtrNew = testPtr + 1;
1102f083884Ss.makeev_local 
1112f083884Ss.makeev_local 
1122f083884Ss.makeev_local 	MT::AtomicPtr<char> atomicPtrRelaxed;
1132f083884Ss.makeev_local 	atomicPtrRelaxed.StoreRelaxed(testPtr);
1142f083884Ss.makeev_local 	CHECK(atomicPtrRelaxed.Load() == testPtr);
1152f083884Ss.makeev_local 
1162f083884Ss.makeev_local 	MT::AtomicPtr<char> atomicPtr;
1172f083884Ss.makeev_local 	CHECK(atomicPtr.Load() == nullptr);
1182f083884Ss.makeev_local 
1192f083884Ss.makeev_local 	atomicPtr.Store(testPtr);
1202f083884Ss.makeev_local 	CHECK(atomicPtr.Load() == testPtr);
1212f083884Ss.makeev_local 
1222f083884Ss.makeev_local 	char* prevPtr = atomicPtr.CompareAndSwap(nullptr, testPtrNew);
1232f083884Ss.makeev_local 	CHECK(prevPtr == testPtr);
1242f083884Ss.makeev_local 	CHECK(atomicPtr.Load() == testPtr);
1252f083884Ss.makeev_local 
1262f083884Ss.makeev_local 	prevPtr = atomicPtr.CompareAndSwap(testPtr, testPtrNew);
1272f083884Ss.makeev_local 	CHECK(prevPtr == testPtr);
1282f083884Ss.makeev_local 	CHECK(atomicPtr.Load() == testPtrNew);
1292f083884Ss.makeev_local 
1302f083884Ss.makeev_local 	char* prevPtr2 = atomicPtr.Exchange(nullptr);
1312f083884Ss.makeev_local 	CHECK(prevPtr2 == testPtrNew);
1322f083884Ss.makeev_local 	CHECK(atomicPtr.Load() == nullptr);
1332f083884Ss.makeev_local }
134c307a34bSSergey Makeev 
135c307a34bSSergey Makeev 
136c307a34bSSergey Makeev MT::Atomic32<uint32> isReady;
137c307a34bSSergey Makeev MT::Atomic32<uint32> a;
138c307a34bSSergey Makeev MT::Atomic32<uint32> b;
139c307a34bSSergey Makeev 
140c307a34bSSergey Makeev uint32 sharedValue = 0;
141c307a34bSSergey Makeev 
142c307a34bSSergey Makeev MT::Atomic32<uint32> simpleLock;
143c307a34bSSergey Makeev 
144c307a34bSSergey Makeev void ThreadFunc( void* userData )
145c307a34bSSergey Makeev {
146c307a34bSSergey Makeev 	MT_UNUSED(userData);
147c307a34bSSergey Makeev 
148c307a34bSSergey Makeev 	MT::SpinWait spinWait;
149c307a34bSSergey Makeev 
150c307a34bSSergey Makeev 	while(isReady.LoadRelaxed() == 0)
151c307a34bSSergey Makeev 	{
152c307a34bSSergey Makeev 		spinWait.SpinOnce();
153c307a34bSSergey Makeev 	}
154c307a34bSSergey Makeev 
155*b2cf4ee1SSergey Makeev 	for(int iteration = 0; iteration < 10000000; iteration++)
156c307a34bSSergey Makeev 	{
157*b2cf4ee1SSergey Makeev 		uint32 prevA = a.AddFetch(1);
158*b2cf4ee1SSergey Makeev 		uint32 prevB = b.AddFetch(1);
159c307a34bSSergey Makeev 
160*b2cf4ee1SSergey Makeev 		// A should be less than B, but can also be a equal due to threads race
161*b2cf4ee1SSergey Makeev 		CHECK(prevA <= prevB);
162*b2cf4ee1SSergey Makeev 		if (prevA > prevB)
163c307a34bSSergey Makeev 		{
164*b2cf4ee1SSergey Makeev 			printf("a = %d, b = %d\n", prevA, prevB);
165c307a34bSSergey Makeev 			break;
166c307a34bSSergey Makeev 		}
167c307a34bSSergey Makeev 	}
168c307a34bSSergey Makeev 
169c307a34bSSergey Makeev 	float res = 0.0f;
170*b2cf4ee1SSergey Makeev 	uint32 randDelay = 1 + (rand() % 4);
171c307a34bSSergey Makeev 	uint32 count = 0;
172*b2cf4ee1SSergey Makeev 	while (count < 10000000)
173c307a34bSSergey Makeev     {
174c307a34bSSergey Makeev 		res = 0.0f;
175d18b3e61SSergey Makeev 		for(uint32 i = 0; i < randDelay; i++)
176c307a34bSSergey Makeev 		{
177c307a34bSSergey Makeev 			res += sin((float)i);
178c307a34bSSergey Makeev 		}
179c307a34bSSergey Makeev 
180c307a34bSSergey Makeev 		if (simpleLock.CompareAndSwap(0, 1) == 0)
181c307a34bSSergey Makeev 		{
182c307a34bSSergey Makeev 			sharedValue++;
183c307a34bSSergey Makeev 			simpleLock.Store(0);
184c307a34bSSergey Makeev 			count++;
185c307a34bSSergey Makeev 		}
186c307a34bSSergey Makeev 	}
187c307a34bSSergey Makeev 
188c307a34bSSergey Makeev 	//prevent compiler optimization
189c307a34bSSergey Makeev 	printf("%3.2f\n", res);
190c307a34bSSergey Makeev 
191c307a34bSSergey Makeev 	//
192c307a34bSSergey Makeev }
193c307a34bSSergey Makeev 
194c307a34bSSergey Makeev /*
195c307a34bSSergey Makeev 
196c307a34bSSergey Makeev Inspired by "This Is Why They Call It a Weakly-Ordered CPU" blog post by Jeff Preshing
197c307a34bSSergey Makeev http://preshing.com/20121019/this-is-why-they-call-it-a-weakly-ordered-cpu/
198c307a34bSSergey Makeev 
199c307a34bSSergey Makeev */
200c307a34bSSergey Makeev TEST(AtomicOrderingTest)
201c307a34bSSergey Makeev {
202c307a34bSSergey Makeev 	isReady.Store(0);
203c307a34bSSergey Makeev 
204c307a34bSSergey Makeev 	a.Store(1);
205c307a34bSSergey Makeev 	b.Store(2);
206c307a34bSSergey Makeev 
207c307a34bSSergey Makeev 	sharedValue = 0;
208c307a34bSSergey Makeev 
209c307a34bSSergey Makeev 	simpleLock.Store(0);
210c307a34bSSergey Makeev 
211c307a34bSSergey Makeev 
212*b2cf4ee1SSergey Makeev 	MT::Thread threads[2];
213*b2cf4ee1SSergey Makeev 	uint32 threadsCount = MT_ARRAY_SIZE(threads);
214c307a34bSSergey Makeev 
215c307a34bSSergey Makeev 	printf("threads count %d\n", threadsCount);
216c307a34bSSergey Makeev 
217c307a34bSSergey Makeev 	for(uint32 i = 0; i < threadsCount; i++)
218c307a34bSSergey Makeev 	{
219c307a34bSSergey Makeev 		threads[i].Start(16384, ThreadFunc, nullptr);
220c307a34bSSergey Makeev 	}
221c307a34bSSergey Makeev 
222c307a34bSSergey Makeev 	isReady.Store(1);
223c307a34bSSergey Makeev 
224c307a34bSSergey Makeev 	for(uint32 i = 0; i < threadsCount; i++)
225c307a34bSSergey Makeev 	{
226c307a34bSSergey Makeev 		threads[i].Join();
227c307a34bSSergey Makeev 	}
228c307a34bSSergey Makeev 
229*b2cf4ee1SSergey Makeev 	uint32 expectedSharedValue = (10000000 * threadsCount);
230c307a34bSSergey Makeev 	CHECK_EQUAL(sharedValue, expectedSharedValue);
231c307a34bSSergey Makeev 
232c307a34bSSergey Makeev 
233c307a34bSSergey Makeev }
2342f083884Ss.makeev_local ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2352f083884Ss.makeev_local }
236