In the recent trunk versions I encountered several assertions in dbuffer.c while in battlescape. Most of the time in my second turn.
The location of the assertions change but the circumstances are similat to this code (I changed the code to highlight the problem):
static struct dbuffer_element * allocate_element (void)
{
struct dbuffer_element *e;
if (free_elements == 0) {
struct dbuffer_element *newBuf = Mem_PoolAlloc(sizeof(struct dbuffer_element), com_genericPool, 0);
newBuf->next = free_element_list;
free_element_list = newBuf;
free_elements++;
allocated_elements++;
} else {
assert(free_element_list);
}
...
}
The important detail is: when the assertions happens, the value of free_elements is 0 although it should not be 0.
The only way i came up to get such a result is a second thread which accesses an other function in dbuffer.c and channges free_elements. I did some other tests that confirm my assumption.
What's the general strategy to handle multiple thread access of resources in Ufo AI ?
What's the best way to secure the global data in dbuffer.c ?