1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8%
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License.
13%
14% The Original Code is  The ECLiPSe Constraint Logic Programming System.
15% The Initial Developer of the Original Code is  Cisco Systems, Inc.
16% Portions created by the Initial Developer are
17% Copyright (C) 1994-2006 Cisco Systems, Inc.  All Rights Reserved.
18%
19% Contributor(s): ECRC GmbH.
20%
21% END LICENSE BLOCK
22
23:- module_interface(wake).
24
25:- export
26	default_wake/0,
27	get_first_suspension/1,
28	get_suspension_counter/1,
29	set_first_suspension/1,
30	set_suspension_counter/1,
31	suspension_mark/3,
32	trace_wake/0.
33
34:- begin_module(wake).
35
36:- import
37	trace_propagation/4,
38	trace_suspension/4,
39	get_parent/1,
40	set_parent/1
41    from grace.
42
43:- import
44	call_suspension/1,
45	first_woken/2,
46	get_flag_body/4,
47	get_priority/1,
48	get_suspension_number/2,
49	last_scheduled/1,
50	last_suspension/1,
51	new_scheduled/2,
52	new_suspensions/2,
53	set_priority/1,
54	set_suspension_number/2
55    from sepia_kernel.
56
57:- local
58	wake/0.
59
60:- make_local_array(suspension_counter),
61   make_local_array(first_suspension).
62
63trace_wake :-
64    get_flag(wake/0, visibility, local) ->
65	call(export(wake/0), sepia_kernel),
66	global(wake/0)
67    ;
68	true.
69
70default_wake :-
71    get_flag_body(wake/0, visibility, exported, sepia_kernel) ->
72	local(wake/0),
73	call(global(wake/0), sepia_kernel)
74    ;
75	true.
76
77wake :-
78    get_priority(Prio),
79    wake_loop(Prio).
80
81wake_loop(Prio) :-
82    (first_woken(Prio, Susp) ->
83	trace_and_call(Susp),
84	wake_goals(Prio)
85    ;
86	set_priority(Prio)
87    ).
88
89wake_goals(Prio) :-
90    (first_woken(Prio, Susp) ->
91	trace_and_call(Susp),
92	wake_goals(Prio)
93    ;
94	set_priority(Prio)
95    ).
96
97trace_and_call(Susp) :-
98    suspension_to_goal(Susp, Goal, Module),
99    (Module = grace ->
100	% our goal
101	call_suspension(Susp)
102    ;
103	trace_suspension('CALL', Goal, Mark, Module),
104	(call_suspension(Susp) ->
105	    trace_suspension('EXIT', Goal, Mark, Module)
106	;
107	    trace_suspension('**FAIL', Goal, Mark, Module),
108	    fail
109	)
110    ).
111
112set_first_suspension(_).
113set_suspension_counter(_).
114