diff options
Diffstat (limited to 'vespamalloc')
-rw-r--r-- | vespamalloc/src/tests/test1/testatomic.cpp | 114 | ||||
-rw-r--r-- | vespamalloc/src/tests/thread/racemanythreads.cpp | 1 | ||||
-rw-r--r-- | vespamalloc/src/tests/thread/thread.cpp | 8 |
3 files changed, 7 insertions, 116 deletions
diff --git a/vespamalloc/src/tests/test1/testatomic.cpp b/vespamalloc/src/tests/test1/testatomic.cpp index 448b0776f1a..818af5fe673 100644 --- a/vespamalloc/src/tests/test1/testatomic.cpp +++ b/vespamalloc/src/tests/test1/testatomic.cpp @@ -1,106 +1,8 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/thread.h> #include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/util/atomic.h> #include <vespamalloc/malloc/allocchunk.h> -class Test : public vespalib::TestApp -{ -public: - int Main() override; -private: - template<typename T> - void testSwap(T initial); - template<typename T> - void testSwapStress(T v, int numThreads); -}; - -template <typename T> -class Stress : public FastOS_Runnable -{ -private: - void Run(FastOS_ThreadInterface * ti, void * arg) override; - void stressSwap(T & value); -public: - Stress(T * value) : _value(value), _successCount(0), _failedCount(0) { } - void wait() { _wait.Lock(); _wait.Unlock(); } - FastOS_Mutex _wait; - T * _value; - size_t _successCount; - size_t _failedCount; -}; - -TEST_APPHOOK(Test); - -template<typename T> -void Test::testSwap(T initial) -{ - T value(initial); - - ASSERT_TRUE(vespalib::Atomic::cmpSwap(&value, initial+1, initial)); - ASSERT_TRUE(value == initial+1); - - ASSERT_TRUE(!vespalib::Atomic::cmpSwap(&value, initial+2, initial)); - ASSERT_TRUE(value == initial+1); -} - -template<typename T> -void Test::testSwapStress(T v, int numThreads) -{ - T old(v); - std::vector<Stress<T> *> contexts; - std::vector<FastOS_ThreadInterface *> threads; - FastOS_ThreadPool threadPool(512*1024); - - for(int i=0; i < numThreads; i++) { - contexts.push_back(new Stress<T>(&v)); - } - - for(size_t i = 0; i < contexts.size(); i++) { - threads.push_back(threadPool.NewThread(contexts[i])); - } - FastOS_Thread::Sleep(1000); - size_t succesCount(0); - size_t failedCount(0); - for(size_t i = 0; i < contexts.size(); i++) { - Stress<T> * s = contexts[i]; - s->wait(); - succesCount += s->_successCount; - failedCount += s->_failedCount; - } - ASSERT_TRUE(v == 0); - ASSERT_TRUE(old == succesCount); - fprintf(stderr, "%ld threads counting down from %" PRIu64 " had %ld succesfull and %ld unsuccessful attempts\n", - contexts.size(), uint64_t(old), succesCount, failedCount); - for(size_t i = 0; i < contexts.size(); i++) { - delete contexts[i]; - } -} - -template <typename T> -void Stress<T>::Run(FastOS_ThreadInterface *, void *) -{ - _wait.Lock(); - stressSwap(*_value); - _wait.Unlock(); -} - -template <typename T> -void Stress<T>::stressSwap(T & value) -{ - for (T old = value; old > 0; old = value) { - if (vespalib::Atomic::cmpSwap(&value, old-1, old)) { - _successCount++; - } else { - _failedCount++; - } - } -} - -int Test::Main() -{ - TEST_INIT("atomic"); - +TEST("verify lock freeness of atomics"){ { std::atomic<uint32_t> uint32V; ASSERT_TRUE(uint32V.is_lock_free()); @@ -120,16 +22,6 @@ int Test::Main() #endif } - testSwap<uint32_t>(6); - testSwap<uint32_t>(7); - testSwap<uint32_t>(uint32_t(-6)); - testSwap<uint32_t>(uint32_t(-7)); - testSwap<uint64_t>(6); - testSwap<uint64_t>(7); - testSwap<uint64_t>(uint64_t(-6)); - testSwap<uint64_t>(uint64_t(-7)); - testSwapStress<uint64_t>(0x1000000, 4); - testSwapStress<uint32_t>(0x1000000, 4); - - TEST_DONE(); } + +TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespamalloc/src/tests/thread/racemanythreads.cpp b/vespamalloc/src/tests/thread/racemanythreads.cpp index fbc8312eb2c..692228ced9e 100644 --- a/vespamalloc/src/tests/thread/racemanythreads.cpp +++ b/vespamalloc/src/tests/thread/racemanythreads.cpp @@ -1,7 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/util/atomic.h> using namespace vespalib; diff --git a/vespamalloc/src/tests/thread/thread.cpp b/vespamalloc/src/tests/thread/thread.cpp index 65904df22c7..3a4fa366c38 100644 --- a/vespamalloc/src/tests/thread/thread.cpp +++ b/vespamalloc/src/tests/thread/thread.cpp @@ -1,7 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/util/atomic.h> +#include <atomic> using namespace vespalib; @@ -47,18 +47,18 @@ struct wait_info { } pthread_cond_t _cond; pthread_mutex_t _mutex; - volatile uint64_t _count; + std::atomic<uint64_t> _count; }; void * just_wait(void * arg) { wait_info * info = (wait_info *) arg; pthread_mutex_lock(&info->_mutex); - vespalib::Atomic::postInc(&info->_count); + info->_count++; pthread_cond_wait(&info->_cond, &info->_mutex); pthread_mutex_unlock(&info->_mutex); pthread_cond_signal(&info->_cond); - vespalib::Atomic::postDec(&info->_count); + info->_count--; return arg; } |