pull-prototypes-to-head-20020821
[openafs.git] / src / afs / HPUX / osi_sleep.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 #include <afsconfig.h>
11 #include "../afs/param.h"
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h"   /* afs statistics */
18
19
20
21
22 static char waitV;
23
24 /* call procedure aproc with arock as an argument, in ams milliseconds */
25 static int afs_osi_CallProc(aproc, arock, ams)
26     register void (*aproc)();
27     register char *arock;
28     afs_int32 ams;
29 {
30     int code;
31
32     AFS_STATCNT(osi_CallProc);
33     AFS_GUNLOCK();
34     /* hz is in cycles/second, and timeout's 3rd parm is in cycles */
35     code = timeout(aproc, arock, (ams * afs_hz)/1000 + 1);
36     AFS_GLOCK();
37     return code;
38 }
39
40 /* cancel a timeout, whether or not it has already occurred */
41 static int afs_osi_CancelProc(aproc, arock)
42     register void (*aproc)();
43     register char *arock; 
44 {
45     int code = 0;
46     AFS_STATCNT(osi_CancelProc);
47
48     AFS_GUNLOCK();
49     code = untimeout(aproc, arock);
50     AFS_GLOCK();
51     return code;
52 }
53
54 static void AfsWaitHack()
55 {
56     AFS_STATCNT(WaitHack);
57     wakeup(&waitV);
58 }
59
60 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
61 {
62     AFS_STATCNT(osi_InitWaitHandle);
63     achandle->proc = (caddr_t) 0;
64 }
65
66 /* cancel osi_Wait */
67 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
68 {
69     caddr_t proc;
70
71     AFS_STATCNT(osi_CancelWait);
72     proc = achandle->proc;
73     if (proc == 0) return;
74     achandle->proc = (caddr_t) 0;   /* so dude can figure out he was signalled */
75     afs_osi_Wakeup(&waitV);
76 }
77
78 /* afs_osi_Wait
79  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
80  * Returns 0 if timeout and EINTR if signalled.
81  */
82 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
83 {
84     int code;
85     afs_int32 endTime, tid;
86
87     AFS_STATCNT(osi_Wait);
88     endTime = osi_Time() + (ams/1000);
89     if (ahandle)
90         ahandle->proc = (caddr_t) u.u_procp;
91     do {
92         AFS_ASSERT_GLOCK();
93         code = 0;
94         /* do not do anything for solaris, digital, AIX, and SGI MP */
95         afs_osi_CallProc(AfsWaitHack, (char *) u.u_procp, ams);
96         afs_osi_Sleep(&waitV); /* for HP 10.0 */
97
98         /* do not do anything for solaris, digital, and SGI MP */
99         afs_osi_CancelProc(AfsWaitHack,  (char *) u.u_procp); 
100         if (code) break;        /* if something happened, quit now */
101         /* if we we're cancelled, quit now */
102         if (ahandle && (ahandle->proc == (caddr_t) 0)) {
103             /* we've been signalled */
104             break;
105         }
106     } while (osi_Time() < endTime);
107     return code;
108 }
109
110 int afs_osi_SleepSig(void *event)
111 {
112     afs_osi_Sleep(event);
113     return 0;
114 }