Skip to content

Commit 160c6a2

Browse files
committed
[io grant] Document IO::Handle.lock/.unlock
Rakudo impl: rakudo/rakudo@214198bfb2 Tests: Raku/roast@4194755c19
1 parent ed7403c commit 160c6a2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

doc/Type/IO/Handle.pod6

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,49 @@ for $data-file.lines -> $line {
6464
@data.push($line.split(','))
6565
}
6666
67+
=head2 method lock
68+
69+
method lock(IO::Handle:D:
70+
Bool:D :$non-blocking = False, Bool:D :$shared = False --> True
71+
)
72+
73+
Places an advisory lock on the filehandle. If C<:$non-blocking> is C<True>
74+
will L«C<fail>|/routine/fail» with C<X::IO::Lock> if lock could not be
75+
obtained, otherwise will block until the lock can be placed. If C<:$shared>
76+
is C<True> will place a shared (read) lock, otherwise will place an
77+
exclusive (read) lock. On success, returns C<True>; L«fails|/routine/fail»
78+
with C<X::IO::Lock> if lock cannot be placed (e.g. when trying to place
79+
a shared lock on a filehandle opened in write mode or trying to
80+
place an exclusive lock on a filehandle opened in read mode).
81+
82+
You can use C<lock> again to replace an existing lock with another one.
83+
Use L«C<close>|/routine/close» the filehandle or use
84+
L«C<unlock>|/routine/unlock» to remove a lock.
85+
86+
=for code :skip-test
87+
# One program writes, the other reads, and thanks to locks either
88+
# will wait for the other to finish before proceeding to read/write
89+
90+
# Writer
91+
with "foo".IO.open(:w) {
92+
.lock;
93+
.spurt: "I ♥ Perl 6!";
94+
.close;
95+
}
96+
97+
# Reader
98+
with "foo".IO.open {
99+
.lock: :shared;
100+
.slurp.say; # OUTPUT: «I ♥ Perl 6!␤»
101+
.close;
102+
}
103+
104+
=head2 method unlock
105+
106+
method unlock(IO::Handle:D: --> True)
107+
108+
Removes a L«C<lock>|/routine/lock» from the filehandle.
109+
67110
=head2 method words
68111
69112
method words($count = Inf)

0 commit comments

Comments
 (0)