Permalink
Browse files

use signaling instead of flag for stream_writer

  • Loading branch information...
1 parent e41b109 commit 6ff6e26b3b35915d5a8c6e22205aadd28ed9885e Yoann Canal committed Jun 6, 2009
Showing with 25 additions and 33 deletions.
  1. +0 −8 list.h
  2. +17 −24 server.cpc
  3. +8 −1 server.h
View
8 list.h
@@ -45,14 +45,6 @@ typedef struct chunk {
struct chunk *next;
} chunk;
-typedef struct peer {
- int stream_writer;
- struct torrent *t;
- int credit;
- struct chunk *list;
-} peer;
-
-
void tr_insert(struct torrent *t);
struct chunk *add_chunk(struct chunk *l, int64_t offset, int begin, int length);
struct chunk *remove_chunk(struct chunk *l, int offset, int begin, int length);
View
@@ -36,6 +36,7 @@ THE SOFTWARE.
#include "util.h"
#include "hashtable.h"
#include "parse.h"
+#include "server.h"
#include "torrent.h"
#include "tracker.h"
#include "list.h"
@@ -343,8 +344,6 @@ send_chunk(int fd, struct chunk *chunk)
unsigned offset;
struct file *f;
- assert(peers[fd].stream_writer);
-
if(chunk->length > MAX_PEER_CREDIT) {
fprintf(stderr, "Chunk larger than max credit!\n");
return -1;
@@ -503,23 +502,22 @@ cps void
stream_writer(int fd)
{
//while request list non empty, send chunks
- struct chunk *cl;
+ struct chunk *chunk;
int rc;
- assert(peers[fd].stream_writer);
+ while(1) {
+ while(!peers[fd].list)
+ cpc_wait(peers[fd].sw_cond);
+
+ chunk = peers[fd].list;
- while((cl = peers[fd].list)) {
peers[fd].list = peers[fd].list->next;
- rc = send_chunk(fd, cl);
- free(cl);
- if(rc < 0) {
+ rc = send_chunk(fd, chunk);
+ if(rc < 0)
shutdown(fd, 2); /* for now */
- break;
- }
+ free(chunk);
cpc_yield;
}
- peers[fd].stream_writer = 0;
- return;
}
cps int
@@ -632,11 +630,7 @@ stream_reader(int fd)
i == last ?
last_len : peers[fd].t->files[i]->length);
}
-
- if(peers[fd].stream_writer == 0) {
- peers[fd].stream_writer = 1;
- cpc_spawn stream_writer(fd);
- }
+ cpc_signal(peers[fd].sw_cond);
} else {
peers[fd].list = remove_chunk(peers[fd].list,
index, begin, chunk_len);
@@ -693,16 +687,15 @@ client(hashtable *t, int client_fd)
fprintf(stderr, "couldn't send unchoke\n");
return;
}
+
+ if(!peers[client_fd].sw_cond) {
+ peers[client_fd].sw_cond = cpc_condvar_get();
+ cpc_spawn stream_writer(client_fd);
+ }
+
rc = stream_reader(client_fd);
done:
- if(peers[client_fd].stream_writer) {
- shutdown(client_fd, 2);
- /* XXX should use explicit signalling here. */
- do {
- cpc_yield;
- } while(peers[client_fd].stream_writer);
- }
free_chunks(client_fd);
peers[client_fd].t = NULL;
numpeers--;
View
@@ -24,6 +24,13 @@ THE SOFTWARE.
#ifndef SERVER_H
#define SERVER_H
-cps int listening(hashtable * table);
+typedef struct peer {
+ cpc_condvar *sw_cond;
+ struct torrent *t;
+ int credit;
+ struct chunk *list;
+} peer;
+
+cps void listening(hashtable * table);
#endif

0 comments on commit 6ff6e26

Please sign in to comment.