Add to git.
[pthrlib.git] / src / pthr_context.h
1 /* Very Linux-specific context switching.
2  * Written by RWMJ with lots of help from GNU pth library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: pthr_context.h,v 1.7 2003/02/05 22:13:32 rich Exp $
19  */
20
21 #ifndef PTHR_CONTEXT_H
22 #define PTHR_CONTEXT_H
23
24 #include <setjmp.h>
25
26 #ifdef HAVE_WORKING_SETCONTEXT
27
28 #include <ucontext.h>
29
30 typedef struct mctx_st {
31   ucontext_t uc;
32 } mctx_t;
33
34 /* Save machine context. */
35 #define mctx_save(mctx) (void) getcontext (&(mctx)->uc)
36
37 /* Restore machine context. */
38 #define mctx_restore(mctx) (void) setcontext (&(mctx)->uc)
39
40 /* Switch machine context. */
41 #define mctx_switch(mctx_old, mctx_new) \
42 (void) swapcontext (&((mctx_old)->uc), &((mctx_new)->uc))
43
44 #else /* setjmp implementation */
45
46 #include <setjmp.h>
47
48 typedef struct mctx_st {
49   jmp_buf jb;
50 } mctx_t;
51
52 /* Save machine context. */
53 #define mctx_save(mctx) (void) setjmp ((mctx)->jb)
54
55 /* Restore machine context. */
56 #define mctx_restore(mctx) longjmp ((mctx)->jb, 1)
57
58 /* Switch machine context. */
59 #define mctx_switch(mctx_old, mctx_new) \
60 do { if (setjmp ((mctx_old)->jb) == 0) longjmp ((mctx_new)->jb, 1); } while(0)
61
62 #endif /* ! USE_UCONTEXT */
63
64 /* Create machine context. */
65 extern void mctx_set (mctx_t *mctx,
66                       void (*sf_addr) (void *), void *sf_arg,
67                       void *sk_addr, int sk_size);
68
69 /* Return PC. */
70 extern unsigned long mctx_get_PC (mctx_t *mctx);
71
72 /* Return SP. */
73 extern unsigned long mctx_get_SP (mctx_t *mctx);
74
75 #endif /* PTHR_CONTEXT_H */