Browse Source

fixing multithreaded version

Stasik0 9 years ago
parent
commit
b4fc36bc88
3 changed files with 11 additions and 10 deletions
  1. 2 2
      include/ua_server.h
  2. 1 0
      src/server/ua_server_internal.h
  3. 8 8
      src/server/ua_server_worker.c

+ 2 - 2
include/ua_server.h

@@ -76,11 +76,11 @@ UA_StatusCode UA_EXPORT UA_Server_run(UA_Server *server, UA_UInt16 nThreads, UA_
 /**
  * The prologue part of UA_Server_run (no need to use if you call UA_Server_run)
  */
-UA_StatusCode UA_EXPORT UA_Server_run_startup(UA_Server *server, UA_UInt16 nThreads);
+UA_StatusCode UA_EXPORT UA_Server_run_startup(UA_Server *server, UA_UInt16 nThreads, UA_Boolean *running);
 /**
  * The epilogue part of UA_Server_run (no need to use if you call UA_Server_run)
  */
-UA_StatusCode UA_EXPORT UA_Server_run_shutdown(UA_Server *server);
+UA_StatusCode UA_EXPORT UA_Server_run_shutdown(UA_Server *server, UA_UInt16 nThreads);
 /**
  * One iteration of UA_Server_run (no need to use if you call UA_Server_run)
  */

+ 1 - 0
src/server/ua_server_internal.h

@@ -60,6 +60,7 @@ struct UA_Server {
     UA_Boolean *running;
     UA_UInt16 nThreads;
     UA_UInt32 **workerCounters;
+    pthread_t *thr;
     struct DelayedWork *delayedWork;
 
     // worker threads wait on the queue

+ 8 - 8
src/server/ua_server_worker.c

@@ -436,19 +436,19 @@ static void dispatchDelayedWork(UA_Server *server, void *data /* not used, but n
 /* Main Server Loop */
 /********************/
 
-UA_StatusCode UA_Server_run_startup(UA_Server *server, UA_UInt16 nThreads){
+UA_StatusCode UA_Server_run_startup(UA_Server *server, UA_UInt16 nThreads, UA_Boolean *running){
 #ifdef UA_MULTITHREADING
     // 1) Prepare the threads
     server->running = running; // the threads need to access the variable
     server->nThreads = nThreads;
     pthread_cond_init(&server->dispatchQueue_condition, 0);
-    pthread_t *thr = UA_malloc(nThreads * sizeof(pthread_t));
+    server->thr = UA_malloc(nThreads * sizeof(pthread_t));
     server->workerCounters = UA_malloc(nThreads * sizeof(UA_UInt32 *));
     for(UA_UInt32 i=0;i<nThreads;i++) {
         struct workerStartData *startData = UA_malloc(sizeof(struct workerStartData));
         startData->server = server;
         startData->workerCounter = &server->workerCounters[i];
-        pthread_create(&thr[i], UA_NULL, (void* (*)(void*))workerLoop, startData);
+        pthread_create(&server->thr[i], UA_NULL, (void* (*)(void*))workerLoop, startData);
     }
 
     UA_WorkItem processDelayed = {.type = UA_WORKITEMTYPE_METHODCALL,
@@ -502,16 +502,16 @@ if(workSize > 0)
     return UA_STATUSCODE_GOOD;
 }
 
-UA_StatusCode UA_Server_run_shutdown(UA_Server *server){
+UA_StatusCode UA_Server_run_shutdown(UA_Server *server, UA_UInt16 nThreads){
 #ifdef UA_MULTITHREADING
     // 4) Clean up: Wait until all worker threads finish, then empty the
     // dispatch queue, then process the remaining delayed work
     for(UA_UInt32 i=0;i<nThreads;i++) {
-        pthread_join(thr[i], UA_NULL);
+        pthread_join(server->thr[i], UA_NULL);
         UA_free(server->workerCounters[i]);
     }
     UA_free(server->workerCounters);
-    UA_free(thr);
+    UA_free(server->thr);
     emptyDispatchQueue(server);
     processDelayedWork(server);
 #endif
@@ -520,7 +520,7 @@ UA_StatusCode UA_Server_run_shutdown(UA_Server *server){
 }
 
 UA_StatusCode UA_Server_run(UA_Server *server, UA_UInt16 nThreads, UA_Boolean *running) {
-    UA_Server_run_startup(server, nThreads);
+    UA_Server_run_startup(server, nThreads, running);
 
     // 3) The loop
     while(1) {
@@ -531,7 +531,7 @@ UA_StatusCode UA_Server_run(UA_Server *server, UA_UInt16 nThreads, UA_Boolean *r
             break;
     }
 
-    UA_Server_run_shutdown(server);
+    UA_Server_run_shutdown(server, nThreads);
 
     return UA_STATUSCODE_GOOD;
 }