Module Lwt_mutex

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 library to any desired (library)and/or (executable) targets in your **/dune files:

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

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    lwt))
type t

Type of Lwt mutexes

valcreate : ``unit-> t

create () creates a new mutex, which is initially unlocked

vallock :t ->``unitLwt.t

lock mutex lockcs the mutex, that is:

  • if the mutex is unlocked, then it is marked as locked and lock returns immediately

  • if it is locked, then lock waits for all threads waiting on the mutex to terminate, then it resumes when the last one unlocks the mutex

Note: threads are woken up in the same order they try to lock the mutex

valunlock :t -> unit

unlock mutex unlock the mutex if no threads is waiting on it. Otherwise it will eventually removes the first one and resumes it.

valis_locked :t -> bool

locked mutex returns whether mutex is currently locked

valis_empty :t -> bool

is_empty mutex returns true if they are no thread waiting on the mutex, and false otherwise

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

with_lock lock f is used to lock a mutex within a block scope. The function f () is called with the mutex locked, and its result is returned from the call to with_lock. If an exception is raised from f, the mutex is also unlocked before the scope of with_lock is exited.