From fa917381937c46824b005835038b1454e21421c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 17 Jun 2010 00:33:57 +0100 Subject: [PATCH] extend SWI compatinbility. --- packages/PLStream/pl-incl.h | 14 ++++++++++- packages/PLStream/pl-yap.c | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/PLStream/pl-incl.h b/packages/PLStream/pl-incl.h index 718b446d1..478dc42f8 100755 --- a/packages/PLStream/pl-incl.h +++ b/packages/PLStream/pl-incl.h @@ -102,6 +102,8 @@ typedef enum typedef struct tempfile * TempFile; /* pl-os.c */ typedef struct canonical_dir * CanonicalDir; /* pl-os.c */ typedef struct on_halt * OnHalt; /* pl-os.c */ +typedef struct extension_cell * ExtensionCell; /* pl-ext.c */ +typedef struct initialise_handle * InitialiseHandle; /* The GD global variable */ typedef struct { @@ -151,6 +153,17 @@ typedef struct { atom_t *for_code[256]; /* code --> one-char-atom */ } atoms; + struct + { ExtensionCell _ext_head; /* head of registered extensions */ + ExtensionCell _ext_tail; /* tail of this chain */ + + InitialiseHandle initialise_head; /* PL_initialise_hook() */ + InitialiseHandle initialise_tail; + PL_dispatch_hook_t dispatch_events; /* PL_dispatch_hook() */ + + int _loaded; /* system extensions are loaded */ + } foreign; + } gds_t; extern gds_t gds; @@ -498,7 +511,6 @@ typedef double real; #endif -#define PL_dispatch(FD, COM) extern int PL_unify_char(term_t chr, int c, int how); extern int PL_get_char(term_t chr, int *c, int eof); extern int PL_get_text(term_t l, PL_chars_t *text, int flags); diff --git a/packages/PLStream/pl-yap.c b/packages/PLStream/pl-yap.c index 25a9d2c8c..ae96e86e3 100644 --- a/packages/PLStream/pl-yap.c +++ b/packages/PLStream/pl-yap.c @@ -608,3 +608,49 @@ warning(const char *fm, ...) return TRUE; } +#if defined(HAVE_SELECT) && !defined(__WINDOWS__) + +#ifdef __WINDOWS__ +#include +#endif + +static int +input_on_fd(int fd) +{ fd_set rfds; + struct timeval tv; + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; + + return select(fd+1, &rfds, NULL, NULL, &tv) != 0; +} + +#else +#define input_on_fd(fd) 1 +#endif + + +X_API int +PL_dispatch(int fd, int wait) +{ if ( wait == PL_DISPATCH_INSTALLED ) + return GD->foreign.dispatch_events ? TRUE : FALSE; + + if ( GD->foreign.dispatch_events && PL_thread_self() == 1 ) + { if ( wait == PL_DISPATCH_WAIT ) + { while( !input_on_fd(fd) ) + { if ( PL_handle_signals() < 0 ) + return FALSE; + (*GD->foreign.dispatch_events)(fd); + } + } else + { (*GD->foreign.dispatch_events)(fd); + if ( PL_handle_signals() < 0 ) + return FALSE; + } + } + + return TRUE; +} +