mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-12 19:59:05 +08:00
Fixed 2 recent regression tests
This commit is contained in:
parent
50968a0a3e
commit
f953c60705
@ -12,11 +12,14 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <Eigen/CXX11/ThreadPool>
|
#include <Eigen/CXX11/ThreadPool>
|
||||||
|
|
||||||
|
// Visual studio doesn't implement a rand_r() function since its
|
||||||
|
// implementation of rand() is already thread safe
|
||||||
|
int rand_reentrant(unsigned int* s) {
|
||||||
#ifdef EIGEN_COMP_MSVC_STRICT
|
#ifdef EIGEN_COMP_MSVC_STRICT
|
||||||
// Visual studio doesn't implementan rand_r() function since its
|
|
||||||
// implementation of rand()is already thread safe
|
|
||||||
int rand_r(unsigned int*) {
|
|
||||||
return rand();
|
return rand();
|
||||||
|
#else
|
||||||
|
return rand_r(s);
|
||||||
|
endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -85,15 +88,15 @@ static void test_stress_eventcount()
|
|||||||
std::vector<std::unique_ptr<std::thread>> producers;
|
std::vector<std::unique_ptr<std::thread>> producers;
|
||||||
for (int i = 0; i < kThreads; i++) {
|
for (int i = 0; i < kThreads; i++) {
|
||||||
producers.emplace_back(new std::thread([&ec, &queues]() {
|
producers.emplace_back(new std::thread([&ec, &queues]() {
|
||||||
unsigned rnd = std::hash<std::thread::id>()(std::this_thread::get_id());
|
unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
||||||
for (int i = 0; i < kEvents; i++) {
|
for (int j = 0; j < kEvents; j++) {
|
||||||
unsigned idx = rand_r(&rnd) % kQueues;
|
unsigned idx = rand_reentrant(&rnd) % kQueues;
|
||||||
if (queues[idx].Push()) {
|
if (queues[idx].Push()) {
|
||||||
ec.Notify(false);
|
ec.Notify(false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
i--;
|
j--;
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@ -102,11 +105,11 @@ static void test_stress_eventcount()
|
|||||||
for (int i = 0; i < kThreads; i++) {
|
for (int i = 0; i < kThreads; i++) {
|
||||||
consumers.emplace_back(new std::thread([&ec, &queues, &waiters, i]() {
|
consumers.emplace_back(new std::thread([&ec, &queues, &waiters, i]() {
|
||||||
EventCount::Waiter& w = waiters[i];
|
EventCount::Waiter& w = waiters[i];
|
||||||
unsigned rnd = std::hash<std::thread::id>()(std::this_thread::get_id());
|
unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
||||||
for (int i = 0; i < kEvents; i++) {
|
for (int j = 0; j < kEvents; k++) {
|
||||||
unsigned idx = rand_r(&rnd) % kQueues;
|
unsigned idx = rand_reentrant(&rnd) % kQueues;
|
||||||
if (queues[idx].Pop()) continue;
|
if (queues[idx].Pop()) continue;
|
||||||
i--;
|
j--;
|
||||||
ec.Prewait(&w);
|
ec.Prewait(&w);
|
||||||
bool empty = true;
|
bool empty = true;
|
||||||
for (int q = 0; q < kQueues; q++) {
|
for (int q = 0; q < kQueues; q++) {
|
||||||
|
@ -14,11 +14,14 @@
|
|||||||
#include <Eigen/CXX11/ThreadPool>
|
#include <Eigen/CXX11/ThreadPool>
|
||||||
|
|
||||||
|
|
||||||
|
// Visual studio doesn't implement a rand_r() function since its
|
||||||
|
// implementation of rand() is already thread safe
|
||||||
|
int rand_reentrant(unsigned int* s) {
|
||||||
#ifdef EIGEN_COMP_MSVC_STRICT
|
#ifdef EIGEN_COMP_MSVC_STRICT
|
||||||
// Visual studio doesn't implementan rand_r() function since its
|
|
||||||
// implementation of rand()is already thread safe
|
|
||||||
int rand_r(unsigned int*) {
|
|
||||||
return rand();
|
return rand();
|
||||||
|
#else
|
||||||
|
return rand_r(s);
|
||||||
|
endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -115,11 +118,11 @@ void test_empty_runqueue()
|
|||||||
unsigned rnd = 0;
|
unsigned rnd = 0;
|
||||||
std::vector<int> stolen;
|
std::vector<int> stolen;
|
||||||
for (int i = 0; i < 1 << 18; i++) {
|
for (int i = 0; i < 1 << 18; i++) {
|
||||||
if (rand_r(&rnd) % 2)
|
if (rand_reentrant(&rnd) % 2)
|
||||||
VERIFY_IS_EQUAL(0, q.PushFront(1));
|
VERIFY_IS_EQUAL(0, q.PushFront(1));
|
||||||
else
|
else
|
||||||
VERIFY_IS_EQUAL(0, q.PushBack(1));
|
VERIFY_IS_EQUAL(0, q.PushBack(1));
|
||||||
if (rand_r(&rnd) % 2)
|
if (rand_reentrant(&rnd) % 2)
|
||||||
VERIFY_IS_EQUAL(1, q.PopFront());
|
VERIFY_IS_EQUAL(1, q.PopFront());
|
||||||
else {
|
else {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@ -176,30 +179,30 @@ void test_stress_runqueue()
|
|||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
threads.emplace_back(new std::thread([&q, &total]() {
|
threads.emplace_back(new std::thread([&q, &total]() {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int i = 1; i < kEvents; i++) {
|
for (int j = 1; j < kEvents; j++) {
|
||||||
if (q.PushBack(i) == 0) {
|
if (q.PushBack(j) == 0) {
|
||||||
sum += i;
|
sum += j;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
i--;
|
j--;
|
||||||
}
|
}
|
||||||
total += sum;
|
total += sum;
|
||||||
}));
|
}));
|
||||||
threads.emplace_back(new std::thread([&q, &total]() {
|
threads.emplace_back(new std::thread([&q, &total]() {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
std::vector<int> stolen;
|
std::vector<int> stolen;
|
||||||
for (int i = 1; i < kEvents;) {
|
for (int j = 1; j < kEvents;) {
|
||||||
if (q.PopBackHalf(&stolen) == 0) {
|
if (q.PopBackHalf(&stolen) == 0) {
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
while (stolen.size() && i < kEvents) {
|
while (stolen.size() && j < kEvents) {
|
||||||
int v = stolen.back();
|
int v = stolen.back();
|
||||||
stolen.pop_back();
|
stolen.pop_back();
|
||||||
VERIFY_IS_NOT_EQUAL(v, 0);
|
VERIFY_IS_NOT_EQUAL(v, 0);
|
||||||
sum += v;
|
sum += v;
|
||||||
i++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (stolen.size()) {
|
while (stolen.size()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user