Re: FIFO lost on Critical Section




--
Timothy Jewett
Jewettware@xxxxxxxxxxxxx


"Jeroen Mostert" wrote:

Timothy Jewett wrote:
I need a little advice. Because SP1 of W2K3 took away the fifo feature of the
CriticalSection object I need a way to find another way to achieve that
feature without a huge performance hit.

Well, finding out why you need this should be good. :-)

What I have is a IOCP queue that handles the socket events for a service
that is a gateway between two networks. I have session objects that
relate to a socket and worker threads that process the socket events.
These events must be handled in the order coming off of the iocp queue.
In the past I would just deal with this by using a Citical Section which
is part of each session object.

I think this is broken to begin with. Although the old CS would release
waiters in FIFO order, you cannot actually guarantee the threads will start
waiting in FIFO order with this scenario.

Imagine we've got two processors, and two threads waiting in
GetQueuedCompletionStatus(), and two events arrive for the same session.
Imagine thread A was the last one to start waiting, which means the IOCP
will wake it up first (IOCPs wake worker threads in LIFO order). Thread A is
now processing event 1, but just as it's figured out which session the event
belongs to and it's about to grab the corresponding critical section, it
gets preempted by some third-party thread.

Now thread B gets woken up to work on event 2. It's not preempted, so it
happily grabs the critical section and processes event 2. Then thread A gets
a chance to run again and it finally processes event 1. Whoops: you've just
processed the events out of order.

This scenario is not *likely*, since thread A will have a low chance of
being preempted right after being woken up, but it's certainly *possible*.
Fixing it is not trivial.

I have been doing the above for 7 years without a problem on 2000 servers.

The work would have been performed in the order comming off the queue.
Now that is not occuring. I tried to create a single thread to take the
items off the socket queue and place them on and internal session queue
and post to a second queue for the worker threads to process the events.
I control the session queue with a second critical section in the session
object. ( I needed to do this because the InterlockedEntrySList is filo
and I needed fifo ). This all works but I lost 25% of my throughput. Is
there another way to handle work coming from the socket iocp queue ?

Using an InterlockedSList with a CS defeats the point of having a lock-free
structure. You could use a proper lock-free FIFO queue instead, but those
aren't easy to implement correctly. Or you could use a CS with a *regular*
FIFO queue, of course. Also, a "free" FIFO queue is available in the form of
a thread's APC queue, which you can post to with QueueUserAPC(). You cannot
combine this with GQCS(), so you'd need separate worker threads for the
sessions, but you can multiplex sessions over threads if you statically
assign affinity (i.e. thread N handles events for sessions ID % N == 0
only). This only works well if sessions have approximately equal loads, though.

I believe you misunderstood the InterlockedSList statment. I couldn't use
the InterlockedSList because it was filo and there is no equivilant fifo
list, so I used a second CS around a <queue> object.

Bottom line it looks like I'm just going to have to take the hit on the
performance.

Thanks.
--
J.
http://symbolsprose.blogspot.com

.



Relevant Pages

  • Re: FIFO lost on Critical Section
    ... CriticalSection object I need a way to find another way to achieve that feature without a huge performance hit. ... These events must be handled in the order coming off of the iocp queue. ... is part of each session object. ... You could use a proper lock-free FIFO queue instead, but those aren't easy to implement correctly. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Block-ram FIFO in Xilinx
    ... In a BlockRAM implementation, this does not make any sense, for width ... Remember, excessive depth or width has no impact, as long as the FIFO ... reliable generation of the Full and Empty flags at high clock rates. ... I would like to use the queue with different sizes of the data bus. ...
    (comp.arch.fpga)
  • Re: Block-ram FIFO in Xilinx
    ... Also Din and Dout have the same width. ... reliable generation of the Full and Empty flags at high clock rates. ... I have generated a block-ram based FIFO queue (2 independent clocks, ... I would like to use the queue with different sizes of the data bus. ...
    (comp.arch.fpga)
  • Re: Interlocked Singly Linked Lists (for Ben or anyone else).
    ... in regards to a FIFO queue design. ... We were discussing a multiple writer, single consumer queue. ... writers each use the InterlockedPushEntrySList function, ...
    (microsoft.public.win32.programmer.kernel)
  • Asynchromous interprocess message queue (possible with fifo ?)
    ... I want to program some queue facilities for independent processes ... read end and a write end, numerous reader processes and writer ... My initial idea is to use fifo, since it's first in first out just ...
    (comp.unix.programmer)

Loading