]> rtime.felk.cvut.cz Git - frescor/forb.git/blobdiff - src/port.c
forb: Split forb_port_destroy() to stop and destroy phases
[frescor/forb.git] / src / port.c
index 60121b089f6bf5121b51d5f18606876fbcdfc19f..029f593faecf1e128d169fa8d8c177c1c59889c0 100644 (file)
@@ -61,6 +61,7 @@
 #include <forb/config.h>
 #include "iop.h"
 #include <ul_log.h>
+#include "discovery.h"
 
 extern UL_LOG_CUST(ulogd_forb_port);
 
@@ -139,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 */
@@ -156,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. */
@@ -167,14 +168,32 @@ void forb_destroy_port(forb_port_t *port)
 #ifndef CONFIG_FORB_PROTO_INET_DEFAULT
        pthread_join(port->discovery_thread.pthread_id, &thread_return);
 #endif
+}
 
-       if (port->desc.proto->port_destroy) {
-               port->desc.proto->port_destroy(port);
-       }
 
+/** 
+ * 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_delete_by_port(forb, port);
+       forb_peer_t *peer;
+       ul_list_for_each_cut(forb_port_peer, port, peer) {
+               forb_peer_get(peer);
+               forb_peer_disconnected(peer);
+               forb_peer_put(peer);
+       }
+       if (port->new_peer)
+               forb_peer_put(port->new_peer);
+
+       if (port->desc.proto->port_destroy) {
+               port->desc.proto->port_destroy(port);
+       }
 
        FORB_CDR_codec_release_buffer(&port->codec);