Recently I have been trying to code and design semaphore in C++ conforming to implementation standard of Java SE 9 documentation. Most of the code is robust now and stable and works as expected. While adding more test examples using the semaphore i discovered a bug case that I am kind of stuck at the moment to solve.
Code location : https://github.com/anandkulkarnisg/Semaphore/
The issue comes in design where fairness policy is introduced in semaphore. i.e request for permits are queued and only served in FIFO fashion. so unless the permits available are sufficient for the first request in the queue pretty much held up.
In Example8.cpp There is a rare bug case where the last permit is released by one thread and condition variable is signalled but 2 items are present in queue and only one gets served but last acquire keeps waiting for condition [ although the condition is met but there is no wakeup call anymore from releasing thread ]. Hence the code is hung up. so last permit acquire is sitting in m_cond.wait and the condition although is satisfied but never wakes up either due to spurious wake up or otherwise.
The bug case report details of this are : https://github.com/anandkulkarnisg/Semaphore/blob/master/bugreports/open/open-signal-coordination-bug.txt Where one can see the below pattern.
Waiting Thread id = 139836827072256, acquireCount = 1
Waiting Thread id = 139836818679552, acquireCount = 2
Waiting Thread id = 139836810286848, acquireCount = 3
Waiting Thread id = 139836801894144, acquireCount = 4
Waiting Thread id = 139836551853824, acquireCount = 9
Waiting Thread id = 139836785108736, acquireCount = 6
Waiting Thread id = 139836543461120, acquireCount = 10
Waiting Thread id = 139836776716032, acquireCount = 7
Waiting Thread id = 139836768323328, acquireCount = 8
Waiting Thread id = 139836793501440, acquireCount = 5
==> [ Semaphore Name = Semaphore.0x6123e0, permits available = -10, fairness = 1, strict = 0, waiting threads queue length = 10. ]
Successfully release the permits = 1
Successfully acquired the permits = 1
Successfully release the permits = 2
Successfully acquired the permits = 2
Successfully release the permits = 3
Successfully acquired the permits = 3
Successfully release the permits = 4
Successfully acquired the permits = 4
Successfully release the permits = 5 ----> Issues originates here two successive releases serve only one acquire :)
Successfully release the permits = 6 -------->
Successfully acquired the permits = 9
Successfully release the permits = 7
Successfully acquired the permits = 6
Successfully release the permits = 8
Successfully acquired the permits = 10
Successfully release the permits = 9
Successfully acquired the permits = 7
Successfully release the permits = 10
Successfully acquired the permits = 8
Help is required in how can this be handled? Code is very clean and mature now and readable .Please have a look and let me know the thoughts ?
Aucun commentaire:
Enregistrer un commentaire