openbsd-20021028
[openafs.git] / src / afs / OBSD / 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
11 #include <afsconfig.h>
12 #include "afs/param.h"
13
14 RCSID("$Header$");
15
16 #include "afs/sysincludes.h"    /* Standard vendor system headers */
17 #include "afs/afsincludes.h"    /* Afs-based standard headers */
18 #include "afs/afs_stats.h"   /* afs statistics */
19
20 static char waitV;
21
22
23 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
24 {
25     AFS_STATCNT(osi_InitWaitHandle);
26     achandle->proc = NULL;
27 }
28
29 /* cancel osi_Wait */
30 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
31 {
32     caddr_t proc;
33
34     AFS_STATCNT(osi_CancelWait);
35     proc = achandle->proc;
36     if (proc == 0)
37         return;
38     achandle->proc = NULL;
39     wakeup(&waitV);
40 }
41
42 /* afs_osi_Wait
43  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
44  * Returns 0 if timeout and EINTR if signalled.
45  */
46 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
47 {
48     int code = 0;
49     afs_int32 endTime;
50     int timo = (ams * afs_hz) / 1000 + 1;
51
52     AFS_STATCNT(osi_Wait);
53     endTime = osi_Time() + (ams / 1000);
54     if (ahandle)
55         ahandle->proc = (caddr_t) curproc;
56     do {
57         if (aintok) {
58             code = tsleep(&waitV, PCATCH | (PZERO+8), "afs_osi_Wait", timo);
59             if (code)   /* if interrupted, return EINTR */
60                 code = EINTR;
61         } else
62             tsleep(&waitV, (PZERO-3), "afs_osi_Wait", timo);
63
64         /* if we were cancelled, quit now */
65         if (ahandle && (ahandle->proc == NULL)) {
66             /* we've been signalled */
67             break;
68         }
69     } while (osi_Time() < endTime);
70     return code;
71 }
72
73 int afs_osi_SleepSig(void *event)
74 {
75     afs_osi_Sleep(event);
76     return 0;
77 }