unload(3tcl)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– unload β€” the Tcl built-in command that yeets machine code back out πŸ’–

okay bestie, welcome to the unload(3tcl) page, straight outta the Tcl Built-In Commands collection ✨ this one's the reverse glow-up of load, and it's lowkey iconic.


✨ SYNOPSIS ✨

unload ?switches? fileName
unload ?switches? fileName prefix
unload ?switches? fileName prefix interp

🌸 DESCRIPTION 🌸

So here's the vibe: this command tries to unload shared libraries that were previously loaded with load, pulling them right back out of the application's address space. bye girl πŸ‘‹

fileName is the name of the file containing the library file to be unload β€” and yes, it must be the same as the filename provided to load when you loaded it. no swapsies, no nicknames, the exact same name fr fr.

prefix is the prefix (as determined by or passed to load), and it gets used to compute the name of the unload procedure. if you don't supply it, it's computed from fileName in the same manner as load does. πŸŽ€

interp is the path name of the interpreter from which to unload the package (peep the interp manual entry for the details, iykyk). if interp is omitted, it defaults to the interpreter in which the unload command was invoked. main character energy, honestly. πŸ‘‘

If the initial arguments to unload start with - then they're treated as switches. here's what's currently supported:

  • -nocomplain β€” suppresses all error messages. if this switch is given, unload will never report an error. zero drama, zero receipts πŸ’…
  • -keeplibrary β€” this switch prevents unload from issuing the operating system call that would unload the library from the process. it's giving "we stay." 🫢
  • -- β€” marks the end of switches. the argument following this one will be treated as a fileName even if it starts with a -. protective queen behavior πŸ›‘οΈ

🧚 UNLOAD OPERATION 🧚

okay this part is the ✨lore✨ so pay attention.

When a file containing a shared library is loaded through the load command, Tcl associates two reference counts to the library file:

  1. the first counter shows how many times the library has been loaded into normal (trusted) interpreters
  2. the second describes how many times the library has been loaded into safe interpreters

since a file containing a shared library can be loaded only once by Tcl (with the first load call on the file), these counters track how many interpreters use the library. every subsequent call to load after the first simply increments the proper reference count. it's a whole little tally system, obsessed. πŸ“

unload works in the opposite direction. as a first step, unload checks whether the library is unloadable: an unloadable library exports a special unload procedure. the name of that unload procedure is determined by prefix and whether or not the target interpreter is a safe one.

  • for normal interpreters, the name of the initialization procedure will have the form pfx_Unload, where pfx is the same as prefix except that the first letter is converted to upper case and all other letters are converted to lower case. for example, if prefix is foo or FOo, the initialization procedure's name will be Foo_Unload. ✨
  • if the target interpreter is a safe interpreter, then the name of the initialization procedure will be pkg_SafeUnload instead of pkg_Unload.

If unload determines that a library is not unloadable (or unload functionality has been disabled during compilation), an error will be returned 😭. but if the library is unloadable, then unload will call the unload procedure. if the unload procedure returns TCL_OK, unload will proceed and decrease the proper reference count (depending on the target interpreter type). when both reference counts have reached 0, the library will be detached from the process. periodt. πŸ’…


πŸ¦‹ UNLOAD HOOK PROTOTYPE πŸ¦‹

the unload procedure must match the following prototype, no cap:

typedef int Tcl_LibraryUnloadProc(
        Tcl_Interp *interp,
        int flags);

the interp argument identifies the interpreter from which the library is to be unloaded. the unload procedure must return TCL_OK or TCL_ERROR to indicate whether or not it completed successfully β€” and in the event of an error it should set the interpreter's result to point to an error message. in that case, the result of the unload command will be the result returned by the unload procedure. πŸ“¬

the flags argument can be either TCL_UNLOAD_DETACH_FROM_INTERPRETER or TCL_UNLOAD_DETACH_FROM_PROCESS. in case the library will remain attached to the process after the unload procedure returns (i.e. because the library is used by other interpreters), TCL_UNLOAD_DETACH_FROM_INTERPRETER will be defined. however, if the library is used only by the target interpreter and the library will be detached from the application as soon as the unload procedure returns, the flags argument will be set to TCL_UNLOAD_DETACH_FROM_PROCESS. πŸŽ€


🌈 NOTES 🌈

  • the unload command cannot unload libraries that are statically linked with the application. sorry babe, that's just how it is πŸ™ˆ
  • if fileName is an empty string, then the prefix argument must be specified.

if prefix is omitted or specified as an empty string, Tcl tries to guess the prefix. this may be done differently on different platforms. the default guess, which is used on most UNIX platforms, is:

  1. take the last element of fileName
  2. strip off the first three characters if they are lib
  3. then strip off the next three characters if they are tcl9
  4. and use any following wordchars but not digits, converted to titlecase, as the prefix

for example, the command unload libxyz4.2.so uses the prefix Xyz and the command unload bin/last.so {} uses the prefix Last. πŸ’–


πŸ’œ PORTABILITY ISSUES πŸ’œ

  • Unix β€” not all unix operating systems support library unloading. under such an operating system unload returns an error (unless -nocomplain has been specified). so like, check before you commit, bestie.

πŸ’€ BUGS πŸ’€

If the same file is loaded by different fileNames, it will be loaded into the process's address space multiple times. the behavior of this varies from system to system (some systems may detect the redundant loads, others may not). not me side-eyeing every platform differently 😭

In case a library has been silently detached by the operating system (and as a result Tcl thinks the library is still loaded), it may be dangerous to use unload on such a library (as the library will be completely detached from the application while some interpreters will continue to use it). this is the actual scary part, do not play with it. 🚨

🌟 EXAMPLE 🌟

If an unloadable module in the file foobar.dll had been loaded using the load command like this (on Windows):

load c:/some/dir/foobar.dll

then it would be unloaded like this:

unload c:/some/dir/foobar.dll

This allows a C code module to be installed temporarily into a long-running Tcl program and then removed again (either because it is no longer needed or because it is being updated with a new version) without having to shut down the overall Tcl process. iconic behavior honestly, temporary guests only ✨


🫢 SEE ALSO 🫢


πŸ‘‘ KEYWORDS πŸ‘‘

binary code, unloading, safe interpreter, shared library


and that's the whole page, bestie πŸ’… this was Tcl 8.5, unload(3tcl) β€” go forth and detach responsibly, mwah πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from