|
@@ -1,9 +1,6 @@
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
#include <unistd.h>
|
|
|
-#ifdef USE_PTHREAD
|
|
|
-#include <pthread.h>
|
|
|
-#endif
|
|
|
|
|
|
#include <grass/gis.h>
|
|
|
#include <grass/glocale.h>
|
|
@@ -19,8 +16,8 @@ int depths, rows, columns;
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
-static void initialize(expression * e);
|
|
|
-static void evaluate(expression * e);
|
|
|
+static void initialize(expression *e);
|
|
|
+static void evaluate(expression *e);
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
@@ -99,125 +96,21 @@ static void initialize(expression * e)
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
-#ifdef USE_PTHREAD
|
|
|
-
|
|
|
-struct worker {
|
|
|
- struct expression *exp;
|
|
|
- pthread_t thread;
|
|
|
- pthread_cond_t cond;
|
|
|
- pthread_mutex_t mutex;
|
|
|
-};
|
|
|
-
|
|
|
-static int num_workers;
|
|
|
-static struct worker *workers;
|
|
|
-
|
|
|
-static pthread_mutex_t worker_mutex;
|
|
|
-
|
|
|
-static void *worker(void *arg)
|
|
|
+static void do_evaluate(void *p)
|
|
|
{
|
|
|
- struct worker *w = arg;
|
|
|
-
|
|
|
- for (;;) {
|
|
|
- pthread_mutex_lock(&w->mutex);
|
|
|
- while (!w->exp)
|
|
|
- pthread_cond_wait(&w->cond, &w->mutex);
|
|
|
- evaluate(w->exp);
|
|
|
- w->exp->worker = NULL;
|
|
|
- w->exp = NULL;
|
|
|
- pthread_mutex_unlock(&w->mutex);
|
|
|
- pthread_cond_signal(&w->cond);
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-static struct worker *get_worker(void)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- for (i = 0; i < num_workers; i++) {
|
|
|
- struct worker *w = &workers[i];
|
|
|
- if (!w->exp)
|
|
|
- return w;
|
|
|
- }
|
|
|
-
|
|
|
- return NULL;
|
|
|
+ evaluate((struct expression *) p);
|
|
|
}
|
|
|
|
|
|
static void begin_evaluate(struct expression *e)
|
|
|
{
|
|
|
- struct worker *w;
|
|
|
-
|
|
|
- if (e->worker)
|
|
|
- G_fatal_error("Expression <%s> already has a worker", format_expression(e));
|
|
|
-
|
|
|
- pthread_mutex_lock(&worker_mutex);
|
|
|
- w = get_worker();
|
|
|
- e->worker = w;
|
|
|
-
|
|
|
- if (!w) {
|
|
|
- pthread_mutex_unlock(&worker_mutex);
|
|
|
- evaluate(e);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- pthread_mutex_lock(&w->mutex);
|
|
|
- w->exp = e;
|
|
|
- pthread_cond_signal(&w->cond);
|
|
|
- pthread_mutex_unlock(&w->mutex);
|
|
|
-
|
|
|
- pthread_mutex_unlock(&worker_mutex);
|
|
|
+ G_begin_execute(do_evaluate, e, &e->worker, 1);
|
|
|
}
|
|
|
|
|
|
static void end_evaluate(struct expression *e)
|
|
|
{
|
|
|
- struct worker *w = e->worker;
|
|
|
-
|
|
|
- if (!w)
|
|
|
- return;
|
|
|
-
|
|
|
- pthread_mutex_lock(&w->mutex);
|
|
|
- while (e->worker)
|
|
|
- pthread_cond_wait(&w->cond, &w->mutex);
|
|
|
- pthread_mutex_unlock(&w->mutex);
|
|
|
+ G_end_execute(&e->worker);
|
|
|
}
|
|
|
|
|
|
-static void init_threads(void)
|
|
|
-{
|
|
|
- const char *p = getenv("WORKERS");
|
|
|
- int i;
|
|
|
-
|
|
|
- pthread_mutex_init(&worker_mutex, NULL);
|
|
|
-
|
|
|
- num_workers = p ? atoi(p) : 8;
|
|
|
- workers = G_calloc(num_workers, sizeof(struct worker));
|
|
|
-
|
|
|
- printf("num_workers = %d\n", num_workers);
|
|
|
-
|
|
|
- for (i = 0; i < num_workers; i++) {
|
|
|
- struct worker *w = &workers[i];
|
|
|
- pthread_mutex_init(&w->mutex, NULL);
|
|
|
- pthread_cond_init(&w->cond, NULL);
|
|
|
- pthread_create(&w->thread, NULL, worker, w);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static void end_threads(void)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- pthread_mutex_destroy(&worker_mutex);
|
|
|
-
|
|
|
- for (i = 0; i < num_workers; i++) {
|
|
|
- struct worker *w = &workers[i];
|
|
|
- pthread_cancel(w->thread);
|
|
|
- pthread_mutex_destroy(&w->mutex);
|
|
|
- pthread_cond_destroy(&w->cond);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-#endif
|
|
|
-
|
|
|
/****************************************************************************/
|
|
|
|
|
|
static void evaluate_constant(expression * e)
|
|
@@ -266,7 +159,6 @@ static void evaluate_function(expression * e)
|
|
|
int i;
|
|
|
int res;
|
|
|
|
|
|
-#ifdef USE_PTHREAD
|
|
|
if (e->data.func.argc > 1 && e->data.func.func != f_eval) {
|
|
|
for (i = 1; i <= e->data.func.argc; i++)
|
|
|
begin_evaluate(e->data.func.args[i]);
|
|
@@ -275,9 +167,8 @@ static void evaluate_function(expression * e)
|
|
|
end_evaluate(e->data.func.args[i]);
|
|
|
}
|
|
|
else
|
|
|
-#endif
|
|
|
- for (i = 1; i <= e->data.func.argc; i++)
|
|
|
- evaluate(e->data.func.args[i]);
|
|
|
+ for (i = 1; i <= e->data.func.argc; i++)
|
|
|
+ evaluate(e->data.func.args[i]);
|
|
|
|
|
|
res = (*e->data.func.func) (e->data.func.argc,
|
|
|
e->data.func.argt, e->data.func.argv);
|
|
@@ -439,9 +330,7 @@ void execute(expr_list * ee)
|
|
|
count = rows * depths;
|
|
|
n = 0;
|
|
|
|
|
|
-#ifdef USE_PTHREAD
|
|
|
- init_threads();
|
|
|
-#endif
|
|
|
+ G_init_workers();
|
|
|
|
|
|
for (current_depth = 0; current_depth < depths; current_depth++)
|
|
|
for (current_row = 0; current_row < rows; current_row++) {
|
|
@@ -464,9 +353,7 @@ void execute(expr_list * ee)
|
|
|
n++;
|
|
|
}
|
|
|
|
|
|
-#ifdef USE_PTHREAD
|
|
|
- end_threads();
|
|
|
-#endif
|
|
|
+ G_finish_workers();
|
|
|
|
|
|
if (verbose)
|
|
|
G_percent(n, count, 2);
|