User-Level Native Thread Primitives (GThread) Library White Paper (G06.26+)

User-Level Native Thread Primitives (GThread Library) 03/31/2005
Hewlett-Packard Company--540065-001 Page 41 of 46
3.6. TNS/E Usage Changes
Specific changes for TNS/E are described in the following sections.
3.6.1. Thread Setup and Initialization
The client is responsible for the allocation of the ‘stack space’ associated with the thread’s runtime
stacks. The allocated buffer is either used directly as a static stack (static threading), or as a save area for
stack contents (swapped threading). The client is also responsible for specifying the exact stack space
size in bytes. This responsibility carries with it an implied assumption that the client has knowledge
(either analytically or empirically) of stack space consumption for that thread in the runtime environment
of the host platform. This issue is further complicated for client code ported between host platforms
where the runtime environment can differ (i.e. TNS/R vs. TNS/E).
In order to alleviate this problem for typical clients, the GThread library provides the following interface
to return an estimated appropriate stack size for the TNS/E native platform given a size for the TNS/R
native platform:
GTH_GET_ESTIMATED_STACK_SIZE_( int32 sizeEstimate )
The sizeEstimate provided by the client is its ‘best guess’ at an appropriate stack space size in bytes for
the TNS/R native system. GTH_GET_ESTIMATED_STACK_SIZE_ returns an estimated size (if
needed) based on the host platform.
The GTH_GET_ESTIMATED_STACK_SIZE_ interface attempts to allow existing TNS/R native clients
to leverage the stack size calculations performed on that platform in order to quickly derive a stack size
that can be applied on a TNS/E platform. The estimate may be appropriate for typical clients, but threads
exhibiting unusual stack-consumption behavior might need different allocations. Ultimately, client
developers must determine the needed thread stack sizes empirically on any platform.
Note that this interface estimates the total space for the mem and reg stacks. The user can control the
ratio of sizes allocated to these two stacks, as described in the next section.
3.6.2. Stack Allocation Control at Thread Initiation
The GThread interfaces attempt to abstract the concept of the existence of a register stack on TNS/E
from the client to the point where, the client does not necessarily need to be cognizant of the existence of
the register store area. However, the GThread primitives cannot predict a ratio of reg stack to mem stack
that utilizes storage efficiently for all possible client code. The ratio can be affected by compiler design
and optimization level. It can vary widely based on the number and size of local variables declared within
functions. For example, local declaration of large arrays or structures decreases the ratio of reg to mem
stack. Small local allocations, coupled with an efficient compiler (utilizing registers in lieu of memory
for saving state, etc) can result in a relatively large reg to mem ratio.