Skip to content

Commit

Permalink
ADDED: PL_get_stream(): SIO_TRYLOCK flag to allow failure if the stre…
Browse files Browse the repository at this point in the history
…am is locked.
  • Loading branch information
JanWielemaker committed Oct 6, 2024
1 parent 8f8c5c5 commit edd9ae8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions man/streams.doc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions src/os/SWI-Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
14 changes: 14 additions & 0 deletions src/os/pl-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit edd9ae8

Please sign in to comment.