libwp: Tidy header includes
[openafs.git] / src / lwp / preempt.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*******************************************************************\
11 *                                                                   *
12 *       Information Technology Center                               *
13 *       Carnegie-Mellon University                                  *
14 *                                                                   *
15 \*******************************************************************/
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 #include <roken.h>
20
21
22 #include "lwp.h"
23 #include "preempt.h"
24
25 #if defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26 int PRE_Block = 0;
27
28
29 int
30 PRE_InitPreempt(struct timeval *slice)
31 {
32     return LWP_SUCCESS;
33 }
34
35 int
36 PRE_EndPreempt(void)
37 {
38     return LWP_SUCCESS;
39 }
40
41 #else
42 #ifdef HAVE_UCONTEXT_H
43 #include <ucontext.h>
44 #endif
45
46 #if defined(AFS_OSF_ENV) || defined(AFS_S390_LINUX20_ENV)
47 int PRE_Block = 0;              /* used in lwp.c and process.s */
48 #else
49 char PRE_Block = 0;             /* used in lwp.c and process.s */
50 #endif
51
52 #if HAVE_SIGACTION && defined(SA_SIGINFO)
53 static void
54 AlarmHandler(int sig, siginfo_t *st, ucontext_t *scp)
55 #else
56 static void
57 AlarmHandler(int sig, int code, struct sigcontext *scp)
58 #endif
59 {
60     if (PRE_Block == 0 && lwp_cpptr->level == 0) {
61         PRE_BeginCritical();
62 #if HAVE_SIGACTION && defined(SA_SIGINFO)
63         sigprocmask(SIG_SETMASK, &scp->uc_sigmask, NULL);
64 #else
65         sigsetmask(scp->sc_mask);
66 #endif
67         LWP_DispatchProcess();
68         PRE_EndCritical();
69     }
70
71 }
72
73 int
74 PRE_InitPreempt(struct timeval *slice)
75 {
76     struct itimerval itv;
77 #if HAVE_SIGACTION && defined(SA_SIGINFO)
78     struct sigaction action;
79 #else
80     struct sigvec vec;
81 #endif
82
83     if (lwp_cpptr == 0)
84         return (LWP_EINIT);
85
86     if (slice == 0) {
87         itv.it_interval.tv_sec = itv.it_value.tv_sec = DEFAULTSLICE;
88         itv.it_interval.tv_usec = itv.it_value.tv_usec = 0;
89     } else {
90         itv.it_interval = itv.it_value = *slice;
91     }
92
93 #if HAVE_SIGACTION && defined(SA_SIGINFO)
94     memset(&action, 0, sizeof(action));
95     action.sa_sigaction = AlarmHandler;
96     action.sa_flags = SA_SIGINFO;
97
98     if ((sigaction(SIGALRM, &action, NULL) == -1)
99         || (setitimer(ITIMER_REAL, &itv, NULL) == -1))
100         return (LWP_ESYSTEM);
101 #else
102     memset(&vec, 0, sizeof(vec));
103     vec.sv_handler = AlarmHandler;
104     vec.sv_mask = vec.sv_onstack = 0;
105
106     if ((sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1)
107         || (setitimer(ITIMER_REAL, &itv, (struct itimerval *)0) == -1))
108         return (LWP_ESYSTEM);
109 #endif
110
111     return (LWP_SUCCESS);
112 }
113
114 int
115 PRE_EndPreempt()
116 {
117     struct itimerval itv;
118 #if HAVE_SIGACTION && defined(SA_SIGINFO)
119     struct sigaction action;
120 #else
121     struct sigvec vec;
122 #endif
123
124     if (lwp_cpptr == 0)
125         return (LWP_EINIT);
126
127     itv.it_value.tv_sec = itv.it_value.tv_usec = 0;
128
129 #if HAVE_SIGACTION && defined(SA_SIGINFO)
130     memset(&action, 0, sizeof(action));
131     action.sa_handler = SIG_DFL;
132
133     if ((setitimer(ITIMER_REAL, &itv, NULL) == -1)
134         || (sigaction(SIGALRM, &action, NULL) == -1))
135         return (LWP_ESYSTEM);
136
137 #else
138     memset(&vec, 0, sizeof(vec));
139     vec.sv_handler = SIG_DFL;
140     vec.sv_mask = vec.sv_onstack = 0;
141
142     if ((setitimer(ITIMER_REAL, &itv, (struct itimerval *)0) == -1)
143         || (sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1))
144         return (LWP_ESYSTEM);
145 #endif
146     return (LWP_SUCCESS);
147 }
148
149 #endif /* AFS_LINUX20_ENV */