A multiple of whitespace / indent fixes.

Move the cache lock up to the allocator of the cache pages. There is
a possible race between tuxtxt_allocate_page and tuxtxt_clear_pages.

One customer reported repeated crashes while zapping. Debugging the core dump
lead me to believe there is a race going on as the variables appeared to have been
erased while it was allocating a page, and in the end crashing on an unallocated
page.





git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@1396 e54a6e83-5905-42d5-8d5c-058d10e6a962


Origin commit data
------------------
Commit: d26267a541
Author: nightshad <nightshad@e54a6e83-5905-42d5-8d5c-058d10e6a962>
Date: 2011-04-12 (Tue, 12 Apr 2011)
This commit is contained in:
nightshad
2011-04-12 09:46:51 +00:00
parent 84d35045aa
commit f449b63688

View File

@@ -71,7 +71,8 @@ void tuxtxt_compress_page(int p, int sp, unsigned char* buffer)
pg->bitmask[i>>3] |= 0x80>>(i&0x07); pg->bitmask[i>>3] |= 0x80>>(i&0x07);
cbuf[j++]=buffer[i]; cbuf[j++]=buffer[i];
} }
if (pg->pData) free(pg->pData);//realloc(pg->pData,j); realloc scheint nicht richtig zu funktionieren? if (pg->pData)
free(pg->pData);//realloc(pg->pData,j); realloc scheint nicht richtig zu funktionieren?
pg->pData = (unsigned char*)malloc(j); pg->pData = (unsigned char*)malloc(j);
if (pg->pData) if (pg->pData)
{ {
@@ -79,18 +80,18 @@ void tuxtxt_compress_page(int p, int sp, unsigned char* buffer)
} }
else else
memset(pg->bitmask,0,sizeof(pg->bitmask)); memset(pg->bitmask,0,sizeof(pg->bitmask));
#else #else
//if (pg->pData) //if (pg->pData)
memmove(pg->data,buffer,23*40); memmove(pg->data,buffer,23*40);
#endif #endif
pthread_mutex_unlock(&tuxtxt_cache_lock); pthread_mutex_unlock(&tuxtxt_cache_lock);
} }
void tuxtxt_decompress_page(int p, int sp, unsigned char* buffer) void tuxtxt_decompress_page(int p, int sp, unsigned char* buffer)
{ {
pthread_mutex_lock(&tuxtxt_cache_lock); pthread_mutex_lock(&tuxtxt_cache_lock);
tstCachedPage* pg = tuxtxt_cache.astCachetable[p][sp]; tstCachedPage* pg = tuxtxt_cache.astCachetable[p][sp];
memset(buffer,' ',23*40); memset(buffer,' ',23*40);
if (!pg) if (!pg)
{ {
@@ -347,7 +348,7 @@ int tuxtxt_GetSubPage(int page, int subpage, int offset)
* clear_cache * * clear_cache *
******************************************************************************/ ******************************************************************************/
void tuxtxt_clear_cache() void tuxtxt_clear_cache(void)
{ {
pthread_mutex_lock(&tuxtxt_cache_lock); pthread_mutex_lock(&tuxtxt_cache_lock);
int clear_page, clear_subpage, d26; int clear_page, clear_subpage, d26;
@@ -501,29 +502,34 @@ void tuxtxt_decode_p2829(unsigned char *vtxt_row, tstExtData **ptExtData)
void tuxtxt_erase_page(int magazine) void tuxtxt_erase_page(int magazine)
{ {
pthread_mutex_lock(&tuxtxt_cache_lock);
tstCachedPage* pg = tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]]; tstCachedPage* pg = tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]];
if (pg) if (pg)
{ {
memset(&(pg->pageinfo), 0, sizeof(tstPageinfo)); /* struct pageinfo */ memset(&(pg->pageinfo), 0, sizeof(tstPageinfo)); /* struct pageinfo */
memset(pg->p0, ' ', 24); memset(pg->p0, ' ', 24);
#if TUXTXT_COMPRESS == 1 #if TUXTXT_COMPRESS == 1
if (pg->pData) {free(pg->pData); pg->pData = NULL;} if (pg->pData) {
free(pg->pData);
pg->pData = NULL;
}
#elif TUXTXT_COMPRESS == 2 #elif TUXTXT_COMPRESS == 2
memset(pg->bitmask, 0, 23*5); memset(pg->bitmask, 0, 23*5);
#else #else
memset(pg->data, ' ', 23*40); memset(pg->data, ' ', 23*40);
#endif #endif
} }
pthread_mutex_unlock(&tuxtxt_cache_lock);
} }
void tuxtxt_allocate_cache(int magazine) void tuxtxt_allocate_cache(int magazine)
{ {
// Lock here as we have a possible race here with
// tuxtxt_clear_cache(). We should not be allocating and
// freeing at the same time.
pthread_mutex_lock(&tuxtxt_cache_lock);
/* check cachetable and allocate memory if needed */ /* check cachetable and allocate memory if needed */
if (tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] == 0) if (tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] == 0)
{ {
tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] = (tstCachedPage*) malloc(sizeof(tstCachedPage)); tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] = (tstCachedPage*) malloc(sizeof(tstCachedPage));
if (tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] ) if (tuxtxt_cache.astCachetable[tuxtxt_cache.current_page[magazine]][tuxtxt_cache.current_subpage[magazine]] )
{ {
@@ -533,12 +539,17 @@ void tuxtxt_allocate_cache(int magazine)
tuxtxt_erase_page(magazine); tuxtxt_erase_page(magazine);
tuxtxt_cache.cached_pages++; tuxtxt_cache.cached_pages++;
} }
else // Be a little verbose in case a crash is going to happen.
{
printf("tuxtxt: memory allocation failed!!! expect a crash\n");
} }
} }
pthread_mutex_unlock(&tuxtxt_cache_lock);
}
/****************************************************************************** /******************************************************************************
* CacheThread * * CacheThread *
******************************************************************************/ ******************************************************************************/
//#define TUXTXT_DEBUG 1 //FIXME
static int stop_cache = 0; static int stop_cache = 0;
void *tuxtxt_CacheThread(void * /*arg*/) void *tuxtxt_CacheThread(void * /*arg*/)
{ {
@@ -561,7 +572,7 @@ void *tuxtxt_CacheThread(void * /*arg*/)
unsigned char pagedata[9][23*40]; unsigned char pagedata[9][23*40];
tstPageinfo *pageinfo_thread; tstPageinfo *pageinfo_thread;
printf("TuxTxt running thread...(%03x)\n",tuxtxt_cache.vtxtpid); printf("TuxTxt running thread...(%04x)\n",tuxtxt_cache.vtxtpid);
tuxtxt_cache.receiving = 1; tuxtxt_cache.receiving = 1;
nice(3); nice(3);
while (!stop_cache) while (!stop_cache)
@@ -569,7 +580,8 @@ void *tuxtxt_CacheThread(void * /*arg*/)
/* check stopsignal */ /* check stopsignal */
pthread_testcancel(); pthread_testcancel();
if (!tuxtxt_cache.receiving) continue; if (!tuxtxt_cache.receiving)
continue;
/* read packet */ /* read packet */
ssize_t readcnt; ssize_t readcnt;
@@ -1027,7 +1039,8 @@ void *tuxtxt_CacheThread(void * /*arg*/)
#endif #endif
} }
} }
return 0;
pthread_exit(NULL);
} }
/****************************************************************************** /******************************************************************************
* start_thread * * start_thread *
@@ -1035,8 +1048,8 @@ void *tuxtxt_CacheThread(void * /*arg*/)
int tuxtxt_start_thread(int source = 0); int tuxtxt_start_thread(int source = 0);
int tuxtxt_start_thread(int source) int tuxtxt_start_thread(int source)
{ {
if (tuxtxt_cache.vtxtpid == -1) return 0; if (tuxtxt_cache.vtxtpid == -1)
return 0;
tuxtxt_cache.thread_starting = 1; tuxtxt_cache.thread_starting = 1;
tuxtxt_init_demuxer(source); tuxtxt_init_demuxer(source);