Changeset: e1e3417ef50d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/e1e3417ef50d
Modified Files:
        geom/lib/libgeom.c
        geom/lib/libgeom.h
        geom/monetdb5/geom.c
Branch: geos-reentrant
Log Message:

use a per thread geom library context


diffs (133 lines):

diff --git a/geom/lib/libgeom.c b/geom/lib/libgeom.c
--- a/geom/lib/libgeom.c
+++ b/geom/lib/libgeom.c
@@ -20,8 +20,6 @@
 #include "monetdb_config.h"
 #include "libgeom.h"
 
-GEOSContextHandle_t geoshandle;
-
 static void __attribute__((__format__(__printf__, 1, 2)))
 geomerror(_In_z_ _Printf_format_string_ const char *fmt, ...)
 {
@@ -34,23 +32,44 @@ geomerror(_In_z_ _Printf_format_string_ 
        va_end(va);
 }
 
-void
+static MT_TLS_t geom_tls_key;
+
+static void
+libgeom_tls_init(void *dummy)
+{
+       (void)dummy;
+       GEOSContextHandle_t ctx = GEOS_init_r ();
+    GEOSContext_setNoticeHandler_r(ctx, (GEOSMessageHandler) geomerror);
+    GEOSContext_setErrorHandler_r(ctx, (GEOSMessageHandler) geomerror);
+       GEOS_setWKBByteOrder_r(ctx, 1); /* NDR (little endian) */
+       MT_tls_set(geom_tls_key, ctx);
+}
+
+static void
+libgeom_tls_exit(void *dummy)
+{
+       (void)dummy;
+       GEOSContextHandle_t ctx = MT_tls_get(geom_tls_key);
+       GEOS_finish_r(ctx);
+}
+
+GEOSContextHandle_t
+libgeom_tls(void)
+{
+       return MT_tls_get(geom_tls_key);
+}
+
+gdk_return
 libgeom_init(void)
 {
-       //geoshandle = initGEOS_r((GEOSMessageHandler) geomerror, 
(GEOSMessageHandler) geomerror);
-       geoshandle = GEOS_init_r ();
-    GEOSContext_setNoticeHandler_r(geoshandle, (GEOSMessageHandler) geomerror);
-    GEOSContext_setErrorHandler_r(geoshandle, (GEOSMessageHandler) geomerror);
+       if (MT_alloc_tls(&geom_tls_key) != GDK_SUCCEED ||
+               MT_thread_init_add_callback(libgeom_tls_init, libgeom_tls_exit, 
NULL) != GDK_SUCCEED)
+               return GDK_FAIL;
+
     // TODO: deprecated call REMOVE
-       GEOS_setWKBByteOrder_r(geoshandle, 1);  /* NDR (little endian) */
        printf("# MonetDB/GIS module loaded\n");
        fflush(stdout);         /* make merovingian see this *now* */
-}
-
-void
-libgeom_exit(void)
-{
-       GEOS_finish_r(geoshandle);
+       return GDK_SUCCEED;
 }
 
 bool
diff --git a/geom/lib/libgeom.h b/geom/lib/libgeom.h
--- a/geom/lib/libgeom.h
+++ b/geom/lib/libgeom.h
@@ -36,8 +36,6 @@
 #include <proj.h>
 #endif
 
-/* TODO make this per thread */
-extern GEOSContextHandle_t geoshandle;
 
 /* geos does not support 3d envelope */
 typedef struct mbr {
@@ -100,6 +98,9 @@ 11 = MULTIPOLYGON
 
 */
 
+libgeom_export GEOSContextHandle_t libgeom_tls(void);
+#define geoshandle (libgeom_tls())
+
 typedef enum wkb_type {
     // TODO: deprecated        type REMOVE
        //wkbGeometry_mbd = 0,
@@ -133,8 +134,7 @@ typedef struct {
        wkb wkb;
 } geom_geometry;
 
-libgeom_export void libgeom_init(void);
-libgeom_export void libgeom_exit(void);
+libgeom_export gdk_return libgeom_init(void);
 
 #define mbr_nil mbrFromGeos(NULL);
 
diff --git a/geom/monetdb5/geom.c b/geom/monetdb5/geom.c
--- a/geom/monetdb5/geom.c
+++ b/geom/monetdb5/geom.c
@@ -2347,18 +2347,9 @@ geom_prelude(void)
        mbrNIL.xmax = flt_nil;
        mbrNIL.ymin = flt_nil;
        mbrNIL.ymax = flt_nil;
-       libgeom_init();
+       if (libgeom_init() != GDK_SUCCEED)
+               throw(MAL, "geom.prelude", SQLSTATE(HY013) MAL_MALLOC_FAIL);
        TYPE_mbr = malAtomSize(sizeof(mbr), "mbr");
-
-       return MAL_SUCCEED;
-}
-
-/* clean geos */
-str
-geom_epilogue(void *ret)
-{
-       (void) ret;
-       libgeom_exit();
        return MAL_SUCCEED;
 }
 
@@ -5780,7 +5771,6 @@ static mel_func geom_init_funcs[] = {
  command("geom", "mbrDistance", mbrDistance, false, "Returns the distance of 
the centroids of the two boxes", args(1,3, 
arg("",dbl),arg("box1",mbr),arg("box2",mbr))),
  command("geom", "coordinateFromWKB", wkbCoordinateFromWKB, false, "returns 
xmin (=1), ymin (=2), xmax (=3) or ymax(=4) of the provided geometry", 
args(1,3, arg("",dbl),arg("",wkb),arg("",int))),
  command("geom", "coordinateFromMBR", wkbCoordinateFromMBR, false, "returns 
xmin (=1), ymin (=2), xmax (=3) or ymax(=4) of the provided mbr", args(1,3, 
arg("",dbl),arg("",mbr),arg("",int))),
- command("geom", "epilogue", geom_epilogue, false, "", args(1,1, 
arg("",void))),
  command("batgeom", "FromText", wkbFromText_bat, false, "", args(1,4, 
batarg("",wkb),batarg("wkt",str),arg("srid",int),arg("type",int))),
  command("batgeom", "ToText", wkbAsText_bat, false, "", args(1,3, 
batarg("",str),batarg("w",wkb),arg("withSRID",int))),
  command("batgeom", "GeometryType", wkbGeometryType_bat, false, "", args(1,3, 
batarg("",str),batarg("w",wkb),arg("flag",int))),
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to