openbsd-20031007
[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
48     ("$Header$");
49
50 #include "afs/sysincludes.h"    /* Standard vendor system headers */
51 #include "afs/afsincludes.h"    /* Afs-based standard headers */
52 #include "afs/afs_stats.h"      /* afs statistics */
53
54 static char waitV;
55
56
57 void
58 afs_osi_InitWaitHandle(struct afs_osi_WaitHandle *achandle)
59 {
60     AFS_STATCNT(osi_InitWaitHandle);
61     achandle->proc = NULL;
62 }
63
64 /* cancel osi_Wait */
65 void
66 afs_osi_CancelWait(struct afs_osi_WaitHandle *achandle)
67 {
68     caddr_t proc;
69
70     AFS_STATCNT(osi_CancelWait);
71     proc = achandle->proc;
72     if (proc == NULL)
73         return;
74     achandle->proc = NULL;
75     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
83 afs_osi_Wait(afs_int32 ams, struct afs_osi_WaitHandle *ahandle, int aintok)
84 {
85     int timo, code = 0;
86     struct timeval atv, endTime;
87
88     AFS_STATCNT(osi_Wait);
89
90     atv.tv_sec = ams / 1000;
91     atv.tv_usec = (ams % 1000) * 1000;
92     timeradd(&atv, &time, &endTime);
93
94     if (ahandle)
95         ahandle->proc = (caddr_t) curproc;
96     AFS_ASSERT_GLOCK();
97     AFS_GUNLOCK();
98
99     do {
100         timersub(&endTime, &time, &atv);
101         timo = atv.tv_sec * hz + atv.tv_usec * hz / 1000000 + 1;
102         if (aintok) {
103             code = tsleep(&waitV, PCATCH | PVFS, "afs_W1", timo);
104             if (code)
105                 code = (code == EWOULDBLOCK) ? 0 : EINTR;
106         } else
107             tsleep(&waitV, PVFS, "afs_W2", timo);
108
109         /* if we were cancelled, quit now */
110         if (ahandle && (ahandle->proc == NULL)) {
111             /* we've been signalled */
112             break;
113         }
114     } while (timercmp(&time, &endTime, <));
115
116     AFS_GLOCK();
117     return code;
118 }
119
120 void
121 afs_osi_Sleep(void *event)
122 {
123     AFS_ASSERT_GLOCK();
124     AFS_GUNLOCK();
125     tsleep(event, PVFS, "afs", 0);
126     AFS_GLOCK();
127 }
128
129 int
130 afs_osi_SleepSig(void *event)
131 {
132     AFS_ASSERT_GLOCK();
133     AFS_GUNLOCK();
134     tsleep(event, PVFS, "afs", 0);
135     AFS_GLOCK();
136     return 0;
137 }
138
139 int
140 afs_osi_Wakeup(void *event)
141 {
142     wakeup(event);
143     return 1;
144 }