From: Michal Sojka Date: Fri, 25 Feb 2011 07:20:28 +0000 (+0100) Subject: forb: Split forb_port_destroy() to stop and destroy phases X-Git-Url: https://rtime.felk.cvut.cz/gitweb/frescor/forb.git/commitdiff_plain/4dc2d2b64bc59fb2ac71335014059e49de090d65 forb: Split forb_port_destroy() to stop and destroy phases This allows for proper termination of forb. Now, there are no leaked file descriptors in the tests. --- diff --git a/src/discovery.c b/src/discovery.c index 9019665..ae2b88c 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -268,6 +268,9 @@ void forb_peer_disconnected(forb_peer_t *peer) { forb_peer_delete(peer->port->forb, peer); forb_port_peer_delete(peer->port, peer); - forb_peer_put(peer); /* This should release the peer and in case on proto_inet close the socker. */ + + /* This should release the peer and in case on proto_inet + * close the socket. */ + forb_peer_put(peer); } diff --git a/src/forb.c b/src/forb.c index 376651f..309c660 100644 --- a/src/forb.c +++ b/src/forb.c @@ -302,13 +302,13 @@ void forb_destroy(forb_orb orb) forb_regref_t *regref; forb_object obj; - /* Destroy ports to prevent remote requests from coming */ - ul_list_for_each_cut(forb_port, forb, port) { - forb_destroy_port(port); + /* Stop ports to prevent remote requests from coming */ + ul_list_for_each(forb_port, forb, port) { + forb_stop_port(port); } - /* Wait for executors to finish all requests (and thus close - * connections to peers). This is very inefficient for big + /* Wait for executors to finish all requests (and thus drop + * all references to peers). This is very inefficient for big * number of objects, but we do not care */ gavl_cust_for_each(forb_objects_nolock, forb, obj) { forb_executor_t *executor; @@ -317,6 +317,13 @@ void forb_destroy(forb_orb orb) forb_executor_synchronize(executor); } + /* Destroy ports - this should drop all remaining references + * to peers and result in closing of all remote + * connections. */ + ul_list_for_each_cut(forb_port, forb, port) { + forb_destroy_port(port); + } + pthread_cancel(forb->execution_thread.pthread_id); pthread_join(forb->execution_thread.pthread_id, NULL); diff --git a/src/port.c b/src/port.c index 637c22e..029f593 100644 --- a/src/port.c +++ b/src/port.c @@ -140,13 +140,12 @@ err: } /** - * Destroys the port and all resources associated with it. + * Stop receiving requests on the port. * * @param port */ -void forb_destroy_port(forb_port_t *port) +void forb_stop_port(forb_port_t *port) { - forb_t *forb = port->forb; void *thread_return; port->finish = true; /* Exit all the threads */ @@ -157,8 +156,9 @@ void forb_destroy_port(forb_port_t *port) /* FIXME: Canceling discovery thread sometimes didn't * work. The discovery thread stayed forever in the * pthread_cond_timedwait(). */ - //pthread_cancel(port->discovery_thread.pthread_id); - +#ifndef CONFIG_FORB_PROTO_INET_DEFAULT + pthread_cancel(port->discovery_thread.pthread_id); +#endif /* Sometimes, cancelation doesn't work and the * discovery_thread hangs in pthread_cond_timedwait * infinitely. */ @@ -168,7 +168,18 @@ void forb_destroy_port(forb_port_t *port) #ifndef CONFIG_FORB_PROTO_INET_DEFAULT pthread_join(port->discovery_thread.pthread_id, &thread_return); #endif +} + +/** + * Destroys the port and all resources associated with it. + * + * @param port + */ +void forb_destroy_port(forb_port_t *port) +{ + forb_t *forb = port->forb; + /* Because of no locking of port->peers, this must be called * after receiver thread is stopped. */ forb_peer_t *peer; diff --git a/src/port.h b/src/port.h index b5b97fa..4d0d010 100644 --- a/src/port.h +++ b/src/port.h @@ -108,6 +108,7 @@ UL_LIST_CUST_DEC(forb_port, /* cust_prefix */ node) /* cust_node_field */ int forb_register_port(forb_orb orb, forb_port_t *port); +void forb_stop_port(forb_port_t *port); void forb_destroy_port(forb_port_t *port);