User-Level Native Thread Primitives (GThread) Library White Paper (G06.27+, H06.03+, J06.03+)

User-Level Native Thread Primitives (GThread Library) 02/15/2012
540065-004 Page 39 of 44
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 a register stack on TNS/E so the client does not
necessarily need to be cognizant of the existence of the register store area.
3.6.2.1. Unprotected Static Stacks
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.
The GThread primitives apply a ‘default’ ratio when allocating the mem stack and reg stack
during thread initialization (GTHREAD_INITIATE_). However, since this ratio may not be
optimal for all client implementations, the interface provides clients with the option to change the
fraction of allocated stack space devoted to the reg stack.
The Swaptions argument to GTHREAD_INITIATE_ provides the client with the ability to
specify the allocation ratio. The ratio is specified as a ‘percentage’ from 10 to 80 percent and
provided using the following define:
GTS_STATIC_RegPerCent(n)
where n is the percentage of the total allocated space devoted to the reg stack. The remaining
space is devoted to the mem stack. This define replaces the define GTS_STATIC for those clients
that wish to control this ratio. A call to GTHREAD_INITIATE_ utilizing the new define would
be as follows:
GTHREAD_INITIATE_(...,length,GTS_STATIC_RegPerCent(30),...);
specifying that 30% of the specified length be devoted to the reg stack and the other 70% to the
mem stack.
GTS_STATIC_RegPerCent(0) is equivalent to GTS_STATIC; GTHREAD_INITIATE_ uses a
default ratio of reg to mem stack. That ratio is implementation-dependent.
On non-TNS/E native platforms, or when the Swaptions argument includes GTS_PROTECTED,
GTS_STATIC_RegPerCent(n) is equivalent to GTS_STATIC; the percentage value n is ignored.
On TNS/E native platforms, GTS_STATIC_RegPerCent takes its argument modulo 128.
GTHREAD_INITIATE_ returns the error GT_BAD_SWAPTIONS if the resulting RegPerCent
value is outside the range of 10 to 80.