f22922f802eb5d983ab7ca08612ded8029db50f8
[openafs.git] / src / afs / OBSD / osi_sleep.c
1 /*
2  * $Id$
3  */
4
5 /*
6 copyright 2002
7 the regents of the university of michigan
8 all rights reserved
9
10 permission is granted to use, copy, create derivative works 
11 and redistribute this software and such derivative works 
12 for any purpose, so long as the name of the university of 
13 michigan is not used in any advertising or publicity 
14 pertaining to the use or distribution of this software 
15 without specific, written prior authorization.  if the 
16 above copyright notice or any other identification of the 
17 university of michigan is included in any copy of any 
18 portion of this software, then the disclaimer below must 
19 also be included.
20
21 this software is provided as is, without representation 
22 from the university of michigan as to its fitness for any 
23 purpose, and without warranty by the university of 
24 michigan of any kind, either express or implied, including 
25 without limitation the implied warranties of 
26 merchantability and fitness for a particular purpose. the 
27 regents of the university of michigan shall not be liable 
28 for any damages, including special, indirect, incidental, or 
29 consequential damages, with respect to any claim arising 
30 out of or in connection with the use of the software, even 
31 if it has been or is hereafter advised of the possibility of 
32 such damages.
33 */
34
35 /*
36  * Copyright 2000, International Business Machines Corporation and others.
37  * All Rights Reserved.
38  *
39  * This software has been released under the terms of the IBM Public
40  * License.  For details, see the LICENSE file in the top-level source
41  * directory or online at http://www.openafs.org/dl/license10.html
42  */
43
44 #include <afsconfig.h>
45 #include "afs/param.h"
46
47 RCSID("$Header$");
48
49 #include "afs/sysincludes.h"    /* Standard vendor system headers */
50 #include "afs/afsincludes.h"    /* Afs-based standard headers */
51 #include "afs/afs_stats.h"   /* afs statistics */
52
53 static char waitV;
54
55
56 void afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
57 {
58     AFS_STATCNT(osi_InitWaitHandle);
59     achandle->proc = NULL;
60 }
61
62 /* cancel osi_Wait */
63 void afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
64 {
65     caddr_t proc;
66
67     AFS_STATCNT(osi_CancelWait);
68     proc = achandle->proc;
69     if (proc == 0)
70         return;
71     achandle->proc = NULL;
72     wakeup(&waitV);
73 }
74
75 /* afs_osi_Wait
76  * Waits for data on ahandle, or ams ms later.  ahandle may be null.
77  * Returns 0 if timeout and EINTR if signalled.
78  */
79 int afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
80 {
81     int code = 0;
82     afs_int32 endTime;
83     int timo = (ams * afs_hz) / 1000 + 1;
84
85     AFS_STATCNT(osi_Wait);
86     endTime = osi_Time() + (ams / 1000);
87     if (ahandle)
88         ahandle->proc = (caddr_t) curproc;
89     AFS_ASSERT_GLOCK();
90     AFS_GUNLOCK();
91     do {
92         if (aintok) {
93             code = tsleep(&waitV, PCATCH | (PZERO+8), "afs_osi_Wait", timo);
94             if (code)   /* if interrupted, return EINTR */
95                 code = EINTR;
96         } else
97             tsleep(&waitV, (PZERO-3), "afs_osi_Wait", timo);
98
99         /* if we were cancelled, quit now */
100         if (ahandle && (ahandle->proc == NULL)) {
101             /* we've been signalled */
102             break;
103         }
104     } while (osi_Time() < endTime);
105     AFS_GLOCK();
106     return code;
107 }
108
109 void afs_osi_Sleep(void *event)
110 {
111     AFS_ASSERT_GLOCK();
112     AFS_GUNLOCK();
113     tsleep(event, PVFS, "afs", 0);
114     AFS_GLOCK();
115 }
116
117 int afs_osi_SleepSig(void *event)
118 {
119     AFS_ASSERT_GLOCK();
120     AFS_GUNLOCK();
121     tsleep(event, PVFS, "afs", 0);
122     AFS_GLOCK();
123     return 0;
124 }
125
126 int afs_osi_Wakeup(void *event)
127 {
128     wakeup(event);
129     return 1;
130 }