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>
* using GRIB library version 1.0.4
......
#! /bin/sh
# 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>.
#
......@@ -269,8 +269,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package.
PACKAGE_NAME='cdi'
PACKAGE_TARNAME='cdi'
PACKAGE_VERSION='1.0.4'
PACKAGE_STRING='cdi 1.0.4'
PACKAGE_VERSION='1.0.5'
PACKAGE_STRING='cdi 1.0.5'
PACKAGE_BUGREPORT='Uwe.Schulzweida@zmaw.de'
# Factoring default headers for most tests.
......@@ -795,7 +795,7 @@ if test "$ac_init_help" = "long"; then
# 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.
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]...
......@@ -862,7 +862,7 @@ fi
if test -n "$ac_init_help"; then
case $ac_init_help in
short | recursive ) echo "Configuration of cdi 1.0.4:";;
short | recursive ) echo "Configuration of cdi 1.0.5:";;
esac
cat <<\_ACEOF
......@@ -1006,7 +1006,7 @@ fi
test -n "$ac_init_help" && exit 0
if $ac_init_version; then
cat <<\_ACEOF
cdi configure 1.0.4
cdi configure 1.0.5
generated by GNU Autoconf 2.59
Copyright (C) 2003 Free Software Foundation, Inc.
......@@ -1020,7 +1020,7 @@ cat >&5 <<_ACEOF
This file contains any messages produced by compilers while
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
$ $0 $@
......@@ -1707,7 +1707,7 @@ fi
# Define the identity of the package.
PACKAGE=cdi
VERSION=1.0.4
VERSION=1.0.5
cat >>confdefs.h <<_ACEOF
......@@ -6400,7 +6400,7 @@ _ASBOX
} >&5
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
CONFIG_FILES = $CONFIG_FILES
......@@ -6463,7 +6463,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF
ac_cs_version="\\
cdi config.status 1.0.4
cdi config.status 1.0.5
configured by $0, generated by GNU Autoconf 2.59,
with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\"
......
# 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
......
......@@ -231,7 +231,7 @@ static void grid_delete_entry(GRID *gridptr)
_gridList[idx].next = _gridAvail;
_gridList[idx].ptr = 0;
_gridAvail = &_gridList[idx];
_gridAvail = &_gridList[idx];
GRID_UNLOCK
......
......@@ -46,6 +46,7 @@ typedef struct {
double *lbounds;
double *ubounds;
double *weights;
int self;
int prec;
int type;
int size;
......@@ -56,94 +57,243 @@ typedef struct {
}
ZAXIS;
static ZAXIS *zaxisTable;
static int zaxisTableSize = 0;
static int ZAXIS_Debug = 0; /* If set to 1, debugging */
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;
zaxisTable[zaxisID].longname[0] = 0;
zaxisTable[zaxisID].units[0] = 0;
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 char func[] = "zaxis_list_delete";
if ( _zaxisList ) free(_zaxisList);
}
static int zaxisTableNewEntry (int zaxistype)
static void zaxis_init_pointer(void)
{
static char func[] = "zaxisTableNewEntry";
int zaxisID = 0;
/*
Look for a free slot in zaxisTable.
(Create the table the first time through).
*/
if ( ! zaxisTableSize )
int i;
for ( i = 0; i < _zaxis_max; i++ )
{
int i;
_zaxisList[i].next = _zaxisList + i + 1;
_zaxisList[i].idx = i;
_zaxisList[i].ptr = 0;
}
zaxisTableSize = 2;
zaxisTable = (ZAXIS *) malloc(zaxisTableSize*sizeof(ZAXIS));
if ( zaxisTable == NULL )
{
Message(func, "zaxisTableSize = %d", zaxisTableSize);
SysError(func, "Allocation of ZAXISTABLE failed");
}
_zaxisList[_zaxis_max-1].next = 0;
_zaxisAvail = _zaxisList;
}
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
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;
zaxisID++;
newptr = _zaxisAvail;
_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
}
/*
If the table overflows, double its size.
*/
if ( zaxisID == zaxisTableSize )
{
int i;
else
Error(func, "Internal problem (pointer %p undefined)", ptr);
zaxisTableSize = 2*zaxisTableSize;
zaxisTable = (ZAXIS *) realloc(zaxisTable, zaxisTableSize*sizeof(ZAXIS));
if ( zaxisTable == NULL )
{
Message(func, "zaxisTableSize = %d", zaxisTableSize);
SysError(func, "Reallocation of ZAXISTABLE failed");
}
return (idx);
}
zaxisID = zaxisTableSize/2;
for ( i = zaxisID; i < zaxisTableSize; i++ )
zaxisTableInitEntry(i);
}
static void zaxis_init_entry(ZAXIS *zaxisptr)
{
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 )
Error(func, "zaxisID %d undefined!", zaxisID);
char *env;
#if defined (HAVE_LIBPTHREAD)
/* initialize global API mutex lock */
pthread_mutex_init(&_zaxis_mutex, NULL);
#endif
if ( zaxisTable[zaxisID].type == CDI_UNDEFID )
Error(func, "zaxisID %d type undefined!", zaxisID);
env = getenv("ZAXIS_DEBUG");
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)
int ilev;
int zaxisID;
double *vals;
ZAXIS *zaxisptr;
if ( CDI_Debug )
Message(func, "zaxistype: %d size: %d ", zaxistype, size);
/* get next free level entry */
zaxisID = zaxisTableNewEntry(zaxistype);
ZAXIS_INIT
zaxisptr = zaxis_new_entry();
if ( ! zaxisptr ) Error(func, "No memory");
zaxisID = zaxisptr->self;
zaxisTable[zaxisID].size = size;
zaxisptr->size = size;
if ( zaxistype > CDI_MaxLeveltype )
Error(func, "Internal problem! zaxistype > CDI_MaxLeveltype");
......@@ -209,7 +364,7 @@ int zaxisCreate(int zaxistype, int size)
for ( ilev = 0; ilev < size; ilev++ )
vals[ilev] = 0.0;
zaxisTable[zaxisID].vals = vals;
zaxisptr->vals = vals;
return (zaxisID);
}
......@@ -227,6 +382,9 @@ int zaxisCreate(int zaxistype, int size)
*/
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.
*/
void zaxisDefName(int zaxisID, const char *name)
{
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
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.
*/
void zaxisDefLongname(int zaxisID, const char *longname)
{
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
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.
*/
void zaxisDefUnits(int zaxisID, const char *units)
{
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
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.
*/
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.
*/
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.
*/
void zaxisInqUnits(int zaxisID, char *units)
{
strcpy(units, zaxisTable[zaxisID].units);
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
strcpy(units, zaxisptr->units);
}
void zaxisDefPrec(int zaxisID, int prec)
{
static char func[] = "zaxisDefPrec";
ZAXIS *zaxisptr;
zaxisCheckID(func, zaxisID);
zaxisptr = zaxis_to_pointer(zaxisID);
zaxisTable[zaxisID].prec = prec;
zaxis_check_ptr(func, zaxisptr);
zaxisptr->prec = prec;
}
int zaxisInqPrec(int zaxisID)
{
static char func[] = "zaxisInqPrec";
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
zaxisCheckID(func, zaxisID);
zaxis_check_ptr(func, zaxisptr);
return (zaxisTable[zaxisID].prec);
return (zaxisptr->prec);
}
......@@ -421,12 +609,15 @@ void zaxisDefLevels(int zaxisID, double *levels)
int ilev;
int size;
double *vals;
ZAXIS *zaxisptr;
zaxisCheckID(func, zaxisID);
zaxisptr = zaxis_to_pointer(zaxisID);
size = zaxisTable[zaxisID].size;
zaxis_check_ptr(func, zaxisptr);
vals = zaxisTable[zaxisID].vals;
size = zaxisptr->size;
vals = zaxisptr->vals;
for ( ilev = 0; ilev < size; ilev++ )
vals[ilev] = levels[ilev];
......@@ -451,11 +642,14 @@ The function @func{zaxisDefLevel} defines one level of a Z-axis.
void zaxisDefLevel(int zaxisID, int levelID, double level)
{
static char func[] = "zaxisDefLevel";
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
zaxisCheckID(func, zaxisID);
zaxis_check_ptr(func, zaxisptr);
if ( levelID >= 0 && levelID < zaxisTable[zaxisID].size )
zaxisTable[zaxisID].vals[levelID] = level;
if ( levelID >= 0 && levelID < zaxisptr->size )
zaxisptr->vals[levelID] = level;
}
......@@ -479,11 +673,14 @@ double zaxisInqLevel(int zaxisID, int levelID)
{
static char func[] = "zaxisInqLevel";
double level = 0;
ZAXIS *zaxisptr;
zaxisptr = zaxis_to_pointer(zaxisID);
zaxisCheckID(func, zaxisID);
zaxis_check_ptr(func, zaxisptr);
if ( levelID >= 0 && levelID < zaxisTable[zaxisID].size )
level = zaxisTable[zaxisID].vals[levelID];
if ( levelID >= 0 && levelID < zaxisptr->size )
level = zaxisptr->vals[levelID];
return (level);
}
......@@ -493,11 +690,14 @@ double zaxisInqLbound(int zaxisID, int index)
{
static char func[] = "zaxisInqLbound";
double level = 0;
ZAXIS *zaxisptr;
zaxisCheckID(func, zaxisID);
zaxisptr = zaxis_to_pointer(zaxisID);
if ( index >= 0 && index < zaxisTable[zaxisID].size )
level = zaxisTable[zaxisID].lbounds[index];
zaxis_check_ptr(func, zaxisptr);
if ( index >= 0 && index < zaxisptr->size )
level = zaxisptr->lbounds[index];
return (level);
}
......@@ -507,11 +707,14 @@ double zaxisInqUbound(int zaxisID, int index)
{
static char func[] = "zaxisInqUbound";
double level = 0;