[plt-scheme] MzScheme + SDL threads

From: Barry Tolnas (btolnas at acm.org)
Date: Tue Apr 26 10:45:51 EDT 2005

I'm having problems embedding MzScheme in a C program such that it runs 
in its own thread.

I would like to embed scheme to add an interactive command line to an 
OpenGL graphics program which uses SDL (simple direct media layer) for 
windowing, event handling, and threads.  I want to put scheme in one 
thread and the SDL event loop in  another.  When I tried writing a 
simple test shown below to put scheme in its own SDL thread (which is a 
pthread underneath)  scheme_basic_env() crashes. A stack trace is shown 
after the code listing. It seems there is an infinite recursion in calls 
to GC_clear_stack_inner. I'm running v299.100 on OS X but the intention 
is to be cross platform.

I understand from the "inside PLT MzScheme" manual that this should be 
OK so long as my call to scheme_basic_env is in the same thread as all 
of the other calls to scheme_* functions.  This is a first try at 
embedding scheme so I don't feel like I know what I'm doing.  Can 
anybody see anything obvious I've done wrong or am I suffering from 
using beta code?

Barry Tolnas
Adjunct Faculty, The Evergreen State College, Washington State
#include "scheme.h"
#include "SDL.h"
#include "SDL_thread.h"

int DoScheme(void *args)
  Scheme_Env* e;

  printf("Scheme thread starting\n");

  scheme_set_stack_base(NULL, 1); /* required for OS X, only */
  e = scheme_basic_env();
   scheme_eval_string("(display \"hello world\n\")", e);

  fprintf(stdout,"Scheme Thread finished\n");
  return 0;

int SDL_main(int argc, char *argv[])
  SDL_Thread* thread;

  thread = SDL_CreateThread(DoScheme,NULL);
  while(1) { };
  return 0;

#0    0x005eca90 in GC_clear_stack_inner
#1    0x005ecac0 in GC_clear_stack_inner
#2    0x005ecac0 in GC_clear_stack_inner
    <lots of stack frames deleted here>
#554    0x005ecac0 in GC_clear_stack_inner
#555    0x005b4ebc in scheme_init_setjumpup
#556    0x005469e0 in scheme_basic_env
#557    0x00002e54 in DoScheme at embed.c:17
#558    0x300287a0 in SDL_RunThread
#559    0x3002a948 in RunThread
#560    0x90024910 in _pthread_body

Posted on the users mailing list.