I have an interesting problem, which I hope is completely my mistake.
I have code that is read from a queue, as in:
do { evt = &newevts[ evt_head++ ]; evt_head &= MAX_EVENTS; if (evt->index <= 0 || evt->index > MAX_INDEX) { printf("RX EVENT BAD NDX: ndx=%dh=%d\n",evt->index, evt_head); continue; } //... etc ... } while(evt_head != evt_tail) ;
Unusual problem: if statement can evaluate that evt-> index is a bad value, but when displaying printf it shows an absolutely valid value! Example:
RX EVENT BAD NDX: ndx=1 h=64
The if statement clearly shows that the condition must be <= 0 OR> 1024 (maximum index). Worse, this only happens from time to time. I am using GCC, Centos 6.3. No threads touch evt_head except this topic. (I renamed it several times and recompiled to be sure.)
The tail is processed by a function that adds elements to the queue in the same way as the head that removes them (then increasing AND). I also added a counter inside the event structure itself to record head / tail values, as events are queued and do not detect lost or missed values. It literally looks like I'm getting a bad memory reading. But this is ridiculous - I would expect system crashes, or at least program crashes, if that were the case.
Any ideas on how in the world this can happen sporadically? (Frequency of about 1 out of 100 views) I appreciate any input!
typedef struct { int index; int event; } EVENT; #define MAX_EVENTS 0x01ff #define MAX_INDEX 1024
None of the one or the other code applies to evt_head. Only this cycle. The queue is never complete. I also have "SPIN LOCK" when entering a subroutine that adds to the queue (in preparation for being available to other threads) and UNLOCK when exiting.
source share