Skip to content

Commit 6b5ad9a

Browse files
committedMar 20, 2013
Add option to run event loop once
1 parent 98e003b commit 6b5ad9a

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed
 

‎base/stream.jl

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
include("uv_constants.jl")
33

44
## types ##
5-
65
typealias Executable Union(Vector{ByteString},Function)
76
typealias Callback Union(Function,Bool)
87
type WaitTask
@@ -113,6 +112,7 @@ end
113112
wait_connect_filter(w::AsyncStream, args...) = !w.open
114113
wait_readable_filter(w::AsyncStream, args...) = nb_available(w.buffer) <= 0
115114
wait_readnb_filter(w::(AsyncStream,Int), args...) = w[1].open && (nb_available(w[1].buffer) < w[2])
115+
wait_readbyte_filter(w::(AsyncStream,Uint8), args...) = w[1].open && (search(w[1].buffer,w[2]) <= 0)
116116
wait_readline_filter(w::AsyncStream, args...) = w.open && (search(w.buffer,'\n') <= 0)
117117

118118
function wait(forwhat::Vector, notify_list_name, filter_fcn)
@@ -143,6 +143,7 @@ wait_readable(x) = wait(x, :readnotify, wait_readable_filter)
143143
wait_readline(x) = wait(x, :readnotify, wait_readline_filter)
144144
wait_readnb(x::(AsyncStream,Int)) = wait(x, :readnotify, wait_readnb_filter)
145145
wait_readnb(x::AsyncStream,b::Int) = wait_readnb((x,b))
146+
wait_readbyte(x::AsyncStream,c::Uint8) = wait((x,c), :readnotify, wait_readbyte_filter)
146147

147148
#from `connect`
148149
function _uv_hook_connectcb(sock::AsyncStream, status::Int32)
@@ -306,7 +307,15 @@ eventloop() = ccall(:jl_global_event_loop,Ptr{Void},())
306307
function run_event_loop(loop::Ptr{Void})
307308
ccall(:jl_run_event_loop,Void,(Ptr{Void},),loop)
308309
end
310+
function process_events(loop::Ptr{Void})
311+
ccall(:jl_process_events,Int32,(Ptr{Void},),loop)
312+
end
313+
function run_event_loop_once(loop::Ptr{Void})
314+
ccall(:jl_run_once,Int32,(Ptr{Void},),loop)
315+
end
316+
process_events() = process_events(eventloop())
309317
run_event_loop() = run_event_loop(eventloop())
318+
run_event_loop_once() = run_event_loop_once(eventloop())
310319

311320
##pipe functions
312321
malloc_pipe() = c_malloc(_sizeof_uv_pipe)
@@ -382,6 +391,7 @@ end
382391
function read(this::AsyncStream,::Type{Uint8})
383392
buf = this.buffer
384393
assert(buf.seekable == false)
394+
start_reading(this)
Has conversations. Original line has conversations.
385395
wait_readnb(this,1)
386396
read(buf,Uint8)
387397
end
@@ -394,6 +404,14 @@ function readline(this::AsyncStream)
394404
readline(buf)
395405
end
396406

407+
function readuntil(this::AsyncStream,c::Uint8)
408+
buf = this.buffer
409+
assert(buf.seekable == false)
410+
start_reading(this)
411+
wait_readbyte(this,c)
412+
readuntil(buf,c)
413+
end
414+
397415
function finish_read(pipe::NamedPipe)
398416
close(pipe) #handles to UV and ios will be invalid after this point
399417
end

‎src/jl_uv.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,19 @@ DLLEXPORT uv_tcp_t *jl_make_tcp(uv_loop_t* loop, jl_value_t *julia_struct)
220220

221221
/** This file contains wrappers for most of libuv's stream functionailty. Once we can allocate structs in Julia, this file will be removed */
222222

223+
DLLEXPORT int jl_run_once(uv_loop_t *loop)
Has conversations. Original line has conversations.
224+
{
225+
if (loop) return uv_run(loop,UV_RUN_ONCE);
226+
}
227+
223228
DLLEXPORT void jl_run_event_loop(uv_loop_t *loop)
224229
{
225230
if (loop) uv_run(loop,UV_RUN_DEFAULT);
226231
}
227232

228-
DLLEXPORT void jl_process_events(uv_loop_t *loop)
233+
DLLEXPORT int jl_process_events(uv_loop_t *loop)
229234
{
230-
if (loop) uv_run(loop,UV_RUN_NOWAIT);
235+
if (loop) return uv_run(loop,UV_RUN_NOWAIT);
Has conversations. Original line has conversations.
231236
}
232237

233238
DLLEXPORT uv_pipe_t *jl_init_pipe(uv_pipe_t *pipe, int writable, int julia_only, jl_value_t *julia_struct)

‎src/julia.expmap

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
jl_reshape_array;
239239
jl_restore_system_image;
240240
jl_run_event_loop;
241+
jl_run_once;
241242
jl_save_system_image;
242243
jl_set_const;
243244
jl_set_current_module;

‎src/julia.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@ DLLEXPORT int jl_spawn(char *name, char **argv, uv_loop_t *loop,
807807
uv_handle_type stdout_type,uv_pipe_t *stdout_pipe,
808808
uv_handle_type stderr_type,uv_pipe_t *stderr_pipe);
809809
DLLEXPORT void jl_run_event_loop(uv_loop_t *loop);
810-
DLLEXPORT void jl_process_events(uv_loop_t *loop);
810+
DLLEXPORT int jl_run_once(uv_loop_t *loop);
811+
DLLEXPORT int jl_process_events(uv_loop_t *loop);
811812

812813
DLLEXPORT uv_loop_t *jl_global_event_loop();
813814

0 commit comments

Comments
 (0)
Please sign in to comment.