50b63f00544031c696cb1df6b350ff43d86b6446
[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 RCSID("$Header$");
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) || defined(AFS_DJGPP_ENV)
26 int PRE_Block = 0;
27
28
29 int PRE_InitPreempt(slice)
30     struct timeval *slice;
31 {
32     return LWP_SUCCESS;
33 }
34
35 int PRE_EndPreempt()
36 {
37     return LWP_SUCCESS;
38 }
39
40 #else
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <ucontext.h>
44
45 #if defined(AFS_OSF_ENV) || defined(AFS_S390_LINUX20_ENV)
46 int PRE_Block = 0;              /* used in lwp.c and process.s */
47 #else
48 char PRE_Block = 0;             /* used in lwp.c and process.s */
49 #endif
50
51 static void AlarmHandler(sig, st, scp)
52     int sig;
53     siginfo_t *st;
54     ucontext_t *scp;
55     {
56     if (PRE_Block == 0 && lwp_cpptr->level == 0)
57         {
58         PRE_BeginCritical();
59         sigprocmask(SIG_SETMASK, &scp->uc_sigmask, NULL);
60         LWP_DispatchProcess();
61         PRE_EndCritical();
62         }
63     
64     }
65
66 int PRE_InitPreempt(slice)
67     struct timeval *slice;
68     {
69     struct itimerval itv;
70     struct sigaction action;
71
72     if (lwp_cpptr == 0) return (LWP_EINIT);
73     
74     if (slice == 0)
75         {
76         itv.it_interval.tv_sec = itv.it_value.tv_sec = DEFAULTSLICE;
77         itv.it_interval.tv_usec = itv.it_value.tv_usec = 0;
78         }
79     else
80         {
81         itv.it_interval = itv.it_value = *slice;
82         }
83
84     memset((char *)&action, 0, sizeof(action));
85     action.sa_sigaction = AlarmHandler;
86     action.sa_flags = SA_SIGINFO;
87
88     if ((sigaction(SIGALRM, &action, NULL) == -1) ||
89         (setitimer(ITIMER_REAL, &itv, NULL) == -1))
90         return(LWP_ESYSTEM);
91
92     return(LWP_SUCCESS);
93     }
94
95 int PRE_EndPreempt()
96     {
97     struct itimerval itv;
98     struct sigaction action;
99
100     if (lwp_cpptr == 0) return (LWP_EINIT);
101     
102     itv.it_value.tv_sec = itv.it_value.tv_usec = 0;
103
104     memset((char *)&action, 0, sizeof(action));
105     action.sa_handler = SIG_DFL;
106
107     if ((setitimer(ITIMER_REAL, &itv, NULL) == -1) ||
108         (sigaction(SIGALRM, &action, NULL) == -1))
109         return(LWP_ESYSTEM);
110
111     return(LWP_SUCCESS);
112     }
113
114 #endif /* AFS_LINUX20_ENV */