Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Thread issue (?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: annan@h...
Subject: [Caml-list] Thread issue (?)

Hello OCaml listers,

  I sent a message in a week or two ago regarding some behavior I didn't
understand, and a whole lot of code.  Not getting any responses, I figured
I'd better get a better understanding of what's going on before asking
the folks on here about it... So, I managed to reproduce the behaviour
with the following code (on linux, with gcc-3.2.1, and ocaml-3.06):

***************** oops.c
#include <pthread.h>
#include <unistd.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/callback.h>

static value * f = 0;
static value * v = 0;
static pthread_t p = 0;

/* so we need a function to call from OCaml that sets the callback, 
   a function that runs the callback, and a function (called from OCaml)

   that starts the thread that runs the function that runs the callback.


   whee.                                                            
   */

value im_setting_the_callback(value no_val) {
  CAMLparam1 (no_val);

  f = caml_named_value("segfault_function");
  v = caml_named_value("segfault_function_val");

  CAMLreturn(Val_unit);
}

void* look_at_me_segfault(void *arg) {
  CAMLparam0 ();
  callback(*f, *v);
  CAMLreturn0;
}

value segfault_thread_in_C(value v) {
  CAMLparam1 (v);
  pthread_create(&p, NULL, &look_at_me_segfault, NULL);
  CAMLreturn(Val_unit);
}
**********

********** oops.ml
external set_callback : unit -> unit = "im_setting_the_callback"
external run_callback_from_C_thread : unit -> unit = "segfault_thread_in_C"

let look_at_me_segfault s = print_string s; flush Pervasives.stdout

let main () = 
  Callback.register "segfault_function" look_at_me_segfault;
  Callback.register "segfault_function_val" "oops, i'm segfaulting\n";

  set_callback ();
  Thread.create run_callback_from_C_thread ();

  Unix.sleep 2;;

main ();;
**********

build with 
gcc -c -g -I /usr/local/lib/ocaml -o oops_c.o oops.c
ocamlopt -o oops -thread -w s -cclib -lpthread unix.cmxa threads.cmxa
oops_c.o oops.ml

********** gdb output
bash-2.05b$ gdb oops
GNU gdb 5.2.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-slackware-linux"...
(gdb) break posix.c:191
Breakpoint 1 at 0x8056346: file posix.c, line 191.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/annan/proj/oop_server/oops 
[New Thread 16384 (LWP 25146)]
[New Thread 32769 (LWP 25147)]
[New Thread 16386 (LWP 25148)]
[New Thread 32771 (LWP 25149)]
[Switching to Thread 32771 (LWP 25149)]

Breakpoint 1, caml_thread_leave_blocking_section () at posix.c:191
191       caml_bottom_of_stack= curr_thread->bottom_of_stack;
1: curr_thread = 0x807cd70
(gdb) c
Continuing.
[New Thread 49156 (LWP 25150)]
[Switching to Thread 49156 (LWP 25150)]

Breakpoint 1, caml_thread_leave_blocking_section () at posix.c:191
191       caml_bottom_of_stack= curr_thread->bottom_of_stack;
1: curr_thread = 0x0
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
caml_thread_leave_blocking_section () at posix.c:191
191       caml_bottom_of_stack= curr_thread->bottom_of_stack;
1: curr_thread = 0x0
************
 
  Can someone help me understand what's going on, and if this is a bug
in my code (very easily possible), or is this a corner case in OCaml
that didn't get provided for?  

  Thanks in advance,
  Annan Harley

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