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.
|
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
|
* 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
|
/* We need this because some tests in cmaths and some executables other
|
||||||
than ngspice and ngnutmeg under LINUX don't know about controlled_exit */
|
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);
|
extern void controlled_exit(int status);
|
||||||
#endif
|
#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
|
#ifndef HAVE_LIBGC
|
||||||
|
|
||||||
/*saj For Tcl module locking*/
|
/*saj For Tcl module locking*/
|
||||||
|
|
@ -40,15 +67,19 @@ tmalloc(size_t num)
|
||||||
/*saj*/
|
/*saj*/
|
||||||
#ifdef TCL_MODULE
|
#ifdef TCL_MODULE
|
||||||
Tcl_MutexLock(alloc);
|
Tcl_MutexLock(alloc);
|
||||||
|
#elif defined SHARED_MODULE
|
||||||
|
mutex_lock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
s = calloc(num,1);
|
s = calloc(num,1);
|
||||||
/*saj*/
|
/*saj*/
|
||||||
#ifdef TCL_MODULE
|
#ifdef TCL_MODULE
|
||||||
Tcl_MutexUnlock(alloc);
|
Tcl_MutexUnlock(alloc);
|
||||||
|
#elif defined SHARED_MODULE
|
||||||
|
mutex_unlock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
if (!s){
|
if (!s){
|
||||||
fprintf(stderr,"malloc: Internal Error: can't allocate %ld bytes. \n",(long)num);
|
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);
|
controlled_exit(EXIT_FAILURE);
|
||||||
#else
|
#else
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
@ -79,16 +110,20 @@ trealloc(void *ptr, size_t num)
|
||||||
/*saj*/
|
/*saj*/
|
||||||
#ifdef TCL_MODULE
|
#ifdef TCL_MODULE
|
||||||
Tcl_MutexLock(alloc);
|
Tcl_MutexLock(alloc);
|
||||||
|
#elif defined SHARED_MODULE
|
||||||
|
mutex_lock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
s = realloc(ptr, num);
|
s = realloc(ptr, num);
|
||||||
/*saj*/
|
/*saj*/
|
||||||
#ifdef TCL_MODULE
|
#ifdef TCL_MODULE
|
||||||
Tcl_MutexUnlock(alloc);
|
Tcl_MutexUnlock(alloc);
|
||||||
|
#elif defined SHARED_MODULE
|
||||||
|
mutex_unlock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if (!s) {
|
if (!s) {
|
||||||
fprintf(stderr,"realloc: Internal Error: can't allocate %ld bytes.\n", (long)num);
|
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);
|
controlled_exit(EXIT_FAILURE);
|
||||||
#else
|
#else
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
@ -106,12 +141,17 @@ txfree(void *ptr)
|
||||||
Tcl_Mutex *alloc;
|
Tcl_Mutex *alloc;
|
||||||
alloc = Tcl_GetAllocMutex();
|
alloc = Tcl_GetAllocMutex();
|
||||||
Tcl_MutexLock(alloc);
|
Tcl_MutexLock(alloc);
|
||||||
|
#endif
|
||||||
|
#ifdef SHARED_MODULE
|
||||||
|
mutex_lock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
if (ptr)
|
if (ptr)
|
||||||
free(ptr);
|
free(ptr);
|
||||||
/*saj*/
|
/*saj*/
|
||||||
#ifdef TCL_MODULE
|
#ifdef TCL_MODULE
|
||||||
Tcl_MutexUnlock(alloc);
|
Tcl_MutexUnlock(alloc);
|
||||||
|
#elif defined SHARED_MODULE
|
||||||
|
mutex_unlock(&allocMutex);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
/*******************/
|
/*******************/
|
||||||
/* Defines */
|
/* Defines */
|
||||||
/*******************/
|
/*******************/
|
||||||
#define CS
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#define SHAREDSPICE_version "25.1"
|
#define SHAREDSPICE_version "25.1"
|
||||||
|
|
@ -61,14 +60,14 @@ myfputc(int inp, FILE* f)
|
||||||
|
|
||||||
#if defined(__MINGW32__) || defined(_MSC_VER)
|
#if defined(__MINGW32__) || defined(_MSC_VER)
|
||||||
//#if defined(_MSC_VER)
|
//#if defined(_MSC_VER)
|
||||||
#ifdef CS
|
#ifdef SRW
|
||||||
#define mutex_lock(a) EnterCriticalSection(a)
|
|
||||||
#define mutex_unlock(a) LeaveCriticalSection(a)
|
|
||||||
typedef CRITICAL_SECTION mutexType;
|
|
||||||
#else
|
|
||||||
#define mutex_lock(a) AcquireSRWLockExclusive(a)
|
#define mutex_lock(a) AcquireSRWLockExclusive(a)
|
||||||
#define mutex_unlock(a) ReleaseSRWLockExclusive(a)
|
#define mutex_unlock(a) ReleaseSRWLockExclusive(a)
|
||||||
typedef SRWLOCK mutexType;
|
typedef SRWLOCK mutexType;
|
||||||
|
#else
|
||||||
|
#define mutex_lock(a) EnterCriticalSection(a)
|
||||||
|
#define mutex_unlock(a) LeaveCriticalSection(a)
|
||||||
|
typedef CRITICAL_SECTION mutexType;
|
||||||
#endif
|
#endif
|
||||||
#define thread_self() GetCurrentThread()
|
#define thread_self() GetCurrentThread()
|
||||||
#define threadid_self() GetThreadId(GetCurrentThread())
|
#define threadid_self() GetThreadId(GetCurrentThread())
|
||||||
|
|
@ -313,7 +312,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(100); // va: windows native
|
Sleep(50); // va: windows native
|
||||||
#else
|
#else
|
||||||
usleep(10000);
|
usleep(10000);
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -479,14 +478,14 @@ ngSpice_Init(SendChar* printfcn, SendStat* statusfcn, ControlledExit* ngspiceexi
|
||||||
pthread_mutex_init(&allocMutex, NULL);
|
pthread_mutex_init(&allocMutex, NULL);
|
||||||
pthread_mutex_init(&fputsMutex, NULL);
|
pthread_mutex_init(&fputsMutex, NULL);
|
||||||
#else
|
#else
|
||||||
#ifdef CS
|
#ifdef SRW
|
||||||
InitializeCriticalSection(&triggerMutex);
|
|
||||||
InitializeCriticalSection(&allocMutex);
|
|
||||||
InitializeCriticalSection(&fputsMutex);
|
|
||||||
#else
|
|
||||||
InitializeSRWLock(&triggerMutex);
|
InitializeSRWLock(&triggerMutex);
|
||||||
InitializeSRWLock(&allocMutex);
|
InitializeSRWLock(&allocMutex);
|
||||||
InitializeSRWLock(&fputsMutex);
|
InitializeSRWLock(&fputsMutex);
|
||||||
|
#else
|
||||||
|
InitializeCriticalSection(&triggerMutex);
|
||||||
|
InitializeCriticalSection(&allocMutex);
|
||||||
|
InitializeCriticalSection(&fputsMutex);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
// Id of primary thread
|
// Id of primary thread
|
||||||
|
|
@ -1384,6 +1383,7 @@ int sh_ExecutePerLoop(void)
|
||||||
{
|
{
|
||||||
struct dvec *d;
|
struct dvec *d;
|
||||||
int i, veclen;
|
int i, veclen;
|
||||||
|
double testval;
|
||||||
struct plot *pl = plot_cur;
|
struct plot *pl = plot_cur;
|
||||||
/* return immediately if callback not wanted */
|
/* return immediately if callback not wanted */
|
||||||
if (nodatawanted)
|
if (nodatawanted)
|
||||||
|
|
@ -1395,6 +1395,7 @@ int sh_ExecutePerLoop(void)
|
||||||
/* test if real */
|
/* test if real */
|
||||||
if (d->v_flags & VF_REAL) {
|
if (d->v_flags & VF_REAL) {
|
||||||
curvecvalsall->vecsa[i]->is_complex = FALSE;
|
curvecvalsall->vecsa[i]->is_complex = FALSE;
|
||||||
|
testval = d->v_realdata[veclen];
|
||||||
curvecvalsall->vecsa[i]->creal = d->v_realdata[veclen];
|
curvecvalsall->vecsa[i]->creal = d->v_realdata[veclen];
|
||||||
curvecvalsall->vecsa[i]->cimag = 0.;
|
curvecvalsall->vecsa[i]->cimag = 0.;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue