caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] enter_blocking_section / leave_blocking_section question
@ 2002-05-23  3:32 Harry Chomsky
  2002-05-27 14:31 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Harry Chomsky @ 2002-05-23  3:32 UTC (permalink / raw)
  To: Caml-list

I'm starting to use threads with my OCaml-Win32 library, and I'd like to
annotate it with appropriate calls to enter_blocking_section and
leave_blocking_section.  Clearly, if I make a call to a Win32 API function
that may take some time, I'll surround the call with enter_blocking_section
/ leave_blocking_section.  But what if that function may make a callback to
my own code, which may in turn pass control back to OCaml?

For instance, my OCaml code calls send_message, implemented as C code that
calls the Win32 SendMessage function.  If I'm sending a message to a system
window, SendMessage just returns.  But if I'm sending a message to an
OCaml-owned window, my C window procedure will be called, and that in turn
will call an OCaml window procedure.

I'm inclined to do something like this:

CAMLprim value send_message(...)
{
    enter_blocking_section();
    SendMessage(...);
    leave_blocking_section();
}

LRESULT window_proc(...)
{
    leave_blocking_section();
    callback(...); // call OCaml window proc
    enter_blocking_section();
}

Obviously I'll need to guarantee that whenever window_proc is called, it
happens during a blocking section.  Is it safe then for window_proc to
temporarily "suspend" the blocking section?  The enter_ and leave_ calls
will always occur in the proper alternation, but the stack will be in a
different state when leave_ is called than when enter_ was called.  I just
want to make sure that this still constitutes safe and proper use of the
blocking system.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] enter_blocking_section / leave_blocking_section question
  2002-05-23  3:32 [Caml-list] enter_blocking_section / leave_blocking_section question Harry Chomsky
@ 2002-05-27 14:31 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2002-05-27 14:31 UTC (permalink / raw)
  To: Harry Chomsky; +Cc: Caml-list

> I'm starting to use threads with my OCaml-Win32 library, and I'd like to
> annotate it with appropriate calls to enter_blocking_section and
> leave_blocking_section.  Clearly, if I make a call to a Win32 API function
> that may take some time, I'll surround the call with enter_blocking_section
> / leave_blocking_section.  But what if that function may make a callback to
> my own code, which may in turn pass control back to OCaml?

I believe the solution you outline in your message is correct:

> LRESULT window_proc(...)
> {
>     leave_blocking_section();
>     callback(...); // call OCaml window proc
>     enter_blocking_section();
> }
> 
> Obviously I'll need to guarantee that whenever window_proc is called, it
> happens during a blocking section.

Agreed.  Moreover, you should make sure that window_proc is always
called from a thread that was created by Caml, and not some other
thread created by the system.  (I don't believe Windows message
handling creates such extra threads, though.)

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-05-27 14:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-23  3:32 [Caml-list] enter_blocking_section / leave_blocking_section question Harry Chomsky
2002-05-27 14:31 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).