Logtalk 2.30.2 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1908 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -51,7 +51,7 @@
|
||||
comment is 'New friend, watch out for his/her birthday.',
|
||||
argnames is ['Name']]).
|
||||
|
||||
% new agents are created as threaded objects:
|
||||
% new agents are created as multi-threading enabled objects:
|
||||
new(Name, Age, Gender) :-
|
||||
this(This),
|
||||
create_object(Name, [extends(This)], [threaded], [age(Age), gender(Gender)]).
|
||||
@@ -70,7 +70,7 @@
|
||||
write('Thanks! Here, have a slice of cake, '), write(From), write('.'), nl,
|
||||
threaded_ignore(From::cake_slice(Self)). % we don't care what happens with the cake slice
|
||||
|
||||
% be nice, give thanks for the cake offer:
|
||||
% be nice, give thanks when someone offer us a slice of cake:
|
||||
cake_slice(From) :-
|
||||
write('Thanks for the cake '), write(From), write('!'), nl,
|
||||
threaded_exit(From::age(Age)), % retrieve our friend's (old) age
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
|
||||
:- category(using). % we can call the threaded_wait/1 and threaded_notify/1 predicates from category
|
||||
% predicates; the importing object threads are used for exchanging notifications
|
||||
% predicates; the importing object message queues are used for exchanging notifications
|
||||
:- public([pick_up/0, release/0]).
|
||||
|
||||
pick_up :-
|
||||
@@ -15,7 +15,7 @@
|
||||
:- object(chalk,
|
||||
imports(using)).
|
||||
|
||||
:- threaded. % the chalk's thread is used for exchanging notifications
|
||||
:- threaded. % the chalk's message queue is used for exchanging notifications
|
||||
:- initialization(::release). % make the chalk initially available
|
||||
|
||||
:- end_object.
|
||||
@@ -24,7 +24,7 @@
|
||||
:- object(eraser,
|
||||
imports(using)).
|
||||
|
||||
:- threaded. % the eraser's thread is used for exchanging notifications
|
||||
:- threaded. % the eraser's message queue is used for exchanging notifications
|
||||
:- initialization(::release). % make the eraser initially available
|
||||
|
||||
:- end_object.
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,34 +1,82 @@
|
||||
|
||||
:- object(buffer).
|
||||
:- object(buffer(_MaxCapacity)).
|
||||
|
||||
:- info([
|
||||
version is 2.0,
|
||||
author is 'Paulo Moura',
|
||||
date is 2007/6/20,
|
||||
comment is 'Producer-consumer problem with a bounded buffer.']).
|
||||
|
||||
:- threaded.
|
||||
|
||||
:- public([put/1, get/1]).
|
||||
:- public(put/1).
|
||||
:- dynamic(put/1).
|
||||
:- mode(put(?integer), one).
|
||||
:- info(put/1, [
|
||||
comment is 'Put an item in the buffer.']).
|
||||
|
||||
:- private(item/1).
|
||||
:- dynamic(item/1).
|
||||
:- public(get/1).
|
||||
:- dynamic(get/1).
|
||||
:- mode(get(?integer), one).
|
||||
:- info(get/1, [
|
||||
comment is 'Get an item from the buffer.']).
|
||||
|
||||
put(N) :-
|
||||
( N > 0 % wait until the previous item is consumed
|
||||
-> NP is N - 1, threaded_wait(consumed(NP)) % (except for the first item!)
|
||||
; true
|
||||
),
|
||||
assertz(item(N)),
|
||||
:- private(item_/1).
|
||||
:- dynamic(item_/1).
|
||||
|
||||
:- private(size_/1).
|
||||
:- dynamic(size_/1).
|
||||
|
||||
size_(0).
|
||||
|
||||
put(Item) :-
|
||||
parameter(1, MaxCapacity),
|
||||
size_(N),
|
||||
( N =:= MaxCapacity -> % if the maximum buffer capacity have been
|
||||
threaded_wait(vacancy), % reached, wait until an item is consumed
|
||||
put(Item) % be sure to consume all "vacancy" notifications before proceeding
|
||||
; put_item(Item),
|
||||
( N =:= 0 ->
|
||||
threaded_notify(not_empty)
|
||||
; true
|
||||
)
|
||||
).
|
||||
|
||||
get(Item) :-
|
||||
parameter(1, MaxCapacity),
|
||||
size_(N),
|
||||
( N =:= 0 -> % if the buffer is empty, wait
|
||||
threaded_wait(not_empty), % until an item is produced
|
||||
get(Item) % be sure to consume all "not_empty" notifications before proceeding
|
||||
; get_item(Item),
|
||||
( N =:= MaxCapacity ->
|
||||
threaded_notify(vacancy)
|
||||
; true
|
||||
)
|
||||
).
|
||||
|
||||
:- synchronized([put_item/1, get_item/1]).
|
||||
|
||||
put_item(Item) :-
|
||||
assertz(item_(Item)),
|
||||
retract(size_(N)),
|
||||
N2 is N + 1,
|
||||
assertz(size_(N2)),
|
||||
sender(Sender),
|
||||
writeq(Sender), write(' wrote item '), write(N), nl,
|
||||
threaded_notify(produced(N)). % notify consumer that a new item is available
|
||||
writeq(Sender), write(' stored item '), write(Item), nl.
|
||||
|
||||
get(N) :-
|
||||
threaded_wait(produced(N)), % wait until an item is available
|
||||
retract(item(N)),
|
||||
get_item(Item) :-
|
||||
retract(item_(Item)),
|
||||
retract(size_(N)),
|
||||
N2 is N - 1,
|
||||
assertz(size_(N2)),
|
||||
sender(Sender),
|
||||
writeq(Sender), write(' read item '), write(N), nl,
|
||||
threaded_notify(consumed(N)). % notify producer that the item was consumed
|
||||
writeq(Sender), write(' consumed item '), write(Item), nl.
|
||||
|
||||
:- end_object.
|
||||
|
||||
|
||||
:- object(producer).
|
||||
:- object(producer(_MaxTime)).
|
||||
|
||||
:- public(run/1).
|
||||
|
||||
@@ -38,16 +86,17 @@
|
||||
run(N, N) :- !.
|
||||
run(M, N) :-
|
||||
M < N,
|
||||
random::random(1, 5, Random), % simulate a variable amount of
|
||||
thread_sleep(Random), % time to produce a new item
|
||||
buffer::put(M),
|
||||
parameter(1, MaxTime),
|
||||
random::random(1, MaxTime, Random), % simulate a variable amount of
|
||||
thread_sleep(Random), % time to produce a new item
|
||||
buffer(7)::put(M),
|
||||
M2 is M + 1,
|
||||
run(M2, N).
|
||||
|
||||
:- end_object.
|
||||
|
||||
|
||||
:- object(consumer).
|
||||
:- object(consumer(_MaxTime)).
|
||||
|
||||
:- public(run/1).
|
||||
|
||||
@@ -57,9 +106,10 @@
|
||||
run(N, N) :- !.
|
||||
run(M, N) :-
|
||||
M < N,
|
||||
random::random(1, 5, Random), % simulate a variable amount of
|
||||
thread_sleep(Random), % time to consume an item
|
||||
buffer::get(M),
|
||||
parameter(1, MaxTime),
|
||||
random::random(1, MaxTime, Random), % simulate a variable amount of
|
||||
thread_sleep(Random), % time to produce a new item
|
||||
buffer(7)::get(_Item),
|
||||
M2 is M + 1,
|
||||
run(M2, N).
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
@@ -1,6 +1,6 @@
|
||||
================================================================
|
||||
Logtalk - Open source object-oriented logic programming language
|
||||
Release 2.30.1
|
||||
Release 2.30.2
|
||||
|
||||
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
||||
================================================================
|
||||
|
Reference in New Issue
Block a user