Operating System for Tech Supp <os@qnx.com> wrote:
When a thread terminates, the clean up handlers are called. Then
any thread specific data, with non-NULL values, have their deconstructors
functions called for those keys. Then it releases any resources left
such as storage for the thread’s return value, the stack, memory
used to store thread register state etc.
Things like file descriptors are process resources, as you
wouldn’t get a new set of file descriptors for each thread
you could spawn. Mutexes generally fall outside of a thread
specific scope (except maybe the main()). If you allocated
a mutex inside a thread (ie. off the stack, not malloc’d),
then it too would be “cleaned” up but it’s a bad design
to have a thread lock on a mutex sitting on another threads
stack.
An interrupt handler may also be associated with a thread, and
cleaned up on its death.
Basicly aside from thread specific information, cleanup handling
stack, thread stack, return values and thread state info, I think
the rest could be considered a process resource.
There are two more levels of cleanup.
Most remaining resources are cleaned up when the process that owns
those resources exits – including memory, coids & fds, channels,
etc. Synchronisation objects (e.g. mutexes, condvars, semaphores,
etc.) are cleaned up when the memory they are in is released – usually
this means they are cleaned up when the process exits.
The exception is objects that have a name in the pathname space associated
with them, or specifically, ones that have a name created by an *open()
function with the O_CREAT flag, e.g.: sem_open(), shm_open(), open(),
mq_open(). These objects are cleaned up when the name has been unlinked
and all references to the object have been closed (for fds) or otherwise
ended (e.g. un-mapped for mmap() references to an object). (A name in
the name space created by, say, resmgr_attach(), will be cleaned up
if the process exits – it is a name, but not of the type above.) A
side-effect of this, of course, is that semaphores in a shared memory will
not be cleaned up when the process that created them exits, but instead
will endure as long as the shared memory area exists, as one would hope.
-David
QNX Training Services
dagibbs@qnx.com