2021-04-06 RZHuangJeff
Problem 1
Problem 2
- The purpose of
greeter_routine
is to accept incoming connections, accepted connections will be queued until a worker_routine
, which aims to deal with the requests, is avaliable.
- Since there are different locks protect enqueueing and dequeueing from data racing, we do not need to worry about several enqueueing/dequeueing overlaps with each other. The only situation we have to concern is overlapping between enqueueing and dequeueing. With requiring
tail_lock
while dequeueing, the fields that are modified both in enqueueing and dequeueing (size
and tail
) is protected in critical section.
Did you consider to add the statement q->size--
?
jserv
Problem 3
- To expend the implementation of ring buffer to fit SPMC and MPMC, we have to introduce atomic operations to make sure that every modification to the buffer is expected. The most important thing is to keep the range between different producers or different consumers is not overlapped, this is where the atomic operations take place. Before a producer or a consumer modifies or reads from the buffer, they have to update the index (
prod_head
and cons_head
) first, by atomic_compare_exchange
. Once they mark their working place successfully, they can modfiy and read from such place without worrying other producers or consumers.
The macro #define ALIGN_FLOOR(val, align) ((val / align + ((val % align) != 0)) * align)
was not elegant. Improve it.
jserv
Problem 4
- With designing callback function in this way, the reciever no needs to poll waiting for certain conditions happend, since once a condition happends, the sender thread will deal with that condition with in its context.
- To reduce affects between different callback functions, a thread pool with several workers could be launched at very beginning. Workers will be locked on specific processors by processor affinity and aims to deal with executing callback functions. Once a message is sent via message bus, the corresponding callback function of reciever is queued for an avaliable worker.