From: Bart Van Assche Date: Sun, 2 Dec 2018 05:51:06 +0000 (-0800) Subject: drd/tests: Fix remaining gcc 8 compiler warnings X-Git-Tag: VALGRIND_3_15_0~144 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65dcbc70db71a17b057637d1b01495ffeba2d967;p=thirdparty%2Fvalgrind.git drd/tests: Fix remaining gcc 8 compiler warnings --- diff --git a/drd/tests/tsan_thread_wrappers_pthread.h b/drd/tests/tsan_thread_wrappers_pthread.h index 7072fead1c..bdca574e97 100644 --- a/drd/tests/tsan_thread_wrappers_pthread.h +++ b/drd/tests/tsan_thread_wrappers_pthread.h @@ -156,22 +156,23 @@ class SpinLock { #endif // NO_SPINLOCK /// Just a boolean condition. Used by Mutex::LockWhen and similar. +template class Condition { public: typedef bool (*func_t)(void*); - template Condition(bool (*func)(T*), T* arg) - : func_(reinterpret_cast(func)), arg_(arg) {} + : func1_(func), arg_(arg) {} Condition(bool (*func)()) - : func_(reinterpret_cast(func)), arg_(NULL) {} + : func0_(func), arg_(NULL) {} - bool Eval() { return func_(arg_); } - private: - func_t func_; - void *arg_; + bool Eval() const { return func1_ ? func1_(arg_) : func0_(); } + private: + bool (*func0_)(); + bool (*func1_)(T*); + T *arg_; }; @@ -211,20 +212,27 @@ class Mutex { bool ReaderTryLock() { return TryLock();} void ReaderUnlock() { Unlock(); } - void LockWhen(Condition cond) { Lock(); WaitLoop(cond); } - void ReaderLockWhen(Condition cond) { Lock(); WaitLoop(cond); } - void Await(Condition cond) { WaitLoop(cond); } + template + void LockWhen(const Condition& cond) { Lock(); WaitLoop(cond); } + template + void ReaderLockWhen(const Condition& cond) { Lock(); WaitLoop(cond); } + template + void Await(const Condition& cond) { WaitLoop(cond); } - bool ReaderLockWhenWithTimeout(Condition cond, int millis) + template + bool ReaderLockWhenWithTimeout(const Condition& cond, int millis) { Lock(); return WaitLoopWithTimeout(cond, millis); } - bool LockWhenWithTimeout(Condition cond, int millis) + template + bool LockWhenWithTimeout(const Condition& cond, int millis) { Lock(); return WaitLoopWithTimeout(cond, millis); } - bool AwaitWithTimeout(Condition cond, int millis) + template + bool AwaitWithTimeout(const Condition& cond, int millis) { return WaitLoopWithTimeout(cond, millis); } private: - void WaitLoop(Condition cond) { + template + void WaitLoop(const Condition& cond) { signal_at_unlock_ = true; while(cond.Eval() == false) { pthread_cond_wait(&cv_, &mu_); @@ -232,7 +240,8 @@ class Mutex { ANNOTATE_CONDVAR_LOCK_WAIT(&cv_, &mu_); } - bool WaitLoopWithTimeout(Condition cond, int millis) { + template + bool WaitLoopWithTimeout(const Condition& cond, int millis) { struct timeval now; struct timespec timeout; int retcode = 0; @@ -341,28 +350,34 @@ class WriterLockScoped { // Scoped RWLock Locker/Unlocker /// Wrapper for pthread_create()/pthread_join(). class MyThread { public: - typedef void *(*worker_t)(void*); - - MyThread(worker_t worker, void *arg = NULL, const char *name = NULL) - :w_(worker), arg_(arg), name_(name) {} + MyThread(void* (*worker)(void *), void *arg = NULL, const char *name = NULL) + :wpvpv_(worker), arg_(arg), name_(name) {} MyThread(void (*worker)(void), void *arg = NULL, const char *name = NULL) - :w_(reinterpret_cast(worker)), arg_(arg), name_(name) {} + :wvv_(worker), arg_(arg), name_(name) {} MyThread(void (*worker)(void *), void *arg = NULL, const char *name = NULL) - :w_(reinterpret_cast(worker)), arg_(arg), name_(name) {} + :wvpv_(worker), arg_(arg), name_(name) {} - ~MyThread(){ w_ = NULL; arg_ = NULL;} - void Start() { CHECK(0 == pthread_create(&t_, NULL, (worker_t)ThreadBody, this));} + void Start() { CHECK(0 == pthread_create(&t_, NULL, ThreadBody, this));} void Join() { CHECK(0 == pthread_join(t_, NULL));} pthread_t tid() const { return t_; } private: - static void ThreadBody(MyThread *my_thread) { + static void *ThreadBody(void *arg) { + MyThread *my_thread = reinterpret_cast(arg); if (my_thread->name_) { ANNOTATE_THREAD_NAME(my_thread->name_); } - my_thread->w_(my_thread->arg_); + if (my_thread->wpvpv_) + return my_thread->wpvpv_(my_thread->arg_); + if (my_thread->wpvpv_) + my_thread->wvpv_(my_thread->arg_); + if (my_thread->wvv_) + my_thread->wvv_(); + return NULL; } pthread_t t_; - worker_t w_; + void *(*wpvpv_)(void*); + void (*wvv_)(void); + void (*wvpv_)(void*); void *arg_; const char *name_; }; @@ -391,7 +406,7 @@ class ProducerConsumerQueue { // Get. // Blocks if the queue is empty. void *Get() { - mu_.LockWhen(Condition(IsQueueNotEmpty, &q_)); + mu_.LockWhen(Condition(IsQueueNotEmpty, &q_)); void * item = NULL; bool ok = TryGetInternal(&item); CHECK(ok); @@ -578,7 +593,7 @@ class BlockingCounter { return count_ == 0; } void Wait() { - mu_.LockWhen(Condition(&IsZero, &count_)); + mu_.LockWhen(Condition(&IsZero, &count_)); mu_.Unlock(); } private: diff --git a/drd/tests/tsan_unittest.cpp b/drd/tests/tsan_unittest.cpp index 79fea6b84c..c504e9f93f 100644 --- a/drd/tests/tsan_unittest.cpp +++ b/drd/tests/tsan_unittest.cpp @@ -437,7 +437,7 @@ void Waiter() { pool.StartWorkers(); COND = 0; pool.Add(NewCallback(Waker)); - MU.LockWhen(Condition(&ArgIsOne, &COND)); // calls ANNOTATE_CONDVAR_WAIT + MU.LockWhen(Condition(&ArgIsOne, &COND)); // calls ANNOTATE_CONDVAR_WAIT MU.Unlock(); // Waker is done! GLOB = 2; @@ -621,7 +621,7 @@ void Waiter() { t.Start(); usleep(100000); // Make sure the signaller gets there first. - MU.LockWhen(Condition(&ArgIsTrue, &COND)); // calls ANNOTATE_CONDVAR_WAIT + MU.LockWhen(Condition(&ArgIsTrue, &COND));// calls ANNOTATE_CONDVAR_WAIT MU.Unlock(); // Signaller is done! GLOB = 2; // If LockWhen didn't catch the signal, a race may be reported here. @@ -866,7 +866,7 @@ void Waiter() { GLOB++; MU.Unlock(); - MU.LockWhen(Condition(&ArgIsOne, &COND)); + MU.LockWhen(Condition(&ArgIsOne, &COND)); MU.Unlock(); GLOB++; } @@ -950,7 +950,7 @@ void Waker() { }; void Waiter() { - MU.LockWhen(Condition(&ArgIsOne, &COND)); + MU.LockWhen(Condition(&ArgIsOne, &COND)); MU.Unlock(); CHECK(GLOB != 777); } @@ -1001,7 +1001,7 @@ void Worker() { MU2.Lock(); COND--; ANNOTATE_CONDVAR_SIGNAL(&MU2); - MU2.Await(Condition(&ArgIsZero, &COND)); + MU2.Await(Condition(&ArgIsZero, &COND)); MU2.Unlock(); CHECK(GLOB == 2); @@ -1035,7 +1035,7 @@ void Worker() { MU2.Lock(); COND--; ANNOTATE_CONDVAR_SIGNAL(&MU2); - MU2.Await(Condition(&ArgIsZero, &COND)); + MU2.Await(Condition(&ArgIsZero, &COND)); MU2.Unlock(); CHECK(GLOB == 3); @@ -1075,7 +1075,7 @@ void Waiter() { pool.Add(NewCallback(Waker)); MU.Lock(); - MU.Await(Condition(&ArgIsOne, &COND)); // calls ANNOTATE_CONDVAR_WAIT + MU.Await(Condition(&ArgIsOne, &COND)); // calls ANNOTATE_CONDVAR_WAIT MU.Unlock(); // Waker is done! GLOB = 2; @@ -1108,7 +1108,7 @@ void Waiter() { pool.Add(NewCallback(Waker)); MU.Lock(); - CHECK(MU.AwaitWithTimeout(Condition(&ArgIsOne, &COND), INT_MAX)); + CHECK(MU.AwaitWithTimeout(Condition(&ArgIsOne, &COND), INT_MAX)); MU.Unlock(); GLOB = 2; @@ -1137,7 +1137,7 @@ void Waiter() { pool.Add(NewCallback(Waker)); MU.Lock(); - CHECK(!MU.AwaitWithTimeout(Condition(&ArgIsOne, &COND), 100)); + CHECK(!MU.AwaitWithTimeout(Condition(&ArgIsOne, &COND), 100)); MU.Unlock(); GLOB = 2; @@ -1167,7 +1167,7 @@ void Waiter() { COND = 0; pool.Add(NewCallback(Waker)); - CHECK(!MU.LockWhenWithTimeout(Condition(&ArgIsOne, &COND), 100)); + CHECK(!MU.LockWhenWithTimeout(Condition(&ArgIsOne, &COND), 100)); MU.Unlock(); GLOB = 2; @@ -1300,7 +1300,7 @@ void Waiter() { pool.StartWorkers(); COND = 0; pool.Add(NewCallback(Waker)); - MU.ReaderLockWhen(Condition(&ArgIsOne, &COND)); + MU.ReaderLockWhen(Condition(&ArgIsOne, &COND)); MU.ReaderUnlock(); GLOB = 2; @@ -1333,7 +1333,7 @@ void Waiter() { pool.StartWorkers(); COND = 0; pool.Add(NewCallback(Waker)); - CHECK(MU.ReaderLockWhenWithTimeout(Condition(&ArgIsOne, &COND), INT_MAX)); + CHECK(MU.ReaderLockWhenWithTimeout(Condition(&ArgIsOne, &COND), INT_MAX)); MU.ReaderUnlock(); GLOB = 2; @@ -1361,7 +1361,7 @@ void Waiter() { pool.StartWorkers(); COND = 0; pool.Add(NewCallback(Waker)); - CHECK(!MU.ReaderLockWhenWithTimeout(Condition(&ArgIsOne, &COND), 100)); + CHECK(!MU.ReaderLockWhenWithTimeout(Condition(&ArgIsOne, &COND), 100)); MU.ReaderUnlock(); GLOB = 2; @@ -4185,7 +4185,7 @@ struct B: A { // The race is here. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< printf("B::~B()\n"); // wait until flag_stopped is true. - mu.LockWhen(Condition(&ArgIsTrue, &flag_stopped)); + mu.LockWhen(Condition(&ArgIsTrue, &flag_stopped)); mu.Unlock(); printf("B::~B() done\n"); } @@ -4256,7 +4256,7 @@ struct B: A { // The race is here. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< printf("B::~B()\n"); // wait until flag_stopped is true. - mu.LockWhen(Condition(&ArgIsTrue, &flag_stopped)); + mu.LockWhen(Condition(&ArgIsTrue, &flag_stopped)); mu.Unlock(); printf("B::~B() done\n"); } @@ -4825,7 +4825,7 @@ Mutex mu; static void Thread1() { for (int i = 0; i < 100; i++) { - mu.LockWhenWithTimeout(Condition(&ArgIsTrue, &GLOB), 5); + mu.LockWhenWithTimeout(Condition(&ArgIsTrue, &GLOB), 5); GLOB = false; mu.Unlock(); usleep(10000); @@ -5338,7 +5338,7 @@ static bool ArgIsTrue(bool *arg) { void f1() { char some_stack[N]; write_to_p(some_stack, 1); - mu.LockWhen(Condition(&ArgIsTrue, &COND)); + mu.LockWhen(Condition(&ArgIsTrue, &COND)); mu.Unlock(); } @@ -6098,7 +6098,7 @@ bool WeirdCondition(int* param) { } void Waiter() { int param = 0; - MU.ReaderLockWhen(Condition(WeirdCondition, ¶m)); + MU.ReaderLockWhen(Condition(WeirdCondition, ¶m)); MU.ReaderUnlock(); CHECK(GLOB > 0); CHECK(param > 0); @@ -7218,7 +7218,7 @@ bool NoElementsLeft(vector *v) { } void WaitForAllThreadsToFinish_Good() { - mu.LockWhen(Condition(NoElementsLeft, vec)); + mu.LockWhen(Condition>(NoElementsLeft, vec)); mu.Unlock(); // It is now safe to access vec w/o lock.