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

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