2 ** GNU Pth - The GNU Portable Threads
3 ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
5 ** This file is part of GNU Pth, a non-preemptive thread scheduling
6 ** library which can be found at http://www.gnu.org/software/pth/.
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 ** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
23 ** pth_uctx.c: Pth user-space context handling (stand-alone sub-API)
25 /* ``It worries me however, to realize
26 how tough an ass-hole I have had to
27 be, in order to get to stick to the
28 principle of doing things right,
29 rather than "just hack it in".''
30 -- Poul-Henning Kamp <phk@FreeBSD.org> */
33 /* user-space context structure */
35 int uc_stack_own; /* whether stack were allocated by us */
36 char *uc_stack_ptr; /* pointer to start address of stack area */
37 size_t uc_stack_len; /* size of stack area */
38 int uc_mctx_set; /* whether uc_mctx is set */
39 pth_mctx_t uc_mctx; /* saved underlying machine context */
42 /* create user-space context structure */
49 /* argument sanity checking */
51 return pth_error(FALSE, EINVAL);
53 /* allocate the context structure */
54 if ((uctx = (pth_uctx_t)malloc(sizeof(struct pth_uctx_st))) == NULL)
55 return pth_error(FALSE, errno);
57 /* initialize the context structure */
58 uctx->uc_stack_own = FALSE;
59 uctx->uc_stack_ptr = NULL;
60 uctx->uc_stack_len = 0;
61 uctx->uc_mctx_set = FALSE;
62 memset((void *)&uctx->uc_mctx, 0, sizeof(pth_mctx_t));
64 /* pass result to caller */
70 /* trampoline context */
72 pth_mctx_t *mctx_parent;
74 pth_uctx_t uctx_after;
75 void (*start_func)(void *);
77 } pth_uctx_trampoline_t;
78 pth_uctx_trampoline_t pth_uctx_trampoline_ctx;
80 /* trampoline function for pth_uctx_make() */
81 static void pth_uctx_trampoline(void)
83 volatile pth_uctx_trampoline_t ctx;
85 /* move context information from global to local storage */
86 ctx.mctx_parent = pth_uctx_trampoline_ctx.mctx_parent;
87 ctx.uctx_this = pth_uctx_trampoline_ctx.uctx_this;
88 ctx.uctx_after = pth_uctx_trampoline_ctx.uctx_after;
89 ctx.start_func = pth_uctx_trampoline_ctx.start_func;
90 ctx.start_arg = pth_uctx_trampoline_ctx.start_arg;
92 /* switch back to parent */
93 pth_mctx_switch(&(ctx.uctx_this->uc_mctx), ctx.mctx_parent);
95 /* enter start function */
96 (*ctx.start_func)(ctx.start_arg);
98 /* switch to successor user-space context */
99 if (ctx.uctx_after != NULL)
100 pth_mctx_restore(&(ctx.uctx_after->uc_mctx));
102 /* terminate process (the only reasonable thing to do here) */
109 /* make setup of user-space context structure */
113 char *sk_addr, size_t sk_size,
114 const sigset_t *sigmask,
115 void (*start_func)(void *), void *start_arg,
116 pth_uctx_t uctx_after)
118 pth_mctx_t mctx_parent;
121 /* argument sanity checking */
122 if (uctx == NULL || start_func == NULL || sk_size < 16*1024)
123 return pth_error(FALSE, EINVAL);
125 /* configure run-time stack */
126 if (sk_addr == NULL) {
127 if ((sk_addr = (char *)malloc(sk_size)) == NULL)
128 return pth_error(FALSE, errno);
129 uctx->uc_stack_own = TRUE;
132 uctx->uc_stack_own = FALSE;
133 uctx->uc_stack_ptr = sk_addr;
134 uctx->uc_stack_len = sk_size;
136 /* configure the underlying machine context */
137 if (!pth_mctx_set(&uctx->uc_mctx, pth_uctx_trampoline,
138 uctx->uc_stack_ptr, uctx->uc_stack_ptr+uctx->uc_stack_len))
139 return pth_error(FALSE, errno);
141 /* move context information into global storage for the trampoline jump */
142 pth_uctx_trampoline_ctx.mctx_parent = &mctx_parent;
143 pth_uctx_trampoline_ctx.uctx_this = uctx;
144 pth_uctx_trampoline_ctx.uctx_after = uctx_after;
145 pth_uctx_trampoline_ctx.start_func = start_func;
146 pth_uctx_trampoline_ctx.start_arg = start_arg;
148 /* optionally establish temporary signal mask */
150 sigprocmask(SIG_SETMASK, sigmask, &ss);
152 /* perform the trampoline step */
153 pth_mctx_switch(&mctx_parent, &(uctx->uc_mctx));
155 /* optionally restore original signal mask */
157 sigprocmask(SIG_SETMASK, &ss, NULL);
159 /* finally flag that the context is now configured */
160 uctx->uc_mctx_set = TRUE;
165 /* switch from current to other user-space context */
168 pth_uctx_t uctx_from,
171 /* argument sanity checking */
172 if (uctx_from == NULL || uctx_to == NULL)
173 return pth_error(FALSE, EINVAL);
174 if (!(uctx_to->uc_mctx_set))
175 return pth_error(FALSE, EPERM);
177 /* switch underlying machine context */
178 uctx_from->uc_mctx_set = TRUE;
179 pth_mctx_switch(&(uctx_from->uc_mctx), &(uctx_to->uc_mctx));
184 /* destroy user-space context structure */
189 /* argument sanity checking */
191 return pth_error(FALSE, EINVAL);
193 /* deallocate dynamically allocated stack */
194 if (uctx->uc_stack_own && uctx->uc_stack_ptr != NULL)
195 free(uctx->uc_stack_ptr);
197 /* deallocate context structure */