1 | /*
|
---|
2 | * Copyright (c) 2010 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This file implements simple session support for the async framework.
|
---|
37 | *
|
---|
38 | * By the term 'session', we mean a logical data path between a client and a
|
---|
39 | * server over which the client can perform multiple concurrent transactions.
|
---|
40 | * Each transaction consists of one or more requests (IPC calls) which can
|
---|
41 | * be potentially blocking.
|
---|
42 | *
|
---|
43 | * Clients and servers are naturally connected using IPC phones, thus an IPC
|
---|
44 | * phone represents a session between a client and a server. In one
|
---|
45 | * session, there can be many outstanding transactions. In the current
|
---|
46 | * implementation each concurrent transaction takes place over a different
|
---|
47 | * connection (there can be at most one active transaction per connection).
|
---|
48 | *
|
---|
49 | * Sessions make it useful for a client or client API to support concurrent
|
---|
50 | * requests, independent of the actual implementation. Sessions provide
|
---|
51 | * an abstract interface to concurrent IPC communication. This is especially
|
---|
52 | * useful for client API stubs that aim to be reentrant (i.e. that allow
|
---|
53 | * themselves to be called from different fibrils and threads concurrently).
|
---|
54 | *
|
---|
55 | * There are several possible implementations of sessions. This implementation
|
---|
56 | * uses additional phones to represent sessions. Using phones both for the
|
---|
57 | * session and also for its transactions/connections has several advantages:
|
---|
58 | *
|
---|
59 | * - to make a series of transactions over a session, the client can continue to
|
---|
60 | * use the existing async framework APIs
|
---|
61 | * - the server supports sessions by the virtue of spawning a new connection
|
---|
62 | * fibril, just as it does for every new connection even without sessions
|
---|
63 | * - the implementation is pretty straightforward; a very naive implementation
|
---|
64 | * would be to make each transaction using a fresh phone (that is what we
|
---|
65 | * have done in the past); a slightly better approach would be to cache
|
---|
66 | * connections so that they can be reused by a later transaction within
|
---|
67 | * the same session (that is what this implementation does)
|
---|
68 | *
|
---|
69 | * The main disadvantages of using phones to represent sessions are:
|
---|
70 | *
|
---|
71 | * - if there are too many transactions (even cached ones), the task may hit its
|
---|
72 | * limit on the maximum number of connected phones, which could prevent the
|
---|
73 | * task from making new IPC connections to other tasks
|
---|
74 | * - if there are too many IPC connections already, it may be impossible to
|
---|
75 | * create a transaction by connecting a new phone thanks to the task's limit on
|
---|
76 | * the maximum number of connected phones
|
---|
77 | *
|
---|
78 | * These problems can be alleviated by increasing the limit on the maximum
|
---|
79 | * number of connected phones to some reasonable value and by limiting the number
|
---|
80 | * of cached connections to some fraction of this limit.
|
---|
81 | *
|
---|
82 | * The cache itself has a mechanism to close some number of unused phones if a
|
---|
83 | * new phone cannot be connected, but the outer world currently does not have a
|
---|
84 | * way to ask the phone cache to shrink.
|
---|
85 | *
|
---|
86 | * To minimize the confusion stemming from the fact that we use phones for two
|
---|
87 | * things (the session itself and also one for each data connection), this file
|
---|
88 | * makes the distinction by using the term 'session phone' for the former and
|
---|
89 | * 'data phone' for the latter. Under the hood, all phones remain equal,
|
---|
90 | * of course.
|
---|
91 | *
|
---|
92 | * There is a small inefficiency in that the cache repeatedly allocates and
|
---|
93 | * deallocates the conn_node_t structures when in fact it could keep the
|
---|
94 | * allocated structures around and reuse them later. But such a solution would
|
---|
95 | * be effectively implementing a poor man's slab allocator while it would be
|
---|
96 | * better to have the slab allocator ported to uspace so that everyone could
|
---|
97 | * benefit from it.
|
---|
98 | */
|
---|
99 |
|
---|
100 | #include <async_sess.h>
|
---|
101 | #include <ipc/ipc.h>
|
---|
102 | #include <fibril_synch.h>
|
---|
103 | #include <adt/list.h>
|
---|
104 | #include <adt/hash_table.h>
|
---|
105 | #include <malloc.h>
|
---|
106 | #include <errno.h>
|
---|
107 | #include <assert.h>
|
---|
108 |
|
---|
109 | typedef struct {
|
---|
110 | link_t conn_link; /**< Link for the list of connections. */
|
---|
111 | link_t global_link; /**< Link for the global list of phones. */
|
---|
112 | int data_phone; /**< Connected data phone. */
|
---|
113 | } conn_node_t;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Mutex protecting the inactive_conn_head list and the session_hash hash table.
|
---|
117 | */
|
---|
118 | static fibril_mutex_t async_sess_mutex;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * List of all currently inactive connections.
|
---|
122 | */
|
---|
123 | static LIST_INITIALIZE(inactive_conn_head);
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * List of all existing sessions.
|
---|
127 | */
|
---|
128 | //static LIST_INITIALIZE(session_list);
|
---|
129 |
|
---|
130 | /** Initialize the async_sess subsystem.
|
---|
131 | *
|
---|
132 | * Needs to be called prior to any other interface in this file.
|
---|
133 | */
|
---|
134 | void _async_sess_init(void)
|
---|
135 | {
|
---|
136 | fibril_mutex_initialize(&async_sess_mutex);
|
---|
137 | list_initialize(&inactive_conn_head);
|
---|
138 | }
|
---|
139 |
|
---|
140 | void async_session_create(async_sess_t *sess, int phone)
|
---|
141 | {
|
---|
142 | sess->sess_phone = phone;
|
---|
143 | list_initialize(&sess->conn_head);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void async_session_destroy(async_sess_t *sess)
|
---|
147 | {
|
---|
148 | sess->sess_phone = -1;
|
---|
149 | /* todo */
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void conn_node_initialize(conn_node_t *conn)
|
---|
153 | {
|
---|
154 | link_initialize(&conn->conn_link);
|
---|
155 | link_initialize(&conn->global_link);
|
---|
156 | conn->data_phone = -1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Start new transaction in a session.
|
---|
160 | *
|
---|
161 | * @param sess_phone Session.
|
---|
162 | * @return Phone representing the new transaction or a negative error
|
---|
163 | * code.
|
---|
164 | */
|
---|
165 | int async_transaction_begin(async_sess_t *sess)
|
---|
166 | {
|
---|
167 | conn_node_t *conn;
|
---|
168 | int data_phone;
|
---|
169 |
|
---|
170 | fibril_mutex_lock(&async_sess_mutex);
|
---|
171 |
|
---|
172 | if (!list_empty(&sess->conn_head)) {
|
---|
173 | /*
|
---|
174 | * There are inactive connections in the session.
|
---|
175 | */
|
---|
176 | conn = list_get_instance(sess->conn_head.next, conn_node_t,
|
---|
177 | conn_link);
|
---|
178 | list_remove(&conn->conn_link);
|
---|
179 | list_remove(&conn->global_link);
|
---|
180 |
|
---|
181 | data_phone = conn->data_phone;
|
---|
182 | free(conn);
|
---|
183 | } else {
|
---|
184 | /*
|
---|
185 | * There are no available connections in the session.
|
---|
186 | * Make a one-time attempt to connect a new data phone.
|
---|
187 | */
|
---|
188 | retry:
|
---|
189 | data_phone = async_connect_me_to(sess->sess_phone, 0, 0, 0);
|
---|
190 | if (data_phone >= 0) {
|
---|
191 | /* success, do nothing */
|
---|
192 | } else if (!list_empty(&inactive_conn_head)) {
|
---|
193 | /*
|
---|
194 | * We did not manage to connect a new phone. But we can
|
---|
195 | * try to close some of the currently inactive
|
---|
196 | * connections in other sessions and try again.
|
---|
197 | */
|
---|
198 | conn = list_get_instance(inactive_conn_head.next,
|
---|
199 | conn_node_t, global_link);
|
---|
200 | list_remove(&conn->global_link);
|
---|
201 | list_remove(&conn->conn_link);
|
---|
202 | data_phone = conn->data_phone;
|
---|
203 | free(conn);
|
---|
204 | ipc_hangup(data_phone);
|
---|
205 | goto retry;
|
---|
206 | } else {
|
---|
207 | /*
|
---|
208 | * This is unfortunate. We failed both to find a cached
|
---|
209 | * connection or to create a new one even after cleaning up
|
---|
210 | * the cache. This is most likely due to too many
|
---|
211 | * open sessions (connected session phones).
|
---|
212 | */
|
---|
213 | data_phone = ELIMIT;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
218 | return data_phone;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Finish a transaction.
|
---|
222 | *
|
---|
223 | * @param sess Session.
|
---|
224 | * @param data_phone Phone representing the transaction within the session.
|
---|
225 | */
|
---|
226 | void async_transaction_end(async_sess_t *sess, int data_phone)
|
---|
227 | {
|
---|
228 | conn_node_t *conn;
|
---|
229 |
|
---|
230 | fibril_mutex_lock(&async_sess_mutex);
|
---|
231 | conn = (conn_node_t *) malloc(sizeof(conn_node_t));
|
---|
232 | if (!conn) {
|
---|
233 | /*
|
---|
234 | * Being unable to remember the connected data phone here
|
---|
235 | * means that we simply hang up.
|
---|
236 | */
|
---|
237 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
238 | ipc_hangup(data_phone);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | conn_node_initialize(conn);
|
---|
243 | conn->data_phone = data_phone;
|
---|
244 | list_append(&conn->conn_link, &sess->conn_head);
|
---|
245 | list_append(&conn->global_link, &inactive_conn_head);
|
---|
246 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** @}
|
---|
250 | */
|
---|