no-copy-libafs-builds-20021015
[openafs.git] / src / rx / rx_multi.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #ifdef  KERNEL
16 #include "rx/rx_kernel.h"
17 #include "rx/rx_multi.h"
18 #else /* KERNEL */
19 # include "rx_user.h"
20 # include "rx_multi.h"
21 #endif /* KERNEL */
22
23 /* multi.c and multi.h, together with some rxgen hooks, provide a way of making multiple, but similar, rx calls to multiple hosts simultaneously */
24
25 struct multi_handle *multi_Init(struct rx_connection **conns, 
26         register int nConns)
27 {
28     register struct rx_call **calls;
29     register short *ready;
30     register struct multi_handle *mh;
31     register int i;
32     /* Note:  all structures that are possibly referenced by other processes must be allocated.  In some kernels variables allocated on a process stack will not be accessible to other processes */
33     calls = (struct rx_call **)osi_Alloc(sizeof (struct rx_call *) * nConns);
34     ready = (short *) osi_Alloc(sizeof(short *) * nConns);
35     mh = (struct multi_handle *) osi_Alloc(sizeof (struct multi_handle));
36     if (!calls || !ready || !mh) osi_Panic("multi_Rx: no mem\n");
37     mh->calls = calls;
38     mh->nextReady = mh->firstNotReady = mh->ready = ready;
39     mh->nReady = 0;
40     mh->nConns = nConns;
41 #ifdef RX_ENABLE_LOCKS
42     MUTEX_INIT(&mh->lock, "rx_multi_lock", MUTEX_DEFAULT, 0);
43     CV_INIT(&mh->cv, "rx_multi_cv", CV_DEFAULT, 0);
44 #endif /* RX_ENABLE_LOCKS */
45     for (i=0; i<nConns; i++) {
46         register struct rx_call *call;
47         call = mh->calls[i] = rx_NewCall(conns[i]);
48         rx_SetArrivalProc(call, multi_Ready, (VOID *)mh, (VOID *)i);
49     }
50     return mh;
51 }
52
53 /* Return the user's connection index of the most recently ready call; that is, a call that has received at least one reply packet */
54 int multi_Select(register struct multi_handle *mh)
55 {
56     int index;
57     SPLVAR;
58     NETPRI;
59 #ifdef RX_ENABLE_LOCKS
60     MUTEX_ENTER(&mh->lock);
61 #endif /* RX_ENABLE_LOCKS */
62     while (mh->nextReady == mh->firstNotReady) {
63         if (mh->nReady == mh->nConns) {
64 #ifdef RX_ENABLE_LOCKS
65             MUTEX_EXIT(&mh->lock);
66 #endif /* RX_ENABLE_LOCKS */
67             USERPRI;
68             return -1;
69         }
70 #ifdef RX_ENABLE_LOCKS
71         CV_WAIT(&mh->cv, &mh->lock);
72 #else /* RX_ENABLE_LOCKS */
73         osi_rxSleep(mh);
74 #endif /* RX_ENABLE_LOCKS */
75     }
76     index = *(mh->nextReady);
77     (mh->nextReady) += 1;
78 #ifdef RX_ENABLE_LOCKS
79     MUTEX_EXIT(&mh->lock);
80 #endif /* RX_ENABLE_LOCKS */
81     USERPRI;
82     return index;
83 }
84
85 /* Called by Rx when the first reply packet of a call is received, or the call is aborted. */
86 void multi_Ready(register struct rx_call *call, 
87         register struct multi_handle *mh, register int index)
88 {
89 #ifdef RX_ENABLE_LOCKS
90     MUTEX_ENTER(&mh->lock);
91 #endif /* RX_ENABLE_LOCKS */
92     *mh->firstNotReady++ = index;
93     mh->nReady++;
94 #ifdef RX_ENABLE_LOCKS
95     CV_SIGNAL(&mh->cv);
96     MUTEX_EXIT(&mh->lock);
97 #else /* RX_ENABLE_LOCKS */
98     osi_rxWakeup(mh);
99 #endif /* RX_ENABLE_LOCKS */
100 }
101
102 /* Called when the multi rx call is over, or when the user aborts it (by using the macro multi_Abort) */
103 void multi_Finalize(register struct multi_handle *mh)
104 {
105     register int i;
106     register int nCalls = mh->nConns;
107     for (i=0; i<nCalls; i++) {
108         register struct rx_call *call = mh->calls[i];
109         if (call) rx_EndCall(call, RX_USER_ABORT);
110     }
111 #ifdef RX_ENABLE_LOCKS
112     MUTEX_DESTROY(&mh->lock);
113     CV_DESTROY(&mh->cv);
114 #endif /* RX_ENABLE_LOCKS */
115     osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
116     osi_Free(mh->ready, sizeof(short *) * nCalls);
117     osi_Free(mh, sizeof(struct multi_handle));
118 }
119
120 /* ignores all remaining multiRx calls */
121 void multi_Finalize_Ignore(register struct multi_handle *mh)
122 {
123     register int i;
124     register int nCalls = mh->nConns;
125     for (i=0; i<nCalls; i++) {
126         register struct rx_call *call = mh->calls[i];
127         if (call) rx_EndCall(call, 0);
128     }
129 #ifdef RX_ENABLE_LOCKS
130     MUTEX_DESTROY(&mh->lock);
131     CV_DESTROY(&mh->cv);
132 #endif /* RX_ENABLE_LOCKS */
133     osi_Free(mh->calls, sizeof (struct rx_call *) * nCalls);
134     osi_Free(mh->ready, sizeof(short *) * nCalls);
135     osi_Free(mh, sizeof(struct multi_handle));
136 }