Whamcloud - gitweb
964b9d89aeda559e4cda09876000a85cb9c93e31
[fs/lustre-release.git] / lnet / lnet / api-eq.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * api/api-eq.c
5  * User-level event queue management routines
6  *
7  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
8  *  Copyright (c) 2001-2002 Sandia National Laboratories
9  *
10  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <portals/api-support.h>
27
28 int ptl_eq_init(void)
29 {
30         /* Nothing to do anymore... */
31         return PTL_OK;
32 }
33
34 void ptl_eq_fini(void)
35 {
36         /* Nothing to do anymore... */
37 }
38
39 int ptl_eq_ni_init(nal_t * nal)
40 {
41         /* Nothing to do anymore... */
42         return PTL_OK;
43 }
44
45 void ptl_eq_ni_fini(nal_t * nal)
46 {
47         /* Nothing to do anymore... */
48 }
49
50 int PtlEQGet(ptl_handle_eq_t eventq, ptl_event_t * ev)
51 {
52         ptl_eq_t *eq;
53         int rc, new_index;
54         unsigned long flags;
55         ptl_event_t *new_event;
56         nal_t *nal;
57         ENTRY;
58
59         if (!ptl_init)
60                 RETURN(PTL_NOINIT);
61
62         nal = ptl_hndl2nal(&eventq);
63         if (!nal)
64                 RETURN(PTL_INV_EQ);
65
66         eq = ptl_handle2usereq(&eventq);
67         nal->lock(nal, &flags);
68
69         /* size must be a power of 2 to handle a wrapped sequence # */
70         LASSERT (eq->size != 0 &&
71                  eq->size == LOWEST_BIT_SET (eq->size));
72
73         new_index = eq->sequence & (eq->size - 1);
74         new_event = &eq->base[new_index];
75         CDEBUG(D_INFO, "new_event: %p, sequence: %lu, eq->size: %u\n",
76                new_event, eq->sequence, eq->size);
77         if (PTL_SEQ_GT (eq->sequence, new_event->sequence)) {
78                 nal->unlock(nal, &flags);
79                 RETURN(PTL_EQ_EMPTY);
80         }
81
82         *ev = *new_event;
83
84         /* ensure event is delivered correctly despite possible 
85            races with lib_finalize */
86         if (eq->sequence != new_event->sequence) {
87                 CERROR("DROPPING EVENT: eq seq %lu ev seq %lu\n",
88                        eq->sequence, new_event->sequence);
89                 rc = PTL_EQ_DROPPED;
90         } else {
91                 rc = PTL_OK;
92         }
93
94         eq->sequence = new_event->sequence + 1;
95         nal->unlock(nal, &flags);
96         RETURN(rc);
97 }
98
99
100 int PtlEQWait(ptl_handle_eq_t eventq_in, ptl_event_t *event_out)
101 {
102         int rc;
103         
104         /* PtlEQGet does the handle checking */
105         while ((rc = PtlEQGet(eventq_in, event_out)) == PTL_EQ_EMPTY) {
106                 nal_t *nal = ptl_hndl2nal(&eventq_in);
107                 
108                 if (nal->yield)
109                         nal->yield(nal);
110         }
111
112         return rc;
113 }
114
115 #ifndef __KERNEL__
116 #if 0
117 static jmp_buf eq_jumpbuf;
118
119 static void eq_timeout(int signal)
120 {
121         sigset_t set;
122
123         /* signal will be automatically disabled in sig handler,
124          * must enable it before long jump
125          */
126         sigemptyset(&set);
127         sigaddset(&set, SIGALRM);
128         sigprocmask(SIG_UNBLOCK, &set, NULL);
129
130         longjmp(eq_jumpbuf, -1);
131 }
132
133 int PtlEQWait_timeout(ptl_handle_eq_t eventq_in, ptl_event_t * event_out,
134                       int timeout)
135 {
136         static void (*prev) (int) = NULL;
137         static int left_over;
138         time_t time_at_start;
139         int rc;
140
141         if (setjmp(eq_jumpbuf)) {
142                 signal(SIGALRM, prev);
143                 alarm(left_over - timeout);
144                 return PTL_EQ_EMPTY;
145         }
146
147         left_over = alarm(timeout);
148         prev = signal(SIGALRM, eq_timeout);
149         time_at_start = time(NULL);
150         if (left_over && left_over < timeout)
151                 alarm(left_over);
152
153         rc = PtlEQWait(eventq_in, event_out);
154
155         signal(SIGALRM, prev);
156         alarm(left_over);       /* Should compute how long we waited */
157
158         return rc;
159 }
160 #else
161 #include <errno.h>
162
163 /* FIXME
164  * Here timeout need a trick with tcpnal, definitely unclean but OK for
165  * this moment.
166  */
167
168 /* global variables defined by tcpnal */
169 extern int __tcpnal_eqwait_timeout_value;
170 extern int __tcpnal_eqwait_timedout;
171
172 int PtlEQWait_timeout(ptl_handle_eq_t eventq_in, ptl_event_t * event_out,
173                       int timeout)
174 {
175         int rc;
176
177         if (!timeout)
178                 return PtlEQWait(eventq_in, event_out);
179
180         __tcpnal_eqwait_timeout_value = timeout;
181
182         while ((rc = PtlEQGet(eventq_in, event_out)) == PTL_EQ_EMPTY) {
183                 nal_t *nal = ptl_hndl2nal(&eventq_in);
184                 
185                 if (nal->yield)
186                         nal->yield(nal);
187
188                 if (__tcpnal_eqwait_timedout) {
189                         if (__tcpnal_eqwait_timedout != ETIMEDOUT)
190                                 printf("Warning: yield return error %d\n",
191                                         __tcpnal_eqwait_timedout);
192                         rc = PTL_EQ_EMPTY;
193                         break;
194                 }
195         }
196
197         __tcpnal_eqwait_timeout_value = 0;
198
199         return rc;
200 }
201 #endif
202 #endif /* __KERNEL__ */