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,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).
|
||||
|
||||
|
Reference in New Issue
Block a user