]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man3/makecontext.3
trunc.3: Make the description a little clearer
[thirdparty/man-pages.git] / man3 / makecontext.3
index cee00ae65f3e0502a2d4ad87632edf81d8961bcc..277af2099eb835ec21a1313d2468489fd7c9f864 100644 (file)
@@ -1,5 +1,7 @@
-.\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
+\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
+.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
 .\"
+.\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
 .\"
-.TH MAKECONTEXT 3 2001-11-15 "GNU" "Linux Programmer's Manual"
+.\" 2006-08-02, mtk, Added example program
+.\"
+.TH MAKECONTEXT 3 2017-09-15 "GNU" "Linux Programmer's Manual"
 .SH NAME
 makecontext, swapcontext \- manipulate user context
 .SH SYNOPSIS
 .B #include <ucontext.h>
-.sp
+.PP
 .BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
 .BI "int " argc ", ...);"
-.br
-.BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
+.PP
+.BI "int swapcontext(ucontext_t *" oucp ", const ucontext_t *" ucp );
 .SH DESCRIPTION
 In a System V-like environment, one has the type \fIucontext_t\fP defined in
 .I <ucontext.h>
 and the four functions
-.BR getcontext (2),
-.BR setcontext (2),
+.BR getcontext (3),
+.BR setcontext (3),
 .BR makecontext ()
 and
 .BR swapcontext ()
 that allow user-level context switching
 between multiple threads of control within a process.
-.LP
+.PP
 For the type and the first two functions, see
-.BR getcontext (2).
-.LP
+.BR getcontext (3).
+.PP
 The
 .BR makecontext ()
 function modifies the context pointed to
 by \fIucp\fP (which was obtained from a call to
-.BR getcontext (2)).
+.BR getcontext (3)).
 Before invoking
 .BR makecontext (),
 the caller must allocate a new stack
-for this context and assign its address to \fIucp->uc_stack\fP,
+for this context and assign its address to \fIucp\->uc_stack\fP,
 and define a successor context and
-assign its address to \fIucp->uc_link\fP.
-
+assign its address to \fIucp\->uc_link\fP.
+.PP
 When this context is later activated (using
-.BR setcontext (2)
+.BR setcontext (3)
 or
 .BR swapcontext ())
 the function \fIfunc\fP is called,
@@ -70,13 +75,13 @@ the caller must specify the number of these arguments in
 .IR argc .
 When this function returns, the successor context is activated.
 If the successor context pointer is NULL, the thread exits.
-.LP
+.PP
 The
 .BR swapcontext ()
 function saves the current context in
 the structure pointed to by \fIoucp\fP, and then activates the
 context pointed to by \fIucp\fP.
-.SH "RETURN VALUE"
+.SH RETURN VALUE
 When successful,
 .BR swapcontext ()
 does not return.
@@ -92,28 +97,70 @@ sets \fIerrno\fP appropriately.
 .TP
 .B ENOMEM
 Insufficient stack space left.
-.SH "CONFORMING TO"
+.SH VERSIONS
+.BR makecontext ()
+and
+.BR swapcontext ()
+are provided in glibc since version 2.1.
+.SH ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.TS
+allbox;
+lb lb lb
+l l l.
+Interface      Attribute       Value
+T{
+.BR makecontext ()
+T}     Thread safety   MT-Safe race:ucp
+T{
+.BR swapcontext ()
+T}     Thread safety   MT-Safe race:oucp race:ucp
+.TE
+.SH CONFORMING TO
 SUSv2, POSIX.1-2001.
+POSIX.1-2008 removes the specifications of
+.BR makecontext ()
+and
+.BR swapcontext (),
+citing portability issues, and
+recommending that applications be rewritten to use POSIX threads instead.
 .SH NOTES
-The interpretation of \fIucp->uc_stack\fP is just as in
+The interpretation of \fIucp\->uc_stack\fP is just as in
 .BR sigaltstack (2),
 namely, this struct contains the start and length of a memory area
 to be used as the stack, regardless of the direction of growth of
 the stack.
 Thus, it is not necessary for the user program to
 worry about this direction.
+.PP
+On architectures where
+.I int
+and pointer types are the same size
+(e.g., x86-32, where both types are 32 bits),
+you may be able to get away with passing pointers as arguments to
+.BR makecontext ()
+following
+.IR argc .
+However, doing this is not guaranteed to be portable,
+is undefined according to the standards,
+and won't work on architectures where pointers are larger than
+.IR int s.
+Nevertheless, starting with version 2.8, glibc makes some changes to
+.BR makecontext (),
+to permit this on some 64-bit architectures (e.g., x86-64).
 .SH EXAMPLE
 .PP
 The example program below demonstrates the use of
-.BR getcontext (2),
+.BR getcontext (3),
 .BR makecontext (),
 and
 .BR swapcontext ().
 Running the program produces the following output:
-.in +0.5i
-.nf
-
-$ ./a.out
+.PP
+.in +4n
+.EX
+.RB "$" " ./a.out"
 main: swapcontext(&uctx_main, &uctx_func2)
 func2: started
 func2: swapcontext(&uctx_func2, &uctx_func1)
@@ -122,17 +169,19 @@ func1: swapcontext(&uctx_func1, &uctx_func2)
 func2: returning
 func1: returning
 main: exiting
-
-.fi
-.in -0.5i
-.nf
+.EE
+.in
+.SS Program source
+\&
+.EX
 #include <ucontext.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 static ucontext_t uctx_main, uctx_func1, uctx_func2;
 
-#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
+#define handle_error(msg) \\
+    do { perror(msg); exit(EXIT_FAILURE); } while (0)
 
 static void
 func1(void)
@@ -140,7 +189,7 @@ func1(void)
     printf("func1: started\\n");
     printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
-        die("swapcontext");
+        handle_error("swapcontext");
     printf("func1: returning\\n");
 }
 
@@ -150,7 +199,7 @@ func2(void)
     printf("func2: started\\n");
     printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
-        die("swapcontext");
+        handle_error("swapcontext");
     printf("func2: returning\\n");
 }
 
@@ -161,14 +210,14 @@ main(int argc, char *argv[])
     char func2_stack[16384];
 
     if (getcontext(&uctx_func1) == \-1)
-        die("getcontext");
+        handle_error("getcontext");
     uctx_func1.uc_stack.ss_sp = func1_stack;
     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
     uctx_func1.uc_link = &uctx_main;
     makecontext(&uctx_func1, func1, 0);
 
     if (getcontext(&uctx_func2) == \-1)
-        die("getcontext");
+        handle_error("getcontext");
     uctx_func2.uc_stack.ss_sp = func2_stack;
     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
     /* Successor context is f1(), unless argc > 1 */
@@ -177,15 +226,15 @@ main(int argc, char *argv[])
 
     printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
-        die("swapcontext");
+        handle_error("swapcontext");
 
     printf("main: exiting\\n");
     exit(EXIT_SUCCESS);
 }
-.fi
-.SH "SEE ALSO"
-.BR getcontext (2),
+.EE
+.SH SEE ALSO
 .BR sigaction (2),
 .BR sigaltstack (2),
 .BR sigprocmask (2),
+.BR getcontext (3),
 .BR sigsetjmp (3)