History

udp: Replace double select() by select+mutex+cond.

When no data was available both the buffer thread as well as the main thread would block in select(), when data becomes available both should move forward and as data is read in the buffer thread the main thread would block in select() later the read data was put in the fifo but the main thread still would be blocked in select() until either the timeout or another packet would come in. This is solved in this commit by using a mutex and a condition variable

Michael Niedermayer committed 14 years ago

Commit bc900501 parent 3dcbafc1 file · +27 −14Browse files

Unified · Split

libavformat/udp.c+27−14
@@ -69,6 +69,8 @@ typedef struct { · 68 unmodified lines
6969 int circular_buffer_error;
7070 #if HAVE_PTHREADS
7171 pthread_t circular_buffer_thread;
72+ pthread_mutex_t mutex;
73+ pthread_cond_t cond;
7274 #endif
7375 uint8_t tmp[UDP_MAX_PKT_SIZE+4];
7476 int remaining_in_dg;
@@ -317,6 +319,7 @@ static int udp_get_file_handle(URLContext *h) · 242 unmodified lines
317319 return s->udp_fd;
318320 }
319321
322+#if HAVE_PTHREADS
320323 static void *circular_buffer_task( void *_URLContext)
321324 {
322325 URLContext *h = _URLContext;
@@ -331,7 +334,7 @@ static void *circular_buffer_task( void *_URLContext) · 8 unmodified lines
331334
332335 if (ff_check_interrupt(&h->interrupt_callback)) {
333336 s->circular_buffer_error = EINTR;
334− return NULL;
337+ goto end;
335338 }
336339
337340 FD_ZERO(&rfds);
@@ -343,7 +346,7 @@ static void *circular_buffer_task( void *_URLContext) · 5 unmodified lines
343346 if (ff_neterrno() == AVERROR(EINTR))
344347 continue;
345348 s->circular_buffer_error = EIO;
346− return NULL;
349+ goto end;
347350 }
348351
349352 if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
@@ -357,23 +360,31 @@ static void *circular_buffer_task( void *_URLContext) · 7 unmodified lines
357360 if(left < UDP_MAX_PKT_SIZE + 4) {
358361 av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
359362 s->circular_buffer_error = EIO;
360− return NULL;
363+ goto end;
361364 }
362365 left = FFMIN(left, s->fifo->end - s->fifo->wptr);
363366 len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
364367 if (len < 0) {
365368 if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
366369 s->circular_buffer_error = EIO;
367− return NULL;
370+ goto end;
368371 }
369372 continue;
370373 }
371374 AV_WL32(s->tmp, len);
375+ pthread_mutex_lock(&s->mutex);
372376 av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
377+ pthread_cond_signal(&s->cond);
378+ pthread_mutex_unlock(&s->mutex);
373379 }
374380
381+end:
382+ pthread_mutex_lock(&s->mutex);
383+ pthread_cond_signal(&s->cond);
384+ pthread_mutex_unlock(&s->mutex);
375385 return NULL;
376386 }
387+#endif
377388
378389 /* put it in UDP context */
379390 /* return non zero if error */
@@ -516,6 +527,8 @@ static int udp_open(URLContext *h, const char *uri, int flags) · 136 unmodified lines
516527 if (!is_output && s->circular_buffer_size) {
517528 /* start the task going */
518529 s->fifo = av_fifo_alloc(s->circular_buffer_size);
530+ pthread_mutex_init(&s->mutex, NULL);
531+ pthread_cond_init(&s->cond, NULL);
519532 if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) {
520533 av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
521534 goto fail;
@@ -536,15 +549,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 14 unmodified lines
536549 UDPContext *s = h->priv_data;
537550 int ret;
538551 int avail;
539− fd_set rfds;
540− struct timeval tv;
541552
553+#if HAVE_PTHREADS
542554 if (s->fifo) {
543−
555+ pthread_mutex_lock(&s->mutex);
544556 do {
545557 avail = av_fifo_size(s->fifo);
546558 if (avail) { // >=size) {
547559 uint8_t tmp[4];
560+ pthread_mutex_unlock(&s->mutex);
548561
549562 av_fifo_generic_read(s->fifo, tmp, 4, NULL);
550563 avail= AV_RL32(tmp);
@@ -557,19 +570,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 6 unmodified lines
557570 av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
558571 return avail;
559572 } else if(s->circular_buffer_error){
573+ pthread_mutex_unlock(&s->mutex);
560574 return s->circular_buffer_error;
561575 }
562576 else {
563− FD_ZERO(&rfds);
564− FD_SET(s->udp_fd, &rfds);
565− tv.tv_sec = 1;
566− tv.tv_usec = 0;
567− ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
568− if (ret<0)
569− return ret;
577+ pthread_cond_wait(&s->cond, &s->mutex);
570578 }
571579 } while( 1);
572580 }
581+#endif
573582
574583 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
575584 ret = ff_network_wait_fd(s->udp_fd, 0);
@@ -610,6 +619,10 @@ static int udp_close(URLContext *h) · 34 unmodified lines
610619 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
611620 closesocket(s->udp_fd);
612621 av_fifo_free(s->fifo);
622+#if HAVE_PTHREADS
623+ pthread_mutex_destroy(&s->mutex);
624+ pthread_cond_destroy(&s->cond);
625+#endif
613626 return 0;
614627 }
615628

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t