Changeset: 160ced20e35c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=160ced20e35c
Added Files:
        documentation/source/json.rst
Removed Files:
        sql/test/rdf/Tests/q8_v_small.reqtests
        sql/test/rdf/Tests/q8_v_small.sql
        sql/test/rdf/Tests/q8_v_small.stable.err
        sql/test/rdf/Tests/q8_v_small.stable.out
Modified Files:
        clients/Tests/MAL-signatures.stable.out
        clients/Tests/MAL-signatures.stable.out.int128
        documentation/source/index.rst
        gdk/gdk.h
        gdk/gdk_bat.c
        gdk/gdk_system.c
        gdk/gdk_tracer.c
        gdk/gdk_tracer.h
        gdk/gdk_utils.c
        monetdb5/modules/atoms/Tests/json15.malC
        monetdb5/modules/atoms/Tests/json15.stable.out
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/json.h
        monetdb5/modules/kernel/mmath.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/mkey.c
        monetdb5/modules/mal/pcre.c
        monetdb5/modules/mal/txtsim.c
        monetdb5/modules/mal/wlc.mal
        sql/backends/monet5/rel_bin.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_cat.c
        sql/backends/monet5/sql_statement.c
        sql/backends/monet5/sqlcatalog.mal
        sql/backends/monet5/wlr.mal
        sql/common/sql_types.c
        sql/include/sql_relation.h
        sql/jdbc/tests/Tests/Test_PSsomeamount.timeout
        sql/rel.txt
        sql/scripts/22_clients.sql
        sql/server/rel_dump.c
        sql/server/rel_optimizer.c
        sql/server/rel_propagate.c
        sql/server/rel_psm.c
        sql/server/rel_rel.c
        sql/server/rel_rel.h
        sql/server/rel_select.c
        sql/server/rel_unnest.c
        sql/server/rel_updates.c
        sql/server/sql_atom.c
        sql/server/sql_mvc.c
        sql/server/sql_semantic.c
        sql/test/BugTracker-2009/Tests/AVG_of_SQRT.SF-2757642.timeout
        sql/test/BugTracker-2015/Tests/window_function_crash.Bug-3861.sql
        sql/test/BugTracker-2015/Tests/window_function_crash.Bug-3861.stable.err
        sql/test/BugTracker-2015/Tests/window_function_crash.Bug-3861.stable.out
        
sql/test/BugTracker-2019/Tests/json-text-empty-separator.Bug-6798.stable.err
        
sql/test/BugTracker-2019/Tests/json-text-empty-separator.Bug-6798.stable.out
        sql/test/emptydb-upgrade-chain-hge/Tests/upgrade.stable.out.int128
        
sql/test/emptydb-upgrade-chain-hge/Tests/upgrade.stable.out.powerpc64.int128
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out.32bit
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out.int128
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out.powerpc64
        sql/test/emptydb-upgrade-chain/Tests/upgrade.stable.out.powerpc64.int128
        sql/test/emptydb-upgrade-hge/Tests/upgrade.stable.out.int128
        sql/test/emptydb-upgrade/Tests/upgrade.stable.out
        sql/test/emptydb-upgrade/Tests/upgrade.stable.out.32bit
        sql/test/emptydb-upgrade/Tests/upgrade.stable.out.int128
        sql/test/emptydb/Tests/check.stable.out
        sql/test/emptydb/Tests/check.stable.out.32bit
        sql/test/emptydb/Tests/check.stable.out.int128
        sql/test/json/Tests/All
        sql/test/json/Tests/books.stable.out
        sql/test/json/Tests/bulkjson.stable.out
        sql/test/json/Tests/bulklength.stable.out
        sql/test/json/Tests/bulkvalid.stable.out
        sql/test/json/Tests/coercions.stable.out
        sql/test/json/Tests/components.stable.out
        sql/test/json/Tests/jsonvalidity.Bug-3753.sql
        sql/test/json/Tests/jsonvalidity.Bug-3753.stable.out
        sql/test/json/Tests/load.stable.out
        sql/test/json/Tests/parsing.stable.err
        sql/test/json/Tests/pathexpr.stable.out
        sql/test/json/Tests/pgexample.stable.out
        sql/test/json/Tests/shop.stable.out
        sql/test/json/Tests/spaceissue.stable.out
        sql/test/merge-partitions/Tests/mergepart20.sql
        sql/test/merge-partitions/Tests/mergepart20.stable.out
        sql/test/miscellaneous/Tests/groupby_error.sql
        sql/test/miscellaneous/Tests/groupby_error.stable.err
        sql/test/miscellaneous/Tests/groupby_error.stable.out
        sql/test/miscellaneous/Tests/simple_selects.sql
        sql/test/miscellaneous/Tests/simple_selects.stable.err
        sql/test/miscellaneous/Tests/simple_selects.stable.out
        sql/test/rdf/Tests/All
        sql/test/rdf/Tests/q8_v.stable.err
        sql/test/rdf/Tests/q8_v.stable.out
        sql/test/rdf/Tests/q8_v.timeout
        sql/test/subquery/Tests/correlated.stable.err
        sql/test/subquery/Tests/subquery.sql
        sql/test/subquery/Tests/subquery.stable.err
        sql/test/subquery/Tests/subquery.stable.out
        sql/test/subquery/Tests/subquery3.sql
        sql/test/subquery/Tests/subquery3.stable.err
        sql/test/subquery/Tests/subquery3.stable.out
        sql/test/subquery/Tests/subquery4.sql
        sql/test/subquery/Tests/subquery4.stable.err
        sql/test/subquery/Tests/subquery4.stable.out
        sql/test/sys-schema/Tests/systemfunctions.stable.out
        sql/test/sys-schema/Tests/systemfunctions.stable.out.int128
        sql/test/testdb-upgrade-chain-hge/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade-chain/Tests/upgrade.stable.out
        sql/test/testdb-upgrade-chain/Tests/upgrade.stable.out.32bit
        sql/test/testdb-upgrade-chain/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade-hge/Tests/upgrade.stable.out.int128
        sql/test/testdb-upgrade/Tests/upgrade.stable.out
        sql/test/testdb-upgrade/Tests/upgrade.stable.out.32bit
        sql/test/testdb-upgrade/Tests/upgrade.stable.out.int128
        sql/test/testdb/Tests/dump-nogeom.stable.out
Branch: batcalc-updated
Log Message:

merged with default


diffs (truncated from 6755 to 300 lines):

diff --git a/clients/Tests/MAL-signatures.stable.out 
b/clients/Tests/MAL-signatures.stable.out
--- a/clients/Tests/MAL-signatures.stable.out
+++ b/clients/Tests/MAL-signatures.stable.out
@@ -18922,6 +18922,7 @@ stdout of test 'MAL-signatures` in direc
 [ "sqlcatalog",        "alter_add_value_partition",    "pattern 
sqlcatalog.alter_add_value_partition(sname:str, mtnme:str, psnme:str, 
ptnme:str, nills:int, update:int):void ",        
"SQLalter_add_value_partition;",        "Catalog operation 
alter_add_value_partition"   ]
 [ "sqlcatalog",        "alter_add_value_partition",    "pattern 
sqlcatalog.alter_add_value_partition(sname:str, mtnme:str, psnme:str, 
ptnme:str, nills:int, update:int, arg:any...):void ",    
"SQLalter_add_value_partition;",        "Catalog operation 
alter_add_value_partition"   ]
 [ "sqlcatalog",        "alter_del_table",      "pattern 
sqlcatalog.alter_del_table(sname:str, mtnme:str, psnme:str, ptnme:str, 
action:int):void ",     "SQLalter_del_table;",  "Catalog operation 
alter_del_table"     ]
+[ "sqlcatalog",        "alter_seq",    "pattern 
sqlcatalog.alter_seq(sname:str, seqname:str, seq:ptr, val:bat[:lng]):void ",   
"SQLalter_seq;",        "Catalog operation alter_seq"   ]
 [ "sqlcatalog",        "alter_seq",    "pattern 
sqlcatalog.alter_seq(sname:str, seqname:str, seq:ptr, val:lng):void ", 
"SQLalter_seq;",        "Catalog operation alter_seq"   ]
 [ "sqlcatalog",        "alter_set_table",      "pattern 
sqlcatalog.alter_set_table(sname:str, tnme:str, access:int):void ",    
"SQLalter_set_table;",  "Catalog operation alter_set_table"     ]
 [ "sqlcatalog",        "alter_table",  "pattern 
sqlcatalog.alter_table(sname:str, tname:str, tbl:ptr, action:int):void ",      
"SQLalter_table;",      "Catalog operation alter_table" ]
@@ -19139,6 +19140,7 @@ stdout of test 'MAL-signatures` in direc
 [ "wlc",       "alter_add_value_partition",    "pattern 
wlc.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int):void ",       "WLCgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlc",       "alter_add_value_partition",    "pattern 
wlc.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int, arg:str...):void ",   "WLCgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlc",       "alter_del_table",      "pattern wlc.alter_del_table(sname:str, 
mtnme:str, psnme:str, ptnme:str, action:int):void ",    "WLCgeneric;",  
"Catalog operation alter_del_table"     ]
+[ "wlc",       "alter_seq",    "pattern wlc.alter_seq(sname:str, seqname:str, 
seq:ptr, val:bat[:lng]):void ",  "WLCgeneric;",  "Catalog operation alter_seq"  
 ]
 [ "wlc",       "alter_seq",    "pattern wlc.alter_seq(sname:str, seqname:str, 
val:lng):void ", "WLCgeneric;",  "Catalog operation alter_seq"   ]
 [ "wlc",       "alter_set_table",      "pattern wlc.alter_set_table(sname:str, 
tnme:str, access:int):void ",   "WLCgeneric;",  "Catalog operation 
alter_set_table"     ]
 [ "wlc",       "alter_table",  "pattern wlc.alter_table(sname:str, tname:str, 
action:int):void ",      "WLCgeneric;",  "Catalog operation alter_table" ]
@@ -19208,6 +19210,7 @@ stdout of test 'MAL-signatures` in direc
 [ "wlr",       "alter_add_value_partition",    "pattern 
wlr.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int):void ",       "WLRgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlr",       "alter_add_value_partition",    "pattern 
wlr.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int, arg:str...):void ",   "WLRgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlr",       "alter_del_table",      "pattern wlr.alter_del_table(sname:str, 
mtnme:str, psnme:str, ptnme:str, action:int):void ",    "WLRgeneric;",  
"Catalog operation alter_del_table"     ]
+[ "wlr",       "alter_seq",    "pattern wlr.alter_seq(sname:str, seqname:str, 
seq:ptr, val:bat[:lng]):void ",  "WLRgeneric;",  "Catalog operation alter_seq"  
 ]
 [ "wlr",       "alter_seq",    "pattern wlr.alter_seq(sname:str, seqname:str, 
val:lng):void ", "WLRgeneric;",  "Catalog operation alter_seq"   ]
 [ "wlr",       "alter_set_table",      "pattern wlr.alter_set_table(sname:str, 
tnme:str, access:int):void ",   "WLRgeneric;",  "Catalog operation 
alter_set_table"     ]
 [ "wlr",       "alter_table",  "pattern wlr.alter_table(sname:str, tname:str, 
action:int):void ",      "WLRgeneric;",  "Catalog operation alter_table" ]
diff --git a/clients/Tests/MAL-signatures.stable.out.int128 
b/clients/Tests/MAL-signatures.stable.out.int128
--- a/clients/Tests/MAL-signatures.stable.out.int128
+++ b/clients/Tests/MAL-signatures.stable.out.int128
@@ -26940,6 +26940,7 @@ stdout of test 'MAL-signatures` in direc
 [ "sqlcatalog",        "alter_add_value_partition",    "pattern 
sqlcatalog.alter_add_value_partition(sname:str, mtnme:str, psnme:str, 
ptnme:str, nills:int, update:int):void ",        
"SQLalter_add_value_partition;",        "Catalog operation 
alter_add_value_partition"   ]
 [ "sqlcatalog",        "alter_add_value_partition",    "pattern 
sqlcatalog.alter_add_value_partition(sname:str, mtnme:str, psnme:str, 
ptnme:str, nills:int, update:int, arg:any...):void ",    
"SQLalter_add_value_partition;",        "Catalog operation 
alter_add_value_partition"   ]
 [ "sqlcatalog",        "alter_del_table",      "pattern 
sqlcatalog.alter_del_table(sname:str, mtnme:str, psnme:str, ptnme:str, 
action:int):void ",     "SQLalter_del_table;",  "Catalog operation 
alter_del_table"     ]
+[ "sqlcatalog",        "alter_seq",    "pattern 
sqlcatalog.alter_seq(sname:str, seqname:str, seq:ptr, val:bat[:lng]):void ",   
"SQLalter_seq;",        "Catalog operation alter_seq"   ]
 [ "sqlcatalog",        "alter_seq",    "pattern 
sqlcatalog.alter_seq(sname:str, seqname:str, seq:ptr, val:lng):void ", 
"SQLalter_seq;",        "Catalog operation alter_seq"   ]
 [ "sqlcatalog",        "alter_set_table",      "pattern 
sqlcatalog.alter_set_table(sname:str, tnme:str, access:int):void ",    
"SQLalter_set_table;",  "Catalog operation alter_set_table"     ]
 [ "sqlcatalog",        "alter_table",  "pattern 
sqlcatalog.alter_table(sname:str, tname:str, tbl:ptr, action:int):void ",      
"SQLalter_table;",      "Catalog operation alter_table" ]
@@ -27158,6 +27159,7 @@ stdout of test 'MAL-signatures` in direc
 [ "wlc",       "alter_add_value_partition",    "pattern 
wlc.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int):void ",       "WLCgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlc",       "alter_add_value_partition",    "pattern 
wlc.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int, arg:str...):void ",   "WLCgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlc",       "alter_del_table",      "pattern wlc.alter_del_table(sname:str, 
mtnme:str, psnme:str, ptnme:str, action:int):void ",    "WLCgeneric;",  
"Catalog operation alter_del_table"     ]
+[ "wlc",       "alter_seq",    "pattern wlc.alter_seq(sname:str, seqname:str, 
seq:ptr, val:bat[:lng]):void ",  "WLCgeneric;",  "Catalog operation alter_seq"  
 ]
 [ "wlc",       "alter_seq",    "pattern wlc.alter_seq(sname:str, seqname:str, 
val:lng):void ", "WLCgeneric;",  "Catalog operation alter_seq"   ]
 [ "wlc",       "alter_set_table",      "pattern wlc.alter_set_table(sname:str, 
tnme:str, access:int):void ",   "WLCgeneric;",  "Catalog operation 
alter_set_table"     ]
 [ "wlc",       "alter_table",  "pattern wlc.alter_table(sname:str, tname:str, 
action:int):void ",      "WLCgeneric;",  "Catalog operation alter_table" ]
@@ -27227,6 +27229,7 @@ stdout of test 'MAL-signatures` in direc
 [ "wlr",       "alter_add_value_partition",    "pattern 
wlr.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int):void ",       "WLRgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlr",       "alter_add_value_partition",    "pattern 
wlr.alter_add_value_partition(sname:str, mtnme:str, psnme:str, ptnme:str, 
nills:int, update:int, arg:str...):void ",   "WLRgeneric;",  "Catalog operation 
alter_add_value_partition"   ]
 [ "wlr",       "alter_del_table",      "pattern wlr.alter_del_table(sname:str, 
mtnme:str, psnme:str, ptnme:str, action:int):void ",    "WLRgeneric;",  
"Catalog operation alter_del_table"     ]
+[ "wlr",       "alter_seq",    "pattern wlr.alter_seq(sname:str, seqname:str, 
seq:ptr, val:bat[:lng]):void ",  "WLRgeneric;",  "Catalog operation alter_seq"  
 ]
 [ "wlr",       "alter_seq",    "pattern wlr.alter_seq(sname:str, seqname:str, 
val:lng):void ", "WLRgeneric;",  "Catalog operation alter_seq"   ]
 [ "wlr",       "alter_set_table",      "pattern wlr.alter_set_table(sname:str, 
tnme:str, access:int):void ",   "WLRgeneric;",  "Catalog operation 
alter_set_table"     ]
 [ "wlr",       "alter_table",  "pattern wlr.alter_table(sname:str, tname:str, 
action:int):void ",      "WLRgeneric;",  "Catalog operation alter_table" ]
diff --git a/documentation/source/index.rst b/documentation/source/index.rst
--- a/documentation/source/index.rst
+++ b/documentation/source/index.rst
@@ -11,6 +11,7 @@ Welcome to MonetDB's documentation!
    :caption: Contents:
 
    input
+   json
    clients
    manual_pages
 
diff --git a/documentation/source/json.rst b/documentation/source/json.rst
new file mode 100644
--- /dev/null
+++ b/documentation/source/json.rst
@@ -0,0 +1,27 @@
+***************************
+Support for JSON in MonetDB
+***************************
+
+MonetDB supports columns having the JSON type.
+
+Usage
+=====
+
+The JSON type is implemented as a subtype of ``VARCHAR``, that is guarranteed 
to
+only contain valid JSON strings. For example
+
+.. code:: sql
+
+  CREATE TABLE json_example (c1 JSON NOT NULL);
+  INSERT INTO json_example VALUES ('{"k1": 1, "k2": "foo", "k3": [1,2,3]}');
+
+MonetDB provides an implementation of `JSONpath`_ as the means to interact with
+JSON data.
+
+.. _JSONpath: https://goessner.net/articles/JsonPath/
+
+============================ 
=========================================================
+``json.filter(J, Pathexpr)`` Extracts the component from ``J`` that satisfied 
the Pathexpr
+``json.filter(J, Number)``   Extracts an indexed component from ``J``
+``json.text()``
+============================ 
=========================================================
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -520,7 +520,11 @@ typedef struct {
        size_t free;            /* index where free area starts. */
        size_t size;            /* size of the heap (bytes) */
        char *base;             /* base pointer in memory. */
+#if SIZEOF_VOID_P == 4
        char filename[32];      /* file containing image of the heap */
+#else
+       char filename[40];      /* file containing image of the heap */
+#endif
 
        bte farmid;             /* id of farm where heap is located */
        bool copied:1,          /* a copy of an existing map. */
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -2093,8 +2093,8 @@ BATmode(BAT *b, bool transient)
 
 #ifdef NDEBUG
 /* assertions are disabled, turn failing tests into a message */
-//#undef assert
-//#define assert(test) ((void) ((test) || TRC_ERROR(BAT_, "Assertion `%s' 
failed\n", #test)))
+#undef assert
+#define assert(test)   ((void) ((test) || TRC_CRITICAL_ENDIF(BAT_, "Assertion 
`%s' failed\n", #test)))
 #endif
 
 /* Assert that properties are set correctly.
@@ -2136,6 +2136,7 @@ BATmode(BAT *b, bool transient)
 void
 BATassertProps(BAT *b)
 {
+       unsigned bbpstatus;
        BATiter bi = bat_iterator(b);
        BUN p, q;
        int (*cmpf)(const void *, const void *);
@@ -2151,9 +2152,7 @@ BATassertProps(BAT *b)
        assert(b->hseqbase <= GDK_oid_max); /* non-nil seqbase */
        assert(b->hseqbase + BATcount(b) <= GDK_oid_max);
 
-#ifndef NDEBUG
-       unsigned bbpstatus = BBP_status(b->batCacheid);
-#endif
+       bbpstatus = BBP_status(b->batCacheid);
        /* only at most one of BBPDELETED, BBPEXISTING, BBPNEW may be set */
        assert(((bbpstatus & BBPDELETED) != 0) +
               ((bbpstatus & BBPEXISTING) != 0) +
@@ -2214,9 +2213,7 @@ BATassertProps(BAT *b)
                                assert(b->tvheap->free <= b->tvheap->size);
                                assert(b->tvheap->free % SIZEOF_OID == 0);
                                if (b->tvheap->free > 0) {
-#ifndef NDEBUG
                                        const oid *oids = (const oid *) 
b->tvheap->base;
-#endif
                                        q = b->tvheap->free / SIZEOF_OID;
                                        assert(oids != NULL);
                                        assert(b->tseqbase + BATcount(b) + q <= 
GDK_oid_max);
@@ -2243,9 +2240,7 @@ BATassertProps(BAT *b)
                assert(b->tkey);
                assert(b->tnonil);
                if ((q = b->batCount) != 0) {
-#ifndef NDEBUG
                        const oid *o = (const oid *) Tloc(b, 0);
-#endif
                        assert(*o == b->tseqbase);
                        for (p = 1; p < q; p++)
                                assert(o[p - 1] + 1 == o[p]);
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -214,10 +214,17 @@ MT_thread_init(void)
 {
        if (threadslot == TLS_OUT_OF_INDEXES) {
                threadslot = TlsAlloc();
-               if (threadslot == TLS_OUT_OF_INDEXES)
+               if (threadslot == TLS_OUT_OF_INDEXES) {
+                       char errmsg[256];
+                       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 
GetLastError(), 0, errmsg, sizeof(errmsg), NULL);
+                       TRC_CRITICAL(GDK, "Creating thread-local slot for 
thread failed: %s\n", errmsg);
                        return false;
+               }
                mainthread.tid = GetCurrentThreadId();
                if (TlsSetValue(threadslot, &mainthread) == 0) {
+                       char errmsg[256];
+                       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 
GetLastError(), 0, errmsg, sizeof(errmsg), NULL);
+                       TRC_CRITICAL(GDK, "Setting thread-local value failed: 
%s\n", errmsg);
                        TlsFree(threadslot);
                        threadslot = TLS_OUT_OF_INDEXES;
                        return false;
@@ -386,12 +393,24 @@ join_detached_threads(void)
 int
 MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach 
d, const char *threadname)
 {
-       struct winthread *w = malloc(sizeof(*w));
-
-       if (w == NULL)
-               return -1;
+       struct winthread *w;
 
        join_threads();
+       if (threadname == NULL) {
+               TRC_CRITICAL(GDK, "Thread must have a name\n");
+               return -1;
+       }
+       if (strlen(threadname) >= sizeof(w->threadname)) {
+               TRC_CRITICAL(GDK, "Thread's name is too large\n");
+               return -1;
+       }
+
+       w = malloc(sizeof(*w));
+       if (w == NULL) {
+               TRC_ERROR(GDK, "Cannot allocate memory\n");
+               return -1;
+       }
+
        *w = (struct winthread) {
                .func = f,
                .data = arg,
@@ -405,13 +424,18 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        w->hdl = CreateThread(NULL, THREAD_STACK_SIZE, thread_starter, w,
                              0, &w->tid);
        if (w->hdl == NULL) {
+               char errmsg[256];
+               FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
+                             0, errmsg, sizeof(errmsg), NULL);
                LeaveCriticalSection(&winthread_cs);
+               free(w);
+               TRC_ERROR(GDK, "Failed to create thread: %s\n", errmsg);
                return -1;
        }
+       /* must not fail after this: the thread has been started */
        w->next = winthreads;
        winthreads = w;
        LeaveCriticalSection(&winthread_cs);
-       /* must not fail after this: the thread has been started */
        *t = (MT_Id) w->tid;
        return 0;
 }
@@ -534,12 +558,12 @@ MT_thread_init(void)
        int ret;
 
        if ((ret = pthread_key_create(&threadkey, NULL)) != 0) {
-               TRC_ERROR(GDK, "Creating specific key for thread failed: %s\n", 
strerror(ret));
+               TRC_CRITICAL(GDK, "Creating specific key for thread failed: 
%s\n", strerror(ret));
                return false;
        }
        mainthread.tid = pthread_self();
        if ((ret = pthread_setspecific(threadkey, &mainthread)) != 0) {
-               TRC_ERROR(GDK, "Setting specific value failed: %s\n", 
strerror(ret));
+               TRC_CRITICAL(GDK, "Setting specific value failed: %s\n", 
strerror(ret));
        }
        return true;
 }
@@ -723,16 +747,14 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        pthread_attr_t attr;
        int ret;
        struct posthread *p;
-       size_t tlen;
 
        join_threads();
        if (threadname == NULL) {
-               TRC_ERROR(GDK, "Thread must have a name\n");
+               TRC_CRITICAL(GDK, "Thread must have a name\n");
                return -1;
        }
-       tlen = strlen(threadname);
-       if (tlen >= sizeof(p->threadname)) {
-               TRC_ERROR(GDK, "Thread's name is too large\n");
+       if (strlen(threadname) >= sizeof(p->threadname)) {
+               TRC_CRITICAL(GDK, "Thread's name is too large\n");
                return -1;
        }
        if ((ret = pthread_attr_init(&attr)) != 0) {
@@ -746,7 +768,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        }
        p = malloc(sizeof(struct posthread));
        if (p == NULL) {
-               TRC_ERROR(GDK, "Cannot allocate memory: %s\n", strerror(errno));
+               TRC_ERROR(GDK, "Cannot allocate memory\n");
                pthread_attr_destroy(&attr);
                return -1;
        }
@@ -758,7 +780,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        };
        ATOMIC_INIT(&p->exited, 0);
 
-       memcpy(p->threadname, threadname, tlen + 1);
+       strcpy_len(p->threadname, threadname, sizeof(p->threadname));
 #ifdef HAVE_PTHREAD_SIGMASK
        sigset_t new_mask, orig_mask;
        (void) sigfillset(&new_mask);
@@ -772,6 +794,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        ret = pthread_create(&p->tid, &attr, thread_starter, p);
        if (ret != 0) {
                TRC_ERROR(GDK, "Cannot start thread: %s\n", strerror(ret));
+               free(p);
                ret = -1;
        } else {
                /* must not fail after this: the thread has been started */
@@ -779,6 +802,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
                posthreads = p;
        }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to