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: threads.yap *
|
|
|
|
* Last rev: 8/2/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: support threads *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2004-02-05 16:57:02 +00:00
|
|
|
:- meta_predicate
|
|
|
|
thread_create(:,-,+),
|
|
|
|
thread_at_exit(:),
|
|
|
|
thread_signal(+,:).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
:- initialization('$init_thread0').
|
|
|
|
|
|
|
|
'$init_thread0' :-
|
|
|
|
no_threads, !.
|
|
|
|
'$init_thread0' :-
|
2004-06-29 20:04:46 +01:00
|
|
|
'$create_mq'(0),
|
|
|
|
'$add_thread_aliases'([main], 0).
|
2004-02-11 01:20:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
'$top_thread_goal'(G, Detached) :-
|
|
|
|
'$thread_self'(Id),
|
|
|
|
(Detached == true -> '$detach_thread'(Id) ; true),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$current_module'(Module),
|
|
|
|
'$system_catch'((G,'$close_thread'),Module,Exception,'$thread_exception'(Exception)).
|
|
|
|
|
|
|
|
'$top_thread_goal'(_) :-
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
recorda('$thread_exit_status', [Id0|false], _),
|
|
|
|
'$run_at_thread_exit'(Id0).
|
|
|
|
|
|
|
|
'$close_thread' :-
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
recorda('$thread_exit_status', [Id0|true], _),
|
|
|
|
'$run_at_thread_exit'(Id0).
|
|
|
|
|
|
|
|
'$thread_exception'(Exception) :-
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
recorda('$thread_exit_status', [Id0|exception(Exception)], _),
|
|
|
|
'$run_at_thread_exit'(Id0).
|
|
|
|
|
|
|
|
thread_create(Goal, Id, Options) :-
|
|
|
|
G0 = thread_create(Goal, Id, Options),
|
|
|
|
'$check_callable'(Goal,G0),
|
|
|
|
'$thread_options'(Options, Aliases, Stack, Trail, System, Detached, G0),
|
2004-02-11 01:20:56 +00:00
|
|
|
'$thread_new_tid'(Id),
|
|
|
|
'$add_thread_aliases'(Aliases, Id),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$clean_db_on_id'(Id),
|
2004-02-11 01:20:56 +00:00
|
|
|
'$create_mq'(Id),
|
|
|
|
'$create_thread'(Goal, Stack, Trail, System, Detached, Id).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$clean_db_on_id'(Id) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
recorded('$thread_exit_status', [Id|_], R),
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$clean_db_on_id'(Id) :-
|
|
|
|
recorded('$thread_alias',[Id|_],R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$clean_db_on_id'(Id) :-
|
|
|
|
recorded('$thread_exit_hook',[Id|_],R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$clean_db_on_id'(_).
|
|
|
|
|
|
|
|
|
|
|
|
'$thread_options'(V, _, _, _, _, _, G) :- var(V), !,
|
|
|
|
'$do_error'(instantiation_error,G).
|
|
|
|
'$thread_options'([], [], Stack, Trail, System, _, _) :-
|
|
|
|
'$thread_ground_stacks'(Stack),
|
|
|
|
'$thread_ground_stacks'(Trail),
|
|
|
|
'$thread_ground_stacks'(System).
|
2004-02-11 01:20:56 +00:00
|
|
|
'$thread_options'([Opt|Opts], Aliases, Stack, Trail, System, Detached, G0) :-
|
|
|
|
'$thread_option'(Opt, Aliases, Stack, Trail, System, Detached, G0, Aliases0),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$thread_options'(Opts, Aliases0, Stack, Trail, System, Detached, G0).
|
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
'$thread_option'(Option, Aliases, _, _, _, _, G0, Aliases) :- var(Option), !,
|
|
|
|
'$do_error'(instantiation_error,G0).
|
2004-01-23 02:23:51 +00:00
|
|
|
'$thread_option'(stacks(Stack), Aliases, Stack, _, _, _, G0, Aliases) :- !,
|
|
|
|
( \+ integer(Stack) -> '$do_error'(type_error(integer,Stack),G0) ; true ).
|
|
|
|
'$thread_option'(trail(Trail), Aliases, _, Trail, _, _, G0, Aliases) :- !,
|
|
|
|
( \+ integer(Trail) -> '$do_error'(type_error(integer,Trail),G0) ; true ).
|
|
|
|
'$thread_option'(system(Trail), Aliases, _, _, System, _, G0, Aliases) :- !,
|
|
|
|
( \+ integer(System) -> '$do_error'(type_error(integer,System),G0) ; true ).
|
|
|
|
'$thread_option'(alias(Alias), [Alias|Aliases], _, _, _, _, G0, Aliases) :- !,
|
|
|
|
( \+ atom(Alias) -> '$do_error'(type_error(atom,Alias),G0) ; true ).
|
|
|
|
'$thread_option'(detached(B), Aliases, _, _, _, B, G0, Aliases) :- !,
|
|
|
|
( B \== true, B \== false -> '$do_error'(domain_error(flag_value,B+[true,false]),G0) ; true ).
|
|
|
|
'$thread_option'(Option, Aliases, _, _, _, _, G0, Aliases) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(domain_error(thread_create_option,Option+[stacks(_),trail(_),system(_),alias(_),detached(_)]),G0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$thread_ground_stacks'(0) :- !.
|
|
|
|
'$thread_ground_stacks'(_).
|
|
|
|
|
|
|
|
'$add_thread_aliases'([Alias|Aliases], Id) :-
|
2004-02-21 20:25:45 +00:00
|
|
|
recorded('$thread_alias',[Id0|Alias],_), !,
|
|
|
|
'$do_error'(permission_error(alias,new,Alias),thread_create_alias(Id,Alias)).
|
|
|
|
'$add_thread_aliases'([Alias|Aliases], Id) :-
|
|
|
|
recorda('$thread_alias',[Id|Alias],_),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$add_thread_aliases'(Aliases, Id).
|
|
|
|
'$add_thread_aliases'([], _).
|
|
|
|
|
|
|
|
thread_self(Id) :-
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
'$check_thread_alias'(Id0,Id).
|
|
|
|
|
|
|
|
'$check_thread_alias'(Id0,Id) :-
|
|
|
|
recorded('$thread_alias',[Id0|Id],_), !.
|
|
|
|
'$check_thread_alias'(Id,Id).
|
|
|
|
|
|
|
|
/* Exit status may be true, false, exception(Term), exited(Term) */
|
|
|
|
thread_join(Id, Status) :-
|
|
|
|
'$check_thread_alias'(Id0,Id),
|
|
|
|
'$thread_join'(Id0),
|
|
|
|
'$erase_thread_aliases'(Id0),
|
|
|
|
recorded('$thread_exit_status',[Id0|Status],R),
|
2004-02-19 19:24:46 +00:00
|
|
|
erase(R),
|
|
|
|
'$thread_destroy'(Id0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$erase_thread_aliases'(Id0) :-
|
|
|
|
recorded('$thread_alias',[Id0|_],R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$erase_thread_aliases'(_).
|
|
|
|
|
|
|
|
thread_detach(Id) :-
|
|
|
|
'$check_thread_alias'(Id0,Id),
|
|
|
|
'$detach_thread'(Id0).
|
|
|
|
|
|
|
|
thread_exit(Term) :-
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
'$run_at_thread_exit'(Id0),
|
|
|
|
recorda('$thread_exit_status', [Id0|Term], _),
|
|
|
|
'$thread_exit'.
|
|
|
|
|
|
|
|
'$run_at_thread_exit'(Id0) :-
|
|
|
|
findall(Hook, (recorded('$thread_exit_hook',[Id0|Hook],R), erase(R)), Hooks),
|
|
|
|
'$run_thread_hooks'(Hooks),
|
|
|
|
message_queue_destroy(Id0).
|
|
|
|
|
|
|
|
'$run_thread_hooks'([]).
|
|
|
|
'$run_thread_hooks'([Hook|Hooks]) :-
|
|
|
|
'$thread_top_goal'(Hook),
|
|
|
|
'$run_thread_hooks'(Hooks).
|
|
|
|
|
|
|
|
thread_at_exit(Goal) :-
|
|
|
|
'$check_callable'(Goal,thread_at_exit(Goal)),
|
|
|
|
'$thread_self'(Id0),
|
|
|
|
recordz('$thread_exit_hook',[Id0|Goal],_).
|
|
|
|
|
|
|
|
current_thread(Tid, Status) :-
|
|
|
|
var(Tid), !,
|
|
|
|
'$cur_threads'(0, Tid, Status).
|
|
|
|
current_thread(Tid, Status) :-
|
|
|
|
'$check_thread_alias'(Id0,Tid), !,
|
|
|
|
'$valid_thread'(Id0),
|
|
|
|
'$thr_status'(Id0, Status).
|
|
|
|
current_thread(Tid, Status) :- integer(Tid), !,
|
|
|
|
'$valid_thread'(Tid),
|
|
|
|
'$thr_status'(Tid, Status).
|
|
|
|
current_thread(Tid, Status) :-
|
|
|
|
'$do_error'(type_error(integer,Tid),current_thread(Tid, Status)).
|
|
|
|
|
|
|
|
'$cur_threads'(Tid, Tid, Status) :-
|
|
|
|
'$valid_thread'(Tid),
|
|
|
|
'$thr_status'(Tid, Status).
|
|
|
|
'$cur_threads'(Tid, TidF, Status) :-
|
|
|
|
'$valid_thread'(Tid),
|
|
|
|
Tid1 is Tid+1,
|
|
|
|
'$cur_threads'(Tid1, TidF, Status).
|
|
|
|
|
|
|
|
'$thr_status'(Tid, Status) :-
|
|
|
|
recorded('$thread_exit_status', [Tid|Status], _), !.
|
2004-02-21 20:25:45 +00:00
|
|
|
'$thr_status'(_, running).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
mutex_create(V) :-
|
|
|
|
var(V), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
'$new_mutex'(V),
|
|
|
|
recorda('$mutex',[V|V],_).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_create(A) :-
|
|
|
|
atom(A),
|
|
|
|
recorded('$mutex',[A|_],_), !,
|
|
|
|
'$do_error'(permission_error(create,mutex,A),mutex_create(A)).
|
|
|
|
mutex_create(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
'$new_mutex'(Id),
|
2004-02-11 01:20:56 +00:00
|
|
|
recorda('$mutex',[A|Id],_).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_create(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_create(V)).
|
|
|
|
|
|
|
|
mutex_destroy(V) :-
|
|
|
|
var(V), !,
|
2004-02-21 20:25:45 +00:00
|
|
|
'$do_error'(instantiation_error,mutex_destroy(V)).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_destroy(A) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
recorded('$mutex',[A|Id],R), !,
|
|
|
|
'$destroy_mutex'(Id),
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(R).
|
|
|
|
mutex_destroy(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
'$do_error'(existence_error(mutex,A),mutex_destroy(A)).
|
|
|
|
mutex_destroy(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_destroy(V)).
|
|
|
|
|
|
|
|
mutex_lock(V) :-
|
|
|
|
var(V), !,
|
2004-02-21 20:25:45 +00:00
|
|
|
'$do_error'(instantiation_error,mutex_lock(V)).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_lock(A) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
recorded('$mutex',[A|Id],_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
'$lock_mutex'(Id).
|
|
|
|
mutex_lock(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
mutex_create(A),
|
|
|
|
mutex_lock(A).
|
|
|
|
mutex_lock(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_lock(V)).
|
|
|
|
|
|
|
|
mutex_trylock(V) :-
|
|
|
|
var(V), !,
|
2004-02-21 20:25:45 +00:00
|
|
|
'$do_error'(instantiation_error,mutex_trylock(V)).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_trylock(A) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
recorded('$mutex',[A|Id],_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
'$trylock_mutex'(Id).
|
|
|
|
mutex_trylock(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
mutex_create(A),
|
|
|
|
mutex_trylock(A).
|
|
|
|
mutex_trylock(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_trylock(V)).
|
|
|
|
|
|
|
|
mutex_unlock(V) :-
|
|
|
|
var(V), !,
|
2004-02-21 20:25:45 +00:00
|
|
|
'$do_error'(instantiation_error,mutex_unlock(V)).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_unlock(A) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
recorded('$mutex',[A|Id],_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
( '$unlock_mutex'(Id) ->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
'$do_error'(permission_error(unlock,mutex,A),mutex_unlock(A))
|
|
|
|
).
|
|
|
|
mutex_unlock(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
'$do_error'(existence_error(mutex,A),mutex_unlock(A)).
|
|
|
|
mutex_unlock(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_unlock(V)).
|
|
|
|
|
|
|
|
mutex_unlock_all :-
|
|
|
|
'$thread_self'(T),
|
|
|
|
recorded('$mutex',[_|Id],_),
|
|
|
|
'$mutex_info'(Id, NRefs, T),
|
|
|
|
NRefs > 0,
|
|
|
|
'$mutex_unlock_all'(Id),
|
|
|
|
fail.
|
|
|
|
mutex_unlock_all.
|
|
|
|
|
|
|
|
'$mutex_unlock_all'(Id) :-
|
|
|
|
'$mutex_info'(Id, NRefs, _),
|
|
|
|
NRefs > 0,
|
|
|
|
'$unlock_mutex'(Id),
|
|
|
|
'$mutex_unlock_all'(Id).
|
|
|
|
|
|
|
|
current_mutex(M, T, NRefs) :-
|
|
|
|
recorded('$mutex',[M|Id],_),
|
|
|
|
'$mutex_info'(Id, NRefs, T).
|
|
|
|
|
|
|
|
message_queue_create(Cond) :-
|
|
|
|
var(Cond), !,
|
|
|
|
mutex_create(Mutex),
|
|
|
|
'$cond_create'(Cond),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$mq_iname'(Cond, CName),
|
|
|
|
recorda('$queue',q(Cond,Mutex,Cond,CName), _).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_create(Name) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
atom(Name),
|
2004-06-29 20:04:46 +01:00
|
|
|
recorded('$thread_alias',[_,Name],_), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(permission_error(create,queue,Name),message_queue_create(Name)).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_create(Name) :-
|
|
|
|
atom(Name), !,
|
|
|
|
'$create_mq'(Name).
|
|
|
|
message_queue_create(Name) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(type_error(atom,Name),message_queue_create(Name)).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$create_mq'(Name) :-
|
|
|
|
mutex_create(Mutex),
|
|
|
|
'$cond_create'(Cond),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$mq_iname'(Name, CName),
|
|
|
|
recorda('$queue',q(Name,Mutex,Cond, CName),_).
|
|
|
|
|
|
|
|
'$mq_iname'(I,X) :-
|
|
|
|
integer(I), !,
|
|
|
|
number_codes(I,Codes),
|
|
|
|
atom_codes(X, [0'$,0'M,0'Q,0'_|Codes]).
|
|
|
|
'$mq_iname'(A,X) :-
|
|
|
|
atom_concat('$MQ_NAME_KEY_',A,X).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
message_queue_destroy(Name) :-
|
|
|
|
var(Name), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(instantiation_error,message_queue_destroy(Name)).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_destroy(Queue) :-
|
2004-03-02 16:44:58 +00:00
|
|
|
recorded('$queue',q(Queue,Mutex,Cond,CName),R), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(R),
|
|
|
|
'$cond_destroy'(Cond),
|
2004-02-21 20:25:45 +00:00
|
|
|
mutex_destroy(Mutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$clean_mqueue'(CName).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_destroy(Queue) :-
|
|
|
|
atom(Queue), !,
|
2004-02-21 20:25:45 +00:00
|
|
|
'$do_error'(existence_error(queue,Queue),message_queue_destroy(QUeue)).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_destroy(Name) :-
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(type_error(atom,Name),message_queue_destroy(Name)).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-02-19 19:24:46 +00:00
|
|
|
'$clean_mqueue'(Queue) :-
|
2004-03-02 16:44:58 +00:00
|
|
|
recorded(Queue,_,R),
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$clean_mqueue'(_).
|
2004-06-29 20:04:46 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_send_message(Queue, Term) :-
|
2004-06-29 20:04:46 +01:00
|
|
|
recorded('$thread_alias',[Id|Queue],_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_send_message(Id, Term).
|
|
|
|
thread_send_message(Queue, Term) :-
|
2004-03-02 16:44:58 +00:00
|
|
|
recorded('$queue',q(Queue,Mutex,Cond,Key),_),
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_lock(Mutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
recordz(Key,Term,_),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$cond_broadcast'(Cond),
|
|
|
|
mutex_unlock(Mutex).
|
|
|
|
|
|
|
|
thread_get_message(Term) :-
|
|
|
|
'$thread_self'(Id),
|
|
|
|
thread_get_message(Id, Term).
|
|
|
|
|
2004-06-29 20:04:46 +01:00
|
|
|
thread_get_message(Queue, Term) :-
|
|
|
|
recorded('$thread_alias',[Id|Queue],_), !,
|
|
|
|
thread_get_message(Id, Term).
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_get_message(Queue, Term) :-
|
2004-03-02 16:44:58 +00:00
|
|
|
recorded('$queue',q(Queue,Mutex,Cond,Key),_),
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_lock(Mutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_get_message_loop'(Key, Term, Mutex, Cond).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_get_message_loop'(Key, Term, Mutex, _) :-
|
|
|
|
recorded(Key,Term,R), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
erase(R),
|
|
|
|
mutex_unlock(Mutex).
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_get_message_loop'(Key, Term, Mutex, Cond) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
'$cond_wait'(Cond, Mutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_get_message_loop'(Key, Term, Mutex, Cond).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
thread_peek_message(Term) :-
|
|
|
|
'$thread_self'(Id),
|
|
|
|
thread_peek_message(Id, Term).
|
|
|
|
|
|
|
|
thread_peek_message(Queue, Term) :-
|
2004-03-02 16:44:58 +00:00
|
|
|
recorded('$queue',q(Queue,Mutex,_,Key),_),
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_lock(Mutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_peek_message2'(Key, Term, Mutex).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_peek_message2'(Key, Term, Mutex) :-
|
|
|
|
recorded(Key,Term,_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_unlock(Mutex).
|
2004-02-21 20:25:45 +00:00
|
|
|
'$thread_peek_message2'(_, _, Mutex) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_unlock(Mutex),
|
|
|
|
fail.
|
2004-02-05 16:57:02 +00:00
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
thread_local(X) :-
|
|
|
|
'$current_module'(M),
|
|
|
|
'$thread_local'(X,M).
|
|
|
|
|
2004-02-05 16:57:02 +00:00
|
|
|
'$thread_local'(X,M) :- var(X), !,
|
|
|
|
'$do_error'(instantiation_error,thread_local(M:X)).
|
|
|
|
'$thread_local'(Mod:Spec,_) :- !,
|
|
|
|
'$thread_local'(Spec,Mod).
|
|
|
|
'$thread_local'([], _) :- !.
|
|
|
|
'$thread_local'([H|L], M) :- !, '$thread_local'(H, M), '$thread_local'(L, M).
|
|
|
|
'$thread_local'((A,B),M) :- !, '$thread_local'(A,M), '$thread_local'(B,M).
|
|
|
|
'$thread_local'(X,M) :- !,
|
|
|
|
'$thread_local2'(X,M).
|
|
|
|
|
|
|
|
'$thread_local2'(A/N, Mod) :- integer(N), atom(A), !,
|
|
|
|
functor(T,A,N),
|
|
|
|
'$flags'(T,Mod,F,F),
|
2004-02-11 13:33:19 +00:00
|
|
|
( '$install_thread_local'(T,Mod) -> true ;
|
2004-02-05 16:57:02 +00:00
|
|
|
F /\ 0x08002000 =\= 0 -> '$do_error'(permission_error(modify,dynamic_procedure,A/N),thread_local(Mod:A/N)) ;
|
|
|
|
'$do_error'(permission_error(modify,static_procedure,A/N),thread_local(Mod:A/N))
|
|
|
|
).
|
|
|
|
'$thread_local2'(X,Mod) :-
|
|
|
|
'$do_error'(type_error(callable,X),thread_local(Mod:X)).
|
|
|
|
|
|
|
|
|
|
|
|
thread_signal(Thread, Goal) :-
|
|
|
|
var(Thread), !,
|
|
|
|
'$do_error'(instantiation_error,thread_signal(Thread, Goal)).
|
|
|
|
thread_signal(Thread, Goal) :-
|
2004-06-29 20:04:46 +01:00
|
|
|
recorded('$thread_alias',[Id|Thread],_),
|
2004-02-05 16:57:02 +00:00
|
|
|
'$thread_signal'(Id, Goal).
|
|
|
|
thread_signal(Thread, Goal) :-
|
|
|
|
integer(Thread), !,
|
|
|
|
'$thread_signal'(Thread, Goal).
|
|
|
|
thread_signal(Thread, Goal) :-
|
|
|
|
'$do_error'(type_error(integer,Thread),thread_signal(Thread, Goal)).
|
|
|
|
|
|
|
|
'$thread_signal'(Thread, Goal) :-
|
|
|
|
( recorded('$thread_signal',[Thread|_],R), erase(R), fail ; true ),
|
|
|
|
recorda('$thread_signal',[Thread|Goal],_),
|
|
|
|
'$signal_thread'(Thread).
|
|
|
|
|
|
|
|
'$thread_gfetch'(G) :-
|
|
|
|
'$thread_self'(Id),
|
2004-02-11 13:59:53 +00:00
|
|
|
recorded('$thread_signal',[Id|G],R),
|
2004-02-05 16:57:02 +00:00
|
|
|
erase(R).
|