Changeset: d6067ec18f34 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=d6067ec18f34
Modified Files:
        monetdb5/mal/mal_dataflow.c
Branch: Feb2013
Log Message:

Add some error checking around thread creation.


diffs (91 lines):

diff --git a/monetdb5/mal/mal_dataflow.c b/monetdb5/mal/mal_dataflow.c
--- a/monetdb5/mal/mal_dataflow.c
+++ b/monetdb5/mal/mal_dataflow.c
@@ -365,24 +365,40 @@ DFLOWworker(void *T)
  * typically is equal to the number of cores
  * The workers are assembled in a local table to enable debugging.
  */
-static void
+static int
 DFLOWinitialize(void)
 {
        int i, limit;
+       int created = 0;
 
        MT_lock_set(&mal_contextLock, "DFLOWinitialize");
        if (todo) {
+               /* somebody else beat us to it */
                MT_lock_unset(&mal_contextLock, "DFLOWinitialize");
-               return;
+               return 0;
        }
        todo = q_create(2048, "DFLOWinitialize");
+       if (todo == NULL) {
+               MT_lock_unset(&mal_contextLock, "DFLOWinitialize");
+               return -1;
+       }
        limit = GDKnr_threads ? GDKnr_threads : 1;
        for (i = 0; i < limit; i++) {
                workers[i].flag = RUNNING;
                if (MT_create_thread(&workers[i].id, DFLOWworker, (void *) 
&workers[i], MT_THR_JOINABLE) < 0)
                        workers[i].flag = IDLE;
+               else
+                       created++;
+       }
+       if (created == 0) {
+               /* no threads created */
+               q_destroy(todo);
+               todo = NULL;
+               MT_lock_unset(&mal_contextLock, "DFLOWinitialize");
+               return -1;
        }
        MT_lock_unset(&mal_contextLock, "DFLOWinitialize");
+       return 0;
 }
 
 /*
@@ -613,6 +629,7 @@ runMALdataflow(Client cntxt, MalBlkPtr m
        if (stk == NULL)
                throw(MAL, "dataflow", "runMALdataflow(): Called with stk == 
NULL");
        ret = (int*) getArgReference(stk,getInstrPtr(mb,startpc),0);
+       *ret = FALSE;
        if (stk->cmd) {
                *ret = TRUE;
                return MAL_SUCCEED;
@@ -622,7 +639,12 @@ runMALdataflow(Client cntxt, MalBlkPtr m
 
        /* check existence of workers */
        if (todo == NULL) {
-               DFLOWinitialize();              /* create the whole pool */
+               /* create thread pool */
+               if (DFLOWinitialize() < 0) {
+                       /* no threads created, run serially */
+                       *ret = TRUE;
+                       return MAL_SUCCEED;
+               }
                i = THREADS;                    /* we didn't create an extra 
thread */
        } else {
                /* create one more worker to compensate for our waiting until
@@ -631,11 +653,21 @@ runMALdataflow(Client cntxt, MalBlkPtr m
                for (i = 0; i < THREADS; i++) {
                        if (workers[i].flag == IDLE) {
                                workers[i].flag = RUNNING;
-                               MT_create_thread(&workers[i].id, DFLOWworker, 
(void *) &workers[i], MT_THR_JOINABLE);
+                               if (MT_create_thread(&workers[i].id, 
DFLOWworker, (void *) &workers[i], MT_THR_JOINABLE) < 0) {
+                                       /* cannot start new thread, run 
serially */
+                                       *ret = TRUE;
+                                       MT_lock_unset(&mal_contextLock, 
"runMALdataflow");
+                                       return MAL_SUCCEED;
+                               }
                                break;
                        }
                }
                MT_lock_unset(&mal_contextLock, "runMALdataflow");
+               if (i == THREADS) {
+                       /* no empty threads slots found, run serially */
+                       *ret = TRUE;
+                       return MAL_SUCCEED;
+               }
        }
        assert(todo);
 
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to