[GRASS-SVN] r65591 - grass/trunk/raster/r.mapcalc

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 14 20:18:21 PDT 2015


Author: glynn
Date: 2015-07-14 20:18:21 -0700 (Tue, 14 Jul 2015)
New Revision: 65591

Modified:
   grass/trunk/raster/r.mapcalc/map.c
Log:
Prevent concurrent raster reads when a mask is present


Modified: grass/trunk/raster/r.mapcalc/map.c
===================================================================
--- grass/trunk/raster/r.mapcalc/map.c	2015-07-14 19:09:54 UTC (rev 65590)
+++ grass/trunk/raster/r.mapcalc/map.c	2015-07-15 03:18:21 UTC (rev 65591)
@@ -71,6 +71,7 @@
 static struct map *maps;
 static int num_maps;
 static int max_maps;
+static int masking;
 
 static int min_row = INT_MAX;
 static int max_row = -INT_MAX;
@@ -81,10 +82,24 @@
 
 #ifdef HAVE_PTHREAD_H
 static pthread_mutex_t cats_mutex;
+static pthread_mutex_t mask_mutex;
 #endif
 
 /****************************************************************************/
 
+static void read_row(int fd, void *buf, int row, int res_type)
+{
+#ifdef HAVE_PTHREAD_H
+    if (masking)
+	pthread_mutex_lock(&mask_mutex);
+#endif
+    Rast_get_row(fd, buf, row, res_type);
+#ifdef HAVE_PTHREAD_H
+    if (masking)
+	pthread_mutex_unlock(&mask_mutex);
+#endif
+}
+
 static void cache_sub_init(struct row_cache *cache, int data_type)
 {
     struct sub_cache *sub = G_malloc(sizeof(struct sub_cache));
@@ -145,7 +160,7 @@
 
     if (i >= 0 && i < cache->nrows) {
 	if (!sub->valid[i]) {
-	    Rast_get_row(cache->fd, sub->buf[i], row + i, data_type);
+	    read_row(cache->fd, sub->buf[i], row + i, data_type);
 	    sub->valid[i] = 1;
 	}
 	return sub->buf[i];
@@ -154,7 +169,7 @@
     if (i <= -cache->nrows || i >= cache->nrows * 2 - 1) {
 	memset(sub->valid, 0, cache->nrows);
 	sub->row = i;
-	Rast_get_row(cache->fd, sub->buf[0], row, data_type);
+	read_row(cache->fd, sub->buf[0], row, data_type);
 	sub->valid[0] = 1;
 	return sub->buf[0];
     }
@@ -182,7 +197,7 @@
     G_freea(tmp);
     G_freea(vtmp);
 
-    Rast_get_row(cache->fd, sub->buf[i], row, data_type);
+    read_row(cache->fd, sub->buf[i], row, data_type);
     sub->valid[i] = 1;
 
     return sub->buf[i];
@@ -365,11 +380,6 @@
 #endif
 }
 
-static void read_row(int fd, void *buf, int row, int res_type)
-{
-    Rast_get_row(fd, buf, row, res_type);
-}
-
 static void setup_map(struct map *m)
 {
     int nrows = m->max_row - m->min_row + 1;
@@ -576,6 +586,8 @@
 
 #ifdef HAVE_PTHREAD_H
     pthread_mutex_init(&cats_mutex, NULL);
+    pthread_mutex_init(&mask_mutex, NULL);
+    masking = Rast_maskfd() >= 0;
 #endif
 
     for (i = 0; i < num_maps; i++)
@@ -635,6 +647,7 @@
 
 #ifdef HAVE_PTHREAD_H
     pthread_mutex_destroy(&cats_mutex);
+    pthread_mutex_destroy(&mask_mutex);
 #endif
 }
 



More information about the grass-commit mailing list