protot/3rdparty/bx/tests/thread_test.cpp

70 lines
1.2 KiB
C++
Raw Normal View History

2016-11-10 21:53:08 +01:00
/*
2018-02-03 17:39:28 +01:00
* Copyright 2010-2018 Branimir Karadzic. All rights reserved.
2016-11-10 21:53:08 +01:00
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include "test.h"
#include <bx/thread.h>
2018-02-03 17:39:28 +01:00
bx::DefaultAllocator s_allocator;
bx::MpScUnboundedBlockingQueue<void> s_mpsc(&s_allocator);
int32_t threadExit0(bx::Thread* _thread, void*)
2016-11-10 21:53:08 +01:00
{
2018-02-03 17:39:28 +01:00
_thread->pop();
s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x1300) ) );
return bx::kExitSuccess;
}
int32_t threadExit1(bx::Thread* _thread, void*)
{
BX_UNUSED(_thread);
s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x89) ) );
return bx::kExitFailure;
2016-11-10 21:53:08 +01:00
}
2018-02-03 17:39:28 +01:00
TEST_CASE("Semaphore", "")
2016-11-10 21:53:08 +01:00
{
2018-02-03 17:39:28 +01:00
bx::Semaphore sem;
REQUIRE(!sem.wait(10) );
sem.post(1);
REQUIRE(sem.wait() );
2016-11-10 21:53:08 +01:00
}
2018-02-03 17:39:28 +01:00
TEST_CASE("Thread", "")
2016-11-10 21:53:08 +01:00
{
bx::Thread th;
2017-04-11 08:16:10 +02:00
REQUIRE(!th.isRunning() );
2016-11-10 21:53:08 +01:00
th.init(threadExit0);
2017-04-11 08:16:10 +02:00
REQUIRE(th.isRunning() );
2018-02-03 17:39:28 +01:00
th.push(NULL);
2016-11-10 21:53:08 +01:00
th.shutdown();
2017-04-11 08:16:10 +02:00
REQUIRE(!th.isRunning() );
REQUIRE(th.getExitCode() == 0);
2016-11-10 21:53:08 +01:00
th.init(threadExit1);
2017-04-11 08:16:10 +02:00
REQUIRE(th.isRunning() );
2016-11-10 21:53:08 +01:00
th.shutdown();
2017-04-11 08:16:10 +02:00
REQUIRE(!th.isRunning() );
REQUIRE(th.getExitCode() == 1);
2016-11-10 21:53:08 +01:00
}
2018-02-03 17:39:28 +01:00
TEST_CASE("MpScUnboundedBlockingQueue", "")
{
void* p0 = s_mpsc.pop();
void* p1 = s_mpsc.pop();
uintptr_t result = uintptr_t(p0) | uintptr_t(p1);
REQUIRE(result == 0x1389);
}