Standards / Extensions | C or C++ | Dependencies |
---|---|---|
Single UNIX Specification, Version 3 |
both | z/OS V1R9 |
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *addr);
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.
If successful, pthread_attr_setstackaddr() returns 0; otherwise, it returns an error number.
⁄* 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);
}