[ Previous ] [ Contents ] [ Index ] [ Next ]

Linked Interpreters and Shared Files

The Tcl interpreters for a virtual server are now linked together in an interpreter group. This capability will simplify maintaining state in the single address space of the multi-threaded AOLserver. The interpreter group shares:

global variables
C commands
Tcl scripts
open files
Tcl command traces
Tcl math function table
Each connection thread that requires Tcl will create a small interpreter which joins the interpreter group instead of waiting for an idle interpreter.

The AutoClose parameter, which is on by default, will close all non-shared files opened by an individual interpreter in the group when Ns_TclDeAllocate is invoked. (Other interpreters in the group are not affected.) Ns_TclDeAllocate is called after each Tcl request procedure.

It is recommended that you leave the AutoClose parameter set to on and share files between interpreters.

Shared Files

To share a file between the interpreters in a group, use the Tcl detach command. A shared file is left open after an interpreter is deallocated when the AutoClose parameter is on (it is on by default). For example:

    #init time:
    set sharedFile [open myfile.dat]
    detach $sharedFile

You must manually close the shared file (by performing a close) when it is no longer needed or when the server is shut down.

Note that there is no implicit locking between interpreters. This means that one interpreter can be performing a gets command on the shared file at the same time another interpreter performs a close on the same file. The results are unpredictable, and could potentially cause a core dump. To avoid this situation, use a mutex to protect access to the file whenever it is accessed.

For example:, at initialization time (for example, in your init.tcl script), open a shared file and create a mutex:

    set sharedLock [ns_mutex create]
    set sharedFile [open myfile.dat]
    detach $sharedFile

At run time, use the mutex to protect any actions you perform on the shared file:

    global sharedLock
    global sharedFile
    ns_mutex lock $sharedLock
    puts $sharedFile ...
    gets $sharedFile ...
    ns_mutex unlock $sharedLock

At shutdown (for example, in your shutdown procedure registered with ns_atshutdown), close the file and destroy the lock:

    global sharedLock
    global sharedFile
    close $SharedFile
    ns_mutex destroy $SharedLock

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1996 America Online, Inc.