Bump version to 0.3.5next
[archipelago] / xseg / peers / user / speer.c
index 4d1b599..3a2f145 100644 (file)
@@ -1,3 +1,37 @@
+/*
+ * Copyright 2012 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/syscall.h>
 #include <sys/time.h>
 #include <signal.h>
+
+#ifdef ST_THREADS
 #include <st.h>
+uint32_t ta = 0;
+#endif
 
 unsigned int verbose;
 struct log_ctx lc;
-uint32_t ta = 0;
 
 inline int canDefer(struct peerd *peer)
 {
@@ -138,11 +175,6 @@ void complete(struct peerd *peer, struct peer_req *pr)
        free_peer_req(peer, pr);
 }
 
-void pending(struct peerd *peer, struct peer_req *pr)
-{
-               pr->req->state = XS_PENDING;
-}
-
 static void handle_accepted(struct peerd *peer, struct peer_req *pr, 
                                struct xseg_request *req)
 {
@@ -150,7 +182,6 @@ static void handle_accepted(struct peerd *peer, struct peer_req *pr,
        //assert xreq == req;
        XSEGLOG2(&lc, D, "Handle accepted");
        xreq->serviced = 0;
-       //xreq->state = XS_ACCEPTED;
        pr->retval = 0;
        dispatch(peer, pr, req, dispatch_accept);
 }
@@ -159,7 +190,6 @@ static void handle_received(struct peerd *peer, struct peer_req *pr,
                                struct xseg_request *req)
 {
        //struct xseg_request *req = pr->req;
-       //assert req->state != XS_ACCEPTED;
        XSEGLOG2(&lc, D, "Handle received \n");
        dispatch(peer, pr, req, dispatch_receive);
 
@@ -264,14 +294,18 @@ static int peerd_loop(struct peerd *peer)
                        if (check_ports(peer))
                                loops = threshold;
                }
+#ifdef ST_THREADS
                if (ta){
                        st_sleep(0);
                } else {
+#endif
                        XSEGLOG2(&lc, I, "Peer goes to sleep\n");
                        xseg_wait_signal(xseg, 10000000UL);
                        xseg_cancel_wait(xseg, peer->portno_start);
                        XSEGLOG2(&lc, I, "Peer woke up\n");
+#ifdef ST_THREADS
                }
+#endif
        }
        xseg_quit_local_signal(xseg, peer->portno_start);
        return 0;
@@ -306,8 +340,9 @@ static struct peerd* peerd_init(uint32_t nr_ops, char* spec, long portno_start,
        struct peerd *peer;
        struct xseg_port *port;
 
+#ifdef ST_THREADS
        st_init();
-
+#endif
        peer = malloc(sizeof(struct peerd));
        if (!peer) {
                perror("malloc");
@@ -361,7 +396,9 @@ malloc_fail:
                peer->peer_reqs[i].retval = 0;
                peer->peer_reqs[i].priv = NULL;
                peer->peer_reqs[i].portno = NoPort;
+#ifdef ST_THREADS
                peer->peer_reqs[i].cond = st_cond_new(); //FIXME err check
+#endif
        }
        return peer;
 }
@@ -448,7 +485,10 @@ int main(int argc, char *argv[])
        r = custom_peer_init(peer, argc, argv);
        if (r < 0)
                return -1;
+#ifdef ST_THREADS
        st_thread_t st = st_thread_create(peerd_loop, peer, 1, 0);
        return st_thread_join(st, NULL);
-//     return peerd_loop(peer);
+#else
+       return peerd_loop(peer);
+#endif
 }