Skip to content

Commit 110333e

Browse files
committed
Add an impure_init/0 function to thread.mvar.m
This change renames the init/0 function in thread.mvar.m to impure_init/0; deprecating the old function. This makss this module more consistent with thread.semaphore.m. Remove the "mvar." prefix where it is unnecessary. Add %----% separators between some definitions in the implementation section. library/thread.mvar.m: As above.
1 parent ece1b7f commit 110333e

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

library/thread.mvar.m

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,45 @@
3434

3535
% Create an empty mvar.
3636
%
37-
:- impure func mvar.init = (mvar(T)::uo) is det.
37+
:- pred init(mvar(T)::out, io::di, io::uo) is det.
3838

3939
% Create an empty mvar.
4040
%
41-
:- pred mvar.init(mvar(T)::out, io::di, io::uo) is det.
41+
:- impure func impure_init = (mvar(T)::uo) is det.
42+
43+
% Create an empty mvar.
44+
%
45+
% This has been renamed to impure_init.
46+
%
47+
:- impure func init = (mvar(T)::uo) is det.
48+
:- pragma obsolete(init/0).
4249

4350
% Take the contents of the mvar out leaving the mvar empty.
4451
% If the mvar is empty, block until some thread fills the mvar.
4552
%
46-
:- pred mvar.take(mvar(T)::in, T::out, io::di, io::uo) is det.
53+
:- pred take(mvar(T)::in, T::out, io::di, io::uo) is det.
4754

4855
% Take the contents of the mvar out leaving the mvar empty.
4956
% Returns immediately with no if the mvar was empty, or yes(X) if
5057
% the mvar contained X.
5158
%
52-
:- pred mvar.try_take(mvar(T)::in, maybe(T)::out, io::di, io::uo) is det.
59+
:- pred try_take(mvar(T)::in, maybe(T)::out, io::di, io::uo) is det.
5360

5461
% Place the value of type T into an empty mvar.
5562
% If the mvar is full block until it becomes empty.
5663
%
57-
:- pred mvar.put(mvar(T)::in, T::in, io::di, io::uo) is det.
64+
:- pred put(mvar(T)::in, T::in, io::di, io::uo) is det.
5865

5966
% Place the value of type T into an empty mvar, returning yes on success.
6067
% If the mvar is full, return no immediately without blocking.
6168
%
62-
:- pred mvar.try_put(mvar(T)::in, T::in, bool::out, io::di, io::uo) is det.
69+
:- pred try_put(mvar(T)::in, T::in, bool::out, io::di, io::uo) is det.
6370

6471
% Read the contents of mvar, without taking it out.
6572
% If the mvar is empty, block until it is full.
6673
% This is equivalent to mvar.take followed by mvar.put.
6774
%
68-
:- pred mvar.read(mvar(T)::in, T::out, io::di, io::uo) is det.
75+
:- pred read(mvar(T)::in, T::out, io::di, io::uo) is det.
6976

7077
%-----------------------------------------------------------------------------%
7178
%-----------------------------------------------------------------------------%
@@ -85,17 +92,24 @@
8592
mutvar(T) % data
8693
).
8794

88-
mvar.init(Mvar, !IO) :-
95+
%-----------------------------------------------------------------------------%
96+
97+
init(Mvar, !IO) :-
8998
promise_pure (
90-
impure Mvar = mvar.init
99+
impure Mvar = impure_init
91100
).
92101

93-
mvar.init = mvar(Full, Empty, Ref) :-
102+
impure_init = mvar(Full, Empty, Ref) :-
94103
impure semaphore.impure_init(0, Full),
95104
impure semaphore.impure_init(1, Empty), % Initially a mvar starts empty.
96105
impure new_mutvar0(Ref).
97106

98-
mvar.take(mvar(Full, Empty, Ref), Data, !IO) :-
107+
init = Mvar :-
108+
impure Mvar = impure_init.
109+
110+
%-----------------------------------------------------------------------------%
111+
112+
take(mvar(Full, Empty, Ref), Data, !IO) :-
99113
promise_pure (
100114
semaphore.wait(Full, !IO),
101115
impure get_mutvar(Ref, Data),
@@ -104,7 +118,9 @@ impure clear_mutvar(Ref),
104118
semaphore.signal(Empty, !IO)
105119
).
106120

107-
mvar.try_take(mvar(Full, Empty, Ref), MaybeData, !IO) :-
121+
%-----------------------------------------------------------------------------%
122+
123+
try_take(mvar(Full, Empty, Ref), MaybeData, !IO) :-
108124
promise_pure (
109125
semaphore.try_wait(Full, Success, !IO),
110126
(
@@ -120,14 +136,18 @@ impure clear_mutvar(Ref),
120136
)
121137
).
122138

123-
mvar.put(mvar(Full, Empty, Ref), Data, !IO) :-
139+
%-----------------------------------------------------------------------------%
140+
141+
put(mvar(Full, Empty, Ref), Data, !IO) :-
124142
promise_pure (
125143
semaphore.wait(Empty, !IO),
126144
impure set_mutvar(Ref, Data),
127145
semaphore.signal(Full, !IO)
128146
).
129147

130-
mvar.try_put(mvar(Full, Empty, Ref), Data, Success, !IO) :-
148+
%-----------------------------------------------------------------------------%
149+
150+
try_put(mvar(Full, Empty, Ref), Data, Success, !IO) :-
131151
promise_pure (
132152
semaphore.try_wait(Empty, Success, !IO),
133153
(
@@ -139,7 +159,9 @@ impure set_mutvar(Ref, Data),
139159
)
140160
).
141161

142-
mvar.read(mvar(Full, _Empty, Ref), Data, !IO) :-
162+
%-----------------------------------------------------------------------------%
163+
164+
read(mvar(Full, _Empty, Ref), Data, !IO) :-
143165
promise_pure (
144166
semaphore.wait(Full, !IO),
145167
impure get_mutvar(Ref, Data),

0 commit comments

Comments
 (0)