text
stringlengths
12
786k
type t = _ ' ThreadPoolExecutor java_instance " ocamljava_threadpoolexecutor_make "
let make ~ core_pool_size ~ max_pool_size time timeunit rhe = threadpoolexecutor_make core_pool_size max_pool_size time timeunit rhe
let await_termination tpe time timeunit = Java . call " ThreadPoolExecutor . awaitTermination ( awaitTerminationlong , TimeUnit ) TimeUnit " tpe time timeunit
let get_active_count tpe = Java . call " ThreadPoolExecutor . getActiveCount ( getActiveCount ) getActiveCount " tpe
let get_completed_task_count tpe = Java . call " ThreadPoolExecutor . getCompletedTaskCount ( getCompletedTaskCount ) getCompletedTaskCount " tpe
let get_core_pool_size tpe = Java . call " ThreadPoolExecutor . getCorePoolSize ( getCorePoolSize ) getCorePoolSize " tpe
let get_keep_alive_time tpe timeunit = Java . call " ThreadPoolExecutor . getKeepAliveTime ( getKeepAliveTimeTimeUnit ) getKeepAliveTimeTimeUnit " tpe timeunit
let get_largest_pool_size tpe = Java . call " ThreadPoolExecutor . getLargestPoolSize ( getLargestPoolSize ) getLargestPoolSize " tpe
let get_maximum_pool_size tpe = Java . call " ThreadPoolExecutor . getMaximumPoolSize ( getMaximumPoolSize ) getMaximumPoolSize " tpe
let get_pool_size tpe = Java . call " ThreadPoolExecutor . getPoolSize ( getPoolSize ) getPoolSize " tpe
let get_rejected_execution_handler tpe = Java . call " ThreadPoolExecutor . getRejectedExecutionHandler ( getRejectedExecutionHandler ) getRejectedExecutionHandler " tpe
let get_task_count tpe = Java . call " ThreadPoolExecutor . getTaskCount ( getTaskCount ) getTaskCount " tpe " ocamljava_threadpoolexecutor_invoke_all " " ocamljava_threadpoolexecutor_invoke_all_time " " ocamljava_threadpoolexecutor_invoke_any " " ocamljava_threadpoolexecutor_invoke_any_time "...
let is_shutdown tpe = Java . call " ThreadPoolExecutor . isShutdown ( isShutdown ) isShutdown " tpe
let is_terminated tpe = Java . call " ThreadPoolExecutor . isTerminated ( isTerminated ) isTerminated " tpe
let is_terminating tpe = Java . call " ThreadPoolExecutor . isTerminating ( isTerminating ) isTerminating " tpe
let set_core_pool_size tpe sz = Java . call " ThreadPoolExecutor . setCorePoolSize ( setCorePoolSizeint ) setCorePoolSizeint " tpe sz
let set_keep_alive_time tpe time timeunit = Java . call " ThreadPoolExecutor . setKeepAliveTime ( setKeepAliveTimelong , TimeUnit ) TimeUnit " tpe time timeunit
let set_maximum_pool_size tpe sz = Java . call " ThreadPoolExecutor . setMaximumPoolSize ( setMaximumPoolSizeint ) setMaximumPoolSizeint " tpe sz
let set_rejected_execution_handler tpe reh = Java . call " ThreadPoolExecutor . setRejectedExecutionHandler ( setRejectedExecutionHandlerRejectedExecutionHandler ) setRejectedExecutionHandlerRejectedExecutionHandler " tpe reh
let shutdown tpe = Java . call " ThreadPoolExecutor . shutdown ( shutdown ) shutdown " tpe " ocamljava_threadpoolexecutor_shutdown_now " " ocamljava_threadpoolexecutor_submit " " java null "
let null = null ( ) " java is_null " " java is_not_null "
let wrap x = if is_null x then None else Some x
let unwrap = function | Some x -> x | None -> null
let is_current ( ask : Queries . ask ) : bool = ask . f Queries . MayBeThreadReturn
module Spec : Analyses . MCPSpec = struct include Analyses . DefaultSpec let name ( ) = " threadreturn " module D = IntDomain . Booleans module C = D let assign ctx ( lval : lval ) ( rval : exp ) : D . t = ctx . local let branch ctx ( exp : exp ) ( tv : bool ) : D . t = ...
let _ = MCP . register_analysis ( module Spec : MCPSpec )
let stopped = ref false
let rec loop ( ) = let rec generate_list n = let rec aux acc = function | 0 -> acc | n -> aux ( float n :: acc ) ( n - 1 ) in aux [ ] n in let long_list = generate_list 100000 in let res = List . length ( List . rev_map sin long_list ) in ignore ( Sys . opaque_identity res )
let thread s = ignore ( Thread . sigmask Unix . SIG_UNBLOCK [ s ] ) ; while not ! stopped do loop ( ) done
let handler tid_exp cnt signal = incr cnt ; if Thread . id ( Thread . self ( ) ) != ! tid_exp then Printf . printf " Signal received in an unexpected thread !\ n "
let _ = ignore ( Thread . sigmask Unix . SIG_BLOCK [ Sys . sigusr1 ; Sys . sigusr2 ] ) ; let ( tid1 , tid2 ) = ( ref 0 , ref 0 ) in let ( cnt1 , cnt2 ) = ( ref 0 , ref 0 ) in Sys . set_signal Sys . sigusr1 ( Sys . Signal_handle ( handler tid1 cnt1 ) ) ; ...
let timed_read fd buff ofs len timeout = if Thread . wait_timed_read fd timeout then Unix . read fd buff ofs len else raise ( Unix_error ( ETIMEDOUT , " timed_read " , " " ) )
let timed_write fd buff ofs len timeout = if Thread . wait_timed_write fd timeout then Unix . write fd buff ofs len else raise ( Unix_error ( ETIMEDOUT , " timed_write " , " " ) )
let timed_write_substring fd buff ofs len timeout = timed_write fd ( Bytes . unsafe_of_string buff ) ofs len timeout
let log_delays thunk = let start = Time . now ( ) in let print_time ( ) = let diff = Time . diff ( Time . now ( ) ) start in Caml . Printf . printf " . " ; %! printf " % s , " ( Time . Span . to_string diff ) in let d = thunk ( ) in Clock . every ( sec 0 ....
type how_to_wait = | After | Busyloop | Busyloop_in_thread | Noalloc_busyloop_in_thread | Noalloc_busyloop_in_thread_2
let busyloop ( ) = let x = ref None in for i = 1 to 50_000_000 do x := Some i done
let noalloc_busyloop ( ) = for _i = 1 to 50_000_000 do ( ) done
let noalloc_busyloop_2 ( ) = let rec loop i = if i = 0 then ( ) else loop ( i - 1 ) in loop 50_000_000
let wait_and_log how_to_wait = let until = match how_to_wait with | After -> ( fun ( ) -> after ( sec 1 . ) ) | Busyloop -> ( fun ( ) -> busyloop ( ) ; return ( ) ) | Busyloop_in_thread -> ( fun ( ) -> In_thread . run busyloop ) | Noalloc_busyloop_in_thread -> ( ...
let how_to_wait = Command . Spec . Arg_type . create ( fun s -> Sexp . of_string s |> how_to_wait_of_sexp )
let ( ) = Command . async_basic ~ summary " : run logger without busy loop " Command . Spec . ( empty +> anon ( " how - to - wait " %: how_to_wait ) ) ( fun how ( ) -> wait_and_log how ) |> Command . run
let thread_main ( ) = printf " Start of thread_main \ n " ; %! failwith " exception thrown out of thread_main "
let main ~ create_thread ~ join_thread = Core . at_exit ( fun ( ) -> printf " Core . at_exit callback ran \ n " ) ; %! Caml . at_exit ( fun ( ) -> printf " Caml . at_exit callback ran \ n " ) ; %! printf " About to create thread \ n " ; %! let thread = create_thread...
let core_command = Command . basic ~ summary " : Test Core . Thread . create ' s ~ on_uncaught_exn argument " [ % map_open . Command let on_uncaught_exn = let arg_type = Command . Arg_type . of_alist_exn [ " print - to - stderr " , ` Print_to_stderr ; " kill - whole - proces...
let caml_command = let main ( ) = main ~ create_thread : Caml . Thread . create ~ join_thread : Caml . Thread . join in Command . basic ~ summary " : Test Caml . Thread . create ' s behaviour when exns are thrown " ( Command . Param . return main )
let command = Command . group ~ summary " : Demonstrate behaviour when exns are thrown out of Thread . create " [ " core " , core_command ; " caml " , caml_command ]
let ( ) = Command . run command
type t = { max_threads : int ; lock : Mutex . t ; condition : Condition . t ; table : ( int , Thread . t ) Hashtbl . t ; }
let create ( ? max_threads = 128 ) ( ) = { max_threads ; lock = Mutex . create ( ) ; condition = Condition . create ( ) ; table = Hashtbl . create max_threads ; }
let signal_work_done thread_id pool = Mutex . lock pool . lock ; try Hashtbl . remove pool . table thread_id ; Condition . signal pool . condition ; Mutex . unlock pool . lock with _ -> Mutex . unlock pool . lock
let add_work f x pool = Mutex . lock pool . lock ; try while Hashtbl . length pool . table >= pool . max_threads do Condition . wait pool . condition pool . lock done ; let f ' x = let thread = Thread . self ( ) in let thread_id = Thread . id thread in let _ = f x in signal_work_don...
let shutdown pool = Hashtbl . iter ( fun _ thread -> Thread . join thread ) pool . table
let create fn arg = thread_new ( fun ( ) -> try fn arg ; ( ) with exn -> flush stdout ; flush stderr ; thread_uncaught_exception exn )
let kill th = invalid_arg " Thread . kill : not implemented "
let preempt signal = yield ( )
let _ = ignore ( Sys . signal Sys . sigvtalrm ( Sys . Signal_handle preempt ) ) ; thread_initialize ( )
let delay time = ignore ( Unix . select [ ] [ ] [ ] time )
let wait_read fd = ( )
let wait_write fd = ( )
let wait_timed_read fd d = match Unix . select [ fd ] [ ] [ ] d with ( [ ] , _ , _ ) -> false | ( _ , _ , _ ) -> true
let wait_timed_write fd d = match Unix . select [ ] [ fd ] [ ] d with ( _ , [ ] , _ ) -> false | ( _ , _ , _ ) -> true
let wait_pid p = Unix . waitpid [ ] p
let run_holding_async_lock ( type a b ) ( ? wakeup_scheduler = true ) t ( f : unit -> a ) ( ~ finish : ( a , exn ) Result . t -> b ) : b = if debug then Debug . log " run_holding_async_lock " t [ % sexp_of : t ] ; if not ( am_holding_lock t ) then lock t ; protect ~...
let ensure_in_a_thread t function_ = if is_main_thread ( ) then raise_s [ % message " cannot call from the main thread " ( function_ : string ) ] ; if am_holding_lock t then raise_s [ % message " cannot call while holding the async lock " ( function_ : string ) ] ; ;
let run_in_async_with_optional_cycle ? wakeup_scheduler t f = if debug then Debug . log " run_in_async_with_optional_cycle " t [ % sexp_of : t ] ; ensure_in_a_thread t " run_in_async_with_optional_cycle " ; run_holding_async_lock ? wakeup_scheduler t f ~ finish ( : function | Error exn -> E...
let block_on_async t f = if debug then Debug . log " block_on_async " t [ % sexp_of : t ] ; if i_am_the_scheduler t || ( am_holding_lock t && not ( is_main_thread ( ) ) ) then raise_s [ % message " called [ block_on_async ] from within async " ] ; let execution_context = Ker...
let block_on_async_exn t f = Result . ok_exn ( block_on_async t f )
let reset_scheduler t = if debug then Debug . log " reset_scheduler " t [ % sexp_of : t ] ; if i_am_the_scheduler t || ( am_holding_lock t && not ( is_main_thread ( ) ) ) then raise_s [ % message " called [ reset_scheduler ] from within async " ] ; if am_holding_lock t then u...
let run_in_async ? wakeup_scheduler t f = if debug then Debug . log " run_in_async " t [ % sexp_of : t ] ; ensure_in_a_thread t " run_in_async " ; run_holding_async_lock ? wakeup_scheduler t f ~ finish : Fn . id ; ;
let run_in_async_exn ? wakeup_scheduler t f = Result . ok_exn ( run_in_async ? wakeup_scheduler t f ) ; ;
let run_in_async_wait t f = if debug then Debug . log " run_in_async_wait " t [ % sexp_of : t ] ; ensure_in_a_thread t " run_in_async_wait " ; block_on_async t f ; ;
let run_in_async_wait_exn t f = Result . ok_exn ( run_in_async_wait t f )
let deferred t = let ivar = if am_holding_lock t then Ivar . create ( ) else run_holding_async_lock t Ivar . create ~ finish : Result . ok_exn in let fill x = run_in_async_exn t ( fun ( ) -> Ivar . fill ivar x ) in Ivar . read ivar , fill ; ;
let t ( ) = the_one_and_only ~ should_lock : false
let am_holding_async_lock ( ) = am_holding_lock ( t ( ) )
let deferred ( ) = deferred ( t ( ) )
let run_in_async_with_optional_cycle ? wakeup_scheduler f = run_in_async_with_optional_cycle ? wakeup_scheduler ( t ( ) ) f ; ;
let run_in_async ? wakeup_scheduler f = run_in_async ? wakeup_scheduler ( t ( ) ) f
let run_in_async_exn ? wakeup_scheduler f = run_in_async_exn ? wakeup_scheduler ( t ( ) ) f
let block_on_async f = block_on_async ( t ( ) ) f
let block_on_async_exn f = block_on_async_exn ( t ( ) ) f
let run_in_async_wait f = run_in_async_wait ( t ( ) ) f
let run_in_async_wait_exn f = run_in_async_wait_exn ( t ( ) ) f
let reset_scheduler ( ) = reset_scheduler ( t ( ) )
type ' a t = { mutable value : ' a option ; mutable num_waiting : int ; mutex : ( Mutex . t [ @ sexp . opaque ] ) ; full : ( Condition . t [ @ sexp . opaque ] ) }
let create ( ) = { value = None ; num_waiting = 0 ; mutex = Mutex . create ( ) ; full = Condition . create ( ) } ; ;
let critical_section t ~ f = Mutex . critical_section t . mutex ~ f
let fill t v = critical_section t ~ f ( : fun ( ) -> if is_some t . value then raise_s [ % message " Thread_safe_ivar . fill of full ivar " ] ; t . value <- Some v ; Condition . broadcast t . full ) ; ;
let read t = match t . value with | Some v -> v | None -> critical_section t ~ f ( : fun ( ) -> match t . value with | Some v -> v | None -> t . num_waiting <- t . num_waiting + 1 ; Condition . wait t . full t . mutex ; t . num_waiting <- t . num_waiting - 1 ; ( match t...
type ' a t = ' a Pipe . Writer . t [ @@ deriving sexp_of ]
let in_async ? wakeup_scheduler f = Thread_safe . run_in_async_exn ? wakeup_scheduler f
let in_async_wait f = Thread_safe . run_in_async_wait_exn f
module Written_or_closed = struct type t = | Written | Closed end
module If_closed = struct type ' a t = | Raise : unit t | Return : Written_or_closed . t t let closed : type a . a t -> a = function | Raise -> raise_s [ % message " pipe is closed " ] | Return -> Closed ; ; let written : type a . a t -> a = function | Raise -> ( ) | Return ...
let in_async_unless_closed ? wakeup_scheduler t f ~ if_closed = in_async ? wakeup_scheduler ( fun ( ) -> if Pipe . is_closed t then If_closed . closed if_closed else ( f ( ) ; If_closed . written if_closed ) ) ; ;
let in_async_unless_closed_wait t f ~ if_closed = in_async_wait ( fun ( ) -> if Pipe . is_closed t then return ( If_closed . closed if_closed ) else ( let % map ( ) = f ( ) in If_closed . written if_closed ) ) ; ;
let create ( ) = if Thread_safe . am_holding_async_lock ( ) then Pipe . create ( ) else in_async Pipe . create ; ;
let pushback t = in_async_wait ( fun ( ) -> Pipe . pushback t )
let transfer_in t ~ from ~ if_closed = in_async_unless_closed_wait t ~ if_closed ( fun ( ) -> Pipe . transfer_in t ~ from ) ; ;