Lisp HUG Maillist Archive

LWM running non-mp safe code after mp is started

Is there a general way to execute arbitrary code in the context of the 
initial thread of a LispWorks image after mutliprocessing has been 
enabled and the REPL is running in its own thread?

Said another way, I'd like to be able to send the initial thread (via a 
mailbox or something) an arbitrary form to run after the LispWorks 
environment is fully started (is the initial thread the "The idle 
process" Priority -536870912 State "Running (preempted)" process?).

The context is a foreign function interface for the CoreMIDI library on 
Mac OS X (10.3.7). The problem I have run into is that calling 
CoreMIDI's MIDIRestart() function to initialize the sytem fails if 
multiprocessing has already been started (which is that case for the 
default LispWorks.app image).

The error I get is:

CoreMIDI: timeout waiting for MIDIServer to start
-10839

However if I use an image which does not start multiprocessing by 
default calling MIDIRestart() succeeds. Once the CoreMIDI library is 
initialized then enabling multiprocessing does not cause any problems. 
What I suspect is happening is that when multiprocessing is enabled the 
handshake between the CoreMIDI library and the external MIDIServer 
process is failing because the MIDIServer is responding (via a Mach IPC 
message) to the wrong thread (again I'm just speculating).

The current hack I have which works for a tty based image involves 
building a special image which calls MIDIRestart() on startup using an 
action. While this basically works I would like to avoid the additional 
1-2 seconds of startup time and avoid the need to maintain a special 
image. Using the following to create the image:

=======================================
(load-all-patches)
(defvar *midi-server-status* nil)
(fli:define-foreign-function (midi-restart "MIDIRestart" :source)
                              nil
                              :result-type :long
                              :language :ansi-c)
(defun midi-startup ()
   (format t "Starting MIDIServer...")
   (fli:register-module 
"/System/Library/Frameworks/CoreMIDI.framework/CoreMIDI")
   (setf *midi-server-status* (midi-restart))
   (format t " done.~%"))

(define-action "When starting image" "MIDI Startup" 'midi-startup 
:after "System starup completed")
(save-image "./lispworks-midi" :environment nil :multiprocessing t)
(quit)
=========================================

Results in:

% ./lispworks-midi
Starting MIDIServer... done.
LispWorks(R): The Common Lisp Programming Environment
Copyright (C) 1987-2004 Xanalys Inc.  All rights reserved.
Version 4.4.0
Saved by greg as lispworks-midi, at 29 Dec 2004 16:15
User greg on evasion.local
; Loading text file /Applications/LispWorks 
4.4/Library/lib/4-4-0-0/config/siteinit.lisp
;  Loading text file /Applications/LispWorks 
4.4/Library/lib/4-4-0-0/private-patches/load.lisp

CL-USER 1 > *midi-server-status*
0             ;; <- this means the startup was successful



Any help/advice would be greatly appreciated.

Thanks

-greg

_______________________________________________________
Greg Wuller                             greg@wuller.com
_______________________________________________________


Re: LWM running non-mp safe code after mp is started

On Dec 29, 2004, at 8:18 PM, Greg Wuller wrote:

> Said another way, I'd like to be able to send the initial thread (via 
> a mailbox or something) an arbitrary form to run after the LispWorks 
> environment is fully started (is the initial thread the "The idle 
> process" Priority -536870912 State "Running (preempted)" process?).

Did you try to define your action on "Initialize LispWorks Tools", 
something like:

(lw:define-action "Initialize LispWorks Tools" "create compilation 
conditions browser" 'cc-browser)

Best,

John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL


Updated at: 2020-12-10 08:54 UTC