2004-01-23 02:23:51 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: stdpreds.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: threads *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2004-01-23 02:23:51 +00:00
|
|
|
#include "eval.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef TABLING
|
|
|
|
#include "tab.macros.h"
|
|
|
|
#endif /* TABLING */
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-02-11 16:18:16 +00:00
|
|
|
#if THREADS
|
|
|
|
|
2004-07-22 22:32:23 +01:00
|
|
|
#include "threads.h"
|
|
|
|
|
2004-02-11 16:18:16 +00:00
|
|
|
/*
|
|
|
|
* This file includes the definition of threads in Yap. Threads
|
|
|
|
* are supposed to be compatible with the SWI-Prolog thread package.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
#if DEBUGX
|
2008-05-28 18:18:35 +01:00
|
|
|
static void DEBUG_TLOCK_ACCESS( int pos, int wid) {
|
2008-08-06 18:32:22 +01:00
|
|
|
fprintf(stderr,"wid=%p %p\n", wid, pos);
|
2008-05-28 18:18:35 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEBUG_TLOCK_ACCESS(WID, POS)
|
|
|
|
#endif
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static int
|
|
|
|
allocate_new_tid(void)
|
|
|
|
{
|
|
|
|
int new_worker_id = 0;
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2008-04-02 16:41:50 +01:00
|
|
|
while(new_worker_id < MAX_THREADS &&
|
2011-05-09 19:36:51 +01:00
|
|
|
Yap_local[new_worker_id] &&
|
2011-05-09 20:19:49 +01:00
|
|
|
(REMOTE_ThreadHandle(new_worker_id).in_use == TRUE ||
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).zombie == TRUE) )
|
2004-01-23 02:23:51 +00:00
|
|
|
new_worker_id++;
|
2011-03-23 15:15:09 +00:00
|
|
|
if (new_worker_id >= MAX_THREADS) {
|
|
|
|
new_worker_id = -1;
|
2011-05-09 19:36:51 +01:00
|
|
|
} else if (!Yap_local[new_worker_id]) {
|
2011-03-11 19:49:32 +00:00
|
|
|
if (!Yap_InitThread(new_worker_id)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(new_worker_id).tlock));
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).in_use = TRUE;
|
2011-03-11 19:49:32 +00:00
|
|
|
} else if (new_worker_id < MAX_THREADS) {
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(new_worker_id).tlock));
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).in_use = TRUE;
|
2008-03-24 23:48:47 +00:00
|
|
|
} else {
|
|
|
|
new_worker_id = -1;
|
|
|
|
}
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-01-23 02:23:51 +00:00
|
|
|
return new_worker_id;
|
|
|
|
}
|
|
|
|
|
2008-04-02 18:37:07 +01:00
|
|
|
static int
|
2010-01-31 23:03:55 +00:00
|
|
|
store_specs(int new_worker_id, UInt ssize, UInt tsize, UInt sysize, Term *tpgoal, Term *tpdetach, Term *tpexit)
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-04-02 18:37:07 +01:00
|
|
|
UInt pm; /* memory to be requested */
|
2008-08-07 21:51:23 +01:00
|
|
|
Term tmod;
|
2010-01-31 23:03:55 +00:00
|
|
|
Term tdetach, tgoal;
|
2008-08-07 21:51:23 +01:00
|
|
|
|
2008-04-02 18:37:07 +01:00
|
|
|
if (tsize < MinTrailSpace)
|
|
|
|
tsize = MinTrailSpace;
|
|
|
|
if (ssize < MinStackSpace)
|
|
|
|
ssize = MinStackSpace;
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).ssize = ssize;
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).tsize = tsize;
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).sysize = sysize;
|
2013-09-17 00:26:37 +01:00
|
|
|
|
|
|
|
if ((REGSTORE *)pthread_getspecific(Yap_yaamregs_key)) {
|
|
|
|
REMOTE_c_input_stream(new_worker_id) = LOCAL_c_input_stream;
|
|
|
|
REMOTE_c_output_stream(new_worker_id) = LOCAL_c_output_stream;
|
|
|
|
REMOTE_c_error_stream(new_worker_id) = LOCAL_c_error_stream;
|
|
|
|
} else {
|
|
|
|
// thread is created by a thread that has never run Prolog
|
|
|
|
REMOTE_c_input_stream(new_worker_id) = REMOTE_c_input_stream(0);
|
|
|
|
REMOTE_c_output_stream(new_worker_id) = REMOTE_c_output_stream(0);
|
|
|
|
REMOTE_c_error_stream(new_worker_id) = REMOTE_c_error_stream(0);
|
|
|
|
}
|
2008-04-02 18:37:07 +01:00
|
|
|
pm = (ssize + tsize)*1024;
|
2011-05-09 20:19:49 +01:00
|
|
|
if (!(REMOTE_ThreadHandle(new_worker_id).stack_address = malloc(pm))) {
|
2008-04-02 18:37:07 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).tgoal =
|
2013-09-17 00:26:37 +01:00
|
|
|
Yap_StoreTermInDB(Deref(*tpgoal), 7);
|
2013-09-28 23:20:39 +01:00
|
|
|
|
|
|
|
if (CurrentModule) {
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).cmod =
|
|
|
|
CurrentModule;
|
|
|
|
} else {
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).cmod = USER_MODULE;
|
|
|
|
}
|
2010-01-31 23:03:55 +00:00
|
|
|
tdetach = Deref(*tpdetach);
|
2004-02-19 19:24:46 +00:00
|
|
|
if (IsVarTerm(tdetach)){
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).tdetach =
|
2004-02-19 19:24:46 +00:00
|
|
|
MkAtomTerm(AtomFalse);
|
|
|
|
} else {
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).tdetach =
|
2004-02-19 19:24:46 +00:00
|
|
|
tdetach;
|
|
|
|
}
|
2010-01-31 23:03:55 +00:00
|
|
|
tgoal = Yap_StripModule(Deref(*tpexit), &tmod);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).texit_mod = tmod;
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).texit =
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_StoreTermInDB(tgoal,7);
|
2011-11-30 13:03:22 +00:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).local_preds =
|
|
|
|
NULL;
|
2011-12-01 11:05:27 +00:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).start_of_timesp =
|
|
|
|
NULL;
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).last_timep =
|
|
|
|
NULL;
|
|
|
|
REMOTE_ScratchPad(new_worker_id).ptr =
|
|
|
|
NULL;
|
2008-04-02 18:37:07 +01:00
|
|
|
return TRUE;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-22 22:32:23 +01:00
|
|
|
static void
|
2008-08-06 18:32:22 +01:00
|
|
|
kill_thread_engine (int wid, int always_die)
|
2004-07-22 22:32:23 +01:00
|
|
|
{
|
2011-05-09 20:19:49 +01:00
|
|
|
Prop p0 = AbsPredProp(REMOTE_ThreadHandle(wid).local_preds);
|
2011-12-01 11:05:27 +00:00
|
|
|
GlobalEntry *gl = REMOTE_GlobalVariables(wid);
|
2004-12-02 06:06:47 +00:00
|
|
|
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).local_preds = NIL;
|
2011-12-01 11:05:27 +00:00
|
|
|
REMOTE_GlobalVariables(wid) = NULL;
|
2004-07-22 22:32:23 +01:00
|
|
|
/* kill all thread local preds */
|
|
|
|
while(p0) {
|
|
|
|
PredEntry *ap = RepPredProp(p0);
|
|
|
|
p0 = ap->NextOfPE;
|
|
|
|
Yap_Abolish(ap);
|
|
|
|
Yap_FreeCodeSpace((char *)ap);
|
|
|
|
}
|
2009-05-17 18:38:39 +01:00
|
|
|
while (gl) {
|
|
|
|
gl->global = TermFoundVar;
|
|
|
|
gl = gl->NextGE;
|
|
|
|
}
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_KillStacks(wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ActiveSignals(wid) = 0L;
|
2011-12-01 11:05:27 +00:00
|
|
|
if (REMOTE_ScratchPad(wid).ptr)
|
|
|
|
free(REMOTE_ScratchPad(wid).ptr);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).current_yaam_regs = NULL;
|
2011-12-01 11:05:27 +00:00
|
|
|
if (REMOTE_ThreadHandle(wid).start_of_timesp)
|
|
|
|
free(REMOTE_ThreadHandle(wid).start_of_timesp);
|
|
|
|
if (REMOTE_ThreadHandle(wid).last_timep)
|
|
|
|
free(REMOTE_ThreadHandle(wid).last_timep);
|
|
|
|
if (REMOTE_ThreadHandle(wid).texit) {
|
|
|
|
Yap_FreeCodeSpace((ADDR)REMOTE_ThreadHandle(wid).texit);
|
|
|
|
}
|
|
|
|
/* FreeCodeSpace requires LOCAL requires yaam_regs */
|
|
|
|
free(REMOTE_ThreadHandle(wid).default_yaam_regs);
|
|
|
|
REMOTE_ThreadHandle(wid).default_yaam_regs = NULL;
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2011-05-09 20:19:49 +01:00
|
|
|
if (REMOTE_ThreadHandle(wid).tdetach == MkAtomTerm(AtomTrue) ||
|
2008-08-06 18:32:22 +01:00
|
|
|
always_die) {
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).zombie = FALSE;
|
|
|
|
REMOTE_ThreadHandle(wid).in_use = FALSE;
|
2008-08-06 18:32:22 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(1, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2008-08-06 18:32:22 +01:00
|
|
|
}
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static void
|
2004-02-21 20:25:45 +00:00
|
|
|
thread_die(int wid, int always_die)
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef TABLING
|
|
|
|
CACHE_REGS
|
|
|
|
tab_ent_ptr tab_ent;
|
|
|
|
|
|
|
|
tab_ent = GLOBAL_root_tab_ent;
|
|
|
|
while (tab_ent) {
|
|
|
|
abolish_table(tab_ent);
|
|
|
|
tab_ent = TabEnt_next(tab_ent);
|
|
|
|
}
|
|
|
|
FREE_DEPENDENCY_FRAME(LOCAL_top_dep_fr);
|
2012-05-14 19:11:58 +01:00
|
|
|
LOCAL_top_dep_fr = NULL;
|
|
|
|
#ifdef USE_PAGES_MALLOC
|
|
|
|
DETACH_PAGES(_pages_void);
|
2012-05-22 14:47:47 +01:00
|
|
|
#endif /* USE_PAGES_MALLOC */
|
2012-05-14 19:11:58 +01:00
|
|
|
DETACH_PAGES(_pages_tab_ent);
|
|
|
|
#if defined(THREADS_FULL_SHARING) || defined(THREADS_CONSUMER_SHARING)
|
|
|
|
DETACH_PAGES(_pages_sg_ent);
|
|
|
|
#endif /* THREADS_FULL_SHARING || THREADS_CONSUMER_SHARING */
|
|
|
|
DETACH_PAGES(_pages_sg_fr);
|
|
|
|
DETACH_PAGES(_pages_dep_fr);
|
|
|
|
DETACH_PAGES(_pages_sg_node);
|
|
|
|
DETACH_PAGES(_pages_sg_hash);
|
|
|
|
DETACH_PAGES(_pages_ans_node);
|
|
|
|
DETACH_PAGES(_pages_ans_hash);
|
|
|
|
#if defined(THREADS_FULL_SHARING)
|
|
|
|
DETACH_PAGES(_pages_ans_ref_node);
|
|
|
|
#endif /* THREADS_FULL_SHARING */
|
|
|
|
DETACH_PAGES(_pages_gt_node);
|
|
|
|
DETACH_PAGES(_pages_gt_hash);
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef OUTPUT_THREADS_TABLING
|
|
|
|
fclose(LOCAL_thread_output);
|
|
|
|
#endif /* OUTPUT_THREADS_TABLING */
|
2012-05-14 19:11:58 +01:00
|
|
|
#endif /* TABLING */
|
|
|
|
GLOBAL_NOfThreads--;
|
2004-03-02 16:44:58 +00:00
|
|
|
if (!always_die) {
|
|
|
|
/* called by thread itself */
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_ThreadsTotalTime += Yap_cputime();
|
2004-03-02 16:44:58 +00:00
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
kill_thread_engine(wid, always_die);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2011-12-01 11:05:27 +00:00
|
|
|
static int
|
2010-07-30 12:08:00 +01:00
|
|
|
setup_engine(int myworker_id, int init_thread)
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-12-02 06:06:47 +00:00
|
|
|
REGSTORE *standard_regs;
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2008-05-11 00:24:13 +01:00
|
|
|
standard_regs = (REGSTORE *)calloc(1,sizeof(REGSTORE));
|
2011-12-01 11:05:27 +00:00
|
|
|
if (!standard_regs)
|
|
|
|
return FALSE;
|
2011-03-10 11:05:39 +00:00
|
|
|
regcache = standard_regs;
|
2004-01-23 02:23:51 +00:00
|
|
|
/* create the YAAM descriptor */
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(myworker_id).default_yaam_regs = standard_regs;
|
2012-12-13 18:12:50 +00:00
|
|
|
Yap_InitExStacks(myworker_id, REMOTE_ThreadHandle(myworker_id).tsize, REMOTE_ThreadHandle(myworker_id).ssize);
|
2011-05-09 20:19:49 +01:00
|
|
|
CurrentModule = REMOTE_ThreadHandle(myworker_id).cmod;
|
2012-12-11 22:07:10 +00:00
|
|
|
Yap_InitTime( myworker_id );
|
|
|
|
Yap_InitYaamRegs( myworker_id );
|
2012-12-13 18:12:50 +00:00
|
|
|
REFRESH_CACHE_REGS
|
2004-12-02 06:06:47 +00:00
|
|
|
Yap_ReleasePreAllocCodeSpace(Yap_PreAllocCodeSpace());
|
2004-07-22 22:32:23 +01:00
|
|
|
/* I exist */
|
2011-05-10 10:06:51 +01:00
|
|
|
GLOBAL_NOfThreadsCreated++;
|
2012-05-14 19:11:58 +01:00
|
|
|
GLOBAL_NOfThreads++;
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(2, myworker_id);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(myworker_id).tlock));
|
2011-07-26 16:49:49 +01:00
|
|
|
#ifdef TABLING
|
2012-12-11 22:07:10 +00:00
|
|
|
new_dependency_frame(REMOTE_top_dep_fr(myworker_id), FALSE, NULL, NULL, B, NULL, FALSE, NULL); /* same as in Yap_init_root_frames() */
|
2011-07-26 16:49:49 +01:00
|
|
|
#endif /* TABLING */
|
2011-12-01 11:05:27 +00:00
|
|
|
return TRUE;
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
start_thread(int myworker_id)
|
|
|
|
{
|
2012-12-13 18:12:50 +00:00
|
|
|
CACHE_REGS
|
|
|
|
pthread_setspecific(Yap_yaamregs_key, (void *)REMOTE_ThreadHandle(myworker_id).default_yaam_regs);
|
|
|
|
REFRESH_CACHE_REGS;
|
|
|
|
worker_id = myworker_id;
|
|
|
|
LOCAL = REMOTE(myworker_id);
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
thread_run(void *widp)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-01-27 11:01:07 +00:00
|
|
|
Term tgoal, t;
|
2004-07-22 22:32:23 +01:00
|
|
|
Term tgs[2];
|
|
|
|
int myworker_id = *((int *)widp);
|
2011-12-22 16:50:20 +00:00
|
|
|
#ifdef OUTPUT_THREADS_TABLING
|
|
|
|
char thread_name[25];
|
|
|
|
char filename[YAP_FILENAME_MAX];
|
2012-12-13 18:12:50 +00:00
|
|
|
|
2011-12-22 16:50:20 +00:00
|
|
|
sprintf(thread_name, "/thread_output_%d", myworker_id);
|
|
|
|
strcpy(filename, YAP_BINDIR);
|
|
|
|
strncat(filename, thread_name, 25);
|
2012-12-13 18:12:50 +00:00
|
|
|
REMOTE_thread_output(myworker_id) = fopen(filename, "w");
|
2011-12-22 16:50:20 +00:00
|
|
|
#endif /* OUTPUT_THREADS_TABLING */
|
2004-07-22 22:32:23 +01:00
|
|
|
start_thread(myworker_id);
|
2012-12-13 18:12:50 +00:00
|
|
|
REFRESH_CACHE_REGS;
|
2008-01-27 11:01:07 +00:00
|
|
|
do {
|
2011-05-09 19:55:06 +01:00
|
|
|
t = tgs[0] = Yap_PopTermFromDB(LOCAL_ThreadHandle.tgoal);
|
2008-01-27 11:01:07 +00:00
|
|
|
if (t == 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE == OUT_OF_ATTVARS_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2008-01-27 11:01:07 +00:00
|
|
|
if (!Yap_growglobal(NULL)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, TermNil, LOCAL_ErrorMessage);
|
2008-01-27 11:01:07 +00:00
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2011-05-09 19:55:06 +01:00
|
|
|
if (!Yap_growstack(LOCAL_ThreadHandle.tgoal->NOfCells*CellSize)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, LOCAL_ErrorMessage);
|
2008-01-27 11:01:07 +00:00
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t == 0);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(myworker_id).tgoal = NULL;
|
2011-05-09 19:55:06 +01:00
|
|
|
tgs[1] = LOCAL_ThreadHandle.tdetach;
|
2004-02-11 01:20:56 +00:00
|
|
|
tgoal = Yap_MkApplTerm(FunctorThreadRun, 2, tgs);
|
2006-05-25 17:57:21 +01:00
|
|
|
Yap_RunTopGoal(tgoal);
|
2004-02-21 20:25:45 +00:00
|
|
|
thread_die(worker_id, FALSE);
|
2004-01-23 02:23:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_new_tid( USES_REGS1 )
|
2004-02-11 01:20:56 +00:00
|
|
|
{
|
2008-03-24 23:48:47 +00:00
|
|
|
int new_worker = allocate_new_tid();
|
|
|
|
if (new_worker == -1) {
|
2008-04-02 16:41:50 +01:00
|
|
|
Yap_Error(RESOURCE_ERROR_MAX_THREADS, MkIntegerTerm(MAX_THREADS), "");
|
2008-03-24 23:48:47 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return Yap_unify(MkIntegerTerm(new_worker), ARG1);
|
2004-02-11 01:20:56 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 18:37:07 +01:00
|
|
|
static int
|
2010-01-31 23:03:55 +00:00
|
|
|
init_thread_engine(int new_worker_id, UInt ssize, UInt tsize, UInt sysize, Term *tgoal, Term *tdetach, Term *texit)
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2008-08-07 21:51:23 +01:00
|
|
|
return store_specs(new_worker_id, ssize, tsize, sysize, tgoal, tdetach, texit);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2004-07-15 16:47:08 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_create_thread( USES_REGS1 )
|
2004-07-15 16:47:08 +01:00
|
|
|
{
|
2008-04-02 18:37:07 +01:00
|
|
|
UInt ssize;
|
|
|
|
UInt tsize;
|
2008-08-07 21:51:23 +01:00
|
|
|
UInt sysize;
|
2008-04-02 18:37:07 +01:00
|
|
|
Term x2 = Deref(ARG2);
|
|
|
|
Term x3 = Deref(ARG3);
|
2008-08-07 21:51:23 +01:00
|
|
|
Term x4 = Deref(ARG4);
|
2012-12-13 18:12:50 +00:00
|
|
|
int new_worker_id = IntegerOfTerm(Deref(ARG7)),
|
|
|
|
owid = worker_id;
|
2004-07-15 16:47:08 +01:00
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
// fprintf(stderr," %d --> %d\n", worker_id, new_worker_id);
|
2008-04-02 18:37:07 +01:00
|
|
|
if (IsBigIntTerm(x2))
|
|
|
|
return FALSE;
|
|
|
|
if (IsBigIntTerm(x3))
|
|
|
|
return FALSE;
|
|
|
|
ssize = IntegerOfTerm(x2);
|
|
|
|
tsize = IntegerOfTerm(x3);
|
2008-08-07 21:51:23 +01:00
|
|
|
sysize = IntegerOfTerm(x4);
|
2008-04-02 18:37:07 +01:00
|
|
|
/* UInt systemsize = IntegerOfTerm(Deref(ARG4)); */
|
2004-07-15 16:47:08 +01:00
|
|
|
if (new_worker_id == -1) {
|
|
|
|
/* YAP ERROR */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
/* make sure we can proceed */
|
2010-01-31 23:03:55 +00:00
|
|
|
if (!init_thread_engine(new_worker_id, ssize, tsize, sysize, &ARG1, &ARG5, &ARG6))
|
2008-04-02 18:37:07 +01:00
|
|
|
return FALSE;
|
2012-06-29 21:37:31 +01:00
|
|
|
//REMOTE_ThreadHandle(new_worker_id).pthread_handle = 0L;
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_worker_id).id = new_worker_id;
|
|
|
|
REMOTE_ThreadHandle(new_worker_id).ref_count = 1;
|
2012-12-13 18:12:50 +00:00
|
|
|
setup_engine(new_worker_id, FALSE);
|
2011-05-09 20:19:49 +01:00
|
|
|
if ((REMOTE_ThreadHandle(new_worker_id).ret = pthread_create(&REMOTE_ThreadHandle(new_worker_id).pthread_handle, NULL, thread_run, (void *)(&(REMOTE_ThreadHandle(new_worker_id).id)))) == 0) {
|
2012-12-13 18:12:50 +00:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, (const void *)REMOTE_ThreadHandle(owid).current_yaam_regs);
|
2004-12-02 06:06:47 +00:00
|
|
|
/* wait until the client is initialised */
|
2004-07-15 16:47:08 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-12-13 18:12:50 +00:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, (const void *)REMOTE_ThreadHandle(owid).current_yaam_regs);
|
2004-07-15 16:47:08 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-05-25 17:28:28 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_sleep( USES_REGS1 )
|
2006-05-25 17:28:28 +01:00
|
|
|
{
|
|
|
|
UInt time = IntegerOfTerm(Deref(ARG1));
|
|
|
|
#if HAVE_NANOSLEEP
|
|
|
|
UInt ntime = IntegerOfTerm(Deref(ARG2));
|
|
|
|
struct timespec req, oreq ;
|
|
|
|
req.tv_sec = time;
|
|
|
|
req.tv_nsec = ntime;
|
2006-12-31 16:10:29 +00:00
|
|
|
if (nanosleep(&req, &oreq)) {
|
2006-05-25 17:28:28 +01:00
|
|
|
#if HAVE_STRERROR
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in thread_sleep/1", strerror(errno));
|
2006-05-25 17:28:28 +01:00
|
|
|
#else
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "error %d in thread_sleep/1", errno);
|
2006-05-25 17:28:28 +01:00
|
|
|
#endif
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG3,MkIntegerTerm(oreq.tv_sec)) &&
|
|
|
|
Yap_unify(ARG4,MkIntegerTerm(oreq.tv_nsec));
|
|
|
|
#elif HAVE_SLEEP
|
|
|
|
UInt rtime;
|
|
|
|
if ((rtime = sleep(time)) < 0) {
|
|
|
|
#if HAVE_STRERROR
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "%s in thread_sleep/1", strerror(errno));
|
2006-05-25 17:28:28 +01:00
|
|
|
#else
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "error %d in thread_sleep/1", errno);
|
2006-05-25 17:28:28 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG3,MkIntegerTerm(rtime)) &&
|
|
|
|
Yap_unify(ARG4,MkIntTerm(0L));
|
|
|
|
#else
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_Error(OPERATING_SYSTEM_ERROR, ARG1, "no support for thread_sleep/1 in this YAP configuration");
|
2006-05-25 17:28:28 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_self( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-08-11 17:14:55 +01:00
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return Yap_unify(MkIntegerTerm(-1), ARG1);
|
2004-01-23 02:23:51 +00:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
2008-01-27 11:01:07 +00:00
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_zombie_self( USES_REGS1 )
|
2008-01-27 11:01:07 +00:00
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return Yap_unify(MkIntegerTerm(-1), ARG1);
|
2011-05-09 19:55:06 +01:00
|
|
|
pthread_mutex_lock(&(LOCAL_ThreadHandle.tlock));
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(4, worker_id);
|
2011-05-04 10:11:41 +01:00
|
|
|
if (LOCAL_ActiveSignals &= YAP_ITI_SIGNAL) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(5, worker_id);
|
2011-05-09 19:55:06 +01:00
|
|
|
pthread_mutex_unlock(&(LOCAL_ThreadHandle.tlock));
|
2008-01-27 11:01:07 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
// fprintf(stderr," -- %d\n", worker_id);
|
2011-05-09 19:55:06 +01:00
|
|
|
LOCAL_ThreadHandle.in_use = FALSE;
|
|
|
|
LOCAL_ThreadHandle.zombie = TRUE;
|
2008-08-06 18:32:22 +01:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_status_lock( USES_REGS1 )
|
2008-08-06 18:32:22 +01:00
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return FALSE;
|
2011-05-09 19:55:06 +01:00
|
|
|
pthread_mutex_lock(&(LOCAL_ThreadHandle.tlock_status));
|
2008-08-06 18:32:22 +01:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_status_unlock( USES_REGS1 )
|
2008-08-06 18:32:22 +01:00
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return FALSE;
|
2011-05-09 19:55:06 +01:00
|
|
|
pthread_mutex_unlock(&(LOCAL_ThreadHandle.tlock_status));
|
2008-01-27 11:01:07 +00:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
Int
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_self(void)
|
2004-07-15 16:47:08 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-08-11 17:14:55 +01:00
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return -1;
|
2004-07-15 16:47:08 +01:00
|
|
|
return worker_id;
|
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
CELL
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_create_engine(thread_attr *ops)
|
|
|
|
{
|
2010-06-14 09:27:48 +01:00
|
|
|
thread_attr opsv;
|
2004-07-22 22:32:23 +01:00
|
|
|
int new_id = allocate_new_tid();
|
2010-07-23 12:07:33 +01:00
|
|
|
Term t = TermNil;
|
|
|
|
|
|
|
|
/*
|
2012-12-11 22:07:10 +00:00
|
|
|
ok, this creates a problem, because we are initializing an engine from
|
|
|
|
some "empty" thread.
|
|
|
|
We need first to fool the thread into believing it is the main thread
|
2010-07-23 12:07:33 +01:00
|
|
|
*/
|
2004-07-22 22:32:23 +01:00
|
|
|
if (new_id == -1) {
|
|
|
|
/* YAP ERROR */
|
2010-07-23 15:54:13 +01:00
|
|
|
return -1;
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
2010-06-14 09:27:48 +01:00
|
|
|
if (ops == NULL) {
|
|
|
|
ops = &opsv;
|
|
|
|
ops->tsize = DefHeapSpace;
|
|
|
|
ops->ssize = DefStackSpace;
|
|
|
|
ops->sysize = 0;
|
|
|
|
ops->egoal = t;
|
|
|
|
}
|
2012-06-29 21:37:31 +01:00
|
|
|
if (!pthread_equal(pthread_self() , GLOBAL_master_thread) ) {
|
2010-07-23 12:07:33 +01:00
|
|
|
/* we are worker_id 0 for now, lock master thread so that no one messes with us */
|
2010-07-25 11:21:15 +01:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, (const void *)&Yap_standard_regs);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(0).tlock));
|
2010-07-23 12:07:33 +01:00
|
|
|
}
|
2010-01-31 23:03:55 +00:00
|
|
|
if (!init_thread_engine(new_id, ops->ssize, ops->tsize, ops->sysize, &t, &t, &(ops->egoal)))
|
2010-07-23 15:54:13 +01:00
|
|
|
return -1;
|
2012-06-29 21:37:31 +01:00
|
|
|
//REMOTE_ThreadHandle(new_id).pthread_handle = 0L;
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(new_id).id = new_id;
|
|
|
|
REMOTE_ThreadHandle(new_id).ref_count = 0;
|
2011-12-01 11:05:27 +00:00
|
|
|
if (!setup_engine(new_id, FALSE))
|
|
|
|
return -1;
|
2012-06-29 21:37:31 +01:00
|
|
|
if (!pthread_equal(pthread_self(), GLOBAL_master_thread)) {
|
2010-07-25 11:21:15 +01:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, NULL);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(0).tlock));
|
2010-07-23 12:07:33 +01:00
|
|
|
}
|
|
|
|
return new_id;
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
Int
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_attach_engine(int wid)
|
|
|
|
{
|
2012-12-13 18:12:50 +00:00
|
|
|
CACHE_REGS
|
2010-07-25 11:21:15 +01:00
|
|
|
/*
|
|
|
|
already locked
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(wid).tlock));
|
2010-07-25 11:21:15 +01:00
|
|
|
*/
|
2011-05-09 20:19:49 +01:00
|
|
|
if (REMOTE_ThreadHandle(wid).ref_count ) {
|
|
|
|
REMOTE_ThreadHandle(wid).ref_count++;
|
|
|
|
REMOTE_ThreadHandle(wid).pthread_handle = pthread_self();
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(8, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2010-07-23 15:54:13 +01:00
|
|
|
return TRUE;
|
2004-08-11 17:14:55 +01:00
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).pthread_handle = pthread_self();
|
|
|
|
REMOTE_ThreadHandle(wid).ref_count++;
|
2012-12-13 18:12:50 +00:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, (const void *)REMOTE_ThreadHandle(wid).current_yaam_regs);
|
|
|
|
REFRESH_CACHE_REGS;
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(9, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2004-07-22 22:32:23 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
Int
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_detach_engine(int wid)
|
|
|
|
{
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(wid).tlock));
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(10, wid);
|
2012-06-29 21:37:31 +01:00
|
|
|
//REMOTE_ThreadHandle(wid).pthread_handle = 0;
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(wid).ref_count--;
|
2010-07-25 11:21:15 +01:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, NULL);
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(11, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2004-07-22 22:32:23 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
Int
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_destroy_engine(int wid)
|
|
|
|
{
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(wid).tlock));
|
2012-10-19 09:28:13 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(10, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
if (REMOTE_ThreadHandle(wid).ref_count == 0) {
|
2008-08-06 18:32:22 +01:00
|
|
|
kill_thread_engine(wid, TRUE);
|
2004-07-22 22:32:23 +01:00
|
|
|
return TRUE;
|
|
|
|
} else {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(12, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2004-07-22 22:32:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-11 17:14:55 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_join( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-19 19:24:46 +00:00
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2011-05-09 20:19:49 +01:00
|
|
|
if (!REMOTE_ThreadHandle(tid).in_use &&
|
|
|
|
!REMOTE_ThreadHandle(tid).zombie) {
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-02-19 19:24:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
if (!REMOTE_ThreadHandle(tid).tdetach == MkAtomTerm(AtomTrue)) {
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-02-19 19:24:46 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-12-02 06:06:47 +00:00
|
|
|
/* make sure this lock is accessible */
|
2011-05-09 20:19:49 +01:00
|
|
|
if (pthread_join(REMOTE_ThreadHandle(tid).pthread_handle, NULL) < 0) {
|
2004-01-23 02:23:51 +00:00
|
|
|
/* ERROR */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-01-27 11:01:07 +00:00
|
|
|
/* notice mutex is already locked */
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-02-19 19:24:46 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_destroy( USES_REGS1 )
|
2004-02-19 19:24:46 +00:00
|
|
|
{
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(tid).zombie = FALSE;
|
|
|
|
REMOTE_ThreadHandle(tid).in_use = FALSE;
|
2008-08-06 18:32:22 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(32, tid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(tid).tlock));
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-02-19 19:24:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_detach( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2008-03-26 14:41:45 +00:00
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(tid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(14, tid);
|
2011-05-09 20:19:49 +01:00
|
|
|
if (pthread_detach(REMOTE_ThreadHandle(tid).pthread_handle) < 0) {
|
2004-01-23 02:23:51 +00:00
|
|
|
/* ERROR */
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(15, tid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(tid).tlock));
|
2004-01-23 02:23:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
REMOTE_ThreadHandle(tid).tdetach =
|
2008-03-26 14:41:45 +00:00
|
|
|
MkAtomTerm(AtomTrue);
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(30, tid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(tid).tlock));
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_detached( USES_REGS1 )
|
2008-08-06 18:32:22 +01:00
|
|
|
{
|
2011-05-09 19:55:06 +01:00
|
|
|
if (LOCAL_ThreadHandle.tdetach)
|
|
|
|
return Yap_unify(ARG1,LOCAL_ThreadHandle.tdetach);
|
2008-08-08 17:05:10 +01:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_detached2( USES_REGS1 )
|
2008-08-08 17:05:10 +01:00
|
|
|
{
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
2011-05-09 20:19:49 +01:00
|
|
|
if (REMOTE_ThreadHandle(tid).tdetach)
|
|
|
|
return Yap_unify(ARG2,REMOTE_ThreadHandle(tid).tdetach);
|
2008-08-08 17:05:10 +01:00
|
|
|
else
|
|
|
|
return FALSE;
|
2008-08-06 18:32:22 +01:00
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_exit( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2008-01-27 11:01:07 +00:00
|
|
|
thread_die(worker_id, FALSE);
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_exit(NULL);
|
2008-08-08 15:05:34 +01:00
|
|
|
/* done, just make gcc happy */
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_set_concurrency( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
|
|
|
Term tnew = Deref(ARG2);
|
|
|
|
int newc, cur;
|
|
|
|
|
|
|
|
if (IsVarTerm(tnew)) {
|
|
|
|
newc = 0;
|
|
|
|
} else if (IsIntegerTerm(tnew)) {
|
|
|
|
newc = IntegerOfTerm(tnew);
|
|
|
|
} else {
|
|
|
|
Yap_Error(TYPE_ERROR_INTEGER,tnew,"thread_set_concurrency/2");
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
cur = MkIntegerTerm(pthread_getconcurrency());
|
|
|
|
if (pthread_setconcurrency(newc) != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG1, MkIntegerTerm(cur));
|
|
|
|
}
|
|
|
|
|
2006-05-04 19:46:50 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_yield( USES_REGS1 )
|
2006-05-04 19:46:50 +01:00
|
|
|
{
|
|
|
|
if (sched_yield() != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_valid_thread( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(Deref(ARG1));
|
2011-05-09 20:19:49 +01:00
|
|
|
return REMOTE_ThreadHandle(i).in_use || REMOTE_ThreadHandle(i).zombie;
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mutex Support */
|
|
|
|
|
|
|
|
typedef struct swi_mutex {
|
|
|
|
UInt owners;
|
|
|
|
Int tid_own;
|
|
|
|
pthread_mutex_t m;
|
|
|
|
} SWIMutex;
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_new_mutex( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
|
|
|
SWIMutex* mutp;
|
|
|
|
pthread_mutexattr_t mat;
|
2012-06-29 21:37:31 +01:00
|
|
|
#if defined(HAVE_PTHREAD_MUTEXATTR_SETKIND_NP) && !defined(__MINGW32__)
|
2011-06-06 10:32:58 +01:00
|
|
|
extern int pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind);
|
|
|
|
#endif
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
mutp = (SWIMutex *)Yap_AllocCodeSpace(sizeof(SWIMutex));
|
|
|
|
if (mutp == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pthread_mutexattr_init(&mat);
|
|
|
|
#ifdef HAVE_PTHREAD_MUTEXATTR_SETKIND_NP
|
|
|
|
pthread_mutexattr_setkind_np(&mat, PTHREAD_MUTEX_RECURSIVE_NP);
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_PTHREAD_MUTEXATTR_SETTYPE
|
|
|
|
pthread_mutexattr_settype(&mat, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
pthread_mutex_init(&mutp->m, &mat);
|
|
|
|
mutp->owners = 0;
|
|
|
|
mutp->tid_own = 0;
|
|
|
|
return Yap_unify(ARG1, MkIntegerTerm((Int)mutp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_destroy_mutex( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_destroy(&mut->m) < 0)
|
|
|
|
return FALSE;
|
|
|
|
Yap_FreeCodeSpace((void *)mut);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_lock_mutex( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_lock(&mut->m) < 0)
|
|
|
|
return FALSE;
|
|
|
|
mut->owners++;
|
|
|
|
mut->tid_own = worker_id;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_trylock_mutex( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_trylock(&mut->m) == EBUSY)
|
|
|
|
return FALSE;
|
|
|
|
mut->owners++;
|
|
|
|
mut->tid_own = worker_id;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_unlock_mutex( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_mutex_unlock(&mut->m) < 0)
|
|
|
|
return FALSE;
|
|
|
|
mut->owners--;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_mutex_info( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
return Yap_unify(ARG2, MkIntegerTerm(mut->owners)) &&
|
2007-03-16 21:58:45 +00:00
|
|
|
Yap_unify(ARG3, MkIntegerTerm(mut->tid_own));
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_cond_create( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
|
|
|
pthread_cond_t* condp;
|
|
|
|
|
2004-02-05 16:57:02 +00:00
|
|
|
condp = (pthread_cond_t *)Yap_AllocCodeSpace(sizeof(pthread_cond_t));
|
2004-01-23 02:23:51 +00:00
|
|
|
if (condp == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
pthread_cond_init(condp, NULL);
|
|
|
|
return Yap_unify(ARG1, MkIntegerTerm((Int)condp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_cond_destroy( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_cond_destroy(condp) < 0)
|
|
|
|
return FALSE;
|
|
|
|
Yap_FreeCodeSpace((void *)condp);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_cond_signal( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_cond_signal(condp) < 0)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_cond_broadcast( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
if (pthread_cond_broadcast(condp) < 0)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_cond_wait( USES_REGS1 )
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_cond_t *condp = (pthread_cond_t *)IntegerOfTerm(Deref(ARG1));
|
|
|
|
SWIMutex *mut = (SWIMutex*)IntegerOfTerm(Deref(ARG2));
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
pthread_cond_wait(condp, &mut->m);
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-08-07 21:51:23 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_stacks( USES_REGS1 )
|
2008-08-07 21:51:23 +01:00
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
Int status= TRUE;
|
|
|
|
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2011-12-22 16:50:20 +00:00
|
|
|
if (!Yap_local[tid] ||
|
|
|
|
(!REMOTE_ThreadHandle(tid).in_use && !REMOTE_ThreadHandle(tid).zombie)) {
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2008-08-07 21:51:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
status &= Yap_unify(ARG2,MkIntegerTerm(REMOTE_ThreadHandle(tid).ssize));
|
|
|
|
status &= Yap_unify(ARG3,MkIntegerTerm(REMOTE_ThreadHandle(tid).tsize));
|
|
|
|
status &= Yap_unify(ARG4,MkIntegerTerm(REMOTE_ThreadHandle(tid).sysize));
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2008-08-07 21:51:23 +01:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_atexit( USES_REGS1 )
|
2008-08-07 21:51:23 +01:00
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Term t;
|
|
|
|
|
2011-05-09 19:55:06 +01:00
|
|
|
if (!LOCAL_ThreadHandle.texit ||
|
|
|
|
LOCAL_ThreadHandle.texit->Entry == MkAtomTerm(AtomTrue)) {
|
2008-08-07 21:51:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
do {
|
2011-05-09 19:55:06 +01:00
|
|
|
t = Yap_PopTermFromDB(LOCAL_ThreadHandle.texit);
|
|
|
|
LOCAL_ThreadHandle.texit = NULL;
|
2008-08-07 21:51:23 +01:00
|
|
|
if (t == 0) {
|
2011-05-23 16:19:47 +01:00
|
|
|
if (LOCAL_Error_TYPE == OUT_OF_ATTVARS_ERROR) {
|
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2008-08-07 21:51:23 +01:00
|
|
|
if (!Yap_growglobal(NULL)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, TermNil, LOCAL_ErrorMessage);
|
2008-08-07 21:51:23 +01:00
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
LOCAL_Error_TYPE = YAP_NO_ERROR;
|
2011-05-09 19:55:06 +01:00
|
|
|
if (!Yap_growstack(LOCAL_ThreadHandle.tgoal->NOfCells*CellSize)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, LOCAL_ErrorMessage);
|
2008-08-07 21:51:23 +01:00
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t == 0);
|
2011-05-09 19:55:06 +01:00
|
|
|
return Yap_unify(ARG1, t) && Yap_unify(ARG2, LOCAL_ThreadHandle.texit_mod);
|
2008-08-07 21:51:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-05 16:57:02 +00:00
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_signal( USES_REGS1 )
|
2004-02-05 16:57:02 +00:00
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Int wid = IntegerOfTerm(Deref(ARG1));
|
2004-02-11 13:59:53 +00:00
|
|
|
/* make sure the lock is available */
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_lock(&(REMOTE_ThreadHandle(wid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(16, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
if (!REMOTE_ThreadHandle(wid).in_use ||
|
|
|
|
!REMOTE_ThreadHandle(wid).current_yaam_regs) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(17, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
return TRUE;
|
2004-02-17 19:29:24 +00:00
|
|
|
}
|
2011-05-09 20:19:49 +01:00
|
|
|
LOCK(REMOTE_SignalLock(wid));
|
|
|
|
REMOTE_ThreadHandle(wid).current_yaam_regs->CreepFlag_ =
|
|
|
|
Unsigned(REMOTE_ThreadHandle(wid).current_yaam_regs->LCL0_);
|
|
|
|
REMOTE_ActiveSignals(wid) |= YAP_ITI_SIGNAL;
|
|
|
|
UNLOCK(REMOTE_SignalLock(wid));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(18, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2004-02-06 17:22:24 +00:00
|
|
|
return TRUE;
|
2004-02-05 16:57:02 +00:00
|
|
|
}
|
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_no_threads( USES_REGS1 )
|
2004-02-11 01:20:56 +00:00
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_nof_threads( USES_REGS1 )
|
2004-03-02 16:44:58 +00:00
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
int i = 0, wid;
|
2011-05-10 10:06:51 +01:00
|
|
|
LOCK(GLOBAL_ThreadHandlesLock);
|
2008-04-02 16:41:50 +01:00
|
|
|
for (wid = 0; wid < MAX_THREADS; wid++) {
|
2011-05-09 19:36:51 +01:00
|
|
|
if (!Yap_local[wid]) break;
|
2011-05-09 20:19:49 +01:00
|
|
|
if (REMOTE_ThreadHandle(wid).in_use)
|
2004-03-02 16:44:58 +00:00
|
|
|
i++;
|
|
|
|
}
|
2011-05-10 10:06:51 +01:00
|
|
|
UNLOCK(GLOBAL_ThreadHandlesLock);
|
2004-03-02 16:44:58 +00:00
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(i));
|
|
|
|
}
|
|
|
|
|
2008-04-02 23:59:52 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_max_workers( USES_REGS1 )
|
2008-04-02 23:59:52 +01:00
|
|
|
{ /* '$max_workers'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(MAX_WORKERS));
|
|
|
|
}
|
|
|
|
|
2007-01-29 10:18:15 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_max_threads( USES_REGS1 )
|
2007-01-29 10:18:15 +00:00
|
|
|
{ /* '$max_threads'(+P) */
|
2008-04-02 16:41:50 +01:00
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(MAX_THREADS));
|
2007-01-29 10:18:15 +00:00
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_nof_threads_created( USES_REGS1 )
|
2004-03-02 16:44:58 +00:00
|
|
|
{ /* '$nof_threads'(+P) */
|
2011-05-10 10:06:51 +01:00
|
|
|
return Yap_unify(ARG1,MkIntTerm(GLOBAL_NOfThreadsCreated));
|
2004-03-02 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_runtime( USES_REGS1 )
|
2004-03-02 16:44:58 +00:00
|
|
|
{ /* '$thread_runtime'(+P) */
|
2011-05-10 10:06:51 +01:00
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(GLOBAL_ThreadsTotalTime));
|
2004-03-02 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_self_lock( USES_REGS1 )
|
2008-08-06 18:32:22 +01:00
|
|
|
{ /* '$thread_unlock' */
|
2011-05-09 19:55:06 +01:00
|
|
|
pthread_mutex_lock(&(LOCAL_ThreadHandle.tlock));
|
2008-08-06 18:32:22 +01:00
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(worker_id));
|
|
|
|
}
|
|
|
|
|
2008-04-03 14:26:38 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_thread_unlock( USES_REGS1 )
|
2008-08-06 18:32:22 +01:00
|
|
|
{ /* '$thread_unlock' */
|
2008-04-08 16:36:53 +01:00
|
|
|
Int wid = IntegerOfTerm(Deref(ARG1));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(19, wid);
|
2011-05-09 20:19:49 +01:00
|
|
|
pthread_mutex_unlock(&(REMOTE_ThreadHandle(wid).tlock));
|
2008-04-03 14:26:38 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
void Yap_InitThreadPreds(void)
|
|
|
|
{
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$no_threads", 0, p_no_threads, 0);
|
|
|
|
Yap_InitCPred("$max_workers", 1, p_max_workers, 0);
|
|
|
|
Yap_InitCPred("$max_threads", 1, p_max_threads, 0);
|
|
|
|
Yap_InitCPred("$thread_new_tid", 1, p_thread_new_tid, 0);
|
|
|
|
Yap_InitCPred("$create_thread", 7, p_create_thread, 0);
|
|
|
|
Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_status_lock", 1, p_thread_status_lock, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_status_unlock", 1, p_thread_status_unlock, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_zombie_self", 1, p_thread_zombie_self, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_join", 1, p_thread_join, 0);
|
|
|
|
Yap_InitCPred("$thread_destroy", 1, p_thread_destroy, 0);
|
2006-05-04 19:46:50 +01:00
|
|
|
Yap_InitCPred("thread_yield", 0, p_thread_yield, 0);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$detach_thread", 1, p_thread_detach, 0);
|
|
|
|
Yap_InitCPred("$thread_detached", 1, p_thread_detached, 0);
|
|
|
|
Yap_InitCPred("$thread_detached", 2, p_thread_detached2, 0);
|
|
|
|
Yap_InitCPred("$thread_exit", 0, p_thread_exit, 0);
|
2004-02-16 19:22:40 +00:00
|
|
|
Yap_InitCPred("thread_setconcurrency", 2, p_thread_set_concurrency, 0);
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$valid_thread", 1, p_valid_thread, 0);
|
|
|
|
Yap_InitCPred("$new_mutex", 1, p_new_mutex, SafePredFlag);
|
|
|
|
Yap_InitCPred("$destroy_mutex", 1, p_destroy_mutex, SafePredFlag);
|
|
|
|
Yap_InitCPred("$lock_mutex", 1, p_lock_mutex, SafePredFlag);
|
|
|
|
Yap_InitCPred("$trylock_mutex", 1, p_trylock_mutex, SafePredFlag);
|
|
|
|
Yap_InitCPred("$unlock_mutex", 1, p_unlock_mutex, SafePredFlag);
|
|
|
|
Yap_InitCPred("$mutex_info", 3, p_mutex_info, SafePredFlag);
|
|
|
|
Yap_InitCPred("$cond_create", 1, p_cond_create, SafePredFlag);
|
|
|
|
Yap_InitCPred("$cond_destroy", 1, p_cond_destroy, SafePredFlag);
|
|
|
|
Yap_InitCPred("$cond_signal", 1, p_cond_signal, SafePredFlag);
|
|
|
|
Yap_InitCPred("$cond_broadcast", 1, p_cond_broadcast, SafePredFlag);
|
|
|
|
Yap_InitCPred("$cond_wait", 2, p_cond_wait, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_stacks", 4, p_thread_stacks, SafePredFlag);
|
|
|
|
Yap_InitCPred("$signal_thread", 1, p_thread_signal, SafePredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads", 1, p_nof_threads, SafePredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads_created", 1, p_nof_threads_created, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_sleep", 4, p_thread_sleep, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_runtime", 1, p_thread_runtime, SafePredFlag);
|
2008-08-06 18:32:22 +01:00
|
|
|
Yap_InitCPred("$thread_self_lock", 1, p_thread_self_lock, SafePredFlag);
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_InitCPred("$thread_run_at_exit", 2, p_thread_atexit, SafePredFlag);
|
2008-04-08 16:36:53 +01:00
|
|
|
Yap_InitCPred("$thread_unlock", 1, p_thread_unlock, SafePredFlag);
|
2004-02-11 01:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_no_threads(void)
|
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
static Int
|
|
|
|
p_nof_threads(void)
|
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(1));
|
|
|
|
}
|
|
|
|
|
2007-01-29 10:18:15 +00:00
|
|
|
static Int
|
|
|
|
p_max_threads(void)
|
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(1));
|
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
static Int
|
|
|
|
p_nof_threads_created(void)
|
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_runtime(void)
|
|
|
|
{ /* '$thread_runtime'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(0));
|
|
|
|
}
|
|
|
|
|
2008-02-22 15:08:37 +00:00
|
|
|
static Int
|
|
|
|
p_thread_self(void)
|
|
|
|
{ /* '$thread_runtime'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(0));
|
|
|
|
}
|
|
|
|
|
2008-08-19 01:44:37 +01:00
|
|
|
static Int
|
2008-08-12 02:27:23 +01:00
|
|
|
p_thread_stacks(void)
|
|
|
|
{ /* '$thread_runtime'(+P) */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-03 14:26:38 +01:00
|
|
|
static Int
|
2008-04-08 16:36:53 +01:00
|
|
|
p_thread_unlock(void)
|
2008-04-03 14:26:38 +01:00
|
|
|
{ /* '$thread_runtime'(+P) */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-03 10:24:28 +01:00
|
|
|
static Int
|
|
|
|
p_max_workers(void)
|
|
|
|
{ /* '$max_workers'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(1));
|
|
|
|
}
|
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
void Yap_InitThreadPreds(void)
|
|
|
|
{
|
2012-10-19 18:10:48 +01:00
|
|
|
Yap_InitCPred("$max_workers", 1, p_max_workers, 0);
|
|
|
|
Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag);
|
|
|
|
Yap_InitCPred("$no_threads", 0, p_no_threads, SafePredFlag);
|
|
|
|
Yap_InitCPred("$max_threads", 1, p_max_threads, SafePredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads", 1, p_nof_threads, SafePredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads_created", 1, p_nof_threads_created, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_stacks", 4, p_thread_stacks, SafePredFlag);
|
|
|
|
Yap_InitCPred("$thread_runtime", 1, p_thread_runtime, SafePredFlag);
|
2008-04-08 16:36:53 +01:00
|
|
|
Yap_InitCPred("$thread_unlock", 1, p_thread_unlock, SafePredFlag);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* THREADS */
|
|
|
|
|
|
|
|
|