Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / portals / portals / 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         /* Set the unlinked_me interface number if there is one to pass
85          * back, since the NAL hasn't a clue what it is and therefore can't
86          * set it. */
87         if (!PtlHandleEqual (ev->unlinked_me, PTL_HANDLE_NONE))
88                 ev->unlinked_me.nal_idx = eventq.nal_idx;
89         
90         /* ensure event is delivered correctly despite possible 
91            races with lib_finalize */
92         if (eq->sequence != new_event->sequence) {
93                 CERROR("DROPPING EVENT: eq seq %lu ev seq %lu\n",
94                        eq->sequence, new_event->sequence);
95                 rc = PTL_EQ_DROPPED;
96         } else {
97                 rc = PTL_OK;
98         }
99
100         eq->sequence = new_event->sequence + 1;
101         nal->unlock(nal, &flags);
102         RETURN(rc);
103 }
104
105
106 int PtlEQWait(ptl_handle_eq_t eventq_in, ptl_event_t *event_out)
107 {
108         int rc;
109         
110         /* PtlEQGet does the handle checking */
111         while ((rc = PtlEQGet(eventq_in, event_out)) == PTL_EQ_EMPTY) {
112                 nal_t *nal = ptl_hndl2nal(&eventq_in);
113                 
114                 if (nal->yield)
115                         nal->yield(nal);
116         }
117
118         return rc;
119 }
120
121 #ifndef __KERNEL__
122 static jmp_buf eq_jumpbuf;
123
124 static void eq_timeout(int signal)
125 {
126         sigset_t set;
127
128         /* signal will be automatically disabled in sig handler,
129          * must enable it before long jump
130          */
131         sigemptyset(&set);
132         sigaddset(&set, SIGALRM);
133         sigprocmask(SIG_UNBLOCK, &set, NULL);
134
135         longjmp(eq_jumpbuf, -1);
136 }
137
138 int PtlEQWait_timeout(ptl_handle_eq_t eventq_in, ptl_event_t * event_out,
139                       int timeout)
140 {
141         static void (*prev) (int) = NULL;
142         static int left_over;
143         time_t time_at_start;
144         int rc;
145
146         if (setjmp(eq_jumpbuf)) {
147                 signal(SIGALRM, prev);
148                 alarm(left_over - timeout);
149                 return PTL_EQ_EMPTY;
150         }
151
152         left_over = alarm(timeout);
153         prev = signal(SIGALRM, eq_timeout);
154         time_at_start = time(NULL);
155         if (left_over && left_over < timeout)
156                 alarm(left_over);
157
158         rc = PtlEQWait(eventq_in, event_out);
159
160         signal(SIGALRM, prev);
161         alarm(left_over);       /* Should compute how long we waited */
162
163         return rc;
164 }
165
166 #endif
167