summaryrefslogtreecommitdiff
path: root/src/multipart-replace.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/multipart-replace.c')
-rw-r--r--src/multipart-replace.c22
1 files changed, 8 insertions, 14 deletions
diff --git a/src/multipart-replace.c b/src/multipart-replace.c
index facca62..1f7a75c 100644
--- a/src/multipart-replace.c
+++ b/src/multipart-replace.c
@@ -127,7 +127,7 @@ int multipart_replace_reader(struct multipart_replace_stream *s, int fd, const c
struct frame *tmp = s->front;
s->front = s->back;
s->back = tmp;
- s->framecount++;
+ pthread_cond_broadcast(&s->newframe);
pthread_rwlock_unlock(&s->frontlock);
}
end:
@@ -141,17 +141,17 @@ int multipart_replace_writer(struct multipart_replace_stream *s, int fd, const c
int ret = 0;
FILE *stream = fdopen(fd, "w");
struct frame myframe = { 0 };
- long lastframe = 0;
char *boundary = boundary_line(_boundary);
size_t boundary_len = strlen(boundary);
- pthread_rwlock_rdlock(&s->frontlock);
while (running) {
+ pthread_cond_wait(&s->newframe, &s->newframe_lock);
+
/* get the most recent frame (copy `s->front` to `myframe`) */
+ pthread_rwlock_rdlock(&s->frontlock);
if (myframe.cap < (size_t)s->front->len)
myframe.buf = xrealloc(myframe.buf, myframe.cap = s->front->len);
memcpy(myframe.buf, s->front->buf, myframe.len = s->front->len);
- lastframe = s->framecount;
pthread_rwlock_unlock(&s->frontlock);
@@ -166,16 +166,7 @@ int multipart_replace_writer(struct multipart_replace_stream *s, int fd, const c
stdioerror(stream, "dst <- nl");
fflush(stream);
}
-
- /* poll until there's a new frame */
- pthread_rwlock_rdlock(&s->frontlock);
- while (s->framecount == lastframe && running) {
- pthread_rwlock_unlock(&s->frontlock);
- usleep(30000); /* a bit over 30 FPS */
- pthread_rwlock_rdlock(&s->frontlock);
- }
}
- pthread_rwlock_unlock(&s->frontlock);
end:
free(boundary);
free(myframe.buf);
@@ -189,11 +180,14 @@ void init_multipart_replace_stream(struct multipart_replace_stream *s) {
s->front = &s->a;
s->back = &s->b;
pthread_rwlock_init(&s->frontlock, NULL);
- s->framecount = 0;
+ pthread_cond_init(&s->newframe, NULL);
+ pthread_mutex_init(&s->newframe_lock, NULL);
}
void destroy_multipart_replace_stream(struct multipart_replace_stream *s) {
free(s->a.buf);
free(s->b.buf);
pthread_rwlock_destroy(&s->frontlock);
+ pthread_cond_destroy(&s->newframe);
+ pthread_mutex_destroy(&s->newframe_lock);
}