1*2f083884Ss.makeev_local // The MIT License (MIT) 2*2f083884Ss.makeev_local // 3*2f083884Ss.makeev_local // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4*2f083884Ss.makeev_local // 5*2f083884Ss.makeev_local // Permission is hereby granted, free of charge, to any person obtaining a copy 6*2f083884Ss.makeev_local // of this software and associated documentation files (the "Software"), to deal 7*2f083884Ss.makeev_local // in the Software without restriction, including without limitation the rights 8*2f083884Ss.makeev_local // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9*2f083884Ss.makeev_local // copies of the Software, and to permit persons to whom the Software is 10*2f083884Ss.makeev_local // furnished to do so, subject to the following conditions: 11*2f083884Ss.makeev_local // 12*2f083884Ss.makeev_local // The above copyright notice and this permission notice shall be included in 13*2f083884Ss.makeev_local // all copies or substantial portions of the Software. 14*2f083884Ss.makeev_local // 15*2f083884Ss.makeev_local // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16*2f083884Ss.makeev_local // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17*2f083884Ss.makeev_local // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18*2f083884Ss.makeev_local // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19*2f083884Ss.makeev_local // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20*2f083884Ss.makeev_local // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21*2f083884Ss.makeev_local // THE SOFTWARE. 22*2f083884Ss.makeev_local 23*2f083884Ss.makeev_local #include "Tests.h" 24*2f083884Ss.makeev_local #include <UnitTest++.h> 25*2f083884Ss.makeev_local #include <MTScheduler.h> 26*2f083884Ss.makeev_local 27*2f083884Ss.makeev_local #ifdef MT_THREAD_SANITIZER 28*2f083884Ss.makeev_local #define SMALLEST_STACK_SIZE (566656) 29*2f083884Ss.makeev_local #else 30*2f083884Ss.makeev_local #define SMALLEST_STACK_SIZE (32768) 31*2f083884Ss.makeev_local #endif 32*2f083884Ss.makeev_local 33*2f083884Ss.makeev_local 34*2f083884Ss.makeev_local SUITE(FiberTests) 35*2f083884Ss.makeev_local { 36*2f083884Ss.makeev_local MT::Atomic32<int32> counter(0); 37*2f083884Ss.makeev_local MT::Fiber* fiberMain = nullptr; 38*2f083884Ss.makeev_local 39*2f083884Ss.makeev_local void FiberFunc( void* userData ) 40*2f083884Ss.makeev_local { 41*2f083884Ss.makeev_local CHECK_EQUAL(0, counter.Load()); 42*2f083884Ss.makeev_local counter.IncFetch(); 43*2f083884Ss.makeev_local 44*2f083884Ss.makeev_local MT::Fiber* currentFiber = (MT::Fiber*)userData; 45*2f083884Ss.makeev_local MT::Fiber::SwitchTo(*currentFiber, *fiberMain); 46*2f083884Ss.makeev_local 47*2f083884Ss.makeev_local CHECK_EQUAL(2, counter.Load()); 48*2f083884Ss.makeev_local counter.IncFetch(); 49*2f083884Ss.makeev_local 50*2f083884Ss.makeev_local MT::Fiber::SwitchTo(*currentFiber, *fiberMain); 51*2f083884Ss.makeev_local } 52*2f083884Ss.makeev_local 53*2f083884Ss.makeev_local void FiberMain(void* userData) 54*2f083884Ss.makeev_local { 55*2f083884Ss.makeev_local MT_UNUSED(userData); 56*2f083884Ss.makeev_local 57*2f083884Ss.makeev_local MT::Fiber fiber1; 58*2f083884Ss.makeev_local 59*2f083884Ss.makeev_local fiber1.Create(SMALLEST_STACK_SIZE, FiberFunc, &fiber1); 60*2f083884Ss.makeev_local 61*2f083884Ss.makeev_local MT::Fiber::SwitchTo(*fiberMain, fiber1); 62*2f083884Ss.makeev_local 63*2f083884Ss.makeev_local CHECK_EQUAL(1, counter.Load()); 64*2f083884Ss.makeev_local counter.IncFetch(); 65*2f083884Ss.makeev_local 66*2f083884Ss.makeev_local MT::Fiber::SwitchTo(*fiberMain, fiber1); 67*2f083884Ss.makeev_local 68*2f083884Ss.makeev_local CHECK_EQUAL(3, counter.Load()); 69*2f083884Ss.makeev_local 70*2f083884Ss.makeev_local fiberMain = nullptr; 71*2f083884Ss.makeev_local } 72*2f083884Ss.makeev_local 73*2f083884Ss.makeev_local void TestThread(void* userData) 74*2f083884Ss.makeev_local { 75*2f083884Ss.makeev_local MT::Fiber fiber; 76*2f083884Ss.makeev_local 77*2f083884Ss.makeev_local fiberMain = &fiber; 78*2f083884Ss.makeev_local 79*2f083884Ss.makeev_local counter.Store(0); 80*2f083884Ss.makeev_local 81*2f083884Ss.makeev_local MT::Thread* thread = (MT::Thread*)userData; 82*2f083884Ss.makeev_local 83*2f083884Ss.makeev_local fiberMain->CreateFromCurrentThreadAndRun(*thread, FiberMain, userData); 84*2f083884Ss.makeev_local } 85*2f083884Ss.makeev_local 86*2f083884Ss.makeev_local 87*2f083884Ss.makeev_local 88*2f083884Ss.makeev_local TEST(FiberSimpleTest) 89*2f083884Ss.makeev_local { 90*2f083884Ss.makeev_local MT::Thread thread; 91*2f083884Ss.makeev_local thread.Start(SMALLEST_STACK_SIZE, TestThread, &thread); 92*2f083884Ss.makeev_local 93*2f083884Ss.makeev_local thread.Join(); 94*2f083884Ss.makeev_local } 95*2f083884Ss.makeev_local //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 96*2f083884Ss.makeev_local } 97