Changeset 0edaf0f6 in mainline for uspace/srv/net/tl/tcp/conn.c
- Timestamp:
- 2011-12-16T18:04:30Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ab9378b4
- Parents:
- 7a8c1c4e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/conn.c
r7a8c1c4e r0edaf0f6 56 56 57 57 LIST_INITIALIZE(conn_list); 58 FIBRIL_MUTEX_INITIALIZE(conn_list_lock); 58 59 59 60 static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg); … … 81 82 goto error; 82 83 84 fibril_mutex_initialize(&conn->lock); 85 86 /* One for the user, one for not being in closed state */ 87 atomic_set(&conn->refcnt, 2); 88 83 89 /* Allocate receive buffer */ 84 fibril_mutex_initialize(&conn->rcv_buf_lock);85 90 fibril_condvar_initialize(&conn->rcv_buf_cv); 86 91 conn->rcv_buf_size = RCV_BUF_SIZE; … … 93 98 94 99 /** Allocate send buffer */ 95 fibril_mutex_initialize(&conn->snd_buf_lock);96 100 fibril_condvar_initialize(&conn->snd_buf_cv); 97 101 conn->snd_buf_size = SND_BUF_SIZE; … … 115 119 116 120 /* Connection state change signalling */ 117 fibril_mutex_initialize(&conn->cstate_lock);118 121 fibril_condvar_initialize(&conn->cstate_cv); 119 122 … … 146 149 /** Destroy connection structure. 147 150 * 148 * Connection structure should be destroyed when both of two conditions are 149 * met: (1) user has deleted the connection and (2) the connection has entered 150 * closed state. 151 * Connection structure should be destroyed when the folowing condtitions 152 * are met: 153 * (1) user has deleted the connection 154 * (2) the connection has entered closed state 155 * (3) nobody is holding references to the connection 156 * 157 * This happens when @a conn->refcnt is zero as we count (1) and (2) 158 * as special references. 151 159 * 152 160 * @param conn Connection … … 166 174 } 167 175 176 /** Add reference to connection. 177 * 178 * Increase connection reference count by one. 179 * 180 * @param conn Connection 181 */ 182 void tcp_conn_addref(tcp_conn_t *conn) 183 { 184 log_msg(LVL_DEBUG, "%s: tcp_conn_addref(%p)", conn->name, conn); 185 atomic_inc(&conn->refcnt); 186 } 187 188 /** Remove reference from connection. 189 * 190 * Decrease connection reference count by one. 191 * 192 * @param conn Connection 193 */ 194 void tcp_conn_delref(tcp_conn_t *conn) 195 { 196 log_msg(LVL_DEBUG, "%s: tcp_conn_delref(%p)", conn->name, conn); 197 198 if (atomic_predec(&conn->refcnt) == 0) 199 tcp_conn_free(conn); 200 } 201 168 202 /** Delete connection. 169 203 * … … 175 209 void tcp_conn_delete(tcp_conn_t *conn) 176 210 { 177 fibril_mutex_lock(&conn->cstate_lock); 178 conn->deleted = true; 179 180 if (conn->cstate == st_closed) { 181 fibril_mutex_unlock(&conn->cstate_lock); 182 tcp_conn_free(conn); 183 } else { 184 fibril_mutex_unlock(&conn->cstate_lock); 185 } 211 log_msg(LVL_DEBUG, "%s: tcp_conn_delete(%p)", conn->name, conn); 212 213 assert(conn->deleted == false); 214 tcp_conn_delref(conn); 186 215 } 187 216 … … 192 221 void tcp_conn_add(tcp_conn_t *conn) 193 222 { 223 fibril_mutex_lock(&conn_list_lock); 194 224 list_append(&conn->link, &conn_list); 225 fibril_mutex_unlock(&conn_list_lock); 195 226 } 196 227 … … 201 232 void tcp_conn_remove(tcp_conn_t *conn) 202 233 { 234 fibril_mutex_lock(&conn_list_lock); 203 235 list_remove(&conn->link); 236 fibril_mutex_unlock(&conn_list_lock); 204 237 } 205 238 206 239 static void tcp_conn_state_set(tcp_conn_t *conn, tcp_cstate_t nstate) 207 240 { 208 fibril_mutex_lock(&conn->cstate_lock); 241 tcp_cstate_t old_state; 242 243 old_state = conn->cstate; 209 244 conn->cstate = nstate; 210 245 fibril_condvar_broadcast(&conn->cstate_cv); 211 246 212 if (nstate == st_closed && conn->deleted) { 213 fibril_mutex_unlock(&conn->cstate_lock); 214 tcp_conn_free(conn); 215 } else { 216 fibril_mutex_unlock(&conn->cstate_lock); 247 assert(old_state != st_closed); 248 if (nstate == st_closed) { 249 /* Drop one reference for now being in closed state */ 250 tcp_conn_delref(conn); 217 251 } 218 252 } … … 302 336 * A connection is uniquely identified by a socket pair. Look up our 303 337 * connection map and return connection structure based on socket pair. 338 * The connection reference count is bumped by one. 304 339 * 305 340 * @param sp Socket pair 306 341 * @return Connection structure or NULL if not found. 307 342 */ 308 tcp_conn_t *tcp_conn_find (tcp_sockpair_t *sp)343 tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp) 309 344 { 310 345 log_msg(LVL_DEBUG, "tcp_conn_find(%p)", sp); 346 347 fibril_mutex_lock(&conn_list_lock); 311 348 312 349 list_foreach(conn_list, link) { … … 317 354 csp->local.addr.ipv4, csp->local.port); 318 355 if (tcp_sockpair_match(sp, csp)) { 356 tcp_conn_addref(conn); 357 fibril_mutex_unlock(&conn_list_lock); 319 358 return conn; 320 359 } 321 360 } 322 361 362 fibril_mutex_unlock(&conn_list_lock); 323 363 return NULL; 324 364 } … … 910 950 tcp_conn_trim_seg_to_wnd(conn, seg); 911 951 912 fibril_mutex_lock(&conn->rcv_buf_lock);913 914 952 /* Determine how many bytes to copy */ 915 953 text_size = tcp_segment_text_size(seg); … … 923 961 /* Signal to the receive function that new data has arrived */ 924 962 fibril_condvar_broadcast(&conn->rcv_buf_cv); 925 fibril_mutex_unlock(&conn->rcv_buf_lock);926 963 927 964 log_msg(LVL_DEBUG, "Received %zu bytes of data.", xfer_size); … … 1013 1050 1014 1051 /* Add FIN to the receive buffer */ 1015 fibril_mutex_lock(&conn->rcv_buf_lock);1016 1052 conn->rcv_buf_fin = true; 1017 1053 fibril_condvar_broadcast(&conn->rcv_buf_cv); 1018 fibril_mutex_unlock(&conn->rcv_buf_lock);1019 1054 1020 1055 tcp_segment_delete(seg); … … 1125 1160 log_msg(LVL_DEBUG, "tw_timeout_func(%p)", conn); 1126 1161 1162 fibril_mutex_lock(&conn->lock); 1163 1127 1164 if (conn->cstate == st_closed) { 1128 1165 log_msg(LVL_DEBUG, "Connection already closed."); 1166 fibril_mutex_unlock(&conn->lock); 1167 tcp_conn_delref(conn); 1129 1168 return; 1130 1169 } … … 1133 1172 tcp_conn_remove(conn); 1134 1173 tcp_conn_state_set(conn, st_closed); 1174 1175 fibril_mutex_unlock(&conn->lock); 1176 tcp_conn_delref(conn); 1135 1177 } 1136 1178 … … 1141 1183 void tcp_conn_tw_timer_set(tcp_conn_t *conn) 1142 1184 { 1185 tcp_conn_addref(conn); 1143 1186 fibril_timer_set(conn->tw_timer, TIME_WAIT_TIMEOUT, tw_timeout_func, 1144 1187 (void *)conn); … … 1151 1194 void tcp_conn_tw_timer_clear(tcp_conn_t *conn) 1152 1195 { 1153 fibril_timer_clear(conn->tw_timer); 1196 if (fibril_timer_clear(conn->tw_timer) == fts_active) 1197 tcp_conn_delref(conn); 1154 1198 } 1155 1199
Note:
See TracChangeset
for help on using the changeset viewer.