alloc.c sharedspice.c: improve thread safety
still much more to be done!
This commit is contained in:
parent
3f4f0e392c
commit
295c808b01
|
|
@ -2,6 +2,11 @@
|
|||
Copyright 1990 Regents of the University of California. All rights reserved.
|
||||
**********/
|
||||
|
||||
/* for thread handling */
|
||||
#if defined __MINGW32__ || defined _MSC_VER
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Memory alloction functions
|
||||
*/
|
||||
|
|
@ -9,10 +14,32 @@ Copyright 1990 Regents of the University of California. All rights reserved.
|
|||
|
||||
/* We need this because some tests in cmaths and some executables other
|
||||
than ngspice and ngnutmeg under LINUX don't know about controlled_exit */
|
||||
#ifdef HAS_WINGUI
|
||||
#if defined HAS_WINGUI || defined SHARED_MODULE
|
||||
extern void controlled_exit(int status);
|
||||
#endif
|
||||
|
||||
#ifdef SHARED_MODULE
|
||||
#ifndef HAVE_LIBPTHREAD
|
||||
#ifdef SRW
|
||||
#define mutex_lock(a) AcquireSRWLockExclusive(a)
|
||||
#define mutex_unlock(a) ReleaseSRWLockExclusive(a)
|
||||
typedef SRWLOCK mutexType;
|
||||
#else
|
||||
#define mutex_lock(a) EnterCriticalSection(a)
|
||||
#define mutex_unlock(a) LeaveCriticalSection(a)
|
||||
typedef CRITICAL_SECTION mutexType;
|
||||
#endif
|
||||
extern mutexType allocMutex;
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#define mutex_lock(a) pthread_mutex_lock(a)
|
||||
#define mutex_unlock(a) pthread_mutex_unlock(a)
|
||||
typedef pthread_mutex_t mutexType;
|
||||
extern mutexType allocMutex;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef HAVE_LIBGC
|
||||
|
||||
/*saj For Tcl module locking*/
|
||||
|
|
@ -40,15 +67,19 @@ tmalloc(size_t num)
|
|||
/*saj*/
|
||||
#ifdef TCL_MODULE
|
||||
Tcl_MutexLock(alloc);
|
||||
#elif defined SHARED_MODULE
|
||||
mutex_lock(&allocMutex);
|
||||
#endif
|
||||
s = calloc(num,1);
|
||||
/*saj*/
|
||||
#ifdef TCL_MODULE
|
||||
Tcl_MutexUnlock(alloc);
|
||||
#elif defined SHARED_MODULE
|
||||
mutex_unlock(&allocMutex);
|
||||
#endif
|
||||
if (!s){
|
||||
fprintf(stderr,"malloc: Internal Error: can't allocate %ld bytes. \n",(long)num);
|
||||
#ifdef HAS_WINGUI
|
||||
#if defined HAS_WINGUI || defined SHARED_MODULE
|
||||
controlled_exit(EXIT_FAILURE);
|
||||
#else
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -79,16 +110,20 @@ trealloc(void *ptr, size_t num)
|
|||
/*saj*/
|
||||
#ifdef TCL_MODULE
|
||||
Tcl_MutexLock(alloc);
|
||||
#elif defined SHARED_MODULE
|
||||
mutex_lock(&allocMutex);
|
||||
#endif
|
||||
s = realloc(ptr, num);
|
||||
/*saj*/
|
||||
#ifdef TCL_MODULE
|
||||
Tcl_MutexUnlock(alloc);
|
||||
#elif defined SHARED_MODULE
|
||||
mutex_unlock(&allocMutex);
|
||||
#endif
|
||||
}
|
||||
if (!s) {
|
||||
fprintf(stderr,"realloc: Internal Error: can't allocate %ld bytes.\n", (long)num);
|
||||
#ifdef HAS_WINGUI
|
||||
#if defined HAS_WINGUI || defined SHARED_MODULE
|
||||
controlled_exit(EXIT_FAILURE);
|
||||
#else
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
@ -106,12 +141,17 @@ txfree(void *ptr)
|
|||
Tcl_Mutex *alloc;
|
||||
alloc = Tcl_GetAllocMutex();
|
||||
Tcl_MutexLock(alloc);
|
||||
#endif
|
||||
#ifdef SHARED_MODULE
|
||||
mutex_lock(&allocMutex);
|
||||
#endif
|
||||
if (ptr)
|
||||
free(ptr);
|
||||
/*saj*/
|
||||
#ifdef TCL_MODULE
|
||||
Tcl_MutexUnlock(alloc);
|
||||
#elif defined SHARED_MODULE
|
||||
mutex_unlock(&allocMutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
/*******************/
|
||||
/* Defines */
|
||||
/*******************/
|
||||
#define CS
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define SHAREDSPICE_version "25.1"
|
||||
|
|
@ -61,14 +60,14 @@ myfputc(int inp, FILE* f)
|
|||
|
||||
#if defined(__MINGW32__) || defined(_MSC_VER)
|
||||
//#if defined(_MSC_VER)
|
||||
#ifdef CS
|
||||
#define mutex_lock(a) EnterCriticalSection(a)
|
||||
#define mutex_unlock(a) LeaveCriticalSection(a)
|
||||
typedef CRITICAL_SECTION mutexType;
|
||||
#else
|
||||
#ifdef SRW
|
||||
#define mutex_lock(a) AcquireSRWLockExclusive(a)
|
||||
#define mutex_unlock(a) ReleaseSRWLockExclusive(a)
|
||||
typedef SRWLOCK mutexType;
|
||||
#else
|
||||
#define mutex_lock(a) EnterCriticalSection(a)
|
||||
#define mutex_unlock(a) LeaveCriticalSection(a)
|
||||
typedef CRITICAL_SECTION mutexType;
|
||||
#endif
|
||||
#define thread_self() GetCurrentThread()
|
||||
#define threadid_self() GetThreadId(GetCurrentThread())
|
||||
|
|
@ -313,7 +312,7 @@ _thread_stop(void)
|
|||
ft_intrpt = TRUE;
|
||||
timeout++;
|
||||
#if defined(__MINGW32__) || defined(_MSC_VER)
|
||||
Sleep(100); // va: windows native
|
||||
Sleep(50); // va: windows native
|
||||
#else
|
||||
usleep(10000);
|
||||
#endif
|
||||
|
|
@ -479,14 +478,14 @@ ngSpice_Init(SendChar* printfcn, SendStat* statusfcn, ControlledExit* ngspiceexi
|
|||
pthread_mutex_init(&allocMutex, NULL);
|
||||
pthread_mutex_init(&fputsMutex, NULL);
|
||||
#else
|
||||
#ifdef CS
|
||||
InitializeCriticalSection(&triggerMutex);
|
||||
InitializeCriticalSection(&allocMutex);
|
||||
InitializeCriticalSection(&fputsMutex);
|
||||
#else
|
||||
#ifdef SRW
|
||||
InitializeSRWLock(&triggerMutex);
|
||||
InitializeSRWLock(&allocMutex);
|
||||
InitializeSRWLock(&fputsMutex);
|
||||
#else
|
||||
InitializeCriticalSection(&triggerMutex);
|
||||
InitializeCriticalSection(&allocMutex);
|
||||
InitializeCriticalSection(&fputsMutex);
|
||||
#endif
|
||||
#endif
|
||||
// Id of primary thread
|
||||
|
|
@ -1384,6 +1383,7 @@ int sh_ExecutePerLoop(void)
|
|||
{
|
||||
struct dvec *d;
|
||||
int i, veclen;
|
||||
double testval;
|
||||
struct plot *pl = plot_cur;
|
||||
/* return immediately if callback not wanted */
|
||||
if (nodatawanted)
|
||||
|
|
@ -1395,6 +1395,7 @@ int sh_ExecutePerLoop(void)
|
|||
/* test if real */
|
||||
if (d->v_flags & VF_REAL) {
|
||||
curvecvalsall->vecsa[i]->is_complex = FALSE;
|
||||
testval = d->v_realdata[veclen];
|
||||
curvecvalsall->vecsa[i]->creal = d->v_realdata[veclen];
|
||||
curvecvalsall->vecsa[i]->cimag = 0.;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue