diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2023-02-23 11:42:36 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2023-02-23 11:42:36 +0000 |
commit | 5b4b5bf533d8c99cda4905ea4ca016804180737b (patch) | |
tree | 23504d1dc016a87382272c1af844b806a0370871 /vespamalloc | |
parent | f66f816102ce0a7c3aaba72d1db61a83157259ed (diff) |
untangle vespamalloc from fastos
Diffstat (limited to 'vespamalloc')
-rw-r--r-- | vespamalloc/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespamalloc/src/tests/allocfree/allocfree.cpp | 14 | ||||
-rw-r--r-- | vespamalloc/src/tests/allocfree/linklist.cpp | 18 | ||||
-rw-r--r-- | vespamalloc/src/tests/allocfree/producerconsumer.cpp | 10 | ||||
-rw-r--r-- | vespamalloc/src/tests/allocfree/producerconsumer.h | 23 | ||||
-rw-r--r-- | vespamalloc/src/tests/test.cpp | 24 |
6 files changed, 45 insertions, 45 deletions
diff --git a/vespamalloc/CMakeLists.txt b/vespamalloc/CMakeLists.txt index af71d8b7d82..a6a33dce7ff 100644 --- a/vespamalloc/CMakeLists.txt +++ b/vespamalloc/CMakeLists.txt @@ -6,7 +6,6 @@ add_definitions(-DPARANOID_LEVEL=0) vespa_define_module( TEST_DEPENDS - fastos vespalib vespalog diff --git a/vespamalloc/src/tests/allocfree/allocfree.cpp b/vespamalloc/src/tests/allocfree/allocfree.cpp index ea6f2105d27..693deb49c55 100644 --- a/vespamalloc/src/tests/allocfree/allocfree.cpp +++ b/vespamalloc/src/tests/allocfree/allocfree.cpp @@ -2,7 +2,6 @@ #include "producerconsumer.h" #include <vespa/vespalib/testkit/testapp.h> #include <map> -#include <thread> #include <vespa/log/log.h> LOG_SETUP("allocfree_test"); @@ -73,7 +72,7 @@ int Test::Main() { } TEST_INIT("allocfree_test"); - FastOS_ThreadPool pool; + vespalib::ThreadPool pool; std::map<int, std::shared_ptr<FreeWorker> > freeWorkers; std::map<int, std::shared_ptr<MallocWorker> > mallocWorkers; @@ -86,22 +85,23 @@ int Test::Main() { mallocFreeWorkers[i] = std::shared_ptr<MallocFreeWorker>(new MallocFreeWorker(200, 16, (i%2) ? true : false)); } - + std::atomic<bool> stop_flag(false); for(std::map<int, std::shared_ptr<FreeWorker> >::iterator it(freeWorkers.begin()), mt(freeWorkers.end()); it != mt; it++) { - ASSERT_TRUE(pool.NewThread(it->second.get(), NULL) != NULL); + it->second->start(pool, stop_flag); } for(std::map<int, std::shared_ptr<MallocWorker> >::iterator it(mallocWorkers.begin()), mt(mallocWorkers.end()); it != mt; it++) { - ASSERT_TRUE(pool.NewThread(it->second.get(), NULL) != NULL); + it->second->start(pool, stop_flag); } for(std::map<int, std::shared_ptr<MallocFreeWorker> >::iterator it(mallocFreeWorkers.begin()), mt(mallocFreeWorkers.end()); it != mt; it++) { - ASSERT_TRUE(pool.NewThread(it->second.get(), NULL) != NULL); + it->second->start(pool, stop_flag); } for (; duration > 0; --duration) { LOG(info, "%d seconds left...", duration); std::this_thread::sleep_for(1s); } - pool.Close(); + stop_flag = true; + pool.join(); size_t numFreeOperations(0); size_t numMallocOperations(0); size_t numSameThreadMallocFreeOperations(0); diff --git a/vespamalloc/src/tests/allocfree/linklist.cpp b/vespamalloc/src/tests/allocfree/linklist.cpp index f3f23d726fd..8ab6eb8de8b 100644 --- a/vespamalloc/src/tests/allocfree/linklist.cpp +++ b/vespamalloc/src/tests/allocfree/linklist.cpp @@ -124,7 +124,7 @@ int Test::Main() { ASSERT_EQUAL(1024ul, sizeof(List)); - FastOS_ThreadPool pool; + vespalib::ThreadPool pool; List::AtomicHeadPtr sharedList(List::HeadPtr(nullptr, 1)); fprintf(stderr, "Start populating list\n"); for (size_t i=0; i < NumBlocks; i++) { @@ -156,18 +156,20 @@ int Test::Main() { LinkInOutAndIn pc1(sharedList, 16, false); LinkInOutAndIn pc2(sharedList, 16, true); - ASSERT_TRUE(pool.NewThread(&c1, nullptr) != nullptr); - ASSERT_TRUE(pool.NewThread(&c2, nullptr) != nullptr); - ASSERT_TRUE(pool.NewThread(&p1, nullptr) != nullptr); - ASSERT_TRUE(pool.NewThread(&p2, nullptr) != nullptr); - ASSERT_TRUE(pool.NewThread(&pc1, nullptr) != nullptr); - ASSERT_TRUE(pool.NewThread(&pc2, nullptr) != nullptr); + std::atomic<bool> stop_flag(false); + c1.start(pool, stop_flag); + c2.start(pool, stop_flag); + p1.start(pool, stop_flag); + p2.start(pool, stop_flag); + pc1.start(pool, stop_flag); + pc2.start(pool, stop_flag); for (; duration > 0; --duration) { LOG(info, "%d seconds left...", duration); std::this_thread::sleep_for(1s); } - pool.Close(); + stop_flag = true; + pool.join(); fprintf(stderr, "Did (%lu + %lu) = %lu linkIn operations\n", c1.operations(), c2.operations(), c1.operations() + c2.operations()); fprintf(stderr, "Did (%lu + %lu) = %lu linkOut operations\n", diff --git a/vespamalloc/src/tests/allocfree/producerconsumer.cpp b/vespamalloc/src/tests/allocfree/producerconsumer.cpp index 7edd1495fde..ab3cfe6dac5 100644 --- a/vespamalloc/src/tests/allocfree/producerconsumer.cpp +++ b/vespamalloc/src/tests/allocfree/producerconsumer.cpp @@ -38,7 +38,7 @@ ProducerConsumer::~ProducerConsumer() } -void Consumer::Run(FastOS_ThreadInterface *, void *) { +void Consumer::run(std::atomic<bool> &) { for (;;) { MemList ml = _queue.dequeue(); if (ml == NULL) { @@ -59,8 +59,8 @@ void Consumer::Run(FastOS_ThreadInterface *, void *) { } } -void Producer::Run(FastOS_ThreadInterface *t, void *) { - while (!t->GetBreakFlag()) { +void Producer::run(std::atomic<bool> &stop_flag) { + while (!stop_flag.load(std::memory_order_relaxed)) { MemList ml = new MemListImpl(); for (uint32_t i = 0; i < _cnt; ++i) { ml->push_back(produce()); @@ -71,8 +71,8 @@ void Producer::Run(FastOS_ThreadInterface *t, void *) { _target.close(); } -void ProducerConsumer::Run(FastOS_ThreadInterface *t, void *) { - while (!t->GetBreakFlag()) { +void ProducerConsumer::run(std::atomic<bool> &stop_flag) { + while (!stop_flag.load(std::memory_order_relaxed)) { MemListImpl ml; for (uint32_t i = 0; i < _cnt; ++i) { ml.push_back(produce()); diff --git a/vespamalloc/src/tests/allocfree/producerconsumer.h b/vespamalloc/src/tests/allocfree/producerconsumer.h index 23fb95facd8..b89e328c628 100644 --- a/vespamalloc/src/tests/allocfree/producerconsumer.h +++ b/vespamalloc/src/tests/allocfree/producerconsumer.h @@ -3,7 +3,8 @@ #include <vector> #include "queue.h" -#include <vespa/fastos/thread.h> +#include <vespa/vespalib/util/thread.h> +#include <atomic> namespace vespalib { @@ -11,7 +12,15 @@ typedef std::vector<void *> MemListImpl; typedef MemListImpl * MemList; typedef vespalib::Queue<MemList> MemQueue; -class Consumer : public FastOS_Runnable { +struct RunWithStopFlag { + virtual void run(std::atomic<bool> &stop_flag) = 0; + void start(vespalib::ThreadPool &pool, std::atomic<bool> &stop_flag) { + pool.start([this,&stop_flag](){run(stop_flag);}); + } + virtual ~RunWithStopFlag() = default; +}; + +class Consumer : public RunWithStopFlag { private: MemQueue _queue; bool _inverse; @@ -22,11 +31,11 @@ public: virtual ~Consumer(); void enqueue(const MemList &mem) { _queue.enqueue(mem); } void close() { _queue.close(); } - void Run(FastOS_ThreadInterface *t, void *) override; + void run(std::atomic<bool> &stop_flag) override; uint64_t operations() const { return _operations; } }; -class Producer : public FastOS_Runnable { +class Producer : public RunWithStopFlag { private: Consumer & _target; uint32_t _cnt; @@ -35,11 +44,11 @@ private: public: Producer(uint32_t cnt, Consumer &target); virtual ~Producer(); - void Run(FastOS_ThreadInterface *t, void *) override; + void run(std::atomic<bool> &stop_flag) override; uint64_t operations() const { return _operations; } }; -class ProducerConsumer : public FastOS_Runnable { +class ProducerConsumer : public RunWithStopFlag { private: uint32_t _cnt; bool _inverse; @@ -50,7 +59,7 @@ private: public: ProducerConsumer(uint32_t cnt, bool inverse); virtual ~ProducerConsumer(); - void Run(FastOS_ThreadInterface *t, void *) override; + void run(std::atomic<bool> &stop_flag) override; uint64_t operationsConsumed() const { return _operationsConsumed; } uint64_t operationsProduced() const { return _operationsProduced; } }; diff --git a/vespamalloc/src/tests/test.cpp b/vespamalloc/src/tests/test.cpp index f413412dff0..b3694a6dc0c 100644 --- a/vespamalloc/src/tests/test.cpp +++ b/vespamalloc/src/tests/test.cpp @@ -1,6 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/thread.h> +#include <vespa/vespalib/util/thread.h> #include <cstdlib> #include <cstdio> @@ -24,31 +24,21 @@ void testdd() free(a); } -class Thread : public FastOS_Runnable -{ -private: - void Run(FastOS_ThreadInterface * ti, void * arg) override; -}; +void thread_run(); int main(int, char *[]) { - FastOS_ThreadPool threadPool; + vespalib::ThreadPool threadPool; printf("Main stack(%p)\n", &threadPool); - Thread context; - FastOS_ThreadInterface * th[4]; - for (size_t i=0; i<sizeof(th)/sizeof(th[0]); i++) { - th[i] = threadPool.NewThread(&context); - } - for (size_t i=0; i<sizeof(th)/sizeof(th[0]); i++) { - th[i]->Join(); - delete th[i]; + for (int i = 0; i < 4; ++i) { + threadPool.start([](){thread_run();}); } - + threadPool.join(); return 0; } -void Thread::Run(FastOS_ThreadInterface *, void *) +void thread_run() { char * a = new char [100]; delete [] a; |