I'm using a C library that allocates some opaque structure, and
returns a pointer to it.  I'd like to save that pointer in a safe
process-wide place, generate my own unique integer key (easy), and
then return that simple key as a handle to my Tcl code.  I also need
to be able to find and use the C pointer from multiple threads.

In the past I've done that sort of thing with my own Tcl_HashTable in
C, but those are a bit low level, not that friendly to deal with.  I'd
rather use nsv_set, nsv_get, etc.!  And I already know how to call
NsTclNsvGetObjCmd() etc. from C.  But the Nsv functions all operate
ONLY on strings...

I suppose I should be able to (very carefully) convert my pointer
value to a string and back each time, but that seems awfully kludgy.
Is there a better way?  Something that lets me store the binary
pointer values as is, without string conversions?  The Tcl_HashTable C
code DOES let me do that.

In general, what's the recommended way for Tcl and/or NaviServer
programmers to allocate handles to C-level pointers, and then access
those handles and pointers from more than one Tcl thread?  Can anyone
point me to examples of code doing it the right way?

Is Tcl_HashTable the right tool to use, or is some nicer Nsv-like
interface available?  (Nsv itself is implemented using Tcl_HashTable,
of course.)  Does Zoran's Thread tsv package have any additional
functionality to help with this, beyond what NaviServer's built-in nsv
commands can do?

I found these wiki pages, but these (handles as values) approaches
seem to basically just be fancier versions of what I've done before
with Tcl_HashTable entries, and don't address use from multiple
threads at all:

  https://wiki.tcl-lang.org/page/Tcl+Handles
  https://wiki.tcl-lang.org/page/Creating+and+Using+Tcl+Handles+in+C+Extensions

(Thanks in advance for your help and advice!)

-- 
Andrew Piskorski <a...@piskorski.com>


_______________________________________________
naviserver-devel mailing list
naviserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/naviserver-devel

Reply via email to