summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-09-10 21:38:47 -0700
committerOri Bernstein <ori@eigenstate.org>2017-09-10 21:38:47 -0700
commitdd8235ce6d9c45e7039a821bdc86818c63c2823d (patch)
tree1e8bb7a2036edf082931253e509f1dcb54564e69
parentd09137d7087bb2909f1e8ee02a098c44e42958ab (diff)
downloadmc-dd8235ce6d9c45e7039a821bdc86818c63c2823d.tar.gz
Refcount the HTTP server.
-rw-r--r--lib/http/server.myr26
-rw-r--r--lib/http/srvdot.myr1
-rw-r--r--lib/http/types.myr1
3 files changed, 25 insertions, 3 deletions
diff --git a/lib/http/server.myr b/lib/http/server.myr
index e6c1923..8b221bd 100644
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -25,18 +25,24 @@ const announce = {ds
| `std.Err e: -> `std.Err `Econn
| `std.Ok lfd:
std.close(afd)
- -> `std.Ok std.mk([.lfd=lfd])
+ -> `std.Ok std.mk([
+ .refs=1,
+ .lfd=lfd,
+ .quit=false
+ ])
;;
}
const serve = {srv, fn
- while true
+ while !srv.quit
match waitconn(srv)
- | `std.Ok fd:
+ | `std.Ok fd:
+ ref(srv)
thread.spawn({;communicate(srv, fd, fn)})
| `std.Err e: /* eh? */
;;
;;
+ unref(srv)
}
const communicate = {srv, fd, fn
@@ -50,6 +56,7 @@ const communicate = {srv, fd, fn
;;
;;
std.close(fd)
+ unref(srv)
}
const respond = {srv, s, resp
@@ -78,6 +85,8 @@ const statusstr = {st
const shutdown = {srv
std.close(srv.lfd)
+ srv.quit = true
+
}
@@ -88,3 +97,14 @@ const waitconn = {srv
;;
}
+
+const ref = {srv
+ thread.xadd(&srv.refs, 1)
+}
+
+const unref = {srv
+ thread.xadd(&srv.refs, -1)
+ if thread.xget(&srv.refs) == 0
+ std.free(srv)
+ ;;
+}
diff --git a/lib/http/srvdot.myr b/lib/http/srvdot.myr
index c47c4ce..e6c0a21 100644
--- a/lib/http/srvdot.myr
+++ b/lib/http/srvdot.myr
@@ -28,6 +28,7 @@ const route = {srv, sess, req
std.put("Reading path {}\n", req.url.path)
match req.url.path
| "/ping": respond(srv, sess, 200, "pong")
+ | "/quit": http.shutdown(srv)
| fspath: showfile(srv, sess, req.url.path)
;;
}
diff --git a/lib/http/types.myr b/lib/http/types.myr
index 2b5d2e7..357c3fe 100644
--- a/lib/http/types.myr
+++ b/lib/http/types.myr
@@ -16,6 +16,7 @@ pkg http =
type server = struct
lfd : std.fd
+ refs : uint32
quit : bool
;;