@@ -91,8 +91,6 @@ function eof(s::LibuvStream)
91
91
! isopen (s) && nb_available (s)<= 0
92
92
end
93
93
94
- const DEFAULT_READ_BUFFER_SZ = 10485760 # 10 MB
95
-
96
94
const StatusUninit = 0 # handle is allocated, but not initialized
97
95
const StatusInit = 1 # handle is valid, but not connected/active
98
96
const StatusConnecting = 2 # handle is in process of connecting
@@ -148,7 +146,6 @@ type PipeEndpoint <: LibuvStream
148
146
closenotify:: Condition
149
147
sendbuf:: Nullable{IOBuffer}
150
148
lock:: ReentrantLock
151
- throttle:: Int
152
149
153
150
PipeEndpoint (handle:: Ptr{Void} = C_NULL ) = new (
154
151
handle,
@@ -158,8 +155,7 @@ type PipeEndpoint <: LibuvStream
158
155
false ,Condition (),
159
156
false ,Condition (),
160
157
false ,Condition (),
161
- nothing , ReentrantLock (),
162
- DEFAULT_READ_BUFFER_SZ)
158
+ nothing , ReentrantLock ())
163
159
end
164
160
165
161
type PipeServer <: LibuvServer
@@ -202,7 +198,6 @@ type TTY <: LibuvStream
202
198
closenotify:: Condition
203
199
sendbuf:: Nullable{IOBuffer}
204
200
lock:: ReentrantLock
205
- throttle:: Int
206
201
@windows_only ispty:: Bool
207
202
function TTY (handle)
208
203
tty = new (
@@ -212,8 +207,7 @@ type TTY <: LibuvStream
212
207
PipeBuffer (),
213
208
false ,Condition (),
214
209
false ,Condition (),
215
- nothing , ReentrantLock (),
216
- DEFAULT_READ_BUFFER_SZ)
210
+ nothing , ReentrantLock ())
217
211
@windows_only tty. ispty = ccall (:jl_ispty , Cint, (Ptr{Void},), handle)!= 0
218
212
tty
219
213
end
@@ -289,6 +283,15 @@ function init_stdio(handle::Ptr{Void})
289
283
end
290
284
end
291
285
286
+ function stream_wait (x,c... )
287
+ preserve_handle (x)
288
+ try
289
+ return wait (c... )
290
+ finally
291
+ unpreserve_handle (x)
292
+ end
293
+ end
294
+
292
295
function reinit_stdio ()
293
296
global uv_jl_asynccb = cfunction (uv_asynccb, Void, (Ptr{Void},))
294
297
global uv_jl_timercb = cfunction (uv_timercb, Void, (Ptr{Void},))
@@ -324,51 +327,27 @@ end
324
327
function wait_connected (x:: Union{LibuvStream, LibuvServer} )
325
328
check_open (x)
326
329
while x. status == StatusConnecting
327
- stream_wait (x, x. connectnotify)
330
+ stream_wait (x,x. connectnotify)
328
331
check_open (x)
329
332
end
330
333
end
331
334
335
+
332
336
function wait_readbyte (x:: LibuvStream , c:: UInt8 )
333
- preserve_handle (x)
334
- try
335
- while isopen (x) && search (x. buffer, c) <= 0
336
- start_reading (x) # ensure we are reading
337
- wait (x. readnotify)
338
- end
339
- finally
340
- if isempty (x. readnotify. waitq)
341
- stop_reading (x) # stop reading iff there are currently no other read clients of the stream
342
- end
343
- unpreserve_handle (x)
337
+ while isopen (x) && search (x. buffer,c) <= 0
338
+ start_reading (x)
339
+ stream_wait (x,x. readnotify)
344
340
end
345
341
end
346
342
347
343
function wait_readnb (x:: LibuvStream , nb:: Int )
348
- oldthrottle = x. throttle
349
- preserve_handle (x)
350
- try
351
- while isopen (x) && nb_available (x. buffer) < nb
352
- x. throttle = max (nb, x. throttle)
353
- start_reading (x) # ensure we are reading
354
- wait (x. readnotify)
355
- end
356
- finally
357
- if oldthrottle <= x. throttle <= nb
358
- x. throttle = oldthrottle
359
- end
360
- if isempty (x. readnotify. waitq)
361
- stop_reading (x) # stop reading iff there are currently no other read clients of the stream
362
- end
363
- unpreserve_handle (x)
344
+ while isopen (x) && nb_available (x. buffer) < nb
345
+ start_reading (x)
346
+ stream_wait (x,x. readnotify)
364
347
end
365
348
end
366
349
367
- function wait_close (x:: Union{LibuvStream, LibuvServer} )
368
- if isopen (x)
369
- stream_wait (x, x. closenotify)
370
- end
371
- end
350
+ wait_close (x) = if isopen (x) stream_wait (x,x. closenotify); end
372
351
373
352
function close (stream:: Union{LibuvStream, LibuvServer} )
374
353
if isopen (stream) && stream. status != StatusClosing
@@ -503,6 +482,7 @@ function notify_filled(stream::LibuvStream, nread::Int)
503
482
end
504
483
end
505
484
485
+ const READ_BUFFER_SZ= 10485760 # 10 MB
506
486
function uv_readcb (handle:: Ptr{Void} , nread:: Cssize_t , buf:: Ptr{Void} )
507
487
stream = @handle_as handle LibuvStream
508
488
nread = Int (nread)
@@ -533,11 +513,11 @@ function uv_readcb(handle::Ptr{Void}, nread::Cssize_t, buf::Ptr{Void})
533
513
notify (stream. readnotify)
534
514
end
535
515
536
- # Stop background reading when
537
- # 1) we have accumulated a lot of unread data OR
516
+ # Stop reading when
517
+ # 1) when we have an infinite buffer, and we have accumulated a lot of unread data OR
538
518
# 2) we have an alternate buffer that has reached its limit.
539
- if (nb_available (stream. buffer) >= stream. throttle ) ||
540
- (nb_available (stream. buffer) > = stream. buffer. maxsize)
519
+ if (is_maxsize_unlimited (stream. buffer) && ( nb_available ( stream. buffer) > READ_BUFFER_SZ ) ) ||
520
+ (nb_available (stream. buffer) = = stream. buffer. maxsize)
541
521
stop_reading (stream)
542
522
end
543
523
nothing
@@ -855,7 +835,7 @@ function start_reading(stream::LibuvStream, cb::Function)
855
835
if nread > 0
856
836
notify_filled (stream, nread)
857
837
end
858
- return failure_code
838
+ nothing
859
839
end
860
840
861
841
function start_reading (stream:: LibuvStream , cb:: Bool )
@@ -892,22 +872,16 @@ function read!(s::LibuvStream, a::Array{UInt8, 1})
892
872
end
893
873
894
874
if nb <= SZ_UNBUFFERED_IO # Under this limit we are OK with copying the array from the stream's buffer
895
- wait_readnb (s, nb)
875
+ wait_readnb (s,nb)
896
876
read! (sbuf, a)
897
877
else
898
- try
899
- stop_reading (s) # Just playing it safe, since we are going to switch buffers.
900
- newbuf = PipeBuffer (a, #= maxsize=# nb)
901
- newbuf. size = 0 # reset the write pointer to the beginning
902
- s. buffer = newbuf
903
- write (newbuf, sbuf)
904
- wait_readnb (s, nb)
905
- finally
906
- s. buffer = sbuf
907
- if ! isempty (s. readnotify. waitq)
908
- start_reading (x) # resume reading iff there are currently other read clients of the stream
909
- end
910
- end
878
+ stop_reading (s) # Just playing it safe, since we are going to switch buffers.
879
+ newbuf = PipeBuffer (a, nb)
880
+ newbuf. size = 0
881
+ s. buffer = newbuf
882
+ write (newbuf, sbuf)
883
+ wait_readnb (s,nb)
884
+ s. buffer = sbuf
911
885
end
912
886
return a
913
887
end
@@ -916,7 +890,8 @@ function read(this::LibuvStream, ::Type{UInt8})
916
890
wait_readnb (this, 1 )
917
891
buf = this. buffer
918
892
@assert buf. seekable == false
919
- read (buf, UInt8)
893
+ wait_readnb (this,1 )
894
+ read (buf,UInt8)
920
895
end
921
896
922
897
function readavailable (this:: LibuvStream )
@@ -964,11 +939,11 @@ function buffer_or_write(s::LibuvStream, p::Ptr, n::Integer)
964
939
965
940
buf = get (s. sendbuf)
966
941
totb = nb_available (buf) + n
967
- if totb < buf . maxsize
942
+ if totb < maxsize (buf)
968
943
nb = write (buf, p, n)
969
944
else
970
945
flush (s)
971
- if n > buf . maxsize
946
+ if n > maxsize (buf)
972
947
nb = uv_write (s, p, n)
973
948
else
974
949
nb = write (buf, p, n)
@@ -1187,10 +1162,19 @@ function wait_readnb(s::BufferStream, nb::Int)
1187
1162
while isopen (s) && nb_available (s. buffer) < nb
1188
1163
wait (s. r_c)
1189
1164
end
1165
+
1166
+ (nb_available (s. buffer) < nb) && error (" closed BufferStream" )
1167
+ end
1168
+
1169
+ function eof (s:: BufferStream )
1170
+ wait_readnb (s,1 )
1171
+ ! isopen (s) && nb_available (s. buffer)<= 0
1190
1172
end
1191
1173
1192
1174
show (io:: IO , s:: BufferStream ) = print (io," BufferStream() bytes waiting:" ,nb_available (s. buffer)," , isopen:" , s. is_open)
1193
1175
1176
+ nb_available (s:: BufferStream ) = nb_available (s. buffer)
1177
+
1194
1178
function wait_readbyte (s:: BufferStream , c:: UInt8 )
1195
1179
while isopen (s) && search (s. buffer,c) <= 0
1196
1180
wait (s. r_c)
0 commit comments