clean up headers, obsd socket.h needs types.h
[arla.git] / lwp / preempt.c
blob94aa8f7ee086f7f90911c617f6d5b57649542212
1 /*
2 ****************************************************************************
3 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
4 * *
5 * Permission to use, copy, modify, and distribute this software and its *
6 * documentation for any purpose and without fee is hereby granted, *
7 * provided that the above copyright notice appear in all copies and *
8 * that both that copyright notice and this permission notice appear in *
9 * supporting documentation, and that the name of IBM not be used in *
10 * advertising or publicity pertaining to distribution of the software *
11 * without specific, written prior permission. *
12 * *
13 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
15 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
16 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
17 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
18 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
19 ****************************************************************************
22 /*******************************************************************\
23 * *
24 * Information Technology Center *
25 * Carnegie-Mellon University *
26 * *
27 * Bradley White and M. Satyanarayanan *
28 \*******************************************************************/
30 #include <sys/time.h>
31 #include <signal.h>
32 #include <lwp.h>
33 #include "preempt.h"
35 RCSID("$Id$");
37 sig_atomic_t PRE_Block = 0; /* used in lwp.c and process.s */
39 #ifdef HAVE_GETITIMER
41 static RETSIGTYPE
42 #if defined(AFS_POSIX_SIGNALS)
43 AlarmHandler(int sig)
44 #else
45 AlarmHandler(int sig, int code, struct sigcontext *scp)
46 #endif
48 #ifdef AFS_POSIX_SIGNALS
49 sigset_t mask ;
50 #endif
52 if (PRE_Block == 0 && lwp_cpptr->level == 0) {
53 PRE_BeginCritical();
54 #if defined(AFS_POSIX_SIGNALS)
55 sigemptyset(&mask);
56 sigaddset(&mask, sig);
57 sigprocmask(SIG_UNBLOCK, &mask, (sigset_t *)0);
58 #else
59 sigsetmask(scp->sc_mask);
60 #endif
61 LWP_DispatchProcess();
62 PRE_EndCritical();
69 int
70 PRE_InitPreempt(struct timeval *slice)
72 struct itimerval itv;
73 #ifdef AFS_POSIX_SIGNALS
74 struct sigaction sa;
75 #else
76 struct sigvec vec;
77 #endif
79 if (lwp_cpptr == 0) return (LWP_EINIT);
81 if (slice == 0) {
82 itv.it_interval.tv_sec = itv.it_value.tv_sec = DEFAULTSLICE;
83 itv.it_interval.tv_usec = itv.it_value.tv_usec = 0;
84 } else {
85 itv.it_interval = itv.it_value = *slice;
88 #ifdef AFS_POSIX_SIGNALS
89 sa.sa_handler = AlarmHandler;
90 #ifndef SA_NODEFER
91 #define SA_NODEFER 0
92 #endif
93 #ifdef SA_SIGINFO
94 sa.sa_flags = SA_SIGINFO|SA_NODEFER;
95 #else
96 sa.sa_flags = SA_NODEFER;
97 #endif
99 if ((sigaction(SIGALRM, &sa, (struct sigaction *)0) == -1) ||
100 (setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1))
101 return(LWP_ESYSTEM);
102 #else
103 vec.sv_handler = AlarmHandler;
104 vec.sv_mask = vec.sv_onstack = 0;
106 if ((sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1) ||
107 (setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1))
108 return(LWP_ESYSTEM);
109 #endif
111 return(LWP_SUCCESS);
114 int
115 PRE_EndPreempt(void)
117 struct itimerval itv;
118 #ifdef AFS_POSIX_SIGNALS
119 struct sigaction sa;
120 #else
121 struct sigvec vec;
122 #endif
124 if (lwp_cpptr == 0)
125 return (LWP_EINIT);
127 itv.it_value.tv_sec = itv.it_value.tv_usec = 0;
129 #ifdef AFS_POSIX_SIGNALS
130 sa.sa_handler = SIG_DFL;
131 sa.sa_flags=0;
133 if ((setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1) ||
134 (sigaction(SIGALRM, &sa, (struct sigaction *)0) == -1))
135 return(LWP_ESYSTEM);
136 #else
137 vec.sv_handler = SIG_DFL;;
138 vec.sv_mask = vec.sv_onstack = 0;
140 if ((setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1) ||
141 (sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1))
142 return(LWP_ESYSTEM);
143 #endif
145 return(LWP_SUCCESS);
148 #else /* !HAVE_GETITIMER */
150 int
151 PRE_InitPreempt(struct timeval *slice)
153 return LWP_SUCCESS;
156 int
157 PRE_EndPreempt(void)
159 return LWP_SUCCESS;
162 #endif /* HAVE_GETITIMER */