-/* Thread function */
-static void thread_func(void *data)
-{
- struct context *ctx = data;
- uint8_t buf[BUFSIZE];
- uint64_t nb_to_send = 0;
- int bytes_written;
- double time_cur, time_last, time_diff;
-
- time_last = g_timer_elapsed(ctx->timer, NULL);
-
- while (thread_running) {
- /* Rate control */
- time_cur = g_timer_elapsed(ctx->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 - ctx->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);
- ctx->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 && ctx->samples_counter >= limit_samples))
- {
- close(ctx->pipe_fds[1]);
- thread_running = 0;
- }
-
- g_usleep(10);
- }
-}
-