+++ /dev/null
-# Copyright (c) 2019 ARM Limited
-# All rights reserved.
-#
-# The license below extends only to copyright in the software and shall
-# not be construed as granting a license to any other intellectual
-# property including but not limited to intellectual property relating
-# to a hardware implementation of the functionality of the software
-# licensed hereunder. You may use the software subject to the license
-# terms below provided that you ensure that this notice is replicated
-# unmodified and in its entirety in all distributions of the software,
-# modified or unmodified, in source code or in binary form.
-#
-# 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 the copyright holders 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
-# OWNER 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.
-
-CROSS_COMPILE = arm-linux-gnueabihf-
-BIN_DIR = bin.aarch32
-
--include Makefile.common
+++ /dev/null
-# Copyright (c) 2019 ARM Limited
-# All rights reserved.
-#
-# The license below extends only to copyright in the software and shall
-# not be construed as granting a license to any other intellectual
-# property including but not limited to intellectual property relating
-# to a hardware implementation of the functionality of the software
-# licensed hereunder. You may use the software subject to the license
-# terms below provided that you ensure that this notice is replicated
-# unmodified and in its entirety in all distributions of the software,
-# modified or unmodified, in source code or in binary form.
-#
-# 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 the copyright holders 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
-# OWNER 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.
-
-CROSS_COMPILE = aarch64-linux-gnu-
-BIN_DIR = bin.aarch64
-
--include Makefile.common
+++ /dev/null
-# Copyright (c) 2019 ARM Limited
-# All rights reserved.
-#
-# The license below extends only to copyright in the software and shall
-# not be construed as granting a license to any other intellectual
-# property including but not limited to intellectual property relating
-# to a hardware implementation of the functionality of the software
-# licensed hereunder. You may use the software subject to the license
-# terms below provided that you ensure that this notice is replicated
-# unmodified and in its entirety in all distributions of the software,
-# modified or unmodified, in source code or in binary form.
-#
-# 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 the copyright holders 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
-# OWNER 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.
-
-CC := $(CROSS_COMPILE)$(CC)
-CXX := $(CROSS_COMPILE)$(CXX)
-LD := $(CXX)
-CCFLAGS ?=
-CXXFLAGS ?= -g -O3 -static -std=c++11
-LDFLAGS ?= -pthread
-
-BIN_DIR ?= ./bin
-
-TESTS = test_pthread_create_seq \
- test_pthread_create_para \
- test_pthread_mutex \
- test_atomic \
- test_pthread_cond \
- test_std_thread \
- test_std_mutex \
- test_std_condition_variable
-
-all: $(addprefix $(BIN_DIR)/,$(TESTS))
-
-$(BIN_DIR)/%: src/%.cpp
- @mkdir -p $(BIN_DIR)
- $(CXX) -o $@ $(CCFLAGS) $(CXXFLAGS) $(LDFLAGS) $^
-
-clean:
- $(RM) $(BIN_DIR)/*
-
-.PHONY: all clean
-
+++ /dev/null
-# Copyright (c) 2019 ARM Limited
-# All rights reserved.
-#
-# The license below extends only to copyright in the software and shall
-# not be construed as granting a license to any other intellectual
-# property including but not limited to intellectual property relating
-# to a hardware implementation of the functionality of the software
-# licensed hereunder. You may use the software subject to the license
-# terms below provided that you ensure that this notice is replicated
-# unmodified and in its entirety in all distributions of the software,
-# modified or unmodified, in source code or in binary form.
-#
-# 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 the copyright holders 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
-# OWNER 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.
-
-CROSS_COMPILE = riscv64-unknown-linux-gnu-
-BIN_DIR = bin.riscv64
-
--include Makefile.common
+++ /dev/null
-# Copyright (c) 2019 ARM Limited
-# All rights reserved.
-#
-# The license below extends only to copyright in the software and shall
-# not be construed as granting a license to any other intellectual
-# property including but not limited to intellectual property relating
-# to a hardware implementation of the functionality of the software
-# licensed hereunder. You may use the software subject to the license
-# terms below provided that you ensure that this notice is replicated
-# unmodified and in its entirety in all distributions of the software,
-# modified or unmodified, in source code or in binary form.
-#
-# 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 the copyright holders 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
-# OWNER 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.
-
-CROSS_COMPILE =
-BIN_DIR = bin.x86
-
--include Makefile.common
+++ /dev/null
-/*
- * 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.
- */
-
-#include <pthread.h>
-
-#include <atomic>
-#include <cstdlib>
-#include <iostream>
-
-//------------------------------------------------------------------------
-// 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<int>* shared_var;
-} ThreadArg;
-
-void* func( void* args )
-{
- ThreadArg* my_args = ( ThreadArg* ) args;
-
- int nsteps = my_args->nsteps;
- std::atomic<int>* 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<int> 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;
-}
+++ /dev/null
-/*
- * 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 <pthread.h>
-
-#include <cstdlib>
-#include <iostream>
-#include <mutex>
-#include <vector>
-
-//------------------------------------------------------------------------
-// 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <pthread.h>
-
-#include <cstdlib>
-#include <iostream>
-
-//------------------------------------------------------------------------
-// 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <pthread.h>
-
-#include <cstdlib>
-#include <iostream>
-
-//------------------------------------------------------------------------
-// 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <pthread.h>
-
-#include <cstdlib>
-#include <iostream>
-
-//------------------------------------------------------------------------
-// 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <condition_variable>
-#include <iostream>
-#include <mutex>
-#include <thread>
-#include <vector>
-
-//------------------------------------------------------------------------
-// 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<std::mutex> lck(mtx);
- while (!ready)
- cv.wait(lck);
- // ...
- std::cout << "thread " << id << '\n';
-}
-
-void go()
-{
- std::unique_lock<std::mutex> 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <cstdlib>
-#include <iostream>
-#include <mutex>
-#include <thread>
-#include <vector>
-
-//------------------------------------------------------------------------
-// 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<std::mutex> 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;
-}
+++ /dev/null
-/*
- * 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.
- */
-
-#include <cstdlib>
-#include <iostream>
-#include <string>
-#include <thread>
-#include <vector>
-
-//------------------------------------------------------------------------
-// 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<int> 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;
-}