Pushfirst! for Channel

Hi all :slight_smile:

Is there an reason why the is no pushfirst! for Channel?

Cheers

For the buffered version it should be something like this:

julia> function Base.pushfirst!(c::Channel, v)
           lock(c)
           try
               while length(c.data) == c.sz_max
                   check_channel_state(c)
                   wait(c.cond_put)
               end
               pushfirst!(c.data, v)
               # notify all, since some of the waiters may be on a "fetch" call.
               notify(c.cond_take, nothing, true, false)
           finally
               unlock(c)
           end
           return v
       end

But what’s the use of pushfirst! on Channel? It seems that this breaks Channel’s abstraction and doesn’t make sense… You can’t even guarantee this pushfirst! will insert element in a certain place.

I use it in a event based system. Events will queue up. Now an very important event happens (for example most current data). I want to process that as soon as possible.

It seems that you need a concurrent priority queue in this case (with 2 priorities). Julia’s Channel is an ordinary queue so it’s quite natural that it doesn’t support pushfirst! out of box…Maybe you have to implement your own concurrent priority queue?

1 Like

Oki :slight_smile: but would it work if I add the function above?

I think the code you post is adopted Julia’s put_bufferred! implementation? This looks fine to me. But it seems that if there are already some writers queuing (that is, the queue is already full), pushfirst! will happen after these writers (since the condition variable also maintain a task queue to ensure FIFO order). So you might also want to check that part of code.

1 Like

Good point