- uint64_t nb_to_send = 0;
- int bytes_written;
-
- double time_cur, time_last, time_diff;
-
- time_last = g_timer_elapsed(mydata->timer, NULL);
-
- while (thread_running) {
- /* Rate control */
- time_cur = g_timer_elapsed(mydata->timer, NULL);
-
- time_diff = time_cur - time_last;
- time_last = time_cur;
-
- nb_to_send = cur_samplerate * time_diff;
-
- if (limit_samples)
- nb_to_send = MIN(nb_to_send,
- limit_samples - mydata->samples_counter);
-
- /* Make sure we don't overflow. */
- nb_to_send = MIN(nb_to_send, BUFSIZE);
-
- if (nb_to_send) {
- samples_generator(buf, nb_to_send, data);
- mydata->samples_counter += nb_to_send;
-
- g_io_channel_write_chars(channels[1], (gchar *)&buf,
- nb_to_send, (gsize *)&bytes_written, NULL);
- }
-
- /* Check if we're done. */
- if ((limit_msec && time_cur * 1000 > limit_msec) ||
- (limit_samples && mydata->samples_counter >= limit_samples))
- {
- close(mydata->pipe_fds[1]);
- thread_running = 0;
- }
-
- g_usleep(10);
+ static uint64_t samples_to_send, expected_samplenum, sending_now;
+ int64_t time, elapsed;
+
+ (void)fd;
+ (void)revents;
+
+ /* How many "virtual" samples should we have collected by now? */
+ time = g_get_monotonic_time();
+ elapsed = time - devc->starttime;
+ expected_samplenum = elapsed * cur_samplerate / 1000000;
+ /* Of those, how many do we still have to send? */
+ samples_to_send = expected_samplenum - devc->samples_counter;
+
+ if (limit_samples) {
+ samples_to_send = MIN(samples_to_send,
+ limit_samples - devc->samples_counter);