Module Lwt_preemptive

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(lwt
        CONSTRAINT "= 5.6.1"
        FINDLIBS lwt lwt.unix)
    DkSDKProject_MakeAvailable(lwt)
  2. Add the Findlib::lwt library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::lwt)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install lwt.5.6.1
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (lwt (>= 5.6.1))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "lwt" {>= "5.6.1"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the lwt.unix library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))
val detach : ``('a -> 'b)`` -> 'a -> 'b Lwt.t

detach f x runs the computation f x in a separate preemptive thread. detach evaluates to an Lwt promise, which is pending until the preemptive thread completes.

detach calls simple_init internally, which means that the number of preemptive threads is capped by default at four. If you would like a higher limit, call init or set_bounds directly.

Note that Lwt thread-local storage (i.e., Lwt.with_value) cannot be safely used from within f. The same goes for most of the rest of Lwt. If you need to run an Lwt thread in f, use run_in_main.

valrun_in_main : ``(``unit-> 'a Lwt.t)`` -> 'a

run_in_main f can be called from a detached computation to execute f () in the main preemptive thread, i.e. the one executing Lwt_main.run. run_in_main f blocks until f () completes, then returns its result. If f () raises an exception, run_in_main f raises the same exception.

Lwt.with_value may be used inside f (). Lwt.get can correctly retrieve values set this way inside f (), but not values set using Lwt.with_value outside f ().

valinit : ``int->``int->``(``string->unit)``-> unit

init min max log initialises this module. i.e. it launches the minimum number of preemptive threads and starts the dispatcher.

  • parameter min

    is the minimum number of threads

  • parameter max

    is the maximum number of threads

  • parameter log

    is used to log error messages

    If Lwt_preemptive has already been initialised, this call only modify bounds and the log function.

valsimple_init : ``unit-> unit

simple_init () checks if the library is not yet initialized, and if not, does a simple initialization. The minimum number of threads is set to zero, maximum to four, and the log function is left unchanged, i.e. the default built-in logging function is used. See Lwt_preemptive.init.

Note: this function is automatically called by detach.

valget_bounds : ``unit-> int * int

get_bounds () returns the minimum and the maximum number of preemptive threads.

valset_bounds : ``(int * int)``-> unit

set_bounds (min, max) set the minimum and the maximum number of preemptive threads.

valset_max_number_of_threads_queued : ``int-> unit

Sets the size of the waiting queue, if no more preemptive threads are available. When the queue is full, detach will sleep until a thread is available.

valget_max_number_of_threads_queued : ``unit-> int

Returns the size of the waiting queue, if no more threads are available