alloc.c sharedspice.c: improve thread safety

(still much more to be done!)
This commit is contained in:
h_vogt 2013-03-24 12:54:11 +01:00
parent d3bf4e8b44
commit 1509cfd9ee
2 changed files with 55 additions and 17 deletions

View File

@ -1,6 +1,10 @@
/**********
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 +13,31 @@ 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 +65,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 +108,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);
@ -97,7 +130,6 @@ trealloc(void *ptr, size_t num)
return(s);
}
void
txfree(void *ptr)
{
@ -106,12 +138,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
}

View File

@ -8,7 +8,6 @@
/*******************/
/* Defines */
/*******************/
#define CS
#ifdef _MSC_VER
#define SHAREDSPICE_version "25.1"
@ -61,14 +60,14 @@ myfputc(const char 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())
@ -336,7 +335,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
@ -496,20 +495,20 @@ ngSpice_Init(SendChar* printfcn, SendStat* statusfcn, ControlledExit* ngspiceexi
immediate = FALSE;
#ifdef THREADS
/* init the mutex */
/* init the mutexes */
#ifdef HAVE_LIBPTHREAD
pthread_mutex_init(&triggerMutex, NULL);
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
@ -1409,6 +1408,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)
@ -1420,6 +1420,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.;
}