diff options
Diffstat (limited to 'vespamalloc/src/tests/thread/racemanythreads.cpp')
-rw-r--r-- | vespamalloc/src/tests/thread/racemanythreads.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/vespamalloc/src/tests/thread/racemanythreads.cpp b/vespamalloc/src/tests/thread/racemanythreads.cpp new file mode 100644 index 00000000000..ba5cc8b7a1c --- /dev/null +++ b/vespamalloc/src/tests/thread/racemanythreads.cpp @@ -0,0 +1,82 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <pthread.h> +#include <stdint.h> +#include <errno.h> +#include <vespa/vespalib/util/atomic.h> +#include <sys/resource.h> +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/log/log.h> + +LOG_SETUP("thread_test"); + +using namespace vespalib; + +class Test : public TestApp +{ +public: + ~Test(); + int Main(); +}; + +Test::~Test() +{ +} + +void * hammer(void * arg) +{ + usleep(4000000); + long seconds = * static_cast<const long *>(arg); + long stopTime(time(NULL) + seconds); + pthread_t id = pthread_self(); + while (time(NULL) < stopTime) { + std::vector<pthread_t *> allocations; + for (size_t i(0); i < 2000; i++) { + pthread_t *t = new pthread_t[20]; + allocations.push_back(t); + for (size_t j(0); j < 20; j++) { + t[j] = id; + } + } + + for (size_t i(0); i < allocations.size(); i++) { + for (size_t j(0); j < 20; j++) { + assert(allocations[i][j] == id); + } + delete [] allocations[i]; + } + } + return arg; +} + +int Test::Main() +{ + TEST_INIT("racemanythreads_test"); + size_t threadCount(1024); + long seconds(10); + if (_argc >= 2) { + threadCount = strtoul(_argv[1], NULL, 0); + if (_argc >= 3) { + seconds = strtoul(_argv[2], NULL, 0); + } + } + + pthread_attr_t attr; + EXPECT_EQUAL(pthread_attr_init(&attr), 0); + EXPECT_EQUAL(pthread_attr_setstacksize(&attr, 64*1024), 0); + std::vector<pthread_t> threads(threadCount); + for (size_t i(0); i < threadCount; i++) { + EXPECT_EQUAL( pthread_create(&threads[i], &attr, hammer, &seconds), 0); + } + for (size_t i(0); i < threadCount; i++) { + void *retval; + EXPECT_EQUAL(pthread_join(threads[i], &retval), 0); + } + + TEST_DONE(); +} + +TEST_APPHOOK(Test); |