Skip to content

Conversation

@raphael-proust
Copy link
Collaborator

TODO:

  • documentation
  • tests

TODO:
- documentation
- tests
@MisterDA
Copy link
Contributor

A bit early for a review I suppose, but the right spelling would be immediately, and you might want to use __FUNCTION__ to retrieve the function's name!

@raphael-proust
Copy link
Collaborator Author

@MisterDA thanks!

@raphael-proust
Copy link
Collaborator Author

proposal:

replace the awaken ~ordering function with two distinct functions: awaken_deferred (which resolves the given promise later) and awaken_nested (which resolves the promise now, and comes back to current code afterwards) (or maybe queued/stacked or fifo/lifo but I think these names are too abstract)

make the type of the two functions distinct: awaken_nested returns Lwt.t to indicate context switch / interleaving / etc. (even though there's no pausing/yielding per se, there is some concurrency shenanigans going on)

I'm unsure about the type thing. Any comments suggestions ideas?

@raphael-proust
Copy link
Collaborator Author

proposal:

make two distinct functions instead of one with parameter

awaken_deferred: awakens the promise the next time the scheduler kicks in (equivalent to doing Lwt.on_success (Lwt.pause ()) (Lwt.wakeup …) rn)
yield_to_awaken: pauses current promise (as per pause) but resolves given promise before jumping to scheduler (equivalent to doing Lwt.wakeup …; Lwt.pause () rn)

The idea being: you can either prioritise the currently executing code and delay the resolving to when the scheduler runs or you can prioritise the resolving promise and you have to actuaslly pause the current promise

This is a more radical change that makes the semantics clearer (one promise executes and then the scheduler kicks in, not both promise) but also backwards incompatible (you can't express wakeup anymore. For this reason I don't think we sould have only those two functions but (a) interesting discussion maybe and (b) possibly as an addition rather than a replacement.

@raphael-proust
Copy link
Collaborator Author

Currently unhappy with the interface (the named parameter + constructor is too long). Alternatives:

  • separate functions? (also makes it easier to have Lwt.t in the return type of the yielding/nested/immeditate awaken)
  • choose one default (delayed/deferred) and use ?nested:unit to give a different behaviour
  • have a default function (delayed/deferred) and then have a function with a constructor.

I'm currently leaning towards this last possibility:

val awaken: 'a u -> 'a -> unit (* causes the promise associated to the [u] to be awoken the next time the scheduler kicks in *)

type _ interleaving =
  | Self_scheduler_target : unit interleaving (* same as awaken *)
  | Target_shceduler_self : unit Lwt.t interleaving (* = wakeup + pause *)
  | Target_self_scheduler : unit Lwt.t interleaving (* = wakeup *)
  (* other?? *)
  | Dont_care : unit Lwt.t
val awaken_control : interleaving: 'r interleaving -> 'a u -> 'a -> 'r

This makes the default case very simple and the complicated cases explicit.

@raphael-proust
Copy link
Collaborator Author

An issue with delaying the resolution of the promise is that the double-wakening exception has no place to be raised at. It happens asyncly meaning it should be handled by the async exception handler which I'm not keen to place more responsibility on. Still might be necessary.

src/core/lwt.mli Outdated
{{!t} promise} associated with {{!u} resolver} [r]. This triggers callbacks
attached to the promise.
(** [ordering] indicates a scheduling strategy for fullfilling or rejection of
promnises associated to resolvers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
promnises associated to resolvers.
promises associated to resolvers.

src/core/lwt.mli Outdated
distinct ways:

- [Deferred]: Resolves the promise later, after the current code reaches a
pause or some I/O. This is often the behaviour you want. It makes the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentence got cut off here.

src/core/lwt.mli Outdated
Comment on lines 442 to 447
(if !has_happened then
(* Using [Later] for [ordering] causes this branch to be taken *)
print_endline "Current code was prioritised over resolved promise"
else
(* Using [Nested] for [ordering] causes this branch to be taken *)
print_endline "Resolved promise code was prioritised over current");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not the reverse way? !has_happened is only true once task1 is resolved.

Comment on lines 32 to 34
(* We do not use [~order:Dont_care] here to avoid a stack overflow
when unlocking a lot of threads. *)
Lwt.wakeup_later (Lwt_sequence.take_l m.waiters) ()
Lwt.awaken ~order:Dont_care (Lwt_sequence.take_l m.waiters) ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment disagrees with the code

* master: (155 commits)
  tweaks
  Match let-operator example
  Add headings to make the sections linkable
  Fix docstring code syntax
  Use sequencing PPX in main module docstrings
  Adjust generic syntax
  Add back documentation on sequencing PPX
  also support type annotations at top-level
  more windows tests in CI
  add workflow to test failing windows build
  engine set/id test: add conditionals to only test what can be
  engine set/id test
  expose a way to identify currently-running engine
  remove `-fdiagnostics-color=always` to fix #1082
  remove debugging-helper executable
  ppx: improve handling of value binding constraints
  fix ppx: correctly move constraint in `let%lwt x : t = …` expressions
  lwt_unix_stubs.c: fix races reported by TSan
  Fix spelling mistakes in documentation
  mark incompatibilities with prereleases
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants