tests: Delete authors lists from test files.
[gem5.git] / tests / test-progs / pthread / src / test_pthread_create_para.cpp
1 /*
2 * Copyright (c) 2018, Cornell University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * Neither the name of Cornell University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <pthread.h>
37
38 #include <cstdlib>
39 #include <iostream>
40
41 //------------------------------------------------------------------------
42 // Create n threads, run them in parallel and wait for them in the master
43 // thread.
44 // Each child thread writes its thread id to an output array
45 //------------------------------------------------------------------------
46
47 #define MAX_N_WORKER_THREADS 10
48
49 typedef struct
50 {
51 int tid;
52 int* output;
53 } ThreadArg;
54
55 void* func( void* args )
56 {
57 ThreadArg* my_args = ( ThreadArg* ) args;
58
59 // write tid to this thread's output
60 (*my_args->output) = my_args->tid;
61
62 return nullptr;
63 }
64
65 int main( int argc, const char* argv[] )
66 {
67 int n_worker_threads = 0;
68
69 // allocate all threads
70 pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
71 ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
72
73 // create an output array for all threads
74 int* outputs = new int[MAX_N_WORKER_THREADS];
75 int ret;
76
77 // try to spawn as many worker threads as possible
78 for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
79
80 // set up thread args
81 t_args[tid].tid = tid;
82 t_args[tid].output = outputs + tid;
83
84 // spawn thread
85 ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
86 if (ret != 0) {
87 break;
88 }
89
90 n_worker_threads++;
91 }
92
93 // sync up all threads
94 for ( int tid = 0; tid < n_worker_threads; ++tid ) {
95 pthread_join( threads[tid], nullptr );
96 }
97
98 // verify
99 bool passed = true;
100 for ( int i = 0; i < n_worker_threads; ++i ) {
101 if ( outputs[i] != i ) {
102 passed = false;
103 }
104 }
105
106 // clean up
107 delete[] threads;
108 delete[] t_args;
109 delete[] outputs;
110
111 // failed if outputs are not correct or no worker thread was spawned
112 if (!passed || n_worker_threads < 1)
113 return EXIT_FAILURE;
114
115 return EXIT_SUCCESS;
116 }