fix locks

This commit is contained in:
Vítor Santos Costa 2015-06-19 10:10:02 +01:00
parent 1c06bfdc05
commit f5b50a4179
10 changed files with 62 additions and 49 deletions

View File

@ -2641,6 +2641,7 @@ YAP_InitConsult(int mode, const char *filename)
return -1;
sno = Yap_OpenStream(f, NULL, TermNil, Input_Stream_f );
RECOVER_MACHINE_REGS();
UNLOCK(GLOBAL_Stream[sno].streamlock);
return sno;
}
@ -2964,7 +2965,7 @@ YAP_Init(YAP_init_args *yap_init)
*/
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG, yap_init->HaltAfterConsult );
}
/* tell the system who should cope with interruptions */
/* tell the system who should cope with interrupts */
Yap_ExecutionMode = yap_init->ExecutionMode;
if (do_bootstrap) {
restore_result = YAP_BOOT_FROM_PROLOG;

View File

@ -741,6 +741,7 @@ p_get_header( USES_REGS1 )
FILE *stream;
Term t1 = Deref(ARG1);
Atom at;
Int rc;
if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR,t1,"read_program/3");
@ -750,8 +751,9 @@ p_get_header( USES_REGS1 )
return FALSE;
}
if ((at = get_header( stream )) == NIL)
return FALSE;
return Yap_unify( ARG2, MkAtomTerm( at ) );
rc = FALSE;
else rc = Yap_unify( ARG2, MkAtomTerm( at ) );
return rc;
}
static void

View File

@ -52,7 +52,7 @@ blob_type_t PL_Message_Queue = {
#if DEBUG_LOCKS||DEBUG_PE_LOCKS
int debug_locks = FALSE, debug_pe_locks = FALSE;
static Int p_debug_locks( USES_REGS1 ) { debugf=stdout; debug_pe_locks = 1; return TRUE; }
static Int p_debug_locks( USES_REGS1 ) { debug_pe_locks = 1; return TRUE; }
static Int p_nodebug_locks( USES_REGS1 ) { debug_locks = 0; debug_pe_locks = 0; return TRUE; }
@ -1590,8 +1590,6 @@ Yap_InitFirstWorkerThreadHandle(void)
LOCAL_ThreadHandle.ref_count = 1;
}
FILE *debugf;
void Yap_InitThreadPreds(void)
{

View File

@ -97,10 +97,19 @@ check_trail_consistency(void) {
CELL old_value = 0L, old_value2 = 0L;
void jmp_deb(int);
void jmp_deb(int), jmp_deb2(void);
void
jmp_deb(int i) {if (i) printf("Here we go\n"); else jmp_deb(0);}
jmp_deb2( void )
{
fprintf(stderr,"Here\n");
}
void
jmp_deb(int i) {
if (i) printf("Here we go %ld\n", old_value++);
if (old_value == 716) jmp_deb2();
}
struct various_codes *sc;

View File

@ -22,13 +22,13 @@
#include <pthread.h>
//#define DEBUG_PE_LOCKS 1
#define DEBUG_LOCKS 1
//#define DEBUG_LOCKS 1
#include <stdio.h>
int Yap_ThreadID( void );
#define debugf stdout
#define debugf ( stderr ? stderr : stdout )
#define INIT_LOCK(LOCK_VAR) (void)(fprintf(debugf, "[%d] %s:%d: LOCK(%p)\n", Yap_ThreadID(),__BASE_FILE__, __LINE__,&(LOCK_VAR)) && pthread_mutex_init(&(LOCK_VAR), NULL) )
#define INIT_LOCK(LOCK_VAR) pthread_mutex_init(&(LOCK_VAR), NULL)
#define DESTROY_LOCK(LOCK_VAR) pthread_mutex_destroy(&(LOCK_VAR))
#define TRY_LOCK(LOCK_VAR) pthread_mutex_trylock(&(LOCK_VAR))
#if DEBUG_LOCKS

View File

@ -808,11 +808,12 @@ peek_code ( USES_REGS1 )
return FALSE;
if (GLOBAL_Stream[sno].status & Binary_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "peek_code/2");
PlIOError(PERMISSION_ERROR_INPUT_BINARY_STREAM, ARG1, "peek_code/2");
return FALSE;
}
if ((ch = dopeek( sno )) < 0)
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(Yap_unify_constant(ARG2,MkIntTerm(ch)));
}
@ -841,6 +842,7 @@ peek_code_1 ( USES_REGS1 )
}
if ((ch = dopeek( sno )) < 0)
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(Yap_unify_constant(ARG2,MkIntTerm(ch)));
}
@ -857,7 +859,7 @@ peek_byte ( USES_REGS1 )
{ /* at_end_of_stream */
/* the next character is a EOF */
int sno = Yap_CheckStream (ARG1, Input_Stream_f, "peek/2");
Int ch;
Int ch;
if (sno < 0)
return(FALSE);
@ -868,6 +870,7 @@ peek_byte ( USES_REGS1 )
}
if ((ch = dopeek( sno )) < 0)
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(Yap_unify_constant(ARG2,MkIntTerm(ch)));
}

View File

@ -347,13 +347,15 @@ file_size(USES_REGS1)
// there
struct stat file_stat;
if (fstat(fileno(GLOBAL_Stream[sno].file), &file_stat) < 0) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError( PERMISSION_ERROR_INPUT_STREAM, ARG1, "%s in file_size/2", strerror(errno));
return false;
}
// and back again
UNLOCK(GLOBAL_Stream[sno].streamlock);
return Yap_unify_constant( ARG2, MkIntegerTerm( file_stat.st_size) );
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return false;
}

View File

@ -1650,7 +1650,7 @@ Yap_OpenStream(FILE *fd, char *name, Term file_name, int flags)
if (sname == AtomUser) {
if (kind & Input_Stream_f) {
if (kind & (Output_Stream_f|Append_Stream_f)) {
Yap_Error(PERMISSION_ERROR_INPUT_STREAM, arg,
PlIOError(PERMISSION_ERROR_INPUT_STREAM, arg,
"ambiguous use of 'user' as a stream");
return (-1);
}
@ -1690,6 +1690,7 @@ Yap_OpenStream(FILE *fd, char *name, Term file_name, int flags)
Yap_Error(PERMISSION_ERROR_OUTPUT_STREAM, arg, msg);
return (-1);
}
jmp_deb(1);
return (sno);
}

View File

@ -824,8 +824,8 @@ read2 ( USES_REGS1 )
if (inp_stream == -1) {
return(FALSE);
}
UNLOCK(GLOBAL_Stream[inp_stream].streamlock);
out = Yap_read_term(inp_stream, TermNil, 1);
UNLOCK(GLOBAL_Stream[inp_stream].streamlock);
return out && Yap_unify(ARG2, out);
}
@ -938,7 +938,8 @@ Yap_StringToTerm(const char *s, size_t len, encoding_t enc, int prio, Term *bind
rval = Yap_read_term(stream, ctl, 3);
Yap_CloseStream(stream);
if (bindings) {
UNLOCK(GLOBAL_Stream[stream].streamlock);
if (rval && bindings) {
*bindings = Yap_GetFromSlot( sl );
Yap_RecoverSlots( sl, 1 PASS_REGS);
}

View File

@ -102,29 +102,35 @@ static void CloseStream(int sno);
FILE *Yap_GetInputStream(Term t, const char *msg)
{
int sno = Yap_CheckStream (t, Input_Stream_f, msg);
FILE *rc;
if(!(GLOBAL_Stream[sno].status & (Tty_Stream_f|Socket_Stream_f|Pipe_Stream_f)))
return GLOBAL_Stream[sno].file;
return NULL;
rc = GLOBAL_Stream[sno].file;
else
rc = NULL;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
FILE *Yap_GetOutputStream(Term t, const char *msg)
{
int sno = Yap_CheckStream (t, Output_Stream_f, msg);
FILE *rc;
if(!(GLOBAL_Stream[sno].status & (Tty_Stream_f|Socket_Stream_f)))
return GLOBAL_Stream[sno].file;
return NULL;
rc = GLOBAL_Stream[sno].file;
else
rc = NULL;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
int
GetFreeStreamD(void)
{
CACHE_REGS
int sno;
for (sno = 0; sno < MaxStreams; ++sno) {
int sno;
for (sno = 0; sno < MaxStreams; ++sno) {
LOCK(GLOBAL_Stream[sno].streamlock);
if (GLOBAL_Stream[sno].status & Free_Stream_f) {
break;
@ -208,7 +214,6 @@ static Int
is_input ( int sno USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
bool rc = GLOBAL_Stream[sno].status & Input_Stream_f;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
@ -216,14 +221,12 @@ static Int
is_output ( int sno USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
bool rc = GLOBAL_Stream[sno].status & (Output_Stream_f|Append_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
static Int
has_bom ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
UNLOCK(GLOBAL_Stream[sno].streamlock);
bool rc = GLOBAL_Stream[sno].status & Seekable_Stream_f;
if (!IsVarTerm(t2) && !boolean(t2)) {
return FALSE;
@ -234,11 +237,11 @@ has_bom ( int sno, Term t2 USES_REGS )
return Yap_unify_constant(t2, TermFalse);
}
}
static Int
has_reposition ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
bool rc = GLOBAL_Stream[sno].status & Seekable_Stream_f;
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !boolean(t2)) {
return FALSE;
}
@ -284,7 +287,6 @@ representation_error ( int sno, Term t2 USES_REGS )
{
stream_flags_t flags = GLOBAL_Stream[sno].status & (RepError_Xml_f|RepError_Prolog_f);
/* '$representation_error'(+Stream,-ErrorMessage) */
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && isatom(t2)) {
return false;
}
@ -308,7 +310,6 @@ file_name ( int sno, Term t2 USES_REGS )
rc = MkAtomTerm(Yap_LookupAtom(name));
else
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !isatom(t2)) {
return FALSE;
}
@ -320,7 +321,6 @@ file_no ( int sno, Term t2 USES_REGS )
{
int f = Yap_GetStreamFd(sno);
Term rc = MkIntTerm(f) ;
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !IsIntTerm(t2)) {
return false;
}
@ -355,7 +355,6 @@ has_close_on_abort ( int sno, Term t2 USES_REGS )
static bool
has_encoding ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -375,7 +374,6 @@ static bool
found_eof ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status & (Past_Eof_Stream_f|Eof_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -391,7 +389,6 @@ static bool
stream_mode ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status & (Input_Stream_f|Output_Stream_f|Append_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -408,7 +405,6 @@ static bool
stream_tty ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status & (Tty_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -421,7 +417,6 @@ static bool
stream_type ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status & (Binary_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -434,7 +429,6 @@ static bool
stream_position ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
Term tout = StreamPosition(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock);
return Yap_unify (t2, tout);
}
@ -474,7 +468,6 @@ static bool
eof_action ( int sno, Term t2 USES_REGS )
{ /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status & (Eof_Error_Stream_f|Reset_Eof_Stream_f|Push_Eof_Stream_f);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
@ -706,6 +699,7 @@ do_set_stream (int sno, Term opts USES_REGS)
args = Yap_ArgListToVector ( opts, set_stream_defs, SET_STREAM_END );
if (args == NULL) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
return false;
}
for (i=0; i < SET_STREAM_END; i ++) {
@ -747,7 +741,7 @@ do_set_stream (int sno, Term opts USES_REGS)
} else {
LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
LOCAL_ErrorMessage = "in set_stream/2:eof_action";
return false;
rc = false;
}
break;
case SET_STREAM_FILE_NAME:
@ -780,7 +774,7 @@ do_set_stream (int sno, Term opts USES_REGS)
} else {
LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
LOCAL_ErrorMessage = "in set_stream/2:eof_action";
return false;
rc = false;
}
}
break;
@ -1265,13 +1259,13 @@ set_stream_position ( USES_REGS1 )
}
if (!(GLOBAL_Stream[sno].status & Seekable_Stream_f) ) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,"set_stream_position/2");
PlIOError(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,"set_stream_position/2");
return(FALSE);
}
if (fseek (GLOBAL_Stream[sno].file, 0L, SEEK_END) == -1) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(SYSTEM_ERROR, tp,
"fseek failed for set_stream_position/2");
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(SYSTEM_ERROR, tp,
"fseek failed for set_stream_position/2: %s", strerror(errno));
return(FALSE);
}
GLOBAL_Stream[sno].stream_getc = PlGetc;
@ -1431,7 +1425,7 @@ Yap_StreamToFileNo(Term t)
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(GLOBAL_Stream[sno].u.socket.fd);
#endif
} else if (GLOBAL_Stream[sno].status & (Null_Stream_f|InMemory_Stream_f)) {
} else if (GLOBAL_Stream[sno].status & (Null_Stream_f)) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
return(-1);
} else {
@ -1458,15 +1452,17 @@ FILE *
Yap_FileDescriptorFromStream(Term t)
{
int sno = Yap_CheckStream (t, Input_Stream_f|Output_Stream_f, "FileDescriptorFromStream");
FILE *rc;
if (sno < 0)
return NULL;
if (GLOBAL_Stream[sno].status & (Null_Stream_f|
InMemory_Stream_f|
Socket_Stream_f|
Pipe_Stream_f|
Free_Stream_f))
return NULL;
return GLOBAL_Stream[sno].file;
rc = NULL;
else rc = GLOBAL_Stream[sno].file;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
void