Commit 3c61f362 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

zaxis.c: mt safe version

parent 13cfaf91
2006-12-?? Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* zaxis.c: mt safe version
* Version 1.0.5 released
2006-11-30 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de> 2006-11-30 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* using GRIB library version 1.0.4 * using GRIB library version 1.0.4
......
#! /bin/sh #! /bin/sh
# Guess values for system-dependent variables and create Makefiles. # Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.59 for cdi 1.0.4. # Generated by GNU Autoconf 2.59 for cdi 1.0.5.
# #
# Report bugs to <Uwe.Schulzweida@zmaw.de>. # Report bugs to <Uwe.Schulzweida@zmaw.de>.
# #
...@@ -269,8 +269,8 @@ SHELL=${CONFIG_SHELL-/bin/sh} ...@@ -269,8 +269,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package. # Identity of this package.
PACKAGE_NAME='cdi' PACKAGE_NAME='cdi'
PACKAGE_TARNAME='cdi' PACKAGE_TARNAME='cdi'
PACKAGE_VERSION='1.0.4' PACKAGE_VERSION='1.0.5'
PACKAGE_STRING='cdi 1.0.4' PACKAGE_STRING='cdi 1.0.5'
PACKAGE_BUGREPORT='Uwe.Schulzweida@zmaw.de' PACKAGE_BUGREPORT='Uwe.Schulzweida@zmaw.de'
# Factoring default headers for most tests. # Factoring default headers for most tests.
...@@ -795,7 +795,7 @@ if test "$ac_init_help" = "long"; then ...@@ -795,7 +795,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing. # Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh. # This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF cat <<_ACEOF
\`configure' configures cdi 1.0.4 to adapt to many kinds of systems. \`configure' configures cdi 1.0.5 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]... Usage: $0 [OPTION]... [VAR=VALUE]...
...@@ -862,7 +862,7 @@ fi ...@@ -862,7 +862,7 @@ fi
if test -n "$ac_init_help"; then if test -n "$ac_init_help"; then
case $ac_init_help in case $ac_init_help in
short | recursive ) echo "Configuration of cdi 1.0.4:";; short | recursive ) echo "Configuration of cdi 1.0.5:";;
esac esac
cat <<\_ACEOF cat <<\_ACEOF
...@@ -1006,7 +1006,7 @@ fi ...@@ -1006,7 +1006,7 @@ fi
test -n "$ac_init_help" && exit 0 test -n "$ac_init_help" && exit 0
if $ac_init_version; then if $ac_init_version; then
cat <<\_ACEOF cat <<\_ACEOF
cdi configure 1.0.4 cdi configure 1.0.5
generated by GNU Autoconf 2.59 generated by GNU Autoconf 2.59
Copyright (C) 2003 Free Software Foundation, Inc. Copyright (C) 2003 Free Software Foundation, Inc.
...@@ -1020,7 +1020,7 @@ cat >&5 <<_ACEOF ...@@ -1020,7 +1020,7 @@ cat >&5 <<_ACEOF
This file contains any messages produced by compilers while This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake. running configure, to aid debugging if configure makes a mistake.
It was created by cdi $as_me 1.0.4, which was It was created by cdi $as_me 1.0.5, which was
generated by GNU Autoconf 2.59. Invocation command line was generated by GNU Autoconf 2.59. Invocation command line was
$ $0 $@ $ $0 $@
...@@ -1707,7 +1707,7 @@ fi ...@@ -1707,7 +1707,7 @@ fi
# Define the identity of the package. # Define the identity of the package.
PACKAGE=cdi PACKAGE=cdi
VERSION=1.0.4 VERSION=1.0.5
cat >>confdefs.h <<_ACEOF cat >>confdefs.h <<_ACEOF
...@@ -6400,7 +6400,7 @@ _ASBOX ...@@ -6400,7 +6400,7 @@ _ASBOX
} >&5 } >&5
cat >&5 <<_CSEOF cat >&5 <<_CSEOF
This file was extended by cdi $as_me 1.0.4, which was This file was extended by cdi $as_me 1.0.5, which was
generated by GNU Autoconf 2.59. Invocation command line was generated by GNU Autoconf 2.59. Invocation command line was
CONFIG_FILES = $CONFIG_FILES CONFIG_FILES = $CONFIG_FILES
...@@ -6463,7 +6463,7 @@ _ACEOF ...@@ -6463,7 +6463,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF cat >>$CONFIG_STATUS <<_ACEOF
ac_cs_version="\\ ac_cs_version="\\
cdi config.status 1.0.4 cdi config.status 1.0.5
configured by $0, generated by GNU Autoconf 2.59, configured by $0, generated by GNU Autoconf 2.59,
with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\" with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\"
......
# Process this file with autoconf to produce a configure script. # Process this file with autoconf to produce a configure script.
AC_INIT(cdi, 1.0.4, Uwe.Schulzweida@zmaw.de) AC_INIT(cdi, 1.0.5, Uwe.Schulzweida@zmaw.de)
CONFIG_ABORT=yes CONFIG_ABORT=yes
......
...@@ -231,7 +231,7 @@ static void grid_delete_entry(GRID *gridptr) ...@@ -231,7 +231,7 @@ static void grid_delete_entry(GRID *gridptr)
_gridList[idx].next = _gridAvail; _gridList[idx].next = _gridAvail;
_gridList[idx].ptr = 0; _gridList[idx].ptr = 0;
_gridAvail = &_gridList[idx]; _gridAvail = &_gridList[idx];
GRID_UNLOCK GRID_UNLOCK
......
...@@ -46,6 +46,7 @@ typedef struct { ...@@ -46,6 +46,7 @@ typedef struct {
double *lbounds; double *lbounds;
double *ubounds; double *ubounds;
double *weights; double *weights;
int self;
int prec; int prec;
int type; int type;
int size; int size;
...@@ -56,94 +57,243 @@ typedef struct { ...@@ -56,94 +57,243 @@ typedef struct {
} }
ZAXIS; ZAXIS;
static ZAXIS *zaxisTable; static int ZAXIS_Debug = 0; /* If set to 1, debugging */
static int zaxisTableSize = 0;
static int _zaxis_max = 1024;
static void zaxisTableInitEntry (int zaxisID) static void zaxis_initialize(void);
static int _zaxis_init = FALSE;
#if defined (HAVE_LIBPTHREAD)
# include <pthread.h>
static pthread_once_t _zaxis_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _zaxis_mutex;
# define ZAXIS_LOCK pthread_mutex_lock(&_zaxis_mutex);
# define ZAXIS_UNLOCK pthread_mutex_unlock(&_zaxis_mutex);
# define ZAXIS_INIT \
if ( _zaxis_init == FALSE ) pthread_once(&_zaxis_init_thread, zaxis_initialize);
#else
# define ZAXIS_LOCK
# define ZAXIS_UNLOCK
# define ZAXIS_INIT \
if ( _zaxis_init == FALSE ) zaxis_initialize();
#endif
typedef struct _zaxisPtrToIdx {
int idx;
ZAXIS *ptr;
struct _zaxisPtrToIdx *next;
} zaxisPtrToIdx;
static zaxisPtrToIdx *_zaxisList;
static zaxisPtrToIdx *_zaxisAvail = 0;
static void zaxis_list_new(void)
{
static char func[] = "zaxis_list_new";
_zaxisList = (zaxisPtrToIdx *) malloc(_zaxis_max*sizeof(zaxisPtrToIdx));
}
static void zaxis_list_delete(void)
{ {
zaxisTable[zaxisID].name[0] = 0; static char func[] = "zaxis_list_delete";
zaxisTable[zaxisID].longname[0] = 0;
zaxisTable[zaxisID].units[0] = 0; if ( _zaxisList ) free(_zaxisList);
zaxisTable[zaxisID].vals = NULL;
zaxisTable[zaxisID].ubounds = NULL;
zaxisTable[zaxisID].lbounds = NULL;
zaxisTable[zaxisID].weights = NULL;
zaxisTable[zaxisID].type = CDI_UNDEFID;
zaxisTable[zaxisID].direction = CDI_UNDEFID;
zaxisTable[zaxisID].prec = 0;
zaxisTable[zaxisID].size = 0;
zaxisTable[zaxisID].vctsize = 0;
zaxisTable[zaxisID].vct = NULL;
} }
static int zaxisTableNewEntry (int zaxistype) static void zaxis_init_pointer(void)
{ {
static char func[] = "zaxisTableNewEntry"; int i;
int zaxisID = 0;
for ( i = 0; i < _zaxis_max; i++ )
/*
Look for a free slot in zaxisTable.
(Create the table the first time through).
*/
if ( ! zaxisTableSize )
{ {
int i; _zaxisList[i].next = _zaxisList + i + 1;
_zaxisList[i].idx = i;
_zaxisList[i].ptr = 0;
}
zaxisTableSize = 2; _zaxisList[_zaxis_max-1].next = 0;
zaxisTable = (ZAXIS *) malloc(zaxisTableSize*sizeof(ZAXIS));
if ( zaxisTable == NULL ) _zaxisAvail = _zaxisList;
{ }
Message(func, "zaxisTableSize = %d", zaxisTableSize);
SysError(func, "Allocation of ZAXISTABLE failed");
}
for ( i = 0; i < zaxisTableSize; i++ )
zaxisTableInitEntry(i); ZAXIS *zaxis_to_pointer(int idx)
{
static char func[] = "zaxis_to_pointer";
ZAXIS *zaxisptr = NULL;
ZAXIS_INIT
if ( idx >= 0 && idx < _zaxis_max )
{
ZAXIS_LOCK
zaxisptr = _zaxisList[idx].ptr;
ZAXIS_UNLOCK
} }
else else
Error(func, "zaxis index %d undefined!", idx);
return (zaxisptr);
}
/* Create an index from a pointer */
static int zaxis_from_pointer(ZAXIS *ptr)
{
static char func[] = "zaxis_from_pointer";
int idx = -1;
zaxisPtrToIdx *newptr;
if ( ptr )
{ {
while ( zaxisID < zaxisTableSize ) ZAXIS_LOCK
if ( _zaxisAvail )
{ {
if ( zaxisTable[zaxisID].type == CDI_UNDEFID ) break; newptr = _zaxisAvail;
zaxisID++; _zaxisAvail = _zaxisAvail->next;
newptr->next = 0;
idx = newptr->idx;
newptr->ptr = ptr;
if ( ZAXIS_Debug )
Message(func, "Pointer %p has idx %d from zaxis list", ptr, idx);
} }
else
Warning(func, "Too many open zaxiss (limit is %d)!", _zaxis_max);
ZAXIS_UNLOCK
} }
/* else
If the table overflows, double its size. Error(func, "Internal problem (pointer %p undefined)", ptr);
*/
if ( zaxisID == zaxisTableSize )
{
int i;
zaxisTableSize = 2*zaxisTableSize; return (idx);
zaxisTable = (ZAXIS *) realloc(zaxisTable, zaxisTableSize*sizeof(ZAXIS)); }
if ( zaxisTable == NULL )
{
Message(func, "zaxisTableSize = %d", zaxisTableSize);
SysError(func, "Reallocation of ZAXISTABLE failed");
}
zaxisID = zaxisTableSize/2;
for ( i = zaxisID; i < zaxisTableSize; i++ ) static void zaxis_init_entry(ZAXIS *zaxisptr)
zaxisTableInitEntry(i); {
} zaxisptr->self = zaxis_from_pointer(zaxisptr);
zaxisptr->name[0] = 0;
zaxisptr->longname[0] = 0;
zaxisptr->units[0] = 0;
zaxisptr->vals = NULL;
zaxisptr->ubounds = NULL;
zaxisptr->lbounds = NULL;
zaxisptr->weights = NULL;
zaxisptr->type = CDI_UNDEFID;
zaxisptr->direction = CDI_UNDEFID;
zaxisptr->prec = 0;
zaxisptr->size = 0;
zaxisptr->vctsize = 0;
zaxisptr->vct = NULL;
}
zaxisTable[zaxisID].type = zaxistype;
return (zaxisID); static ZAXIS *zaxis_new_entry(void)
{
static char func[] = "zaxis_new_entry";
ZAXIS *zaxisptr;
zaxisptr = (ZAXIS *) malloc(sizeof(ZAXIS));
if ( zaxisptr ) zaxis_init_entry(zaxisptr);
return (zaxisptr);
}
static void zaxis_delete_entry(ZAXIS *zaxisptr)
{
static char func[] = "zaxis_delete_entry";
int idx;
idx = zaxisptr->self;
ZAXIS_LOCK
free(zaxisptr);
_zaxisList[idx].next = _zaxisAvail;
_zaxisList[idx].ptr = 0;
_zaxisAvail = &_zaxisList[idx];
ZAXIS_UNLOCK
if ( ZAXIS_Debug )
Message(func, "Removed idx %d from zaxis list", idx);
} }
void zaxisCheckID(const char *func, int zaxisID) static void zaxis_initialize(void)
{ {
if ( zaxisID < 0 || zaxisID >= zaxisTableSize ) char *env;
Error(func, "zaxisID %d undefined!", zaxisID);
#if defined (HAVE_LIBPTHREAD)
/* initialize global API mutex lock */
pthread_mutex_init(&_zaxis_mutex, NULL);
#endif
if ( zaxisTable[zaxisID].type == CDI_UNDEFID ) env = getenv("ZAXIS_DEBUG");
Error(func, "zaxisID %d type undefined!", zaxisID); if ( env ) ZAXIS_Debug = atoi(env);
zaxis_list_new();
atexit(zaxis_list_delete);
zaxis_init_pointer();
_zaxis_init = TRUE;
}
static void zaxis_copy(ZAXIS *zaxisptr2, ZAXIS *zaxisptr1)
{
int zaxisID2;
zaxisID2 = zaxisptr2->self;
memcpy(zaxisptr2, zaxisptr1, sizeof(ZAXIS));
zaxisptr2->self = zaxisID2;
}
static void zaxis_check_ptr(const char *func, ZAXIS *zaxisptr)
{
if ( zaxisptr == NULL )
Error(func, "zaxis undefined!");
}
int zaxisSize(void)
{
int zaxissize = 0;
int i;
ZAXIS_INIT
ZAXIS_LOCK
for ( i = 0; i < _zaxis_max; i++ )
if ( _zaxisList[i].ptr ) zaxissize++;
ZAXIS_UNLOCK
return (zaxissize);
} }
...@@ -188,14 +338,19 @@ int zaxisCreate(int zaxistype, int size) ...@@ -188,14 +338,19 @@ int zaxisCreate(int zaxistype, int size)
int ilev; int ilev;
int zaxisID; int zaxisID;
double *vals; double *vals;
ZAXIS *zaxisptr;
if ( CDI_Debug ) if ( CDI_Debug )
Message(func, "zaxistype: %d size: %d ", zaxistype, size); Message(func, "zaxistype: %d size: %d ", zaxistype, size);
/* get next free level entry */ ZAXIS_INIT
zaxisID = zaxisTableNewEntry(zaxistype);
zaxisptr = zaxis_new_entry();
if ( ! zaxisptr ) Error(func, "No memory");
zaxisID = zaxisptr->self;
zaxisTable[zaxisID].size = size; zaxisptr->size = size;
if ( zaxistype > CDI_MaxLeveltype ) if ( zaxistype > CDI_MaxLeveltype )
Error(func, "Internal problem! zaxistype > CDI_MaxLeveltype"); Error(func, "Internal problem! zaxistype > CDI_MaxLeveltype");
...@@ -209,7 +364,7 @@ int zaxisCreate(int zaxistype, int size) ...@@ -209,7 +364,7 @@ int zaxisCreate(int zaxistype, int size)
for ( ilev = 0; ilev < size; ilev++ ) for ( ilev = 0; ilev < size; ilev++ )
vals[ilev] = 0.0; vals[ilev] = 0.0;
zaxisTable[zaxisID].vals = vals; zaxisptr->vals = vals;
return (zaxisID); return (zaxisID);
} }
...@@ -227,6 +382,9 @@ int zaxisCreate(int zaxistype, int size) ...@@ -227,6 +382,9 @@ int zaxisCreate(int zaxistype, int size)
*/ */
void zaxisDestroy(int zaxisID) void zaxisDestroy(int zaxisID)
{ {
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
} }
...@@ -265,8 +423,12 @@ The function @func{zaxisDefName} defines the name of a Z-axis. ...@@ -265,8 +423,12 @@ The function @func{zaxisDefName} defines the name of a Z-axis.
*/ */
void zaxisDefName(int zaxisID, const char *name) void zaxisDefName(int zaxisID, const char *name)
{ {
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
if ( name ) if ( name )
strcpy(zaxisTable[zaxisID].name, name); strcpy(zaxisptr->name, name);
} }
...@@ -286,8 +448,12 @@ The function @func{zaxisDefLongname} defines the longname of a Z-axis. ...@@ -286,8 +448,12 @@ The function @func{zaxisDefLongname} defines the longname of a Z-axis.
*/ */
void zaxisDefLongname(int zaxisID, const char *longname) void zaxisDefLongname(int zaxisID, const char *longname)
{ {
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
if ( longname ) if ( longname )
strcpy(zaxisTable[zaxisID].longname, longname); strcpy(zaxisptr->longname, longname);
} }
...@@ -307,8 +473,12 @@ The function @func{zaxisDefUnits} defines the units of a Z-axis. ...@@ -307,8 +473,12 @@ The function @func{zaxisDefUnits} defines the units of a Z-axis.
*/ */
void zaxisDefUnits(int zaxisID, const char *units) void zaxisDefUnits(int zaxisID, const char *units)
{ {
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
if ( units ) if ( units )
strcpy(zaxisTable[zaxisID].units, units); strcpy(zaxisptr->units, units);
} }
...@@ -331,7 +501,11 @@ The function @func{zaxisInqName} returns the name of a Z-axis. ...@@ -331,7 +501,11 @@ The function @func{zaxisInqName} returns the name of a Z-axis.
*/ */
void zaxisInqName(int zaxisID, char *name) void zaxisInqName(int zaxisID, char *name)
{ {
strcpy(name, zaxisTable[zaxisID].name); ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
strcpy(name, zaxisptr->name);
} }
...@@ -354,7 +528,11 @@ The function @func{zaxisInqLongname} returns the longname of a Z-axis. ...@@ -354,7 +528,11 @@ The function @func{zaxisInqLongname} returns the longname of a Z-axis.
*/ */
void zaxisInqLongname(int zaxisID, char *longname) void zaxisInqLongname(int zaxisID, char *longname)
{ {
strcpy(longname, zaxisTable[zaxisID].longname); ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
strcpy(longname, zaxisptr->longname);
} }
...@@ -377,27 +555,37 @@ The function @func{zaxisInqUnits} returns the units of a Z-axis. ...@@ -377,27 +555,37 @@ The function @func{zaxisInqUnits} returns the units of a Z-axis.
*/ */
void zaxisInqUnits(int zaxisID, char *units) void zaxisInqUnits(int zaxisID,