mirror of
https://github.com/mpv-player/mpv
synced 2025-01-03 05:22:23 +00:00
stream: split out pthread helper function
Also split the function itself into 3.
This commit is contained in:
parent
2556f45f2e
commit
b78d11d328
1
Makefile
1
Makefile
@ -228,6 +228,7 @@ SOURCES = audio/audio.c \
|
||||
osdep/io.c \
|
||||
osdep/numcores.c \
|
||||
osdep/timer.c \
|
||||
osdep/threads.c \
|
||||
stream/cookies.c \
|
||||
stream/rar.c \
|
||||
stream/stream.c \
|
||||
|
57
osdep/threads.c
Normal file
57
osdep/threads.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This file is part of mpv.
|
||||
*
|
||||
* mpv is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* mpv is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "threads.h"
|
||||
|
||||
static void get_pthread_time(struct timespec *out_ts)
|
||||
{
|
||||
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
|
||||
clock_gettime(CLOCK_REALTIME, out_ts);
|
||||
#else
|
||||
// OSX
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
out_ts->tv_sec = tv.tv_sec;
|
||||
out_ts->tv_nsec = tv.tv_usec * 1000UL;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void timespec_add_seconds(struct timespec *ts, double seconds)
|
||||
{
|
||||
unsigned long secs = (int)seconds;
|
||||
unsigned long nsecs = (seconds - secs) * 1000000000UL;
|
||||
if (nsecs + ts->tv_nsec >= 1000000000UL) {
|
||||
secs += 1;
|
||||
nsecs -= 1000000000UL;
|
||||
}
|
||||
ts->tv_sec += secs;
|
||||
ts->tv_nsec += nsecs;
|
||||
}
|
||||
|
||||
// Call pthread_cond_timedwait() with a relative timeout in seconds
|
||||
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout)
|
||||
{
|
||||
struct timespec ts;
|
||||
get_pthread_time(&ts);
|
||||
timespec_add_seconds(&ts, timeout);
|
||||
return pthread_cond_timedwait(cond, mutex, &ts);
|
||||
}
|
9
osdep/threads.h
Normal file
9
osdep/threads.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef MP_OSDEP_THREADS_H_
|
||||
#define MP_OSDEP_THREADS_H_
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout);
|
||||
|
||||
#endif
|
@ -50,6 +50,7 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "osdep/timer.h"
|
||||
#include "osdep/threads.h"
|
||||
|
||||
#include "mpvcore/mp_msg.h"
|
||||
|
||||
@ -129,30 +130,6 @@ static int64_t mp_clipi64(int64_t val, int64_t min, int64_t max)
|
||||
return val;
|
||||
}
|
||||
|
||||
// pthread_cond_timedwait() with a relative timeout in seconds
|
||||
static int cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout)
|
||||
{
|
||||
struct timespec ts;
|
||||
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
#else
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
ts.tv_sec = tv.tv_sec;
|
||||
ts.tv_nsec = tv.tv_usec * 1000UL;
|
||||
#endif
|
||||
unsigned long seconds = (int)timeout;
|
||||
unsigned long nsecs = (timeout - seconds) * 1000000000UL;
|
||||
if (nsecs + ts.tv_nsec >= 1000000000UL) {
|
||||
seconds += 1;
|
||||
nsecs -= 1000000000UL;
|
||||
}
|
||||
ts.tv_sec += seconds;
|
||||
ts.tv_nsec += nsecs;
|
||||
return pthread_cond_timedwait(cond, mutex, &ts);
|
||||
}
|
||||
|
||||
// Used by the main thread to wakeup the cache thread, and to wait for the
|
||||
// cache thread. The cache mutex has to be locked when calling this function.
|
||||
// *retry_time should be set to 0 on the first call.
|
||||
@ -173,7 +150,7 @@ static int cache_wakeup_and_wait(struct priv *s, double *retry_time)
|
||||
double start = mp_time_sec();
|
||||
|
||||
pthread_cond_signal(&s->wakeup);
|
||||
cond_timed_wait(&s->wakeup, &s->mutex, CACHE_WAIT_TIME);
|
||||
mpthread_cond_timed_wait(&s->wakeup, &s->mutex, CACHE_WAIT_TIME);
|
||||
|
||||
*retry_time += mp_time_sec() - start;
|
||||
|
||||
@ -450,7 +427,7 @@ static void *cache_thread(void *arg)
|
||||
s->control = CACHE_CTRL_NONE;
|
||||
}
|
||||
if (s->idle && s->control == CACHE_CTRL_NONE)
|
||||
cond_timed_wait(&s->wakeup, &s->mutex, CACHE_IDLE_SLEEP_TIME);
|
||||
mpthread_cond_timed_wait(&s->wakeup, &s->mutex, CACHE_IDLE_SLEEP_TIME);
|
||||
}
|
||||
pthread_cond_signal(&s->wakeup);
|
||||
pthread_mutex_unlock(&s->mutex);
|
||||
|
Loading…
Reference in New Issue
Block a user