os: make sure tv_nsec is less than 1,000,000,000

This commit is contained in:
Andrew Kelley 2015-07-04 03:37:50 -07:00
parent e587851048
commit 60b731ab4f
3 changed files with 24 additions and 6 deletions

View file

@ -57,6 +57,12 @@ int main(int argc, char **argv) {
soundio_output_device_create(device, SoundIoSampleFormatFloat, 0.1, NULL,
write_callback, underrun_callback, &output_device);
if ((err = soundio_output_device_start(output_device)))
panic("unable to start device: %s", soundio_error_string(err));
for (;;)
soundio_wait_events(soundio);
soundio_output_device_destroy(output_device);
soundio_device_unref(device);
soundio_destroy(soundio);

View file

@ -229,6 +229,12 @@ int soundio_os_thread_create(
soundio_os_thread_destroy(thread);
return SoundIoErrorSystemResources;
}
if ((err = pthread_attr_setschedpolicy(&thread->attr, SCHED_FIFO))) {
soundio_os_thread_destroy(thread);
return SoundIoErrorSystemResources;
}
struct sched_param param;
param.sched_priority = max_priority;
if ((err = pthread_attr_setschedparam(&thread->attr, &param))) {
@ -236,10 +242,6 @@ int soundio_os_thread_create(
return SoundIoErrorSystemResources;
}
if ((err = pthread_attr_setschedpolicy(&thread->attr, SCHED_FIFO))) {
soundio_os_thread_destroy(thread);
return SoundIoErrorSystemResources;
}
}
if ((err = pthread_create(&thread->id, &thread->attr, run_pthread, thread))) {
@ -478,19 +480,21 @@ void soundio_os_cond_timed_wait(struct SoundIoOsCond *cond,
target_mutex = &locked_mutex->id;
} else {
target_mutex = &cond->default_mutex_id;
assert_no_err(pthread_mutex_lock(&cond->default_mutex_id));
assert_no_err(pthread_mutex_lock(target_mutex));
}
// this time is absolute
struct timespec tms;
clock_gettime(CLOCK_MONOTONIC, &tms);
tms.tv_nsec += (seconds * 1000000000L);
tms.tv_sec += tms.tv_nsec / 1000000000L;
tms.tv_nsec = tms.tv_nsec % 1000000000L;
int err;
if ((err = pthread_cond_timedwait(&cond->id, target_mutex, &tms))) {
assert(err != EPERM);
assert(err != EINVAL);
}
if (!locked_mutex)
assert_no_err(pthread_mutex_unlock(&cond->default_mutex_id));
assert_no_err(pthread_mutex_unlock(target_mutex));
#endif
}

View file

@ -75,6 +75,8 @@ void soundio_destroy(struct SoundIo *soundio) {
destroy(soundio);
}
static void default_on_devices_change(struct SoundIo *) { }
struct SoundIo * soundio_create(void) {
soundio_os_init();
struct SoundIo *soundio = create<SoundIo>();
@ -82,6 +84,7 @@ struct SoundIo * soundio_create(void) {
soundio_destroy(soundio);
return NULL;
}
soundio->on_devices_change = default_on_devices_change;
return soundio;
}
@ -319,3 +322,8 @@ void soundio_output_device_destroy(SoundIoOutputDevice *output_device) {
soundio_device_unref(output_device->device);
destroy(output_device);
}
int soundio_output_device_start(struct SoundIoOutputDevice *output_device) {
SoundIo *soundio = output_device->device->soundio;
return soundio->output_device_start(soundio, output_device);
}