Browse Source

sharedspice.c: thread handling updated

pre-master-46
h_vogt 13 years ago
committed by rlar
parent
commit
f95f7c661d
  1. 51
      src/sharedspice.c

51
src/sharedspice.c

@ -16,6 +16,8 @@
#define STDERR_FILENO 2 #define STDERR_FILENO 2
#endif #endif
//#define low_latency
/**********************************************************************/ /**********************************************************************/
/* Header files for C functions */ /* Header files for C functions */
/**********************************************************************/ /**********************************************************************/
@ -198,7 +200,7 @@ void wl_delete_first(wordlist **wlstart, wordlist **wlend);
#if !defined(low_latency) #if !defined(low_latency)
static char* outstorage(char*, bool); static char* outstorage(char*, bool);
static void* printsend(void);
static void printsend(void);
#endif #endif
#include "ngspice/sharedspice.h" #include "ngspice/sharedspice.h"
@ -312,7 +314,7 @@ _thread_stop(void)
ft_intrpt = TRUE; ft_intrpt = TRUE;
timeout++; timeout++;
#if defined(__MINGW32__) || defined(_MSC_VER) #if defined(__MINGW32__) || defined(_MSC_VER)
Sleep(50); // va: windows native
Sleep(100); // va: windows native
#else #else
usleep(10000); usleep(10000);
#endif #endif
@ -937,6 +939,8 @@ static char* outstringout = NULL;
#ifdef low_latency #ifdef low_latency
/* using the strings by the caller sent directly to the caller
has to fast enough (low latency) */
int int
sh_fputsll(const char *input, FILE* outf) sh_fputsll(const char *input, FILE* outf)
{ {
@ -966,10 +970,7 @@ sh_fputsll(const char *input, FILE* outf)
strcat(prstring, "stderr "); strcat(prstring, "stderr ");
strcat(prstring, newstring); strcat(prstring, newstring);
printtid = (HANDLE)_beginthreadex(NULL, 0, (unsigned int (__stdcall *)(void *))printing,
(void*)prstring, 0, NULL);
// result = pfcn(prstring, userptr);
result = pfcn(prstring, userptr);
tfree(newstring); tfree(newstring);
tfree(prstring); tfree(prstring);
} }
@ -1046,6 +1047,7 @@ sh_fputs(const char *input, FILE* outf)
again fcn outstoraghe(), top entry is deleted and string is again fcn outstoraghe(), top entry is deleted and string is
sent to caller in an endless loop by fcn printsend() */ sent to caller in an endless loop by fcn printsend() */
static wordlist *wlstart = NULL, *wlend = NULL; static wordlist *wlstart = NULL, *wlend = NULL;
static bool printstopp = FALSE;
int int
@ -1142,18 +1144,26 @@ sh_fputs(const char *input, FILE* outf)
return 0; return 0;
} }
static char *outsend = NULL;
/* Endless loop in its own thread for reading data from FIFO and sending to caller */ /* Endless loop in its own thread for reading data from FIFO and sending to caller */
static void *
static void
printsend(void) printsend(void)
{ {
char *outsend = NULL;
for (;;) { for (;;) {
#if defined(__MINGW32__) || defined(_MSC_VER) #if defined(__MINGW32__) || defined(_MSC_VER)
Sleep(100);
Sleep(50); // loop delay
#else #else
usleep(100000);
usleep(50000);
#endif #endif
if (printstopp) { // issued by shared_exit()
// catch the final error message
mutex_lock(&fputsMutex);
outsend = outstorage(NULL, FALSE);
mutex_unlock(&fputsMutex);
break;
}
mutex_lock(&fputsMutex); mutex_lock(&fputsMutex);
outsend = outstorage(NULL, FALSE); outsend = outstorage(NULL, FALSE);
mutex_unlock(&fputsMutex); mutex_unlock(&fputsMutex);
@ -1301,7 +1311,26 @@ void shared_exit(int status)
coquit = FALSE; coquit = FALSE;
fprintf(stderr, "Error: ngspice.dll cannot recover and awaits to be detached\n"); fprintf(stderr, "Error: ngspice.dll cannot recover and awaits to be detached\n");
} }
#ifndef low_latency
// set flag to stop the printsend thread
printstopp = TRUE;
// leave this thread for 100ms to stop the printsend thread
#if defined __MINGW32__ || defined _MSC_VER
Sleep(100);
#else
usleep(100000);
#endif
// send the final error message already caught in printsend()
if (outsend) {
/* requires outsend to be copied by the caller,
because it is freed immediately */
pfcn(outsend, userptr);
tfree(outsend);
}
#endif
// set a flag in caller to detach ngspice.dll
ngexit(status, immediate, coquit, userptr); ngexit(status, immediate, coquit, userptr);
// jump back to finish the calling function
if (!intermj) if (!intermj)
longjmp(errbufm,1); /* jump back to ngSpice_Circ() */ longjmp(errbufm,1); /* jump back to ngSpice_Circ() */
else else

Loading…
Cancel
Save