From 8008ce25e9ff7e82e4bd0b7da041a31dc841057b Mon Sep 17 00:00:00 2001 From: Tuan Ta Date: Tue, 13 Feb 2018 09:15:40 -0500 Subject: tests: add some pthread and std::thread unit tests This patch adds some pthread and C++11 std::thread unit tests. Change-Id: I9706b542e5fa927a87c6e8ae2a6330fab7bb5d72 Reviewed-on: https://gem5-review.googlesource.com/8221 Reviewed-by: Jason Lowe-Power Maintainer: Brandon Potter --- tests/test-progs/pthread/Makefile.riscv | 32 +++++ tests/test-progs/pthread/Makefile.x86 | 34 ++++++ tests/test-progs/pthread/src/test_atomic.cpp | 117 ++++++++++++++++++ tests/test-progs/pthread/src/test_pthread_cond.cpp | 111 +++++++++++++++++ .../pthread/src/test_pthread_create_para.cpp | 118 ++++++++++++++++++ .../pthread/src/test_pthread_create_seq.cpp | 116 ++++++++++++++++++ .../test-progs/pthread/src/test_pthread_mutex.cpp | 133 +++++++++++++++++++++ .../pthread/src/test_std_condition_variable.cpp | 98 +++++++++++++++ tests/test-progs/pthread/src/test_std_mutex.cpp | 92 ++++++++++++++ tests/test-progs/pthread/src/test_std_thread.cpp | 91 ++++++++++++++ 10 files changed, 942 insertions(+) create mode 100644 tests/test-progs/pthread/Makefile.riscv create mode 100644 tests/test-progs/pthread/Makefile.x86 create mode 100644 tests/test-progs/pthread/src/test_atomic.cpp create mode 100644 tests/test-progs/pthread/src/test_pthread_cond.cpp create mode 100644 tests/test-progs/pthread/src/test_pthread_create_para.cpp create mode 100644 tests/test-progs/pthread/src/test_pthread_create_seq.cpp create mode 100644 tests/test-progs/pthread/src/test_pthread_mutex.cpp create mode 100644 tests/test-progs/pthread/src/test_std_condition_variable.cpp create mode 100644 tests/test-progs/pthread/src/test_std_mutex.cpp create mode 100644 tests/test-progs/pthread/src/test_std_thread.cpp (limited to 'tests') diff --git a/tests/test-progs/pthread/Makefile.riscv b/tests/test-progs/pthread/Makefile.riscv new file mode 100644 index 000000000..0122187a0 --- /dev/null +++ b/tests/test-progs/pthread/Makefile.riscv @@ -0,0 +1,32 @@ +CPP := riscv64-unknown-linux-gnu-g++ +OBJDUMP := riscv64-unknown-linux-gnu-objdump +CPPFLAGS := -g -O3 -static -std=c++11 -march=rv64gc +LFLAGS := -pthread -Wl,--whole-archive -lpthread -latomic -Wl,--no-whole-archive + +SRC_DIR := ./src +BIN_DIR := ./bin + +TEST_SRC := test_pthread_create_seq.cpp \ + test_pthread_create_para.cpp \ + test_pthread_mutex.cpp \ + test_atomic.cpp \ + test_pthread_cond.cpp \ + test_std_thread.cpp \ + test_std_mutex.cpp \ + test_std_condition_variable.cpp \ + +TEST_BIN := $(patsubst %.cpp,%,$(TEST_SRC)) +TEST_DUMP := $(patsubst %.cpp,%.objdump,$(TEST_SRC)) + +# Rules + +.PHONY: default clean + +default: $(TEST_BIN) + +$(TEST_BIN): + mkdir -p $(BIN_DIR) + $(CPP) $(CPPFLAGS) $(SRC_DIR)/$@.cpp $(LFLAGS) -o $(BIN_DIR)/$@ + +clean: + $(RM) -r $(BIN_DIR)/* diff --git a/tests/test-progs/pthread/Makefile.x86 b/tests/test-progs/pthread/Makefile.x86 new file mode 100644 index 000000000..7ad89f07f --- /dev/null +++ b/tests/test-progs/pthread/Makefile.x86 @@ -0,0 +1,34 @@ +# x86 - native + +CPP := g++ +OBJDUMP := objdump +CPPFLAGS := -g -O3 -static -std=c++11 +LFLAGS := -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive + +SRC_DIR := ./src +BIN_DIR := ./bin + +TEST_SRC := test_pthread_create_seq.cpp \ + test_pthread_create_para.cpp \ + test_pthread_mutex.cpp \ + test_atomic.cpp \ + test_pthread_cond.cpp \ + test_std_thread.cpp \ + test_std_mutex.cpp \ + test_std_condition_variable.cpp \ + +TEST_BIN := $(patsubst %.cpp,%,$(TEST_SRC)) +TEST_DUMP := $(patsubst %.cpp,%.objdump,$(TEST_SRC)) + +# Rules + +.PHONY: default clean + +default: $(TEST_BIN) + +$(TEST_BIN): + mkdir -p $(BIN_DIR) + $(CPP) $(CPPFLAGS) $(SRC_DIR)/$@.cpp $(LFLAGS) -o $(BIN_DIR)/$@ + +clean: + $(RM) -r $(BIN_DIR)/* diff --git a/tests/test-progs/pthread/src/test_atomic.cpp b/tests/test-progs/pthread/src/test_atomic.cpp new file mode 100644 index 000000000..edcb046ca --- /dev/null +++ b/tests/test-progs/pthread/src/test_atomic.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Tuan Ta + */ + +#include + +#include +#include +#include + +//------------------------------------------------------------------------ +// Create n threads, run them in parallel and wait for them in the master +// thread. +// Each child thread increments a shared variable m times atomically +//------------------------------------------------------------------------ + +#define MAX_N_WORKER_THREADS 10 + +typedef struct +{ + int nsteps; + std::atomic* shared_var; +} ThreadArg; + +void* func( void* args ) +{ + ThreadArg* my_args = ( ThreadArg* ) args; + + int nsteps = my_args->nsteps; + std::atomic* shared_var = my_args->shared_var; + + for ( int i = 0; i < nsteps; ++i ) { + std::atomic_fetch_add(shared_var, 1); + } + + return nullptr; +} + +int main( int argc, const char* argv[] ) +{ + int n_worker_threads = 0; + + // allocate all threads + pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS]; + + // variable shared among all threads + std::atomic shared_var(0); + + // number of steps each thread increments the shared_var + int nsteps = 1000; + + // set up threads' arguments + ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS]; + + int ret = 0; + for ( size_t tid = 0; tid < MAX_N_WORKER_THREADS; tid++ ){ + t_args[tid].nsteps = nsteps; + t_args[tid].shared_var = &shared_var; + + // spawn thread + ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] ); + + if (ret != 0) { + break; + } + + n_worker_threads++; + } + + // sync up all threads + for ( int tid = 0; tid < n_worker_threads; ++tid ) { + pthread_join( threads[tid], nullptr ); + } + + // clean up + delete[] threads; + delete[] t_args; + + // verify + if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_pthread_cond.cpp b/tests/test-progs/pthread/src/test_pthread_cond.cpp new file mode 100644 index 000000000..5552f7853 --- /dev/null +++ b/tests/test-progs/pthread/src/test_pthread_cond.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Moyang Wang + */ + +#include + +#include +#include +#include +#include + +//------------------------------------------------------------------------ +// Test pthread_cond +//------------------------------------------------------------------------ +// The master thread creates N threads, each of which waits on a +// condition variable of a signal to start. The master thread then set +// the signal and notifies all other threads to begin. + +#define MAX_N_WORKER_THREADS 10 + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t cv = PTHREAD_COND_INITIALIZER; + +bool ready = false; + +void* print_id( void* arg_vptr ) +{ + pthread_mutex_lock( &mutex ); + long id = (long)arg_vptr; + + while (!ready) { + pthread_cond_wait( &cv, &mutex ); + } + // ... + std::cout << "thread " << id << '\n'; + + pthread_mutex_unlock( &mutex ); + + return nullptr; +} + +void go() +{ + pthread_mutex_lock( &mutex ); + ready = true; + pthread_cond_broadcast( &cv ); + pthread_mutex_unlock( &mutex ); +} + +int main( int argc, char* argv[] ) +{ + size_t n_worker_threads = 0; + + std::vector< pthread_t > threads( MAX_N_WORKER_THREADS ); + + int ret = 0; + for ( size_t i = 0; i < MAX_N_WORKER_THREADS; i++ ){ + ret = pthread_create( &threads[i], nullptr, print_id, (void*)i ); + if (ret != 0) { + break; + } + n_worker_threads++; + } + + std::cout << n_worker_threads << " threads ready to race...\n"; + + go(); + + for ( size_t i = 1; i < n_worker_threads; i++ ) { + pthread_join( threads[i], nullptr ); + } + + if (n_worker_threads < 1) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_pthread_create_para.cpp b/tests/test-progs/pthread/src/test_pthread_create_para.cpp new file mode 100644 index 000000000..2ef11b216 --- /dev/null +++ b/tests/test-progs/pthread/src/test_pthread_create_para.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Tuan Ta + */ + +#include + +#include +#include + +//------------------------------------------------------------------------ +// Create n threads, run them in parallel and wait for them in the master +// thread. +// Each child thread writes its thread id to an output array +//------------------------------------------------------------------------ + +#define MAX_N_WORKER_THREADS 10 + +typedef struct +{ + int tid; + int* output; +} ThreadArg; + +void* func( void* args ) +{ + ThreadArg* my_args = ( ThreadArg* ) args; + + // write tid to this thread's output + (*my_args->output) = my_args->tid; + + return nullptr; +} + +int main( int argc, const char* argv[] ) +{ + int n_worker_threads = 0; + + // allocate all threads + pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS]; + ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS]; + + // create an output array for all threads + int* outputs = new int[MAX_N_WORKER_THREADS]; + int ret; + + // try to spawn as many worker threads as possible + for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { + + // set up thread args + t_args[tid].tid = tid; + t_args[tid].output = outputs + tid; + + // spawn thread + ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] ); + if (ret != 0) { + break; + } + + n_worker_threads++; + } + + // sync up all threads + for ( int tid = 0; tid < n_worker_threads; ++tid ) { + pthread_join( threads[tid], nullptr ); + } + + // verify + bool passed = true; + for ( int i = 0; i < n_worker_threads; ++i ) { + if ( outputs[i] != i ) { + passed = false; + } + } + + // clean up + delete[] threads; + delete[] t_args; + delete[] outputs; + + // failed if outputs are not correct or no worker thread was spawned + if (!passed || n_worker_threads < 1) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_pthread_create_seq.cpp b/tests/test-progs/pthread/src/test_pthread_create_seq.cpp new file mode 100644 index 000000000..fdffd7957 --- /dev/null +++ b/tests/test-progs/pthread/src/test_pthread_create_seq.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Tuan Ta + */ + +#include + +#include +#include + +//------------------------------------------------------------------------ +// Create n threads and run them one after another +// Each child thread writes its thread id to an output array +//------------------------------------------------------------------------ + +#define MAX_N_WORKER_THREADS 10 + +typedef struct +{ + int tid; + int* output; +} ThreadArg; + +void* func( void* args ) +{ + ThreadArg* my_args = ( ThreadArg* ) args; + + // write tid to this thread's output + (*my_args->output) = my_args->tid; + + return nullptr; +} + +int main( int argc, const char* argv[] ) +{ + int n_worker_threads = 0; + + // allocate all threads + pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS]; + ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS]; + + // create an output array for all threads + int* outputs = new int[MAX_N_WORKER_THREADS]; + int ret; + + // try to spawn as many worker threads as possible + for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { + + // set up thread args + t_args[tid].tid = tid; + t_args[tid].output = outputs + tid; + + // spawn thread + ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] ); + if (ret != 0 ) { + break; + } + + n_worker_threads++; + + // wait for the thread to join before moving on + pthread_join( threads[tid], nullptr ); + } + + // verify + bool passed = true; + for ( int i = 0; i < n_worker_threads; ++i ) { + if ( outputs[i] != i ) { + passed = false; + break; + } + } + + // clean up + delete[] threads; + delete[] t_args; + delete[] outputs; + + // failed if outputs are not correct or no worker thread was spawned + if (!passed || n_worker_threads < 1) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_pthread_mutex.cpp b/tests/test-progs/pthread/src/test_pthread_mutex.cpp new file mode 100644 index 000000000..9528a3607 --- /dev/null +++ b/tests/test-progs/pthread/src/test_pthread_mutex.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Tuan Ta + */ + +#include + +#include +#include + +//------------------------------------------------------------------------ +// Create n threads, run them in parallel and wait for them in the master +// thread. +// Each child thread increments a shared variable m times +//------------------------------------------------------------------------ + +#define MAX_N_WORKER_THREADS 10 + +typedef struct +{ + int nsteps; + int* shared_var; + pthread_mutex_t* lock; +} ThreadArg; + +void* func( void* args ) +{ + ThreadArg* my_args = ( ThreadArg* ) args; + + int nsteps = my_args->nsteps; + int* shared_var = my_args->shared_var; + pthread_mutex_t* lock = my_args->lock; + + for ( int i = 0; i < nsteps; ++i ) { + // acquire the lock + pthread_mutex_lock(lock); + + // increment the shared_var + (*shared_var)++; + + // release the lock + pthread_mutex_unlock(lock); + } + + return nullptr; +} + +int main( int argc, const char* argv[] ) +{ + int n_worker_threads = 0; + + // allocate all threads + pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS]; + ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS]; + + // variable shared among all threads + int shared_var = 0; + + // number of steps each thread increments the shared_var + int nsteps = 10000; + + // create a shared lock + pthread_mutex_t lock; + pthread_mutex_init(&lock, NULL); + + int ret; + + // try to spawn as many worker threads as possible + for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { + t_args[tid].nsteps = nsteps; + t_args[tid].shared_var = &shared_var; + t_args[tid].lock = &lock; + + // spawn thread + ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] ); + + if (ret != 0) + break; + + n_worker_threads++; + } + + // sync up all threads + for ( int tid = 0; tid < n_worker_threads; ++tid ) { + pthread_join( threads[tid], nullptr ); + } + + // verify + bool passed = true; + if ( shared_var != n_worker_threads * nsteps ) + passed = false; + + // clean up + delete[] threads; + delete[] t_args; + + if (!passed || n_worker_threads < 1) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_std_condition_variable.cpp b/tests/test-progs/pthread/src/test_std_condition_variable.cpp new file mode 100644 index 000000000..012c509eb --- /dev/null +++ b/tests/test-progs/pthread/src/test_std_condition_variable.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Moyang Wang + */ + +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------ +// Test std::condition_variable +//------------------------------------------------------------------------ +// The master thread creates N threads, each of which waits on a +// condition variable of a signal to start. The master thread then set +// the signal and notifies all other threads to begin. + +#define MAX_N_WORKER_THREADS 10 + +std::mutex mtx; +std::condition_variable cv; +bool ready = false; + +void print_id( size_t id ) +{ + std::unique_lock lck(mtx); + while (!ready) + cv.wait(lck); + // ... + std::cout << "thread " << id << '\n'; +} + +void go() +{ + std::unique_lock lck(mtx); + ready = true; + cv.notify_all(); +} + +int main( int argc, char* argv[] ) +{ + size_t n_worker_threads = 0; + + std::vector< std::thread > threads; + + for ( size_t i = 0; i < MAX_N_WORKER_THREADS; i++ ) { + try { + threads.push_back( std::thread( print_id, i ) ); + } catch ( const std::system_error& err ) { + break; + } + n_worker_threads++; + } + + std::cout << n_worker_threads << " threads ready to race...\n"; + go(); // go! + + for (int i = 0; i < n_worker_threads; ++i) { + threads[i].join(); + } + + // if there is no timeout (i.e., threads are all waken up properly, this + // test always succeeds) + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_std_mutex.cpp b/tests/test-progs/pthread/src/test_std_mutex.cpp new file mode 100644 index 000000000..f97147702 --- /dev/null +++ b/tests/test-progs/pthread/src/test_std_mutex.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Tuan Ta, Moyang Wang + */ + +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------ +// Create n threads, run them in parallel and wait for them in the master +// thread. +// Each child thread increments a shared variable m times +//------------------------------------------------------------------------ + +#define MAX_N_WORKER_THREADS 10 + +int main( int argc, const char* argv[] ) +{ + int n_worker_threads = 0; + + // allocate all threads + std::vector< std::thread > threads; + + // mutex to protect the shared variable + std::mutex my_mutex; + + // variable shared among all threads + int shared_var = 0; + + // number of steps each thread increments the shared_var + int nsteps = 1000; + + for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { + try { + threads.push_back( std::thread( [&] { + std::lock_guard guard(my_mutex); + for ( int i = 0; i < nsteps; ++i ) + shared_var++; + } ) ); + } catch ( const std::system_error& err ) { + break; + } + n_worker_threads++; + } + + // sync up all threads + for (int i = 0; i < n_worker_threads; ++i) { + threads[i].join(); + } + + // verify + if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/tests/test-progs/pthread/src/test_std_thread.cpp b/tests/test-progs/pthread/src/test_std_thread.cpp new file mode 100644 index 000000000..2351a36a0 --- /dev/null +++ b/tests/test-progs/pthread/src/test_std_thread.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2018, Cornell University + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of Cornell University nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Authors: Moyang Wang + */ + +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------ +// Test std::thread +//------------------------------------------------------------------------ +// Create n threads, run them in parallel and wait for them in the master +// thread. +// Each child thread writes its thread id to an output array + +#define MAX_N_WORKER_THREADS 10 + +int main( int argc, char* argv[] ) +{ + int n_worker_threads = 0; + + std::vector< std::thread > threads; + std::vector outputs( MAX_N_WORKER_THREADS, 0 ); + + for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { + try { + threads.push_back( std::thread( [&] (size_t thread_id ) { + std::cout << "Hello from thread " << thread_id + << std::endl; + outputs[thread_id] = thread_id; + }, tid ) ); + } catch ( const std::system_error& err ) { + break; + } + n_worker_threads++; + } + + std::cout << "Hello from master thread" << std::endl; + + // sync up all threads + for (int i = 0; i < n_worker_threads; ++i) { + threads[i].join(); + } + + if (n_worker_threads < 1) { + return EXIT_FAILURE; + } + + for ( int i = 0; i < n_worker_threads; ++i ) { + if ( outputs[i] != i ) { + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3