Changeset 78aca91b in mainline for uspace/srv/audio/hound/hound_ctx.c


Ignore:
Timestamp:
2013-04-06T18:02:40Z (11 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
98af9cc
Parents:
2c0b348
Message:

hound: Use synchronization primitives instead of sleep waiting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/audio/hound/hound_ctx.c

    r2c0b348 r78aca91b  
    111111        int flags;
    112112        size_t allowed_size;
     113        fibril_mutex_t guard;
     114        fibril_condvar_t change;
    113115} hound_ctx_stream_t;
    114116
     
    144146                audio_pipe_init(&stream->fifo);
    145147                link_initialize(&stream->link);
     148                fibril_mutex_initialize(&stream->guard);
     149                fibril_condvar_initialize(&stream->change);
    146150                stream->ctx = ctx;
    147151                stream->flags = flags;
     
    182186                return EINVAL;
    183187
    184         if (stream->allowed_size &&
    185             (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size))
    186                 return EBUSY;
    187 
    188         return audio_pipe_push_data(&stream->fifo, data, size, stream->format);
     188        fibril_mutex_lock(&stream->guard);
     189        while (stream->allowed_size &&
     190            (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size)) {
     191            fibril_condvar_wait(&stream->change, &stream->guard);
     192
     193        }
     194
     195        const int ret =
     196            audio_pipe_push_data(&stream->fifo, data, size, stream->format);
     197        fibril_mutex_unlock(&stream->guard);
     198        return ret;
    189199}
    190200
     
    199209{
    200210        assert(stream);
    201         const ssize_t copied_size =
    202             audio_pipe_mix_data(&stream->fifo, data, size, f);
    203         if (copied_size != (ssize_t)size)
    204                 log_warning("Not enough data in stream buffer");
    205         return copied_size;
     211        fibril_mutex_lock(&stream->guard);
     212        const int ret = audio_pipe_mix_data(&stream->fifo, data, size, f);
     213        fibril_condvar_signal(&stream->change);
     214        fibril_mutex_unlock(&stream->guard);
     215        return ret;
    206216}
    207217
     
    210220        assert(stream);
    211221        log_debug("Draining stream");
     222        fibril_mutex_lock(&stream->guard);
    212223        while (audio_pipe_bytes(&stream->fifo))
    213                 async_usleep(10000);
     224                fibril_condvar_wait(&stream->change, &stream->guard);
     225        fibril_mutex_unlock(&stream->guard);
    214226}
    215227
     
    233245        list_foreach(ctx->streams, it) {
    234246                hound_ctx_stream_t *stream = hound_ctx_stream_from_link(it);
    235                 hound_ctx_stream_add_self(stream, buffer, size, &source->format);
     247                ssize_t copied = hound_ctx_stream_add_self(
     248                    stream, buffer, size, &source->format);
     249                if (copied != (ssize_t)size)
     250                        log_warning("Not enough data in stream buffer");
    236251        }
    237252        log_verbose("CTX: %p. Pushing audio to %zu connections", ctx,
Note: See TracChangeset for help on using the changeset viewer.