1 MAKECONTEXT(3C)          Standard C Library Functions          MAKECONTEXT(3C)
   2 
   3 NAME
   4      makecontext, swapcontext, swapcontext_extd - manipulate user contexts
   5 
   6 SYNOPSIS
   7      #include <ucontext.h>
   8 
   9      void
  10      makecontext(ucontext_t *ucp, void (*ifunc)(), int argc, ...);
  11 
  12      int
  13      swapcontext(ucontext_t *restrict oucp, const ucontext_t *restrict ucp);
  14 
  15      int
  16      swapcontext_extd(ucontext_t *restrict oucp, uint32_t flags,
  17          const ucontext_t *restrict ucp);
  18 
  19 DESCRIPTION
  20      The makecontext() function modifies the context specified by ucp, which
  21      has been initialized using getcontext(2) or getcontext_extd(2).  When
  22      this context is resumed using swapcontext(), swapcontext_extd(), or
  23      setcontext(2), execution continues by calling the function func, passing
  24      it the arguments that follow argc in the makecontext() call.  The value
  25      of argc must match the number of pointer-sized integer arguments passed
  26      to func, otherwise the behavior is undefined.
  27 
  28      Before a call is made to makecontext(), the context being modified should
  29      have a stack allocated for it.  The stack is assigned to the context by
  30      initializing the uc_stack member.
  31 
  32      The uc_link member is used to determine the context that will be resumed
  33      when the context being modified by makecontext() returns.  The uc_link
  34      member should be initialized prior to the call to makecontext().  If the
  35      uc_link member is initialized to NULL, the thread executing func will
  36      exit when func returns. See pthread_exit(3C).
  37 
  38      The swapcontext() function saves the current context in the context
  39      structure pointed to by oucp and sets the context to the context
  40      structure pointed to by ucp.
  41 
  42      If the ucp or oucp argument points to an invalid address, the behavior is
  43      undefined and errno may be set to EFAULT.
  44 
  45      The swapcontext_extd() function is similar to swapcontext() except that
  46      it performs a call to getcontext_extd(2) to get and save the current
  47      context, passing the flags argument to getcontext_extd(2).  Note, the
  48      same constraints around the initialization of the ucontext_t that are
  49      discussed in getcontext_extd(2) still apply.  Mainly, the context must
  50      either have originally come from ucontext_alloc(3C) or prior to its first
  51      use been zeroed.  See getcontext_extd(2) for more information.
  52 
  53 RETURN VALUES
  54      On successful completion, swapcontext() and swapcontext_extd() return 0.
  55      Otherwise, -1 is returned and errno is set to indicate the error.
  56 
  57 EXAMPLES
  58      Example 1 Alternate execution context on a stack whose memory was
  59      allocated using mmap().
  60 
  61       #include <stdio.h>
  62       #include <ucontext.h>
  63       #include <sys/mman.h>
  64 
  65       void
  66       assign(long a, int *b)
  67       {
  68               *b = (int)a;
  69       }
  70 
  71       int
  72       main(int argc, char **argv)
  73       {
  74               ucontext_t uc, back;
  75               size_t sz = 0x10000;
  76               int value = 0;
  77 
  78               getcontext(&uc);
  79 
  80               uc.uc_stack.ss_sp = mmap(0, sz,
  81                   PROT_READ | PROT_WRITE | PROT_EXEC,
  82                   MAP_PRIVATE | MAP_ANON, -1, 0);
  83               uc.uc_stack.ss_size = sz;
  84               uc.uc_stack.ss_flags = 0;
  85 
  86               uc.uc_link = &back;
  87 
  88               makecontext(&uc, assign, 2, 100L,     &value);
  89               swapcontext(&back, &uc);
  90 
  91               printf("done %d\n", value);
  92 
  93               return (0);
  94       }
  95 
  96 ERRORS
  97      The swapcontext() and swapcontext_extd() function will fail if:
  98 
  99      ENOMEM             The ucp argument does not have enough stack left to
 100                         complete the operation.
 101 
 102      The swapcontext() and swapcontext_extd() functions may fail if:
 103 
 104      EFAULT             The ucp or oucp argument points to an invalid address.
 105 
 106      The swapcontext_extd() function may additionally fail if:
 107 
 108      EINVAL             The flags argument contains invalid values.
 109 
 110 USAGE
 111      These functions are useful for implementing user-level context switching
 112      between multiple threads of control within a process (co-processing).
 113      More effective multiple threads of control can be obtained by using
 114      native support for multithreading.  See threads(7).
 115 
 116 INTERFACE STABILITY
 117      Committed
 118 
 119 MT-LEVEL
 120      MT-Safe
 121 
 122 SEE ALSO
 123      getcontext(2), getcontext_extd(2), mmap(2), sigaction(2), sigprocmask(2),
 124      pthread_exit(3C), ucontext_alloc(3C), ucontext.h(3HEAD), attributes(7),
 125      standards(7), threads(7)
 126 
 127 NOTES
 128      The semantics of the uc_stack member of the ucontext_t structure have
 129      changed as they apply to inputs to makecontext().  Prior to Solaris 10,
 130      the ss_sp member of the uc_stack tructure represented the high memory
 131      address of the area reserved for the stack.  The ss_sp member now
 132      represents the base (low memory address), in keeping with other uses of
 133      ss_sp.  This change in the meaning of ss_sp is the default behavior.
 134 
 135      Binary compatibility has been preserved with releases prior to Solaris
 136      10.  Before recompiling, applications that use makecontext() must be
 137      updated to reflect this behavior change.  The example below demonstrates
 138      a typical change that must be applied:
 139 
 140       --- example1_s9.c       Thu Oct  3 11:58:17 2002
 141       +++ example1.c  Thu Jun 27 13:28:16 2002
 142       @@ -27,12 +27,9 @@
 143               uc.uc_stack.ss_sp = mmap(0, sz,
 144                   PROT_READ | PROT_WRITE | PROT_EXEC,
 145                   MAP_PRIVATE | MAP_ANON, -1, 0);
 146       -       uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
 147               uc.uc_stack.ss_size = sz;
 148               uc.uc_stack.ss_flags = 0;
 149 
 150               uc.uc_link = &back
 151 
 152               makecontext(&uc, assign, 2, 100L,     &value);
 153 
 154 illumos                         March 20, 2023                         illumos