Permalink
Browse files

use long for forpool

  • Loading branch information...
1 parent 49900f0 commit de09fb7dff67be7c1a58e5be9fee4b4a9ca3a265 @lh3 lh3 committed Mar 4, 2017
Showing with 9 additions and 7 deletions.
  1. +9 −7 kthread.c
View
@@ -158,13 +158,15 @@ void kt_forpool_destroy(void *_fp)
void kt_forpool(void *_fp, void (*func)(void*,long,int), void *data, long n)
{
kt_forpool_t *fp = (kt_forpool_t*)_fp;
- int i;
- fp->n = n, fp->func = func, fp->data = data, fp->n_pending = fp->n_threads;
- for (i = 0; i < fp->n_threads; ++i) fp->w[i].i = i, fp->w[i].action = 1;
- pthread_mutex_lock(&fp->mutex);
- pthread_cond_broadcast(&fp->cv_s);
- while (fp->n_pending) pthread_cond_wait(&fp->cv_m, &fp->mutex);
- pthread_mutex_unlock(&fp->mutex);
+ long i;
+ if (fp && fp->n_threads > 1) {
+ fp->n = n, fp->func = func, fp->data = data, fp->n_pending = fp->n_threads;
+ for (i = 0; i < fp->n_threads; ++i) fp->w[i].i = i, fp->w[i].action = 1;
+ pthread_mutex_lock(&fp->mutex);
+ pthread_cond_broadcast(&fp->cv_s);
+ while (fp->n_pending) pthread_cond_wait(&fp->cv_m, &fp->mutex);
+ pthread_mutex_unlock(&fp->mutex);
+ } else for (i = 0; i < n; ++i) func(data, i, 0);
}
/*****************

0 comments on commit de09fb7

Please sign in to comment.