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(:,-,+),
|
2006-12-31 19:33:27 +00:00
|
|
|
thread_create(:,-),
|
2007-06-19 11:55:35 +01:00
|
|
|
thread_create(:),
|
2004-02-05 16:57:02 +00:00
|
|
|
thread_at_exit(:),
|
2007-01-01 16:54:39 +00:00
|
|
|
thread_signal(+,:),
|
|
|
|
with_mutex(+,:).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2004-02-11 01:20:56 +00:00
|
|
|
:- initialization('$init_thread0').
|
|
|
|
|
|
|
|
'$init_thread0' :-
|
2008-02-22 15:08:37 +00:00
|
|
|
no_threads, !,
|
|
|
|
recorda('$thread_alias', [0|main], _).
|
2004-02-11 01:20:56 +00:00
|
|
|
'$init_thread0' :-
|
2007-01-02 02:51:31 +00:00
|
|
|
'$record_thread_info'(0, main, [0, 0, 0], false, '$init_thread0'),
|
2007-01-08 11:30:38 +00:00
|
|
|
recorda('$thread_defaults', [0, 0, 0, false], _),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$new_mutex'(QId),
|
|
|
|
assert('$global_queue_mutex'(QId)),
|
|
|
|
'$create_mq'(0),
|
2007-01-08 11:30:38 +00:00
|
|
|
'$new_mutex'(Id),
|
2008-01-27 11:01:07 +00:00
|
|
|
assert('$with_mutex_mutex'(Id)).
|
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),
|
2008-01-23 17:57:56 +00:00
|
|
|
% always finish with a throw to make sure we clean stacks.
|
|
|
|
'$system_catch'((G -> throw('$thread_finished'(true)) ; throw('$thread_finished'(false))),Module,Exception,'$close_thread'(Exception,Detached)).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2008-01-27 11:01:07 +00:00
|
|
|
'$close_thread'(Status, Detached) :-
|
|
|
|
'$thread_zombie_self'(Id0), !,
|
|
|
|
'$close_thread'(Status, Detached, Id0).
|
|
|
|
'$close_thread'(Status, Detached) :- !,
|
|
|
|
% one self will fail if it had messages
|
|
|
|
'$close_thread'(Status, Detached).
|
|
|
|
|
|
|
|
|
|
|
|
'$close_thread'('$thread_finished'(Status), Detached, Id0) :- !,
|
|
|
|
'$run_at_thread_exit'(Id0),
|
2006-05-25 17:57:21 +01:00
|
|
|
(Detached == true ->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
recorda('$thread_exit_status', [Id0|Status], _)
|
2008-01-27 11:01:07 +00:00
|
|
|
).
|
|
|
|
% format(user_error,'closing thread ~w~n',[v([Id0|Status])]).
|
2008-01-23 17:57:56 +00:00
|
|
|
'$close_thread'(Exception,Detached) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$run_at_thread_exit'(Id0),
|
2006-05-25 17:57:21 +01:00
|
|
|
(Detached == true ->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
recorda('$thread_exit_status', [Id0|exception(Exception)], _)
|
2008-01-27 11:01:07 +00:00
|
|
|
).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-06-19 11:55:35 +01:00
|
|
|
thread_create(Goal) :-
|
|
|
|
G0 = thread_create(Goal),
|
|
|
|
'$check_callable'(Goal, G0),
|
|
|
|
'$thread_options'([detached(true)], [], Stack, Trail, System, Detached, G0),
|
|
|
|
'$thread_new_tid'(Id),
|
|
|
|
'$erase_thread_info'(Id),
|
|
|
|
'$record_thread_info'(Id, [Stack, Trail, System], true),
|
|
|
|
'$create_mq'(Id),
|
|
|
|
'$create_thread'(Goal, Stack, Trail, System, Detached, Id).
|
|
|
|
|
2007-07-12 01:18:41 +01:00
|
|
|
thread_create(Goal, OutId) :-
|
2006-12-31 19:33:27 +00:00
|
|
|
G0 = thread_create(Goal, Id),
|
2007-01-01 19:40:17 +00:00
|
|
|
'$check_callable'(Goal, G0),
|
2006-05-25 11:46:23 +01:00
|
|
|
( nonvar(Id) -> '$do_error'(type_error(variable,Id),G0) ; true ),
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_options'([], [], Stack, Trail, System, Detached, G0),
|
2006-05-24 22:41:00 +01:00
|
|
|
'$thread_new_tid'(Id),
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(Id),
|
|
|
|
'$record_thread_info'(Id, [Stack, Trail, System], Detached),
|
2006-05-24 22:41:00 +01:00
|
|
|
'$create_mq'(Id),
|
2007-07-12 01:18:41 +01:00
|
|
|
'$create_thread'(Goal, Stack, Trail, System, Detached, Id),
|
|
|
|
OutId = Id.
|
2006-05-24 22:41:00 +01:00
|
|
|
|
2007-07-12 01:18:41 +01:00
|
|
|
thread_create(Goal, OutId, Options) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
G0 = thread_create(Goal, Id, Options),
|
|
|
|
'$check_callable'(Goal,G0),
|
2006-05-25 11:46:23 +01:00
|
|
|
( nonvar(Id) -> '$do_error'(type_error(variable,Id),G0) ; true ),
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_options'(Options, Alias, Stack, Trail, System, Detached, G0),
|
2004-02-11 01:20:56 +00:00
|
|
|
'$thread_new_tid'(Id),
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(Id),
|
|
|
|
( var(Alias) ->
|
|
|
|
'$record_thread_info'(Id, [Stack, Trail, System], Detached)
|
|
|
|
; '$record_thread_info'(Id, Alias, [Stack, Trail, System], Detached, G0)
|
|
|
|
),
|
2004-02-11 01:20:56 +00:00
|
|
|
'$create_mq'(Id),
|
2007-07-12 01:18:41 +01:00
|
|
|
'$create_thread'(Goal, Stack, Trail, System, Detached, Id),
|
|
|
|
OutId = Id.
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(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.
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(Id) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
recorded('$thread_alias',[Id|_],R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(Id) :-
|
|
|
|
recorded('$thread_sizes', [Id|_], R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$erase_thread_info'(Id) :-
|
|
|
|
recorded('$thread_detached', [Id|_], R),
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(R),
|
|
|
|
fail.
|
2007-01-02 02:51:31 +00:00
|
|
|
'$erase_thread_info'(Id) :-
|
|
|
|
recorded('$thread_exit_hook', [Id|_], R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$erase_thread_info'(_).
|
2006-05-24 15:40:50 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$thread_options'(V, _, _, _, _, _, G) :- var(V), !,
|
|
|
|
'$do_error'(instantiation_error,G).
|
2007-01-02 02:51:31 +00:00
|
|
|
'$thread_options'([], _, Stack, Trail, System, Detached, _) :-
|
2006-12-31 19:33:27 +00:00
|
|
|
recorded('$thread_defaults', [DefaultStack, DefaultTrail, DefaultSystem, DefaultDetached], _),
|
2006-05-24 16:27:45 +01:00
|
|
|
( var(Stack) -> Stack = DefaultStack; true ),
|
|
|
|
( var(Trail) -> Trail = DefaultTrail; true ),
|
2006-12-31 19:33:27 +00:00
|
|
|
( var(System) -> System = DefaultSystem; true ),
|
|
|
|
( var(Detached) -> Detached = DefaultDetached; true ).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_options'([Opt|Opts], Alias, Stack, Trail, System, Detached, G0) :-
|
|
|
|
'$thread_option'(Opt, Alias, Stack, Trail, System, Detached, G0),
|
|
|
|
'$thread_options'(Opts, Alias, Stack, Trail, System, Detached, G0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-24 14:20:04 +00:00
|
|
|
'$thread_option'(Option, _, _, _, _, _, G0) :- var(Option), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
'$do_error'(instantiation_error,G0).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_option'(stack(Stack), _, Stack, _, _, _, G0) :- !,
|
2004-01-23 02:23:51 +00:00
|
|
|
( \+ integer(Stack) -> '$do_error'(type_error(integer,Stack),G0) ; true ).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_option'(trail(Trail), _, _, Trail, _, _, G0) :- !,
|
2004-01-23 02:23:51 +00:00
|
|
|
( \+ integer(Trail) -> '$do_error'(type_error(integer,Trail),G0) ; true ).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_option'(system(System), _, _, _, System, _, G0) :- !,
|
2004-01-23 02:23:51 +00:00
|
|
|
( \+ integer(System) -> '$do_error'(type_error(integer,System),G0) ; true ).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_option'(alias(Alias), Alias, _, _, _, _, G0) :- !,
|
2004-01-23 02:23:51 +00:00
|
|
|
( \+ atom(Alias) -> '$do_error'(type_error(atom,Alias),G0) ; true ).
|
2007-01-01 16:54:39 +00:00
|
|
|
'$thread_option'(detached(Detached), _, _, _, _, Detached, G0) :- !,
|
|
|
|
( Detached \== true, Detached \== false -> '$do_error'(domain_error(thread_option,Detached+[true,false]),G0) ; true ).
|
|
|
|
'$thread_option'(Option, _, _, _, _, _, G0) :-
|
|
|
|
'$do_error'(domain_error(thread_option,Option),G0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-02 02:51:31 +00:00
|
|
|
'$record_thread_info'(_, Alias, _, _, Goal) :-
|
|
|
|
recorded('$thread_alias', [_|Alias], _), !,
|
|
|
|
'$do_error'(permission_error(create,thread,alias(Alias)), Goal).
|
|
|
|
'$record_thread_info'(Id, Alias, Sizes, Detached, _) :-
|
|
|
|
recorda('$thread_alias', [Id|Alias], _),
|
|
|
|
'$record_thread_info'(Id, Sizes, Detached).
|
|
|
|
|
|
|
|
'$record_thread_info'(Id, Sizes, Detached) :-
|
|
|
|
recorda('$thread_sizes', [Id|Sizes], _),
|
|
|
|
recorda('$thread_detached', [Id|Detached], _).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-24 14:20:04 +00:00
|
|
|
% vsc: ?????
|
2007-01-02 02:51:31 +00:00
|
|
|
thread_defaults(Defaults) :-
|
|
|
|
nonvar(Defaults), !,
|
2007-01-28 16:00:31 +00:00
|
|
|
'$do_error'(type_error(variable, Defaults), thread_defaults(Defaults)).
|
2006-12-31 19:33:27 +00:00
|
|
|
thread_defaults([stack(Stack), trail(Trail), system(System), detached(Detached)]) :-
|
|
|
|
recorded('$thread_defaults',[Stack, Trail, System, Detached], _).
|
2006-05-24 15:40:50 +01:00
|
|
|
|
2006-12-31 20:33:55 +00:00
|
|
|
thread_default(Default) :-
|
|
|
|
var(Default), !,
|
|
|
|
recorded('$thread_defaults', Defaults, _),
|
|
|
|
'$thread_default'(Default, Defaults).
|
|
|
|
thread_default(stack(Stack)) :- !,
|
|
|
|
recorded('$thread_defaults',[Stack, _, _, _], _).
|
|
|
|
thread_default(trail(Trail)) :- !,
|
|
|
|
recorded('$thread_defaults',[_, Trail, _, _], _).
|
|
|
|
thread_default(system(System)) :- !,
|
|
|
|
recorded('$thread_defaults',[_, _, System, _], _).
|
|
|
|
thread_default(detached(Detached)) :- !,
|
|
|
|
recorded('$thread_defaults',[_, _, _, Detached], _).
|
|
|
|
thread_default(Default) :-
|
|
|
|
'$do_error'(type_error(thread_option,Default),thread_default(Default)).
|
|
|
|
|
|
|
|
'$thread_default'(stack(Stack), [Stack, _, _, _]).
|
|
|
|
'$thread_default'(trail(Trail), [_, Trail, _, _]).
|
|
|
|
'$thread_default'(stack(System), [_, _, System, _]).
|
|
|
|
'$thread_default'(detached(Detached), [_, _, _, Detached]).
|
|
|
|
|
2006-05-24 15:40:50 +01:00
|
|
|
thread_set_defaults(V) :- var(V), !,
|
|
|
|
'$do_error'(instantiation_error, thread_set_defaults(V)).
|
|
|
|
thread_set_defaults([Default| Defaults]) :- !,
|
|
|
|
'$thread_set_defaults'([Default| Defaults], thread_set_defaults([Default| Defaults])).
|
|
|
|
thread_set_defaults(T) :-
|
|
|
|
'$do_error'(type_error(list, T), thread_set_defaults(T)).
|
|
|
|
|
|
|
|
'$thread_set_defaults'([], _).
|
|
|
|
'$thread_set_defaults'([Default| Defaults], G) :- !,
|
|
|
|
'$thread_set_default'(Default, G),
|
|
|
|
'$thread_set_defaults'(Defaults, G).
|
|
|
|
|
2006-12-31 20:33:55 +00:00
|
|
|
thread_set_default(V) :- var(V), !,
|
|
|
|
'$do_error'(instantiation_error, thread_set_default(V)).
|
|
|
|
thread_set_default(Default) :-
|
|
|
|
'$thread_set_default'(Default, thread_set_default(Default)).
|
|
|
|
|
2006-05-24 15:40:50 +01:00
|
|
|
'$thread_set_default'(stack(Stack), G) :-
|
|
|
|
\+ integer(Stack), !,
|
|
|
|
'$do_error'(type_error(integer, Stack), G).
|
|
|
|
'$thread_set_default'(stack(Stack), G) :-
|
|
|
|
Stack < 0, !,
|
|
|
|
'$do_error'(domain_error(not_less_than_zero, Stack), G).
|
2007-01-24 14:20:04 +00:00
|
|
|
'$thread_set_default'(stack(Stack), _) :- !,
|
2006-12-31 20:33:55 +00:00
|
|
|
recorded('$thread_defaults', [_, Trail, System, Detached], Ref),
|
|
|
|
erase(Ref),
|
2006-12-31 19:33:27 +00:00
|
|
|
recorda('$thread_defaults', [Stack, Trail, System, Detached], _).
|
2006-05-24 15:40:50 +01:00
|
|
|
|
|
|
|
'$thread_set_default'(trail(Trail), G) :-
|
|
|
|
\+ integer(Trail), !,
|
|
|
|
'$do_error'(type_error(integer, Trail), G).
|
|
|
|
'$thread_set_default'(trail(Trail), G) :-
|
|
|
|
Trail < 0, !,
|
|
|
|
'$do_error'(domain_error(not_less_than_zero, Trail), G).
|
2007-01-24 14:20:04 +00:00
|
|
|
'$thread_set_default'(trail(Trail), _) :- !,
|
2006-12-31 20:33:55 +00:00
|
|
|
recorded('$thread_defaults', [Stack, _, System, Detached], Ref),
|
|
|
|
erase(Ref),
|
2006-12-31 19:33:27 +00:00
|
|
|
recorda('$thread_defaults', [Stack, Trail, System, Detached], _).
|
2006-05-24 15:40:50 +01:00
|
|
|
|
|
|
|
'$thread_set_default'(system(System), G) :-
|
|
|
|
\+ integer(System), !,
|
|
|
|
'$do_error'(type_error(integer, System), G).
|
|
|
|
'$thread_set_default'(system(System), G0) :-
|
|
|
|
System < 0, !,
|
|
|
|
'$do_error'(domain_error(not_less_than_zero, System), G0).
|
2007-01-24 14:20:04 +00:00
|
|
|
'$thread_set_default'(system(System), _) :- !,
|
2006-12-31 20:33:55 +00:00
|
|
|
recorded('$thread_defaults', [Stack, Trail, _, Detached], Ref),
|
|
|
|
erase(Ref),
|
2006-12-31 19:33:27 +00:00
|
|
|
recorda('$thread_defaults', [Stack, Trail, System, Detached], _).
|
|
|
|
|
|
|
|
'$thread_set_default'(detached(Detached), G) :-
|
|
|
|
Detached \== true, Detached \== false, !,
|
|
|
|
'$do_error'(type_error(boolean, Detached), G).
|
2007-01-24 14:20:04 +00:00
|
|
|
'$thread_set_default'(detached(Detached), _) :- !,
|
2006-12-31 20:33:55 +00:00
|
|
|
recorded('$thread_defaults', [Stack, Trail, System, _], Ref),
|
|
|
|
erase(Ref),
|
2006-12-31 19:33:27 +00:00
|
|
|
recorda('$thread_defaults', [Stack, Trail, System, Detached], _).
|
2006-05-24 15:40:50 +01:00
|
|
|
|
|
|
|
'$thread_set_default'(Default, G) :-
|
|
|
|
'$do_error'(domain_error(thread_default, Default), G).
|
|
|
|
|
2007-01-01 16:54:39 +00:00
|
|
|
thread_self(Id) :-
|
|
|
|
nonvar(Id), \+ integer(Id), \+ atom(Id), !,
|
|
|
|
'$do_error'(domain_error(thread_or_alias, Id), thread_self(Id)).
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_self(Id) :-
|
|
|
|
'$thread_self'(Id0),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-01 16:54:39 +00:00
|
|
|
/* Exit status may be either true, false, exception(Term), or exited(Term) */
|
|
|
|
thread_join(Id, Status) :-
|
|
|
|
nonvar(Status), !,
|
2006-05-25 11:46:23 +01:00
|
|
|
'$do_error'(type_error(variable,Status),thread_join(Id, Status)).
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_join(Id, Status) :-
|
2007-01-01 18:11:24 +00:00
|
|
|
'$check_thread_or_alias'(Id, thread_join(Id, Status)),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$thread_join'(Id0),
|
2007-01-02 03:01:20 +00:00
|
|
|
recorded('$thread_exit_status', [Id0|Status], _),
|
|
|
|
'$erase_thread_info'(Id0),
|
2004-02-19 19:24:46 +00:00
|
|
|
'$thread_destroy'(Id0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-02 11:10:58 +00:00
|
|
|
thread_cancel(Id) :-
|
|
|
|
(Id == main; Id == 0), !,
|
|
|
|
'$do_error'(permission_error(cancel, thread, main), thread_cancel(Id)).
|
2007-01-01 18:11:24 +00:00
|
|
|
thread_cancel(Id) :-
|
|
|
|
'$check_thread_or_alias'(Id, thread_cancel(Id)),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id),
|
2007-01-01 18:11:24 +00:00
|
|
|
'$unlock_all_thread_mutexes'(Id0),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$erase_thread_info'(Id0),
|
2007-01-01 18:11:24 +00:00
|
|
|
'$thread_destroy'(Id0).
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_detach(Id) :-
|
2007-01-01 18:11:24 +00:00
|
|
|
'$check_thread_or_alias'(Id, thread_detach(Id)),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id),
|
2006-12-31 22:23:24 +00:00
|
|
|
'$detach_thread'(Id0).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-01 17:20:56 +00:00
|
|
|
thread_exit(Term) :-
|
|
|
|
var(Term), !,
|
|
|
|
'$do_error'(instantiation_error, thread_exit(Term)).
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_exit(Term) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$close_thread'('$thread_finished'(exited(Term)), Detached).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$run_at_thread_exit'(Id0) :-
|
|
|
|
findall(Hook, (recorded('$thread_exit_hook',[Id0|Hook],R), erase(R)), Hooks),
|
|
|
|
'$run_thread_hooks'(Hooks),
|
2008-01-27 11:01:07 +00:00
|
|
|
fail.
|
|
|
|
'$run_at_thread_exit'(Id0) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
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],_).
|
|
|
|
|
2007-01-02 02:51:31 +00:00
|
|
|
current_thread(Id, Status) :-
|
|
|
|
thread_property(Id, status(Status)).
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id, Alias) :-
|
2007-01-02 02:51:31 +00:00
|
|
|
recorded('$thread_alias', [Id|Alias], _), !.
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id, Id).
|
2006-05-04 19:46:50 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-28 16:00:31 +00:00
|
|
|
'$mutex_id_alias'(Id, Alias) :-
|
|
|
|
recorded('$mutex_alias', [Id|Alias], _), !.
|
|
|
|
'$mutex_id_alias'(Id, Id).
|
|
|
|
|
|
|
|
|
2007-03-18 14:15:53 +00:00
|
|
|
mutex_create(Mutex) :-
|
|
|
|
( atom(Mutex) ->
|
|
|
|
mutex_create(_, [alias(Mutex)])
|
|
|
|
; mutex_create(Mutex, [])
|
|
|
|
).
|
|
|
|
|
|
|
|
mutex_create(Id, Options) :-
|
|
|
|
nonvar(Id), !,
|
|
|
|
'$do_error'(type_error(variable, Id), mutex_create(Id, Options)).
|
|
|
|
mutex_create(Id, Options) :-
|
|
|
|
Goal = mutex_create(Id, Options),
|
|
|
|
'$mutex_options'(Options, Alias, Goal),
|
|
|
|
( atom(Alias) ->
|
|
|
|
( recorded('$mutex_alias',[_| Alias], _) ->
|
|
|
|
'$do_error'(permission_error(create, mutex, Alias), Goal)
|
|
|
|
; '$new_mutex'(Id),
|
|
|
|
recorda('$mutex_alias', [Id| Alias], _)
|
|
|
|
)
|
|
|
|
; '$new_mutex'(Id),
|
|
|
|
recorda('$mutex_alias', [Id| Id], _)
|
|
|
|
).
|
|
|
|
|
|
|
|
'$mutex_options'(Var, _, Goal) :-
|
|
|
|
var(Var), !,
|
|
|
|
'$do_error'(instantiation_error, Goal).
|
|
|
|
'$mutex_options'([], _, _) :- !.
|
|
|
|
'$mutex_options'([Option| Options], Alias, Goal) :- !,
|
|
|
|
'$mutex_option'(Option, Alias, Goal),
|
|
|
|
'$mutex_options'(Options, Alias, Goal).
|
|
|
|
'$mutex_options'(Options, _, Goal) :-
|
|
|
|
'$do_error'(type_error(list, Options), Goal).
|
|
|
|
|
|
|
|
'$mutex_option'(Var, _, Goal) :-
|
|
|
|
var(Var), !,
|
|
|
|
'$do_error'(instantiation_error, Goal).
|
|
|
|
'$mutex_option'(alias(Alias), Alias, Goal) :- !,
|
|
|
|
( atom(Alias) ->
|
|
|
|
true
|
|
|
|
; '$do_error'(type_error(atom, Alias), Goal)
|
|
|
|
).
|
|
|
|
'$mutex_option'(Option, _, Goal) :-
|
|
|
|
'$do_error'(domain_error(mutex_option, Option), Goal).
|
|
|
|
|
|
|
|
/*
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_create(V) :-
|
|
|
|
var(V), !,
|
2004-02-11 01:20:56 +00:00
|
|
|
'$new_mutex'(V),
|
2007-01-28 16:00:31 +00:00
|
|
|
recorda('$mutex_alias',[V|V],_).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_create(A) :-
|
|
|
|
atom(A),
|
2007-01-28 16:00:31 +00:00
|
|
|
recorded('$mutex_alias',[_|A],_), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
'$do_error'(permission_error(create,mutex,A),mutex_create(A)).
|
|
|
|
mutex_create(A) :-
|
|
|
|
atom(A), !,
|
|
|
|
'$new_mutex'(Id),
|
2007-01-28 16:00:31 +00:00
|
|
|
recorda('$mutex_alias',[Id|A],_).
|
2004-01-23 02:23:51 +00:00
|
|
|
mutex_create(V) :-
|
|
|
|
'$do_error'(type_error(atom,V),mutex_create(V)).
|
2007-03-18 14:15:53 +00:00
|
|
|
*/
|
2004-01-23 02:23:51 +00:00
|
|
|
|
2007-01-28 16:00:31 +00:00
|
|
|
mutex_destroy(Mutex) :-
|
|
|
|
'$check_mutex_or_alias'(Mutex, mutex_destroy(Mutex)),
|
|
|
|
'$mutex_id_alias'(Id, Mutex),
|
2004-02-11 01:20:56 +00:00
|
|
|
'$destroy_mutex'(Id),
|
2007-01-28 16:00:31 +00:00
|
|
|
'$erase_mutex_info'(Id).
|
|
|
|
|
|
|
|
'$erase_mutex_info'(Id) :-
|
|
|
|
recorded('$mutex_alias',[Id|_],R),
|
|
|
|
erase(R),
|
|
|
|
fail.
|
|
|
|
'$erase_mutex_info'(_).
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
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) :-
|
2007-01-28 16:00:31 +00:00
|
|
|
recorded('$mutex_alias',[Id|A],_), !,
|
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) :-
|
2007-01-28 16:00:31 +00:00
|
|
|
recorded('$mutex_alias',[Id|A],_), !,
|
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)).
|
|
|
|
|
2007-01-28 16:00:31 +00:00
|
|
|
mutex_unlock(Mutex) :-
|
|
|
|
'$check_mutex_or_alias'(Mutex, mutex_unlock(Mutex)),
|
|
|
|
'$mutex_id_alias'(Id, Mutex),
|
2004-01-23 02:23:51 +00:00
|
|
|
( '$unlock_mutex'(Id) ->
|
|
|
|
true
|
|
|
|
;
|
2007-01-28 16:00:31 +00:00
|
|
|
'$do_error'(permission_error(unlock,mutex,Mutex),mutex_unlock(Mutex))
|
2004-01-23 02:23:51 +00:00
|
|
|
).
|
|
|
|
|
|
|
|
mutex_unlock_all :-
|
2007-01-01 18:11:24 +00:00
|
|
|
'$thread_self'(Tid),
|
|
|
|
'$unlock_all_thread_mutexes'(Tid).
|
|
|
|
|
|
|
|
'$unlock_all_thread_mutexes'(Tid) :-
|
2007-01-28 16:00:31 +00:00
|
|
|
recorded('$mutex_alias',[Id|_],_),
|
2007-01-01 18:11:24 +00:00
|
|
|
'$mutex_info'(Id, NRefs, Tid),
|
2004-01-23 02:23:51 +00:00
|
|
|
NRefs > 0,
|
|
|
|
'$mutex_unlock_all'(Id),
|
|
|
|
fail.
|
2007-01-01 18:11:24 +00:00
|
|
|
'$unlock_all_thread_mutexes'(_).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$mutex_unlock_all'(Id) :-
|
|
|
|
'$mutex_info'(Id, NRefs, _),
|
|
|
|
NRefs > 0,
|
|
|
|
'$unlock_mutex'(Id),
|
|
|
|
'$mutex_unlock_all'(Id).
|
2006-12-30 10:49:37 +00:00
|
|
|
|
|
|
|
with_mutex(M, G) :-
|
|
|
|
var(M), !,
|
|
|
|
'$do_error'(instantiation_error,with_mutex(M, G)).
|
|
|
|
with_mutex(M, G) :-
|
|
|
|
var(G), !,
|
|
|
|
'$do_error'(instantiation_error,with_mutex(M, G)).
|
|
|
|
with_mutex(M, G) :-
|
2007-01-08 11:30:38 +00:00
|
|
|
\+ callable(G), !,
|
2006-12-30 10:49:37 +00:00
|
|
|
'$do_error'(type_error(callable,G),with_mutex(M, G)).
|
|
|
|
with_mutex(M, G) :-
|
|
|
|
atom(M), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$with_mutex_mutex'(WMId),
|
2007-01-08 11:30:38 +00:00
|
|
|
'$lock_mutex'(WMId),
|
2007-01-28 16:00:31 +00:00
|
|
|
( recorded('$mutex_alias',[Id|M],_) ->
|
2006-12-30 10:49:37 +00:00
|
|
|
true
|
|
|
|
; '$new_mutex'(Id),
|
2007-01-28 16:00:31 +00:00
|
|
|
recorda('$mutex_alias',[Id|M],_)
|
2006-12-30 10:49:37 +00:00
|
|
|
),
|
|
|
|
'$lock_mutex'(Id),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$unlock_mutex'(WMId),
|
2007-01-08 11:30:38 +00:00
|
|
|
( catch('$execute'(G), E, ('$unlock_mutex'(Id), throw(E))) ->
|
|
|
|
'$unlock_mutex'(Id)
|
|
|
|
; '$unlock_mutex'(Id),
|
|
|
|
fail
|
|
|
|
).
|
2006-12-30 10:49:37 +00:00
|
|
|
with_mutex(M, G) :-
|
|
|
|
'$do_error'(type_error(atom,M),with_mutex(M, G)).
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
current_mutex(M, T, NRefs) :-
|
2007-01-28 16:00:31 +00:00
|
|
|
recorded('$mutex_alias',[Id|M],_),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$mutex_info'(Id, NRefs, T).
|
|
|
|
|
2007-03-16 09:38:48 +00:00
|
|
|
mutex_property(Mutex, Prop) :-
|
|
|
|
( nonvar(Mutex) ->
|
|
|
|
'$check_mutex_or_alias'(Mutex, mutex_property(Mutex, Prop))
|
|
|
|
; recorded('$mutex_alias', [_|Mutex], _)
|
|
|
|
),
|
|
|
|
'$check_mutex_property'(Prop, mutex_property(Mutex, Prop)),
|
|
|
|
'$mutex_id_alias'(Id, Mutex),
|
|
|
|
'$mutex_property'(Id, Prop).
|
|
|
|
|
|
|
|
'$mutex_property'(Id, alias(Alias)) :-
|
2007-03-18 14:15:53 +00:00
|
|
|
recorded('$mutex_alias', [Id|Alias], _),
|
|
|
|
Id \= Alias.
|
2007-10-22 13:58:27 +01:00
|
|
|
'$mutex_property'(Id, status(Status)) :-
|
2007-03-18 13:10:23 +00:00
|
|
|
'$mutex_info'(Id, Count, HoldingThread),
|
2007-10-22 13:58:27 +01:00
|
|
|
( Count =:= 0 ->
|
|
|
|
Status = unlocked
|
|
|
|
; % Count > 0,
|
|
|
|
'$thread_id_alias'(HoldingThread, Alias),
|
|
|
|
once((Thread = Alias; Thread = HoldingThread)),
|
|
|
|
Status = locked(Thread, Count)
|
|
|
|
).
|
|
|
|
/*
|
|
|
|
message_queue_create(Id, Options) :-
|
|
|
|
nonvar(Id), !,
|
|
|
|
'$do_error'(type_error(variable, Id), message_queue_create(Id, Options)).
|
|
|
|
message_queue_create(Id, Options) :-
|
|
|
|
var(Options), !,
|
|
|
|
'$do_error'(instantiation_error, message_queue_create(Id, Options)).
|
2008-02-09 13:04:32 +00:00
|
|
|
message_queue_create(Id, []) :- !,
|
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$new_mutex'(Mutex),
|
|
|
|
'$cond_create'(Cond),
|
|
|
|
'$thread_new_qid'(Id),
|
|
|
|
recorda('$queue',q(Id,Mutex,Cond,Id), _),
|
|
|
|
'$unlock_mutex'(QMutex).
|
2007-10-22 13:58:27 +01:00
|
|
|
message_queue_create(Id, [alias(Alias)]) :-
|
|
|
|
var(Alias), !,
|
2008-02-09 13:04:32 +00:00
|
|
|
'$do_error'(instantiation_error, message_queue_create(Id, [alias(Alias)])).
|
2007-10-22 13:58:27 +01:00
|
|
|
message_queue_create(Id, [alias(Alias)]) :-
|
2008-02-09 13:04:32 +00:00
|
|
|
\+ atom(Alias), !,
|
|
|
|
'$do_error'(type_error(atom,Alias), message_queue_create(Id, [alias(Alias)])).
|
2008-02-24 12:28:05 +00:00
|
|
|
message_queue_create(Id, [alias(Alias)]) :- !,
|
2008-02-09 13:04:32 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
|
|
|
'$new_mutex'(Mutex),
|
|
|
|
'$cond_create'(Cond),
|
|
|
|
'$thread_new_qid'(Id),
|
|
|
|
( recorded('$queue', q(Alias,_,_,_), _) ->
|
|
|
|
'$unlock_mutex'(QMutex),
|
|
|
|
'$do_error'(permission_error(create,queue,alias(Alias)),message_queue_create(Id, [alias(Alias)]))
|
|
|
|
; recorded('$thread_alias', [_,Alias], _) ->
|
|
|
|
'$unlock_mutex'(QMutex),
|
|
|
|
'$do_error'(permission_error(create,queue,alias(Alias)),message_queue_create(Id, [alias(Alias)]))
|
|
|
|
; recorda('$queue',q(Alias,Mutex,Cond,Id), _),
|
|
|
|
'$unlock_mutex'(QMutex)
|
|
|
|
).
|
2008-02-24 12:28:05 +00:00
|
|
|
message_queue_create(Id, [Option| _]) :-
|
|
|
|
'$do_error'(domain_error(queue_option, Option), message_queue_create(Id, [Option| _])).
|
|
|
|
message_queue_create(Id, Options) :-
|
|
|
|
'$do_error'(type_error(list, Options), message_queue_create(Id, Options)).
|
2007-10-22 13:58:27 +01:00
|
|
|
|
|
|
|
message_queue_create(Id) :-
|
2008-02-09 13:04:32 +00:00
|
|
|
( var(Id) -> % ISO DTR
|
2007-10-22 13:58:27 +01:00
|
|
|
message_queue_create(Id, [])
|
2008-02-09 13:04:32 +00:00
|
|
|
; atom(Id) -> % old behavior
|
2007-10-22 13:58:27 +01:00
|
|
|
message_queue_create(_, [alias(Id)])
|
2008-02-24 12:28:05 +00:00
|
|
|
; '$do_error'(type_error(variable, Id), message_queue_create(Id)).
|
2007-10-22 13:58:27 +01:00
|
|
|
).
|
|
|
|
*/
|
|
|
|
message_queue_create(_, [alias(Alias)]) :- % TEMPORARY FIX
|
|
|
|
message_queue_create(Alias).
|
2007-03-16 09:38:48 +00:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_create(Cond) :-
|
|
|
|
var(Cond), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$create_mq'(Cond).
|
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],_), !,
|
2006-04-05 01:16:55 +01:00
|
|
|
'$do_error'(permission_error(create,message_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) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$new_mutex'(Mutex),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$cond_create'(Cond),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$mq_iname'(Name, CName),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
|
|
|
( recorded('$queue',q(Name,_,_, _),_) ->
|
|
|
|
'$unlock_mutex'(QMutex),
|
|
|
|
'$do_error'(permission_error(create,message_queue,Name),message_queue_create(Name))
|
|
|
|
;
|
|
|
|
recorda('$queue',q(Name,Mutex,Cond, CName),_),
|
|
|
|
'$unlock_mutex'(QMutex)
|
|
|
|
).
|
2004-03-02 16:44:58 +00:00
|
|
|
|
|
|
|
'$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) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
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),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$destroy_mutex'(Mutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$clean_mqueue'(CName).
|
2004-01-23 02:23:51 +00:00
|
|
|
message_queue_destroy(Queue) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
|
|
|
atomic(Queue), !,
|
2006-04-05 01:16:55 +01:00
|
|
|
'$do_error'(existence_error(message_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
|
|
|
|
2007-09-18 18:50:12 +01:00
|
|
|
thread_send_message(Term) :-
|
|
|
|
'$thread_self'(Id),
|
|
|
|
thread_send_message(Id, Term).
|
|
|
|
|
2006-05-25 11:46:23 +01:00
|
|
|
thread_send_message(Queue, Term) :- var(Queue), !,
|
|
|
|
'$do_error'(instantiation_error,thread_send_message(Queue,Term)).
|
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) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
recorded('$queue',q(Queue,Mutex,Cond,Key),_), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$lock_mutex'(Mutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
recordz(Key,Term,_),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$cond_broadcast'(Cond),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$unlock_mutex'(Mutex).
|
2006-04-05 01:16:55 +01:00
|
|
|
thread_send_message(Queue, Term) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
'$do_error'(existence_error(message_queue,Queue),thread_send_message(Queue,Term)).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
thread_get_message(Term) :-
|
|
|
|
'$thread_self'(Id),
|
|
|
|
thread_get_message(Id, Term).
|
|
|
|
|
2006-05-25 11:46:23 +01:00
|
|
|
thread_get_message(Queue, Term) :- var(Queue), !,
|
|
|
|
'$do_error'(instantiation_error,thread_get_message(Queue,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) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
recorded('$queue',q(Queue,Mutex,Cond,Key),_), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$lock_mutex'(Mutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_get_message_loop'(Key, Term, Mutex, Cond).
|
2006-04-05 01:16:55 +01:00
|
|
|
thread_get_message(Queue, Term) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
'$do_error'(existence_error(message_queue,Queue),thread_get_message(Queue,Term)).
|
|
|
|
|
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),
|
2008-01-27 11:01:07 +00:00
|
|
|
'$unlock_mutex'(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).
|
|
|
|
|
2006-05-25 11:46:23 +01:00
|
|
|
thread_peek_message(Queue, Term) :- var(Queue), !,
|
|
|
|
'$do_error'(instantiation_error,thread_peek_message(Queue,Term)).
|
2006-04-22 12:51:38 +01:00
|
|
|
thread_peek_message(Queue, Term) :-
|
|
|
|
recorded('$thread_alias',[Id|Queue],_), !,
|
|
|
|
thread_peek_message(Id, Term).
|
2004-01-23 02:23:51 +00:00
|
|
|
thread_peek_message(Queue, Term) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$lock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
recorded('$queue',q(Queue,Mutex,_,Key),_), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$lock_mutex'(Mutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2004-03-02 16:44:58 +00:00
|
|
|
'$thread_peek_message2'(Key, Term, Mutex).
|
2006-04-05 01:16:55 +01:00
|
|
|
thread_peek_message(Queue, Term) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$global_queue_mutex'(QMutex),
|
|
|
|
'$unlock_mutex'(QMutex),
|
2006-04-05 01:16:55 +01:00
|
|
|
'$do_error'(existence_error(message_queue,Queue),thread_peek_message(Queue,Term)).
|
|
|
|
|
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,_), !,
|
2008-01-27 11:01:07 +00:00
|
|
|
'$unlock_mutex'(Mutex).
|
2004-02-21 20:25:45 +00:00
|
|
|
'$thread_peek_message2'(_, _, Mutex) :-
|
2008-01-27 11:01:07 +00:00
|
|
|
'$unlock_mutex'(Mutex),
|
2004-01-23 02:23:51 +00:00
|
|
|
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),
|
2005-03-15 18:29:25 +00:00
|
|
|
(Mod \= idb -> '$flags'(T,Mod,F,F) ; true),
|
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)).
|
|
|
|
|
|
|
|
|
2006-05-25 17:28:28 +01:00
|
|
|
thread_sleep(Time) :-
|
|
|
|
var(Time), !,
|
|
|
|
'$do_error'(instantiation_error,thread_sleep(Time)).
|
|
|
|
thread_sleep(Time) :-
|
2006-12-31 16:10:29 +00:00
|
|
|
integer(Time), !,
|
|
|
|
( Time > 0 ->
|
|
|
|
'$thread_sleep'(Time,0,_,_)
|
|
|
|
; true
|
|
|
|
).
|
2006-05-25 17:28:28 +01:00
|
|
|
thread_sleep(Time) :-
|
2006-12-31 16:10:29 +00:00
|
|
|
float(Time), !,
|
2007-01-01 16:54:39 +00:00
|
|
|
( Time > 0.0 ->
|
2006-12-31 16:10:29 +00:00
|
|
|
STime is integer(float_integer_part(Time)),
|
|
|
|
NTime is integer(float_fractional_part(Time))*1000000000,
|
|
|
|
'$thread_sleep'(STime,NTime,_,_)
|
|
|
|
; true
|
|
|
|
).
|
2006-05-25 17:28:28 +01:00
|
|
|
thread_sleep(Time) :-
|
|
|
|
'$do_error'(type_error(number,Time),thread_sleep(Time)).
|
|
|
|
|
|
|
|
|
2007-01-01 18:11:24 +00:00
|
|
|
thread_signal(Id, Goal) :-
|
|
|
|
'$check_thread_or_alias'(Id, thread_signal(Id, Goal)),
|
2007-01-24 14:20:04 +00:00
|
|
|
'$check_callable'(Goal, thread_signal(Id, Goal)),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id),
|
2007-01-01 18:11:24 +00:00
|
|
|
( recorded('$thread_signal', [Id0| _], R), erase(R), fail
|
|
|
|
; true
|
|
|
|
),
|
|
|
|
recorda('$thread_signal', [Id0| Goal], _),
|
|
|
|
'$signal_thread'(Id0).
|
2004-02-05 16:57:02 +00:00
|
|
|
|
|
|
|
'$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).
|
2006-05-26 00:46:57 +01:00
|
|
|
|
|
|
|
|
2007-01-01 19:40:17 +00:00
|
|
|
thread_property(Prop) :-
|
|
|
|
'$check_thread_property'(Prop, thread_property(Prop)),
|
|
|
|
'$thread_self'(Id),
|
|
|
|
'$thread_property'(Id, Prop).
|
|
|
|
|
|
|
|
thread_property(Id, Prop) :-
|
|
|
|
( nonvar(Id) ->
|
|
|
|
'$check_thread_or_alias'(Id, thread_property(Id, Prop))
|
2008-02-24 12:40:40 +00:00
|
|
|
; recorded('$thread_sizes', [Id| _], _)
|
2007-01-01 19:40:17 +00:00
|
|
|
),
|
|
|
|
'$check_thread_property'(Prop, thread_property(Id, Prop)),
|
2007-01-02 03:01:20 +00:00
|
|
|
'$thread_id_alias'(Id0, Id),
|
2007-01-01 19:40:17 +00:00
|
|
|
'$thread_property'(Id0, Prop).
|
|
|
|
|
|
|
|
'$thread_property'(Id, alias(Alias)) :-
|
|
|
|
recorded('$thread_alias', [Id|Alias], _).
|
|
|
|
'$thread_property'(Id, status(Status)) :-
|
2007-01-02 02:51:31 +00:00
|
|
|
( recorded('$thread_exit_status', [Id|Exit], _) ->
|
|
|
|
Status = Exit
|
|
|
|
; Status = running
|
|
|
|
).
|
|
|
|
'$thread_property'(Id, detached(Detached)) :-
|
|
|
|
recorded('$thread_detached', [Id|Detached], _).
|
|
|
|
'$thread_property'(Id, stack(Stack)) :-
|
|
|
|
recorded('$thread_sizes', [Id, Stack, _, _], _).
|
|
|
|
'$thread_property'(Id, trail(Trail)) :-
|
|
|
|
recorded('$thread_sizes', [Id, _, Trail, _], _).
|
|
|
|
'$thread_property'(Id, system(System)) :-
|
|
|
|
recorded('$thread_sizes', [Id, _, _, System], _).
|
|
|
|
|
|
|
|
|
2006-05-26 00:46:57 +01:00
|
|
|
threads :-
|
2007-01-02 02:51:31 +00:00
|
|
|
write('------------------------------------------------------------------------'), nl,
|
|
|
|
format("~t~a~48+~n", 'Thread Detached Status'),
|
|
|
|
write('------------------------------------------------------------------------'), nl,
|
2008-02-24 12:40:40 +00:00
|
|
|
'$thread_property'(Id, detached(Detached)),
|
2008-02-23 11:31:13 +00:00
|
|
|
'$thread_property'(Id, status(Status)),
|
|
|
|
'$thread_id_alias'(Id, Alias),
|
2007-01-02 02:51:31 +00:00
|
|
|
format("~t~q~30+~33|~w~42|~q~n", [Alias, Detached, Status]),
|
2006-05-26 00:46:57 +01:00
|
|
|
fail.
|
|
|
|
threads :-
|
2007-01-02 02:51:31 +00:00
|
|
|
write('------------------------------------------------------------------------'), nl.
|
2007-01-01 18:11:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
'$check_thread_or_alias'(Term, Goal) :-
|
|
|
|
var(Term), !,
|
|
|
|
'$do_error'(instantiation_error, Goal).
|
|
|
|
'$check_thread_or_alias'(Term, Goal) :-
|
|
|
|
\+ integer(Term), \+ atom(Term), !,
|
|
|
|
'$do_error'(domain_error(thread_or_alias, Term), Goal).
|
|
|
|
'$check_thread_or_alias'(Term, Goal) :-
|
|
|
|
atom(Term), \+ recorded('$thread_alias',[_|Term],_), !,
|
|
|
|
'$do_error'(existence_error(thread, Term), Goal).
|
|
|
|
'$check_thread_or_alias'(Term, Goal) :-
|
|
|
|
integer(Term), \+ '$valid_thread'(Term), !,
|
|
|
|
'$do_error'(existence_error(thread, Term), Goal).
|
|
|
|
'$check_thread_or_alias'(_,_).
|
2007-01-01 19:40:17 +00:00
|
|
|
|
|
|
|
'$check_thread_property'(Term, _) :-
|
|
|
|
var(Term), !.
|
|
|
|
'$check_thread_property'(alias(_), _) :- !.
|
|
|
|
'$check_thread_property'(detached(_), _) :- !.
|
|
|
|
'$check_thread_property'(status(_), _) :- !.
|
2007-01-02 02:51:31 +00:00
|
|
|
'$check_thread_property'(stack(_), _) :- !.
|
|
|
|
'$check_thread_property'(trail(_), _) :- !.
|
|
|
|
'$check_thread_property'(system(_), _) :- !.
|
2007-01-01 19:40:17 +00:00
|
|
|
'$check_thread_property'(Term, Goal) :-
|
|
|
|
'$do_error'(domain_error(thread_property, Term), Goal).
|
2007-01-28 16:00:31 +00:00
|
|
|
|
|
|
|
'$check_mutex_or_alias'(Term, Goal) :-
|
|
|
|
var(Term), !,
|
|
|
|
'$do_error'(instantiation_error, Goal).
|
|
|
|
'$check_mutex_or_alias'(Term, Goal) :-
|
|
|
|
\+ integer(Term), \+ atom(Term), !,
|
|
|
|
'$do_error'(domain_error(mutex_or_alias, Term), Goal).
|
|
|
|
'$check_mutex_or_alias'(Term, Goal) :-
|
|
|
|
atom(Term), \+ recorded('$mutex_alias',[_|Term],_), !,
|
|
|
|
'$do_error'(existence_error(mutex, Term), Goal).
|
|
|
|
'$check_mutex_or_alias'(Term, Goal) :-
|
|
|
|
% integer(Term), \+ '$valid_mutex'(Term), !,
|
|
|
|
integer(Term), \+ recorded('$mutex_alias',[Term|_],_), !,
|
|
|
|
'$do_error'(existence_error(mutex, Term), Goal).
|
|
|
|
'$check_mutex_or_alias'(_,_).
|
|
|
|
|
|
|
|
'$check_mutex_property'(Term, _) :-
|
|
|
|
var(Term), !.
|
|
|
|
'$check_mutex_property'(alias(_), _) :- !.
|
2007-10-22 13:58:27 +01:00
|
|
|
'$check_mutex_property'(status(Status), Goal) :- !,
|
|
|
|
( var(Status) ->
|
|
|
|
true
|
|
|
|
; Status = unlocked ->
|
|
|
|
true
|
|
|
|
; Status = locked(_, _) ->
|
|
|
|
true
|
|
|
|
; '$do_error'(domain_error(mutex_property, status(Status)), Goal)
|
|
|
|
).
|
2007-03-16 09:38:48 +00:00
|
|
|
'$check_mutex_property'(Term, Goal) :-
|
|
|
|
'$do_error'(domain_error(mutex_property, Term), Goal).
|