text stringlengths 0 601k |
|---|
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 ~ finally ( : fun ( ) -> if wakeup_scheduler then threa... |
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 -> Error exn | Ok ( maybe_run_a_c... |
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 = Kernel_scheduler . current_execution_context t .... |
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 unlock t ; Raw_scheduler . thread_safe_reset ... |
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 . value with | Some v -> v | None -> assert false... |
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 -> Written ; ; end |
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 ) ; ; |
let write t a ~ if_closed = in_async_unless_closed_wait t ~ if_closed ( fun ( ) -> Pipe . write t a ) ; ; |
let transfer_in_without_pushback ? wakeup_scheduler t ~ from ~ if_closed = in_async_unless_closed ? wakeup_scheduler t ~ if_closed ( fun ( ) -> Pipe . transfer_in_without_pushback t ~ from ) ; ; |
let write_without_pushback ? wakeup_scheduler t a ~ if_closed = in_async_unless_closed ? wakeup_scheduler t ~ if_closed ( fun ( ) -> Pipe . write_without_pushback t a ) ; ; |
let close t = in_async ( fun ( ) -> Pipe . close t ) |
let is_closed t = in_async ( fun ( ) -> Pipe . is_closed t ) |
let closed t = in_async_wait ( fun ( ) -> Pipe . closed t ) |
module Elt = struct type ' a t = { mutable value : ' a Uopt . t ; mutable next : ( ' a t Uopt . t [ @ sexp . opaque ] ) } [ @@ deriving sexp_of ] let create ( ) = { value = Uopt . none ; next = Uopt . none } end |
type ' a t = { mutable length : int ; mutable front : ' a Elt . t ; mutable back : ' a Elt . t ; mutable unused_elts : ' a Elt . t Uopt . t } |
let invariant _invariant_a t = Invariant . invariant [ % here ] t [ % sexp_of : _ t ] ( fun ( ) -> let check f = Invariant . check_field t f in Fields . iter ~ length ( : check ( fun length -> assert ( length >= 0 ) ) ) ~ front : ( check ( fun front -> let i = ref t . length in let r = ref front in while ! i > 0 do dec... |
let create ( ) = let elt = Elt . create ( ) in { front = elt ; back = elt ; length = 0 ; unused_elts = Uopt . none } ; ; |
let get_unused_elt t = if Uopt . is_some t . unused_elts then ( let elt = Uopt . unsafe_value t . unused_elts in t . unused_elts <- elt . next ; elt ) else Elt . create ( ) ; ; |
let enqueue ( type a ) ( t : a t ) ( a : a ) = let new_back = get_unused_elt t in t . length <- t . length + 1 ; t . back . value <- Uopt . some a ; t . back . next <- Uopt . some new_back ; t . back <- new_back ; ; |
let return_unused_elt t ( elt : _ Elt . t ) = elt . value <- Uopt . none ; elt . next <- t . unused_elts ; t . unused_elts <- Uopt . some elt ; ( ) ; ; failwiths ~ here [ :% here ] " Thread_safe_queue . dequeue_exn of empty queue " t [ % sexp_of : _ t ] ; ; |
let dequeue_exn t = if t . length = 0 then raise_dequeue_empty t ; let elt = t . front in let a = elt . value in t . front <- Uopt . unsafe_value elt . next ; t . length <- t . length - 1 ; return_unused_elt t elt ; Uopt . unsafe_value a ; ; |
let clear_internal_pool t = t . unused_elts <- Uopt . none |
module Private = struct module Uopt = Uopt end |
let create fn arg = thread_new ( fun ( ) -> try fn arg ; ( ) with Thread_exit -> ( ) | exn -> flush stdout ; flush stderr ; thread_uncaught_exception exn ) |
let exit ( ) = raise Thread_exit |
let kill th = invalid_arg " Thread . kill : not implemented " |
let preempt signal = yield ( ) |
let _ = ignore ( Sys . signal Sys . sigterm ( Sys . Signal_handle preempt ) ) ; thread_initialize ( ) |
let wait_read fd = ( ) |
let wait_write fd = ( ) |
let wait_timed_read fd delay = true |
let wait_timed_write fd delay = true |
let select rd wr ex delay = invalid_arg " Thread . select : not implemented " |
let wait_pid p = Unix . waitpid [ ] p |
let sigmask cmd set = invalid_arg " Thread . sigmask : not implemented " |
let wait_signal set = invalid_arg " Thread . wait_signal : not implemented " |
let with_mutex t ~ f = Mutex . lock t ; let res = f ( ) in Mutex . unlock t ; res |
type state = | Running | Stopped | Finished |
type t = { work : ( unit -> unit ) Queue . t ; mutable state : state ; mutex : Mutex . t ; work_available : Condition . t } |
let is_running t = match t . state with | Running -> true | Stopped | Finished -> false |
let run t = let rec loop ( ) = match t . state with | Stopped -> ( match Queue . pop t . work with | None -> t . state <- Finished | Some job -> do_work job ) | Finished -> ( ) | Running -> ( match Queue . pop t . work with | Some job -> do_work job | None -> while Queue . is_empty t . work && is_running t do Condition... |
let create ~ spawn_thread = let t = { work = Queue . create ( ) ; state = Finished ; mutex = Mutex . create ( ) ; work_available = Condition . create ( ) } in t . state <- Running ; spawn_thread ( fun ( ) -> run t ) ; t |
let add_work t ~ f = with_mutex t . mutex ~ f ( : fun ( ) -> if is_running t then ( Queue . push t . work f ; Condition . signal t . work_available ; Ok ( ) ) else Error ` Stopped ) |
let stop t = with_mutex t . mutex ~ f ( : fun ( ) -> match t . state with | Running -> t . state <- Stopped ; Condition . signal t . work_available | Stopped | Finished -> ( ) ) |
let text_no_final_nl ( ) = print_string " one \ ntwo \ nthree " in text_no_final_nl ( ) ; [ % expect { | one two three } ] ; | let text ( ) = print_string " one \ ntwo \ nthree \ n " in text ( ) ; [ % expect { | one two three } ] ; | text ( ) ; [ % expect { | one two three } ] ; | text ( ) ; [ % expect { | one two thre... |
object val mutable message = " " method get_message = message method set_message s = message <- s end ; ; |
module Transport = struct type exn_type = | UNKNOWN | NOT_OPEN | ALREADY_OPEN | TIMED_OUT | END_OF_FILE ; ; exception E of exn_type * string class virtual t = object ( self ) method virtual isOpen : bool method virtual opn : unit method virtual close : unit method virtual read : bytes -> int -> int -> int method readAl... |
module Protocol = struct type t_type = | T_STOP | T_VOID | T_BOOL | T_BINARY | T_BYTE | T_I08 | T_I16 | T_I32 | T_U64 | T_I64 | T_DOUBLE | T_STRING | T_UTF7 | T_STRUCT | T_MAP | T_SET | T_LIST | T_UTF8 | T_UTF16 [ @@ deriving show , eq ] let t_type_to_i = function T_STOP -> 0 | T_VOID -> 1 | T_BOOL -> 2 | T_BYTE -> 3 |... |
module Processor = struct class virtual t = object method virtual process : Protocol . t -> Protocol . t -> bool end ; ; class factory ( processor : t ) = object val processor_ = processor method getProcessor ( _trans : Transport . t ) = processor_ end ; ; end |
module Application_Exn = struct type typ = | UNKNOWN | UNKNOWN_METHOD | INVALID_MESSAGE_TYPE | WRONG_METHOD_NAME | BAD_SEQUENCE_ID | MISSING_RESULT | INTERNAL_ERROR | PROTOCOL_ERROR | INVALID_TRANSFORM | INVALID_PROTOCOL | UNSUPPORTED_CLIENT_TYPE let typ_of_i = function 0l -> UNKNOWN | 1l -> UNKNOWN_METHOD | 2l -> INVA... |
type ' a outcome = [ ` Ok of ' a | ` Aborted | ` Raised of exn ] |
module Internal_job : sig type ' a t [ @@ deriving sexp_of ] val create : ( ' a -> ' b Deferred . t ) -> ' a t * ' b outcome Deferred . t val run : ' a t -> ' a -> [ ` Ok | ` Raised ] Deferred . t val abort : _ t -> unit type ' a t = { start : [ ` Abort | ` Start of ' a ] Ivar . t ; outcome : [ ` Ok | ` Aborted | ` Rai... |
type ' a t = { continue_on_error : bool ; max_concurrent_jobs : int ; job_resources_not_in_use : ' a Stack_or_counter . t ; jobs_waiting_to_start : ' a Internal_job . t Queue . t ; mutable num_jobs_running : int ; mutable capacity_available : unit Ivar . t option ; mutable is_dead : bool ; mutable cleans : ( ' a -> uni... |
let invariant invariant_a t : unit = try let check f field = f ( Field . get field t ) in Fields . iter ~ continue_on_error : ignore ~ max_concurrent_jobs : ( check ( fun max_concurrent_jobs -> assert ( max_concurrent_jobs > 0 ) ) ) ~ job_resources_not_in_use : ( check ( fun job_resources_not_in_use -> Stack_or_counter... |
module T2 = struct type nonrec ( ' a , ' kind ) t = ' a t [ @@ deriving sexp_of ] let invariant invariant_a _ t = invariant invariant_a t end |
let num_jobs_waiting_to_start t = Queue . length t . jobs_waiting_to_start |
let clean_resource t a = Deferred . all_unit ( List . map t . cleans ~ f ( : fun f -> f a ) ) >>> fun ( ) -> t . num_resources_not_cleaned <- t . num_resources_not_cleaned - 1 ; if t . num_resources_not_cleaned = 0 then Ivar . fill t . cleaned ( ) ; ; |
let kill t = if not t . is_dead then ( t . is_dead <- true ; Queue . iter t . jobs_waiting_to_start ~ f : Internal_job . abort ; Queue . clear t . jobs_waiting_to_start ; Stack_or_counter . iter t . job_resources_not_in_use ~ f ( : fun a -> clean_resource t a ) ; Stack_or_counter . clear t . job_resources_not_in_use ) ... |
let at_kill t f = let f = unstage ( Monitor . Exported_for_scheduler . preserve_execution_context ' f ) in t . cleans <- f :: t . cleans ; ; |
let cleaned t = Ivar . read t . cleaned |
let rec start_job t = assert ( not t . is_dead ) ; assert ( t . num_jobs_running < t . max_concurrent_jobs ) ; assert ( not ( Queue . is_empty t . jobs_waiting_to_start ) ) ; let job = Queue . dequeue_exn t . jobs_waiting_to_start in t . num_jobs_running <- t . num_jobs_running + 1 ; let job_resource = Stack_or_counter... |
let create_internal ~ continue_on_error job_resources = let max_concurrent_jobs = Stack_or_counter . length job_resources in { continue_on_error ; max_concurrent_jobs ; job_resources_not_in_use = job_resources ; jobs_waiting_to_start = Queue . create ( ) ; num_jobs_running = 0 ; capacity_available = None ; is_dead = fa... |
let create_with ~ continue_on_error job_resources = create_internal ~ continue_on_error ( Stack_or_counter . of_list job_resources ) ; ; |
module Sequencer = struct type nonrec ' a t = ' a t [ @@ deriving sexp_of ] let create ( ? continue_on_error = false ) a = create_with ~ continue_on_error [ a ] end |
let create ~ continue_on_error ~ max_concurrent_jobs = if max_concurrent_jobs <= 0 then raise_s [ % message " Throttle . create requires positive max_concurrent_jobs , but got " ( max_concurrent_jobs : int ) ] ; create_internal ~ continue_on_error ( Stack_or_counter . create_counter ~ length : max_concurrent_jobs ) ; ; |
module Job = struct type ( ' a , ' b ) t = { internal_job : ' a Internal_job . t ; result : [ ` Ok of ' b | ` Aborted | ` Raised of exn ] Deferred . t } let result t = t . result let abort t = Internal_job . abort t . internal_job let create f = let internal_job , result = Internal_job . create f in { internal_job ; re... |
let enqueue ' t f = let job = Job . create f in if t . is_dead then Job . abort job else ( Queue . enqueue t . jobs_waiting_to_start job . internal_job ; if t . num_jobs_running < t . max_concurrent_jobs then start_job t ) ; Job . result job ; ; |
let handle_enqueue_result result = match result with | ` Ok a -> a | ` Aborted -> raise_s [ % message " throttle aborted job " ] | ` Raised exn -> raise exn ; ; |
let enqueue t f = enqueue ' t f >>| handle_enqueue_result |
let enqueue_exclusive t f = let n = t . max_concurrent_jobs in if Int . ( >= ) n 1_000_000 then raise_s [ % sexp " [ enqueue_exclusive ] was called with a very large value of \ [ max_concurrent_jobs ] . This doesn ' t work . " ] ; let done_ = Ivar . create ( ) in assert ( n > 0 ) ; let f_placeholder _slot = Ivar . read... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.