Skip to content

Commit a6c51d0

Browse files
committed
Bugfix: Do not set number of threads in advance, increase it when threads are created.
1 parent ea55027 commit a6c51d0

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

tst_threads.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "config.h"
2-
#ifdef HAVE_PTHREAD_H
3-
# include <pthread.h>
4-
#endif
2+
3+
#include <assert.h>
4+
#include <pthread.h>
55

66
#include "mpi_test_suite.h"
77

88

9-
static int num_threads; /* Number of available threads - 1 */
9+
static int num_threads; /**< Number of available threads - 1 */
1010
static int working;
1111
static pthread_mutex_t working_mutex = PTHREAD_MUTEX_INITIALIZER;
1212
static pthread_cond_t working_cond = PTHREAD_COND_INITIALIZER;
@@ -171,18 +171,14 @@ static void * tst_thread_dispatcher (void * arg)
171171
}
172172

173173

174-
/*
175-
* Initialize the thread-framework
176-
*/
177-
int tst_thread_init (int max_threads, struct tst_thread_env_t *** thread_env)
178-
{
174+
/** \brief Initialize the thread-framework */
175+
int tst_thread_init(int max_threads, struct tst_thread_env_t ***thread_env) {
179176
int i;
180177
struct tst_thread_env_t ** env;
181178

182-
if (max_threads <= 0 || thread_env == NULL)
183-
ERROR (EINVAL, "Called with faulty arguments");
179+
assert(max_threads > 0);
180+
assert(thread_env != NULL);
184181

185-
num_threads = max_threads - 1;
186182
/*
187183
* Without the pthread_mutex_lock, as no threads are started, yet
188184
*/
@@ -201,20 +197,21 @@ int tst_thread_init (int max_threads, struct tst_thread_env_t *** thread_env)
201197
*/
202198
tst_thread_tid_array[0] = pthread_self();
203199

204-
for (i = 0; i < num_threads; i++)
205-
{
206-
int ret;
207-
env[i] = malloc (sizeof (struct tst_thread_env_t));
208-
memset (env[i], 0, sizeof (struct tst_thread_env_t));
209-
env[i]->thread_num = i;
210-
env[i]->state = TST_THREAD_STATE_IDLE;
211-
ret = pthread_create (&(env[i]->tid), NULL, tst_thread_dispatcher, env[i]);
212-
if (ret != 0)
213-
ERROR (errno, "tst_thread_init: pthread_create");
214-
tst_thread_tid_array[i + 1] = env[i]->tid;
215-
}
216-
200+
for (i = 0; i < max_threads - 1; i++) {
201+
int ret;
202+
env[i] = malloc (sizeof (struct tst_thread_env_t));
203+
memset (env[i], 0, sizeof (struct tst_thread_env_t));
204+
env[i]->thread_num = i;
205+
env[i]->state = TST_THREAD_STATE_IDLE;
206+
ret = pthread_create (&(env[i]->tid), NULL, tst_thread_dispatcher, env[i]);
207+
if (ret != 0)
208+
ERROR (errno, "tst_thread_init: pthread_create");
209+
tst_thread_tid_array[i + 1] = env[i]->tid;
210+
211+
num_threads++;
212+
}
217213

214+
assert(num_threads == max_threads - 1);
218215
*thread_env = env;
219216
return 0;
220217
}

0 commit comments

Comments
 (0)