Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / lwp / preempt.c
1
2 #ifndef lint
3 #endif
4
5 /*
6 ****************************************************************************
7 *        Copyright IBM Corporation 1988, 1989 - All Rights Reserved        *
8 *                                                                          *
9 * Permission to use, copy, modify, and distribute this software and its    *
10 * documentation for any purpose and without fee is hereby granted,         *
11 * provided that the above copyright notice appear in all copies and        *
12 * that both that copyright notice and this permission notice appear in     *
13 * supporting documentation, and that the name of IBM not be used in        *
14 * advertising or publicity pertaining to distribution of the software      *
15 * without specific, written prior permission.                              *
16 *                                                                          *
17 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
19 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY      *
20 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER  *
21 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING   *
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.    *
23 ****************************************************************************
24 */
25
26 /*******************************************************************\
27 *                                                                   *
28 *       Information Technology Center                               *
29 *       Carnegie-Mellon University                                  *
30 *                                                                   *
31 \*******************************************************************/
32 #include <afs/param.h>
33
34 #ifdef AFS_LINUX20_ENV
35 int PRE_Block = 0;
36 #else
37 #include <sys/time.h>
38 #include <signal.h>
39 #include <ucontext.h>
40 #include "lwp.h"
41 #include "preempt.h"
42
43 #ifdef  AFS_OSF_ENV
44 int PRE_Block = 0;              /* used in lwp.c and process.s */
45 #else
46 char PRE_Block = 0;             /* used in lwp.c and process.s */
47 #endif
48
49 static void AlarmHandler(sig, st, scp)
50     int sig;
51     siginfo_t *st;
52     ucontext_t *scp;
53     {
54     if (PRE_Block == 0 && lwp_cpptr->level == 0)
55         {
56         PRE_BeginCritical();
57         sigprocmask(SIG_SETMASK, &scp->uc_sigmask, NULL);
58         LWP_DispatchProcess();
59         PRE_EndCritical();
60         }
61     
62     }
63
64 int PRE_InitPreempt(slice)
65     struct timeval *slice;
66     {
67     struct itimerval itv;
68     struct sigaction action;
69
70     if (lwp_cpptr == 0) return (LWP_EINIT);
71     
72     if (slice == 0)
73         {
74         itv.it_interval.tv_sec = itv.it_value.tv_sec = DEFAULTSLICE;
75         itv.it_interval.tv_usec = itv.it_value.tv_usec = 0;
76         }
77     else
78         {
79         itv.it_interval = itv.it_value = *slice;
80         }
81
82     bzero((char *)&action, sizeof(action));
83     action.sa_sigaction = AlarmHandler;
84     action.sa_flags = SA_SIGINFO;
85
86     if ((sigaction(SIGALRM, &action, (struct sigaction *)0) == -1) ||
87         (setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1))
88         return(LWP_ESYSTEM);
89
90     return(LWP_SUCCESS);
91     }
92
93 int PRE_EndPreempt()
94     {
95     struct itimerval itv;
96     struct sigaction action;
97
98     if (lwp_cpptr == 0) return (LWP_EINIT);
99     
100     itv.it_value.tv_sec = itv.it_value.tv_usec = 0;
101
102     bzero((char *)&action, sizeof(action));
103     action.sa_handler = SIG_DFL;
104
105     if ((setitimer(ITIMER_REAL, &itv, (struct itimerval *) 0) == -1) ||
106         (sigaction(SIGALRM, &action, (struct sigaction *)0) == -1))
107         return(LWP_ESYSTEM);
108
109     return(LWP_SUCCESS);
110     }
111
112 #endif /* AFS_I386_LINUX20_ENV */