]> git.kaiwu.me - klib.git/commitdiff
use long for forpool
authorHeng Li <lh3@me.com>
Sat, 4 Mar 2017 14:03:57 +0000 (09:03 -0500)
committerHeng Li <lh3@me.com>
Sat, 4 Mar 2017 14:03:57 +0000 (09:03 -0500)
kthread.c

index d83df88c76a27bb9c0ee44cce11cd92cddfb1391..986f695de6fe72d31a9283120ed74374d1f29464 100644 (file)
--- a/kthread.c
+++ b/kthread.c
@@ -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);
 }
 
 /*****************