2010-03-21 22:12:42 +00:00
|
|
|
|
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
|
|
|
|
|
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;
|
|
|
|
LOCK(ThreadHandlesLock);
|
2008-04-02 16:41:50 +01:00
|
|
|
while(new_worker_id < MAX_THREADS &&
|
2010-03-21 22:12:42 +00:00
|
|
|
(FOREIGN_ThreadHandle(new_worker_id).in_use == TRUE ||
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).zombie == TRUE) )
|
2004-01-23 02:23:51 +00:00
|
|
|
new_worker_id++;
|
2008-04-02 16:41:50 +01:00
|
|
|
if (new_worker_id < MAX_THREADS) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(new_worker_id, 0);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(FOREIGN_ThreadHandle(new_worker_id).tlock));
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).in_use = TRUE;
|
2008-03-24 23:48:47 +00:00
|
|
|
} else {
|
|
|
|
new_worker_id = -1;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
UNLOCK(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
|
|
|
{
|
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;
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).ssize = ssize;
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).tsize = tsize;
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).sysize = sysize;
|
2008-04-02 18:37:07 +01:00
|
|
|
pm = (ssize + tsize)*1024;
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!(FOREIGN_ThreadHandle(new_worker_id).stack_address = malloc(pm))) {
|
2008-04-02 18:37:07 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).tgoal =
|
2010-01-31 23:03:55 +00:00
|
|
|
Yap_StoreTermInDB(Deref(*tpgoal),7);
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).cmod =
|
2004-02-11 01:20:56 +00:00
|
|
|
CurrentModule;
|
2010-01-31 23:03:55 +00:00
|
|
|
tdetach = Deref(*tpdetach);
|
2004-02-19 19:24:46 +00:00
|
|
|
if (IsVarTerm(tdetach)){
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).tdetach =
|
2004-02-19 19:24:46 +00:00
|
|
|
MkAtomTerm(AtomFalse);
|
|
|
|
} else {
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_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);
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).texit_mod = tmod;
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).texit =
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_StoreTermInDB(tgoal,7);
|
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
|
|
|
{
|
2010-03-21 22:12:42 +00:00
|
|
|
Prop p0 = AbsPredProp(FOREIGN_ThreadHandle(wid).local_preds);
|
2009-05-17 18:38:39 +01:00
|
|
|
GlobalEntry *gl = GlobalVariables;
|
2004-12-02 06:06:47 +00:00
|
|
|
|
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);
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_WL(wid)->active_signals = 0L;
|
|
|
|
free(FOREIGN_WL(wid)->scratchpad.ptr);
|
|
|
|
free(FOREIGN_ThreadHandle(wid).default_yaam_regs);
|
|
|
|
FOREIGN_ThreadHandle(wid).current_yaam_regs = NULL;
|
|
|
|
free(FOREIGN_ThreadHandle(wid).start_of_timesp);
|
|
|
|
free(FOREIGN_ThreadHandle(wid).last_timep);
|
|
|
|
Yap_FreeCodeSpace((ADDR)FOREIGN_ThreadHandle(wid).texit);
|
2008-08-06 18:32:22 +01:00
|
|
|
LOCK(ThreadHandlesLock);
|
2010-03-21 22:12:42 +00:00
|
|
|
if (FOREIGN_ThreadHandle(wid).tdetach == MkAtomTerm(AtomTrue) ||
|
2008-08-06 18:32:22 +01:00
|
|
|
always_die) {
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(wid).zombie = FALSE;
|
|
|
|
FOREIGN_ThreadHandle(wid).in_use = FALSE;
|
2008-08-06 18:32:22 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(1, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(wid).tlock));
|
2008-08-06 18:32:22 +01:00
|
|
|
}
|
|
|
|
UNLOCK(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
|
|
|
{
|
2004-03-02 16:44:58 +00:00
|
|
|
if (!always_die) {
|
|
|
|
/* called by thread itself */
|
|
|
|
ThreadsTotalTime += Yap_cputime();
|
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
kill_thread_engine(wid, always_die);
|
2004-01-23 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2004-07-22 22:32:23 +01:00
|
|
|
static void
|
|
|
|
setup_engine(int myworker_id)
|
2004-01-23 02:23:51 +00:00
|
|
|
{
|
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));
|
2004-01-23 02:23:51 +00:00
|
|
|
/* create the YAAM descriptor */
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(myworker_id).default_yaam_regs = standard_regs;
|
2004-02-06 17:22:24 +00:00
|
|
|
pthread_setspecific(Yap_yaamregs_key, (void *)standard_regs);
|
2004-12-02 06:06:47 +00:00
|
|
|
worker_id = myworker_id;
|
2010-03-21 22:12:42 +00:00
|
|
|
Yap_InitExStacks(FOREIGN_ThreadHandle(myworker_id).tsize, FOREIGN_ThreadHandle(myworker_id).ssize);
|
|
|
|
CurrentModule = FOREIGN_ThreadHandle(myworker_id).cmod;
|
2004-03-02 16:44:58 +00:00
|
|
|
Yap_InitTime();
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_InitYaamRegs();
|
2010-01-14 15:58:19 +00:00
|
|
|
#ifdef YAPOR
|
|
|
|
Yap_init_local();
|
|
|
|
#endif
|
2004-12-02 06:06:47 +00:00
|
|
|
Yap_ReleasePreAllocCodeSpace(Yap_PreAllocCodeSpace());
|
2004-07-22 22:32:23 +01:00
|
|
|
/* I exist */
|
|
|
|
NOfThreadsCreated++;
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(2, myworker_id);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(myworker_id).tlock));
|
2004-07-22 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
start_thread(int myworker_id)
|
|
|
|
{
|
|
|
|
setup_engine(myworker_id);
|
|
|
|
worker_id = myworker_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
thread_run(void *widp)
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
start_thread(myworker_id);
|
2008-01-27 11:01:07 +00:00
|
|
|
do {
|
2010-03-21 22:12:42 +00:00
|
|
|
t = tgs[0] = Yap_PopTermFromDB(MY_ThreadHandle.tgoal);
|
2008-01-27 11:01:07 +00:00
|
|
|
if (t == 0) {
|
|
|
|
if (Yap_Error_TYPE == OUT_OF_ATTVARS_ERROR) {
|
|
|
|
Yap_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Yap_Error_TYPE = YAP_NO_ERROR;
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!Yap_growstack(MY_ThreadHandle.tgoal->NOfCells*CellSize)) {
|
2008-01-27 11:01:07 +00:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t == 0);
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(myworker_id).tgoal = NULL;
|
|
|
|
tgs[1] = MY_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
|
|
|
|
p_thread_new_tid(void)
|
|
|
|
{
|
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
|
2004-07-22 22:32:23 +01:00
|
|
|
p_create_thread(void)
|
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);
|
|
|
|
int new_worker_id = IntegerOfTerm(Deref(ARG7));
|
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;
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_worker_id).id = new_worker_id;
|
|
|
|
FOREIGN_ThreadHandle(new_worker_id).ref_count = 1;
|
|
|
|
if ((FOREIGN_ThreadHandle(new_worker_id).ret = pthread_create(&FOREIGN_ThreadHandle(new_worker_id).pthread_handle, NULL, thread_run, (void *)(&(FOREIGN_ThreadHandle(new_worker_id).id)))) == 0) {
|
2004-12-02 06:06:47 +00:00
|
|
|
/* wait until the client is initialised */
|
2004-07-15 16:47:08 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-05-25 17:28:28 +01:00
|
|
|
static Int
|
|
|
|
p_thread_sleep(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
p_thread_self(void)
|
|
|
|
{
|
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
|
|
|
|
p_thread_zombie_self(void)
|
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return Yap_unify(MkIntegerTerm(-1), ARG1);
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(4, worker_id);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(MY_ThreadHandle.tlock));
|
|
|
|
if (ActiveSignals &= YAP_ITI_SIGNAL) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(5, worker_id);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(MY_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);
|
2010-03-21 22:12:42 +00:00
|
|
|
MY_ThreadHandle.in_use = FALSE;
|
|
|
|
MY_ThreadHandle.zombie = TRUE;
|
2008-08-06 18:32:22 +01:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_status_lock(void)
|
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return FALSE;
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(MY_ThreadHandle.tlock_status));
|
2008-08-06 18:32:22 +01:00
|
|
|
return Yap_unify(MkIntegerTerm(worker_id), ARG1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_status_unlock(void)
|
|
|
|
{
|
|
|
|
/* make sure the lock is available */
|
|
|
|
if (pthread_getspecific(Yap_yaamregs_key) == NULL)
|
|
|
|
return FALSE;
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(MY_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
|
|
|
{
|
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-01-31 23:03:55 +00:00
|
|
|
Term t = TermNil;
|
2004-07-22 22:32:23 +01:00
|
|
|
int new_id = allocate_new_tid();
|
|
|
|
if (new_id == -1) {
|
|
|
|
/* YAP ERROR */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-31 23:03:55 +00:00
|
|
|
if (!init_thread_engine(new_id, ops->ssize, ops->tsize, ops->sysize, &t, &t, &(ops->egoal)))
|
2008-04-02 18:37:07 +01:00
|
|
|
return FALSE;
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(new_id).id = new_id;
|
|
|
|
FOREIGN_ThreadHandle(new_id).pthread_handle = pthread_self();
|
|
|
|
FOREIGN_ThreadHandle(new_id).ref_count = 0;
|
2004-07-22 22:32:23 +01:00
|
|
|
setup_engine(new_id);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-03-13 06:26:13 +00:00
|
|
|
Int
|
2004-07-22 22:32:23 +01:00
|
|
|
Yap_thread_attach_engine(int wid)
|
|
|
|
{
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(7, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(FOREIGN_ThreadHandle(wid).tlock));
|
|
|
|
if (FOREIGN_ThreadHandle(wid).ref_count ) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(8, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(wid).tlock));
|
2004-08-11 17:14:55 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(wid).pthread_handle = pthread_self();
|
|
|
|
FOREIGN_ThreadHandle(wid).ref_count++;
|
2004-07-22 22:32:23 +01:00
|
|
|
worker_id = wid;
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(9, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_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)
|
|
|
|
{
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(10, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(FOREIGN_ThreadHandle(wid).tlock));
|
|
|
|
FOREIGN_ThreadHandle(wid).ref_count--;
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(11, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_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)
|
|
|
|
{
|
2010-03-21 22:12:42 +00:00
|
|
|
if (FOREIGN_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);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_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
|
|
|
|
p_thread_join(void)
|
|
|
|
{
|
2004-02-19 19:24:46 +00:00
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
|
|
|
|
LOCK(ThreadHandlesLock);
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!FOREIGN_ThreadHandle(tid).in_use &&
|
|
|
|
!FOREIGN_ThreadHandle(tid).zombie) {
|
2004-02-19 19:24:46 +00:00
|
|
|
UNLOCK(ThreadHandlesLock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!FOREIGN_ThreadHandle(tid).tdetach == MkAtomTerm(AtomTrue)) {
|
2004-02-19 19:24:46 +00:00
|
|
|
UNLOCK(ThreadHandlesLock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
UNLOCK(ThreadHandlesLock);
|
2004-12-02 06:06:47 +00:00
|
|
|
/* make sure this lock is accessible */
|
2010-03-21 22:12:42 +00:00
|
|
|
if (pthread_join(FOREIGN_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
|
|
|
|
p_thread_destroy(void)
|
|
|
|
{
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
LOCK(ThreadHandlesLock);
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_ThreadHandle(tid).zombie = FALSE;
|
|
|
|
FOREIGN_ThreadHandle(tid).in_use = FALSE;
|
2008-08-06 18:32:22 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(32, tid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(tid).tlock));
|
2008-08-06 18:32:22 +01:00
|
|
|
UNLOCK(ThreadHandlesLock);
|
2004-02-19 19:24:46 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
|
|
|
p_thread_detach(void)
|
|
|
|
{
|
2008-03-26 14:41:45 +00:00
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(FOREIGN_ThreadHandle(tid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(14, tid);
|
2010-03-21 22:12:42 +00:00
|
|
|
if (pthread_detach(FOREIGN_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);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(tid).tlock));
|
2004-01-23 02:23:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
FOREIGN_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);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(tid).tlock));
|
2004-01-23 02:23:51 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
static Int
|
|
|
|
p_thread_detached(void)
|
|
|
|
{
|
2010-03-21 22:12:42 +00:00
|
|
|
if (MY_ThreadHandle.tdetach)
|
|
|
|
return Yap_unify(ARG1,MY_ThreadHandle.tdetach);
|
2008-08-08 17:05:10 +01:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_detached2(void)
|
|
|
|
{
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
2010-03-21 22:12:42 +00:00
|
|
|
if (FOREIGN_ThreadHandle(tid).tdetach)
|
|
|
|
return Yap_unify(ARG2,FOREIGN_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
|
|
|
|
p_thread_exit(void)
|
|
|
|
{
|
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
|
|
|
|
p_thread_set_concurrency(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
p_thread_yield(void)
|
|
|
|
{
|
|
|
|
if (sched_yield() != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
static Int
|
|
|
|
p_valid_thread(void)
|
|
|
|
{
|
|
|
|
Int i = IntegerOfTerm(Deref(ARG1));
|
2010-03-21 22:12:42 +00:00
|
|
|
return FOREIGN_ThreadHandle(i).in_use || FOREIGN_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
|
|
|
|
p_new_mutex(void)
|
|
|
|
{
|
|
|
|
SWIMutex* mutp;
|
|
|
|
pthread_mutexattr_t mat;
|
|
|
|
|
|
|
|
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
|
|
|
|
p_destroy_mutex(void)
|
|
|
|
{
|
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
|
|
|
|
p_lock_mutex(void)
|
|
|
|
{
|
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
|
|
|
|
p_trylock_mutex(void)
|
|
|
|
{
|
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
|
|
|
|
p_unlock_mutex(void)
|
|
|
|
{
|
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
|
2007-03-16 21:58:45 +00:00
|
|
|
p_mutex_info(void)
|
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
|
|
|
|
p_cond_create(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
p_cond_destroy(void)
|
|
|
|
{
|
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
|
|
|
|
p_cond_signal(void)
|
|
|
|
{
|
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
|
|
|
|
p_cond_broadcast(void)
|
|
|
|
{
|
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
|
|
|
|
p_cond_wait(void)
|
|
|
|
{
|
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
|
|
|
|
p_thread_stacks(void)
|
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Int tid = IntegerOfTerm(Deref(ARG1));
|
|
|
|
Int status= TRUE;
|
|
|
|
|
|
|
|
LOCK(ThreadHandlesLock);
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!FOREIGN_ThreadHandle(tid).in_use &&
|
|
|
|
!FOREIGN_ThreadHandle(tid).zombie) {
|
2008-08-07 21:51:23 +01:00
|
|
|
UNLOCK(ThreadHandlesLock);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
status &= Yap_unify(ARG2,MkIntegerTerm(FOREIGN_ThreadHandle(tid).ssize));
|
|
|
|
status &= Yap_unify(ARG3,MkIntegerTerm(FOREIGN_ThreadHandle(tid).tsize));
|
|
|
|
status &= Yap_unify(ARG4,MkIntegerTerm(FOREIGN_ThreadHandle(tid).sysize));
|
2008-08-07 21:51:23 +01:00
|
|
|
UNLOCK(ThreadHandlesLock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_atexit(void)
|
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Term t;
|
|
|
|
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!MY_ThreadHandle.texit ||
|
|
|
|
MY_ThreadHandle.texit->Entry == MkAtomTerm(AtomTrue)) {
|
2008-08-07 21:51:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
do {
|
2010-03-21 22:12:42 +00:00
|
|
|
t = Yap_PopTermFromDB(MY_ThreadHandle.texit);
|
|
|
|
MY_ThreadHandle.texit = NULL;
|
2008-08-07 21:51:23 +01:00
|
|
|
if (t == 0) {
|
|
|
|
if (Yap_Error_TYPE == OUT_OF_ATTVARS_ERROR) {
|
|
|
|
Yap_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (!Yap_growglobal(NULL)) {
|
|
|
|
Yap_Error(OUT_OF_ATTVARS_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Yap_Error_TYPE = YAP_NO_ERROR;
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!Yap_growstack(MY_ThreadHandle.tgoal->NOfCells*CellSize)) {
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
thread_die(worker_id, FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (t == 0);
|
2010-03-21 22:12:42 +00:00
|
|
|
return Yap_unify(ARG1, t) && Yap_unify(ARG2, MY_ThreadHandle.texit_mod);
|
2008-08-07 21:51:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-05 16:57:02 +00:00
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_signal(void)
|
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
Int wid = IntegerOfTerm(Deref(ARG1));
|
2004-02-11 13:59:53 +00:00
|
|
|
/* make sure the lock is available */
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(FOREIGN_ThreadHandle(wid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(16, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
if (!FOREIGN_ThreadHandle(wid).in_use ||
|
|
|
|
!FOREIGN_ThreadHandle(wid).current_yaam_regs) {
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(17, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_ThreadHandle(wid).tlock));
|
2008-05-28 18:18:35 +01:00
|
|
|
return TRUE;
|
2004-02-17 19:29:24 +00:00
|
|
|
}
|
2010-03-21 22:12:42 +00:00
|
|
|
LOCK(FOREIGN_WL(wid)->signal_lock);
|
|
|
|
FOREIGN_ThreadHandle(wid).current_yaam_regs->CreepFlag_ =
|
|
|
|
Unsigned(FOREIGN_ThreadHandle(wid).current_yaam_regs->LCL0_);
|
|
|
|
FOREIGN_WL(wid)->active_signals |= YAP_ITI_SIGNAL;
|
|
|
|
UNLOCK(FOREIGN_WL(wid)->signal_lock);
|
2008-05-28 18:18:35 +01:00
|
|
|
DEBUG_TLOCK_ACCESS(18, wid);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_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
|
|
|
|
p_no_threads(void)
|
|
|
|
{ /* '$thread_signal'(+P) */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
static Int
|
|
|
|
p_nof_threads(void)
|
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
int i = 0, wid;
|
|
|
|
LOCK(ThreadHandlesLock);
|
2008-04-02 16:41:50 +01:00
|
|
|
for (wid = 0; wid < MAX_THREADS; wid++) {
|
2010-03-21 22:12:42 +00:00
|
|
|
if (FOREIGN_ThreadHandle(wid).in_use)
|
2004-03-02 16:44:58 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
UNLOCK(ThreadHandlesLock);
|
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(i));
|
|
|
|
}
|
|
|
|
|
2008-04-02 23:59:52 +01:00
|
|
|
static Int
|
|
|
|
p_max_workers(void)
|
|
|
|
{ /* '$max_workers'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(MAX_WORKERS));
|
|
|
|
}
|
|
|
|
|
2007-01-29 10:18:15 +00:00
|
|
|
static Int
|
|
|
|
p_max_threads(void)
|
|
|
|
{ /* '$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
|
|
|
|
p_nof_threads_created(void)
|
|
|
|
{ /* '$nof_threads'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntTerm(NOfThreadsCreated));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_thread_runtime(void)
|
|
|
|
{ /* '$thread_runtime'(+P) */
|
|
|
|
return Yap_unify(ARG1,MkIntegerTerm(ThreadsTotalTime));
|
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
static Int
|
|
|
|
p_thread_self_lock(void)
|
|
|
|
{ /* '$thread_unlock' */
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_lock(&(MY_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
|
2008-04-08 16:36:53 +01:00
|
|
|
p_thread_unlock(void)
|
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);
|
2010-03-21 22:12:42 +00:00
|
|
|
pthread_mutex_unlock(&(FOREIGN_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)
|
|
|
|
{
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$no_threads", 0, p_no_threads, HiddenPredFlag);
|
2008-04-02 23:59:52 +01:00
|
|
|
Yap_InitCPred("$max_workers", 1, p_max_workers, HiddenPredFlag);
|
2007-01-29 10:18:15 +00:00
|
|
|
Yap_InitCPred("$max_threads", 1, p_max_threads, HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_new_tid", 1, p_thread_new_tid, HiddenPredFlag);
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_InitCPred("$create_thread", 7, p_create_thread, HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag|HiddenPredFlag);
|
2008-08-06 18:32:22 +01:00
|
|
|
Yap_InitCPred("$thread_status_lock", 1, p_thread_status_lock, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$thread_status_unlock", 1, p_thread_status_unlock, SafePredFlag|HiddenPredFlag);
|
2008-01-27 11:01:07 +00:00
|
|
|
Yap_InitCPred("$thread_zombie_self", 1, p_thread_zombie_self, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_join", 1, p_thread_join, HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$thread_destroy", 1, p_thread_destroy, HiddenPredFlag);
|
2006-05-04 19:46:50 +01:00
|
|
|
Yap_InitCPred("thread_yield", 0, p_thread_yield, 0);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$detach_thread", 1, p_thread_detach, HiddenPredFlag);
|
2008-08-06 18:32:22 +01:00
|
|
|
Yap_InitCPred("$thread_detached", 1, p_thread_detached, HiddenPredFlag);
|
2008-08-08 17:05:10 +01:00
|
|
|
Yap_InitCPred("$thread_detached", 2, p_thread_detached2, HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_exit", 0, p_thread_exit, HiddenPredFlag);
|
2004-02-16 19:22:40 +00:00
|
|
|
Yap_InitCPred("thread_setconcurrency", 2, p_thread_set_concurrency, 0);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$valid_thread", 1, p_valid_thread, HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$new_mutex", 1, p_new_mutex, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$destroy_mutex", 1, p_destroy_mutex, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$lock_mutex", 1, p_lock_mutex, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$trylock_mutex", 1, p_trylock_mutex, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$unlock_mutex", 1, p_unlock_mutex, SafePredFlag|HiddenPredFlag);
|
2007-03-16 21:58:45 +00:00
|
|
|
Yap_InitCPred("$mutex_info", 3, p_mutex_info, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$cond_create", 1, p_cond_create, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$cond_destroy", 1, p_cond_destroy, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$cond_signal", 1, p_cond_signal, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$cond_broadcast", 1, p_cond_broadcast, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$cond_wait", 2, p_cond_wait, SafePredFlag|HiddenPredFlag);
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_InitCPred("$thread_stacks", 4, p_thread_stacks, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$signal_thread", 1, p_thread_signal, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads", 1, p_nof_threads, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads_created", 1, p_nof_threads_created, SafePredFlag|HiddenPredFlag);
|
2006-12-31 16:10:29 +00:00
|
|
|
Yap_InitCPred("$thread_sleep", 4, p_thread_sleep, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_runtime", 1, p_thread_runtime, SafePredFlag|HiddenPredFlag);
|
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)
|
|
|
|
{
|
2008-06-03 10:24:28 +01:00
|
|
|
Yap_InitCPred("$max_workers", 1, p_max_workers, HiddenPredFlag);
|
2008-02-22 15:08:37 +00:00
|
|
|
Yap_InitCPred("$thread_self", 1, p_thread_self, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$no_threads", 0, p_no_threads, SafePredFlag|HiddenPredFlag);
|
2007-01-29 10:18:15 +00:00
|
|
|
Yap_InitCPred("$max_threads", 1, p_max_threads, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$nof_threads", 1, p_nof_threads, SafePredFlag|HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$nof_threads_created", 1, p_nof_threads_created, SafePredFlag|HiddenPredFlag);
|
2008-08-07 21:51:23 +01:00
|
|
|
Yap_InitCPred("$thread_stacks", 4, p_thread_stacks, SafePredFlag|HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$thread_runtime", 1, p_thread_runtime, SafePredFlag|HiddenPredFlag);
|
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 */
|
|
|
|
|
|
|
|
|