caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Threads + Win32 API = crash
@ 2002-04-13 20:46 Harry Chomsky
  2002-04-15 14:54 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: Harry Chomsky @ 2002-04-13 20:46 UTC (permalink / raw)
  To: Caml-list

[-- Attachment #1: Type: text/plain, Size: 1264 bytes --]

I'm having trouble incorporating multithreading support into my OCaml-Win32
library.  Basically, any program that uses OCaml threading and also invokes
a Win32 message loop will crash after a few seconds with an illegal attempt
to access memory at "0x00000001".  This behavior is demonstrated by the
enclosed sample program, which is very simple and does not require my
OCaml-Win32 library.

I tried following the suggestion from the mailing list of adding "if not
(!Sys.interactive && signal=(-11))" to the preempt function, but it doesn't
seem to help.

I don't really know how to investigate this problem further.  I've
determined that the problem goes away if I disable the tick thread, so
presumably it has something to do with the interaction of the tick thread
and the Windows message loop.  I've also noticed that if I call
enter_blocking_section / leave_blocking_section in my C code, the behavior
changes: the program exits abruptly with no error message.  By inserting
printf's, I've concluded that the GPF and/or abrupt exit do not occur during
execution of any of my C code.  Beyond that, I don't know what to try.

Does anybody have any idea why OCaml's threads might be incompatible with a
Windows message loop, or how I can work on debugging this?

[-- Attachment #2: thr_c.c --]
[-- Type: application/octet-stream, Size: 730 bytes --]

#include <windows.h>
#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/custom.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

CAMLprim value make_window(value unit)
{
    HWND hWnd;

    hWnd = CreateWindow("EDIT",
        "Generic Application",
        WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
        0,
        0,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        GetModuleHandle(NULL),
        NULL);

    ShowWindow(hWnd, SW_SHOWDEFAULT);

    return Val_unit;
}

CAMLprim value msg(value unit)
{
    MSG msg;

    if (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return Val_unit;
}

[-- Attachment #3: thr.ml --]
[-- Type: application/octet-stream, Size: 199 bytes --]

let _ = Thread.create

external make_window : unit -> unit = "make_window"

external msg : unit -> unit = "msg"

let rec msg_loop () =
    msg ();
    msg_loop ()

let _ = make_window(); msg_loop ()

[-- Attachment #4: build.bat --]
[-- Type: application/octet-stream, Size: 82 bytes --]

ocamlc -custom -thread -o thr.exe unix.cma threads.cma thr.ml thr_c.c user32.lib

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

* Re: [Caml-list] Threads + Win32 API = crash
  2002-04-13 20:46 [Caml-list] Threads + Win32 API = crash Harry Chomsky
@ 2002-04-15 14:54 ` Xavier Leroy
  2002-04-16 18:06   ` Harry Chomsky
  0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 2002-04-15 14:54 UTC (permalink / raw)
  To: Harry Chomsky; +Cc: Caml-list

> I'm having trouble incorporating multithreading support into my OCaml-Win32
> library.  Basically, any program that uses OCaml threading and also invokes
> a Win32 message loop will crash after a few seconds with an illegal attempt
> to access memory at "0x00000001".  This behavior is demonstrated by the
> enclosed sample program, which is very simple and does not require my
> OCaml-Win32 library.
> I don't really know how to investigate this problem further.  I've
> determined that the problem goes away if I disable the tick thread

Several users reported similar problems with threads under Windows in
OCaml 3.04.  After investigation, I found that I managed to break the
timer-based preemption while trying to work around another issue in
the Windows port.  

If you're willing to recompile from sources, here is the fix: in
byterun/signals.c, replace

  CAMLexport void leave_blocking_section(void)
  {
  #ifdef _WIN32
    /* Under Win32, asynchronous signals such as ctrl-C are not processed
       immediately (see ctrl_handler in win32.c), but simply set
       pending_signal and let the system call run to completion.
       Hence, test pending_signal here and act upon it, before we get
       a chance to process the result of the system call. */
    int signal_number = pending_signal;
    pending_signal = 0;
    if (signal_number) execute_signal(signal_number, 1);
  #endif
    if (leave_blocking_section_hook != NULL) leave_blocking_section_hook();
    Assert(async_signal_mode);
    async_signal_mode = 0;
  }

by

  CAMLexport void leave_blocking_section(void)
  {
  #ifdef _WIN32
    int signal_number;
  #endif

    if (leave_blocking_section_hook != NULL) leave_blocking_section_hook();
  #ifdef _WIN32
    /* Under Win32, asynchronous signals such as ctrl-C are not processed
       immediately (see ctrl_handler in win32.c), but simply set
       pending_signal and let the system call run to completion.
       Hence, test pending_signal here and act upon it, before we get
       a chance to process the result of the system call. */
    signal_number = pending_signal;
    pending_signal = 0;
    if (signal_number) execute_signal(signal_number, 1);
  #endif
    Assert(async_signal_mode);
    async_signal_mode = 0;
  }

Hope this helps,

- 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] 3+ messages in thread

* Re: [Caml-list] Threads + Win32 API = crash
  2002-04-15 14:54 ` Xavier Leroy
@ 2002-04-16 18:06   ` Harry Chomsky
  0 siblings, 0 replies; 3+ messages in thread
From: Harry Chomsky @ 2002-04-16 18:06 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: Caml-list

Xavier Leroy wrote:
> After investigation, I found that I managed to break the
> timer-based preemption while trying to work around another issue in
> the Windows port.  
> 
> If you're willing to recompile from sources, here is the fix:

Thanks -- that solved the problem!

-------------------
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] 3+ messages in thread

end of thread, other threads:[~2002-04-16 18:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-13 20:46 [Caml-list] Threads + Win32 API = crash Harry Chomsky
2002-04-15 14:54 ` Xavier Leroy
2002-04-16 18:06   ` Harry Chomsky

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).