Git Product home page Git Product logo

lwt_glib's Introduction

ocsigen.org

lwt_glib's People

Contributors

aantron avatar agarwal avatar andrewray avatar jeremiedimino avatar jsthomas avatar let-def avatar talex5 avatar whitequark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lwt_glib's Issues

Calling g_main_context_dispatch in a thread ?

Since g_main_context_dispatch is called in the main Lwt thread, the callbacks associated to Gtk events cannot call Lwt functions. Do you think it would be possible to call this function in a detached thread (using Lwt_preemptive.detach), so that callbacks could call Lwt_preemptive.run_in_main ? It may not be wishable for all applications, so this would have to be optional. And I don't know if Glib would be happy with this.

lwt_unix.sleep unexpectedly hangs when invoked inside a gtk callback

Hi,

Consider this piece of code (using lwt_ppx syntax) :

let tests () =
   Printf.printf "Before sleep.\n%!" ;
   let%lwt () = Lwt_unix.sleep 0.5 in
   Printf.printf "After sleep.\n%!" ;
   Lwt.return_unit
  • It behaves as expected in a stand-alone lwt program.
  • It behaves as expected in a lablgtk program, using lwt_glib, when invoked outside of a gtk callback.
  • However, it hangs when used inside a gtk callback. Let us give some details :

In order to use lwt code within a gtk callback, I use the (supposedly) canonical way to do this:

let cb _ =
    let () = Lwt.async tests in
    true

This callback is used, for instance, on an event_box (called 'zone') :

let _ = zone#event#connect#button_release ~callback:cb

Then, I observe the following behaviour:
If I click in the zone (only click, the mouse must not move), the message "Before sleep" is printed. It hangs undefinitely, until I move the mouse (which generates a gtk event). Then, the message "After sleep" is displayed.

In short : Lwt_unix.sleep hangs until some arbitrary gtk event occurs.

Note that if you use a button instead of an event box, the button generates at least one event after it has been clicked (most likely to redraw itself), hence the lwt_unix.sleep does not block

A quick fix consists in adding a gtk timer :

let _ = GMain.Timeout.add ~ms:100 ~callback:(fun () -> true)

..but this is not really satisfactory.

Here is a zip exposing the bug. Requires dune, lablgtk, lwt_glib. Type 'make'.

(github does not support .tgz ??????)

Lwt_hangs.zip

Releasing OCaml runtime lock when calling g_main_context_dispatch

I'm currently developing a proof of concept application, using Chromium Embedded Library (libcef) and Gtk3 bindings.

Libcef runs various threads and takes callbacks. Some of these callbacks are OCaml functions in my application. So the code of these callbacks can be called at any moment (depending on what the browser wants to load, etc.). So for every call to libcef C function, I release the OCaml runtime lock and my callbacks acquire it. To do so, I use Ctypes and the runtime_lock argument of Foreign.foreign and foreign.funptr.

Now comes the moment when I want to handle Gtk events. I use my own bindings (not published yet, under development). In these bindings, the OCaml callbacks associated to Gtk events also acquire the runtime lock and release it after, and every C function call releases the OCaml runtime lock too, so that everything is fine with the libcef threads.

In some of the Cef and Gtk callbacks, I want to perform IO, using Lwt. I found Lwt_glib which is great, thank you ! I use the glib_into_lwt mode but it blocks. To make it work, I had to release the runtime lock when calling g_main_context_dispatch function, so that my callbacks can acquire the lock. I can make a pull request, but wanted to be sure it would be ok for other code not releasing the ocaml runtime lock (I think so).

By the way, using the lwt_into_glib mode does not work in my case, but I did not find why yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.