pthread_attr_setstackaddr() - Set stackaddr attribute

Standards

Standards / Extensions C or C++ Dependencies

Single UNIX Specification, Version 3

both

z/OS V1R9
POSIX(ON)

Format

#define _UNIX03_THREADS
#include <pthread.h>

int pthread_attr_setstackaddr(pthread_attr_t *attr, void *addr); 

General description

The pthread_attr_setstackaddr() function sets the stackaddr attribute in attr using the value of addr.

attr is a pointer to a thread attribute object initialized by pthread_attr_init().

addr is the lowest addressable byte of the memory designated for use as the initial stack segment. It must have at least PTHREAD_STACK_MIN storage allocated. The PTHREAD_STACK_MIN constant is defined in <limits.h>. The addr value must also be aligned with the stack frame size, a multiple of 4K in 31-bit applications and one megabyte in AMODE 64.

The thread must have permission to read and write to all pages within the stack referenced by addr.

A stacksize is required at pthread creation. If the value is not present in the thread attribute object, the stacksize will default to PTHREAD_STACK_MIN. Subsequent calls to pthread_attr_setstacksize() can overwrite the stacksize prior to pthread creation.

Usage notes

  1. The pthread_attr_setstackaddr() function is provided for historical reasons. It is marked obsolescent in the Single UNIX Specification, Version 3 (SUSv3). New applications should use the newer function pthread_attr_setstack(), which provides functionality compatible with the SUSv3 standard.
  2. An attribute object with the stackaddr attribute set may not be used more than once, unless it is destroyed and reinitialized,or its stackaddr attribute changed. For more details, see "Thread stack attributes" in the z/OS® X/L C/C++ Programming Guide.
  3. An XPLINK application uses two stacks, an upward-growing stack and a downward-growing stack. The variable addr always refers to lowest addressable byte of the downward-growing stack.
  4. The Language Environment® storage report tolerates but does not maintain statistics on application-managed stacks. Also, the runtime storage option, suboption for dsa initialization does not support application-managed stacks.

Returned value

If successful, pthread_attr_setstackaddr() returns 0; otherwise, it returns an error number.

Error Code
Description
EINVAL
The value of addr does not have proper alignment to be used as a stack or the value specified by attr does not refer to an initialized thread attribute object.

Example

⁄* CELEBP68 *⁄                                   
⁄* Example using SUSv3 pthread_attr_setstackaddr() interface *⁄ 

#define _UNIX03_THREADS 1

#include <stdio.h>                                                              
#include <stdlib.h>
#include <pthread.h>                                                            
#include <limits.h>                                                            
#include <errno.h>                                                            
                                                                                
int main(void)
{
   pthread_attr_t attr;                                                         
   int              rc; 
   void  *mystack;
                                                                                
   if (pthread_attr_init(&attr) == -1) {                                        
      perror("error in pthread_attr_init");                                     
      exit(1);                                                                  
   }                                                                            

   ⁄* Get a big enough stack and align it on 4K boundary. *⁄
   mystack = malloc(PTHREAD_STACK_MIN * 2);
   if (mystack != NULL) {
      printf("Using PTHREAD_STACK_MIN to align stackaddr %x.\n", mystack);
      mystack = (void *)((((long)mystack + (PTHREAD_STACK_MIN - 1)) ⁄
                          PTHREAD_STACK_MIN) * PTHREAD_STACK_MIN);
   } else {
      perror("Unable to acquire storage.");
      exit(2);
   }
                                                                                
   printf("Setting stackaddr to %x\n", mystack);
   rc = pthread_attr_setstackaddr(&attr, mystack);
   if (rc != 0) {                                           
      printf("pthread_attr_setstackaddr returned: %d\n", rc); 
      printf("Error: %d, Errno_Jr: %08x\n", errno, __errno2());
      exit(3);                                                                  
   } else {
      printf("Set stackaddr to %x\n", mystack);
   }

   rc = pthread_attr_destroy(&attr);
   if (rc != 0) {                                     
      perror("error in pthread_attr_destroy");                                  
      printf("Returned: %d, Error: %d\n", rc, errno); 
      printf("Errno_Jr: %x\n", __errno2());
      exit(4);                                                                  
   }                                                                            

   exit(0);                                                                     
}                                                                               

Related information