From edd9ae8ebd02e853c48d76ac25ab99fef8e5e7a0 Mon Sep 17 00:00:00 2001 From: Jan Wielemaker Date: Sun, 6 Oct 2024 20:44:54 +0200 Subject: [PATCH] ADDED: PL_get_stream(): SIO_TRYLOCK flag to allow failure if the stream is locked. --- man/streams.doc | 3 +++ src/os/SWI-Stream.h | 2 ++ src/os/pl-file.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/man/streams.doc b/man/streams.doc index 0ad65f3f41..8139cc7cdd 100644 --- a/man/streams.doc +++ b/man/streams.doc @@ -61,6 +61,9 @@ flags: Get an \jargon{output stream}. See \const{SIO_INPUT} for details. If neither \const{SIO_OUTPUT} nor \const{SIO_INPUT} is given \arg{t} may not be a \jargon{pair}. + \definition{\const{SIO_TRYLOCK}} + Return \const{FALSE} if the stream cannot be locked + immediately. No error is generated. \definition{\const{SIO_NOERROR}} If the function fails no exception is produced. \end{description} diff --git a/src/os/SWI-Stream.h b/src/os/SWI-Stream.h index 23abc65784..7733264bab 100644 --- a/src/os/SWI-Stream.h +++ b/src/os/SWI-Stream.h @@ -283,6 +283,8 @@ typedef struct io_stream #define SIO_BOM SmakeFlag(31) /* BOM was detected/written */ #define SIO_REPPLU SmakeFlag(32) /* Bad char --> Prolog \uXXXX */ +#define SIO_TRYLOCK SIO_CLOSING /* Used by PL_get_stream() */ + #define SIO_SEEK_SET 0 /* From beginning of file. */ #define SIO_SEEK_CUR 1 /* From current position. */ #define SIO_SEEK_END 2 /* From end of file. */ diff --git a/src/os/pl-file.c b/src/os/pl-file.c index 79ea8ccf11..7d2162a287 100644 --- a/src/os/pl-file.c +++ b/src/os/pl-file.c @@ -780,6 +780,7 @@ static PL_blob_t stream_blob = #define SH_OUTPUT 0x08 /* We want an output stream */ #define SH_INPUT 0x10 /* We want an input stream */ #define SH_NOPAIR 0x20 /* Do not allow for a pair */ +#define SH_TRYLOCK 0x40 /* Fail if we cannot lock */ #define get_stream_handle(a, sp, flags) LDFUNC(get_stream_handle, a, sp, flags) static bool @@ -828,6 +829,12 @@ get_stream_handle(DECL_LD atom_t a, IOSTREAM **sp, int flags) { assert( s->magic == SIO_MAGIC || s->magic == SIO_CMAGIC ); *sp = s; return true; + } else if ( flags & SH_TRYLOCK ) + { if ( (s=tryGetStream(s)) ) + { *sp = s; + return true; + } else + return false; /* exception */ } else if ( (s=getStream(s)) ) { *sp = s; return true; @@ -859,6 +866,12 @@ get_stream_handle(DECL_LD atom_t a, IOSTREAM **sp, int flags) { *sp = stream; return true; } + } else if ( flags & SH_TRYLOCK ) + { if ( (s=tryGetStream(stream)) ) + { *sp = s; + return true; + } else + return false; /* exception? */ } else if ( (*sp = getStream(stream)) ) return true; goto noent; @@ -921,6 +934,7 @@ PL_get_stream_from_blob(atom_t a, IOSTREAM **s, int flags) if ( flags&SIO_INPUT ) myflags |= SH_INPUT; if ( flags&SIO_OUTPUT ) myflags |= SH_OUTPUT; + if ( flags&SIO_TRYLOCK ) myflags |= SH_TRYLOCK; if ( flags&SIO_NOERROR ) myflags &= ~SH_ERRORS; if ( !(flags&(SIO_INPUT|SIO_OUTPUT)) ) myflags |= SH_NOPAIR;