libgcobol: Only use random_r if it is available [PR119295]

We do not have a replacement at the moment, so fall back to using
regular random and friends.

	PR cobol/119295

libgcobol/ChangeLog:

	* config.h.in: Regenerate.
	* configure: Regenerate.
	* configure.ac: Configure random_r and friends
	* intrinsic.cc (__gg__random): Use random_r when available.
	(__gg__random_next): Likewise.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
This commit is contained in:
Iain Sandoe 2025-03-16 09:32:12 +00:00
parent ce1cf36199
commit 66a41a0a96
4 changed files with 70 additions and 7 deletions

View file

@ -9,6 +9,9 @@
/* Define if you have the iconv() function and it works. */
#undef HAVE_ICONV
/* Define to 1 if you have the `initstate_r' function. */
#undef HAVE_INITSTATE_R
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
@ -18,6 +21,15 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the `random_r' function. */
#undef HAVE_RANDOM_R
/* Define to 1 if you have the `setstate_r' function. */
#undef HAVE_SETSTATE_R
/* Define to 1 if you have the `srandom_r' function. */
#undef HAVE_SRANDOM_R
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

35
libgcobol/configure vendored
View file

@ -629,6 +629,7 @@ ac_includes_default="\
# include <unistd.h>
#endif"
ac_func_list=
ac_unique_file="Makefile.am"
ac_subst_vars='am__EXEEXT_FALSE
am__EXEEXT_TRUE
@ -2515,6 +2516,10 @@ $as_echo "$as_me: creating cache $cache_file" >&6;}
>$cache_file
fi
as_fn_append ac_func_list " random_r"
as_fn_append ac_func_list " srandom_r"
as_fn_append ac_func_list " initstate_r"
as_fn_append ac_func_list " setstate_r"
# Check that the precious variables saved in the cache have kept the same
# value.
ac_cache_corrupted=false
@ -12901,7 +12906,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
#line 12904 "configure"
#line 12909 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@ -13007,7 +13012,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
#line 13010 "configure"
#line 13015 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@ -16354,6 +16359,32 @@ fi
# These are GLIBC
for ac_func in $ac_func_list
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
_ACEOF
fi
done
if test "${multilib}" = "yes"; then
multilib_arg="--enable-multilib"
else

View file

@ -215,6 +215,9 @@ AM_CONDITIONAL([ENABLE_DARWIN_AT_RPATH], [test x$enable_darwin_at_rpath = xyes])
AC_SUBST(enable_shared)
AC_SUBST(enable_static)
# These are GLIBC
AC_CHECK_FUNCS_ONCE(random_r srandom_r initstate_r setstate_r)
if test "${multilib}" = "yes"; then
multilib_arg="--enable-multilib"
else

View file

@ -44,6 +44,8 @@
#include <langinfo.h>
#include <string.h>
#include "config.h"
#include "ec.h"
#include "common-defs.h"
#include "io.h"
@ -3409,9 +3411,13 @@ __gg__trim( cblc_field_t *dest,
}
}
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
static struct random_data *buf = NULL;
static char *state = NULL;
static const size_t state_len = 256;
#else
static unsigned seed = 0;
#endif
extern "C"
void
@ -3420,6 +3426,9 @@ __gg__random( cblc_field_t *dest,
size_t input_offset,
size_t input_size)
{
int32_t retval_31;
int rdigits;
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
// This creates a thread-safe pseudo-random number generator
// using input as the seed
@ -3436,16 +3445,21 @@ __gg__random( cblc_field_t *dest,
__gg__clock_gettime(CLOCK_REALTIME, &ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
int rdigits;
int seed = (int)__gg__binary_value_from_qualified_field(&rdigits,
input,
input_offset,
input_size);
srandom_r(seed, buf);
int32_t retval_31;
random_r(buf, &retval_31);
#else
seed = (unsigned)__gg__binary_value_from_qualified_field(&rdigits,
input,
input_offset,
input_size);
srandom (seed);
retval_31 = random ();
#endif
// We are going to convert this to a value between zero and not quite one:
double retval = double(retval_31) / double(0x80000000UL);
__gg__double_to_target( dest,
@ -3457,6 +3471,8 @@ extern "C"
void
__gg__random_next(cblc_field_t *dest)
{
int32_t retval_31;
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R
// The return value is between zero and not quite one
if( !buf )
@ -3469,9 +3485,10 @@ __gg__random_next(cblc_field_t *dest)
__gg__clock_gettime(CLOCK_REALTIME, &ts);
initstate_r( ts.tv_nsec, state, state_len, buf);
}
int32_t retval_31;
random_r(buf, &retval_31);
#else
retval_31 = random ();
#endif
// We are going to convert this to a value between zero and not quite one:
double retval = double(retval_31) / double(0x80000000UL);
__gg__double_to_target( dest,