diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/invalid/invalid.c | 30 | ||||
| -rw-r--r-- | src/invalid/invalid_thread.c | 9 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 686bf23..560b6e7 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -241,10 +241,10 @@ static size_t invalid_curves_threaded(const curve_t *curve, pari_ulong *primes, struct pari_thread pari_threads[cfg->threads]; pari_thread_sync(); - size_t generated = 0; - state_e states[nprimes]; - state_e old_states[nprimes]; - curve_t *local_curves[nprimes]; + size_t *generated = try_calloc(sizeof(size_t)); + state_e *states = try_calloc(sizeof(state_e) * nprimes); + state_e *old_states = try_calloc(sizeof(state_e) * nprimes); + curve_t **local_curves = try_calloc(sizeof(curve_t *) * nprimes); for (size_t i = 0; i < nprimes; ++i) { states[i] = STATE_FREE; old_states[i] = STATE_FREE; @@ -262,7 +262,7 @@ static size_t invalid_curves_threaded(const curve_t *curve, pari_ulong *primes, threads[i].primes = primes; threads[i].states = states; threads[i].curves = local_curves; - threads[i].generated = &generated; + threads[i].generated = generated; threads[i].mutex_state = &state_mutex; threads[i].cond_generated = &generated_cond; threads[i].cfg = cfg; @@ -275,22 +275,22 @@ static size_t invalid_curves_threaded(const curve_t *curve, pari_ulong *primes, (void *)&threads[i]); } - bool running = true; - do { + while (true) { pthread_cond_wait(&generated_cond, &state_mutex); for (size_t i = 0; i < nprimes; ++i) { if (old_states[i] != states[i] && states[i] == STATE_GENERATED) { output_o(local_curves[i]); - if (generated != nprimes) { + if (*generated != nprimes) { output_o_separator(); } old_states[i] = states[i]; } } - if (generated == nprimes) running = false; - pthread_mutex_unlock(&state_mutex); - } while (running); + if (*generated == nprimes) + break; + } + pthread_mutex_unlock(&state_mutex); for (size_t i = 0; i < cfg->threads; ++i) { pthread_join(pthreads[i], NULL); @@ -307,7 +307,13 @@ static size_t invalid_curves_threaded(const curve_t *curve, pari_ulong *primes, pthread_mutex_destroy(&state_mutex); pthread_cond_destroy(&generated_cond); - return generated; + size_t result = *generated; + try_free(generated); + try_free(states); + try_free(old_states); + try_free(local_curves); + + return result; } curve_t *invalid_original_curve(exhaustive_t *setup) { diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c index 74f247c..2e01109 100644 --- a/src/invalid/invalid_thread.c +++ b/src/invalid/invalid_thread.c @@ -11,6 +11,13 @@ #include "util/random.h" #include "util/timeout.h" +static size_t invalid_get_generated(thread_t *thread) { + pthread_mutex_lock(thread->mutex_state); + size_t result = *thread->generated; + pthread_mutex_unlock(thread->mutex_state); + return result; +} + void *invalid_thread(void *arg) { thread_t *thread = (thread_t *)arg; pari_thread_start(thread->pari_thread); @@ -26,7 +33,7 @@ void *invalid_thread(void *arg) { invalid->field = gcopy(thread->original_curve->field); invalid->a = gcopy(thread->original_curve->a); - while (*thread->generated < thread->nprimes) { + while (invalid_get_generated(thread) < thread->nprimes) { pari_sp btop = avma; exhaustive_gen(invalid, thread->setup, OFFSET_B, OFFSET_GENERATORS); size_t ndivides = 0; |
