1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/TimeProfiler.h"
#include "System/Log/ILog.h"
#include "System/Threading/SpringMutex.h"
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <functional>
#ifndef _WIN32
#include <mutex>
#include <sys/syscall.h>
#include <linux/futex.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#define BOOST_TEST_MODULE Mutex
#include <boost/test/unit_test.hpp>
#ifndef _WIN32
typedef boost::uint32_t futex;
static void futex_init(futex* m)
{
*m = 0;
}
static void futex_destroy(futex* m)
{
*m = 0;
}
static void futex_lock(futex* m)
{
futex c;
if ((c = __sync_val_compare_and_swap(m, 0, 1)) != 0) {
do {
if ((c == 2) || __sync_val_compare_and_swap(m, 1, 2) != 0)
syscall(SYS_futex, m, FUTEX_WAIT_PRIVATE, 2, NULL, NULL, 0);
} while((c = __sync_val_compare_and_swap(m, 0, 2)) != 0);
}
}
static void futex_unlock(futex* m)
{
if (__sync_fetch_and_sub(m, 1) != 1) {
*m = 0;
syscall(SYS_futex, m, FUTEX_WAKE_PRIVATE, 1, NULL, NULL, 0);
}
}
#endif
typedef std::function<void()> voidFnc;
spring_time Test(const char* name, voidFnc pre, voidFnc post)
{
ScopedOnceTimer timer(name);
volatile int x = 0;
for (int i = 0; i < 0xFFFF; ++i) {
for (int j = 0; j < 0xFF; ++j) {
pre();
x += j / 3;
x *= 0.33f;
post();
}
}
return timer.GetDuration();
}
BOOST_AUTO_TEST_CASE( Mutex )
{
spring_clock::PushTickRate();
spring_time::setstarttime(spring_time::gettime(true));
spring::mutex spmtx;
spring::recursive_mutex sprmtx;
boost::mutex mtx;
boost::recursive_mutex rmtx;
spring_time tRaw = Test("raw", []{ }, []{ });
spring_time tSpMtx = Test("spring::mutex", [&]{ spmtx.lock(); }, [&]{ spmtx.unlock(); });
spring_time tSpRMtx = Test("spring::recursive_mutex", [&]{ sprmtx.lock(); }, [&]{ sprmtx.unlock(); });
spring_time tMtx = Test("boost::mutex", [&]{ mtx.lock(); }, [&]{ mtx.unlock(); });
spring_time tRMtx = Test("boost::recursive_mutex", [&]{ rmtx.lock(); }, [&]{ rmtx.unlock(); });
#ifndef _WIN32
std::mutex smtx;
std::recursive_mutex srmtx;
spring_time tSMtx = Test("std::mutex", [&]{ smtx.lock(); }, [&]{ smtx.unlock(); });
spring_time tSRMtx = Test("std::recursive_mutex", [&]{ srmtx.lock(); }, [&]{ srmtx.unlock(); });
#endif
#ifndef _WIN32
futex ftx;
futex_init(&ftx);
spring_time tCrit = Test("futex", [&]{ futex_lock(&ftx); }, [&]{ futex_unlock(&ftx); });
futex_init(&ftx);
#endif
#ifdef _WIN32
CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);
spring_time tCrit = Test("critical section", [&]{ EnterCriticalSection(&cs); }, [&]{ LeaveCriticalSection(&cs); });
DeleteCriticalSection(&cs);
#endif
//BOOST_CHECK(tMtx.toMilliSecsi() <= 4 * tRaw.toMilliSecsi());
//BOOST_CHECK(tRMtx.toMilliSecsi() <= 4 * tRaw.toMilliSecsi());
}
|