Use GCond for rendering thread

This commit is contained in:
Moritz Lipp 2011-01-26 19:06:57 +08:00
parent 056269fcae
commit e043e55b13
2 changed files with 48 additions and 11 deletions

View File

@ -10,17 +10,15 @@ render_job(void* data)
render_thread_t* render_thread = (render_thread_t*) data; render_thread_t* render_thread = (render_thread_t*) data;
while(true) { while(true) {
g_static_mutex_lock(&(render_thread->lock)); g_mutex_lock(render_thread->lock);
if(girara_list_size(render_thread->list) <= 0) { if(girara_list_size(render_thread->list) <= 0) {
g_static_mutex_unlock(&(render_thread->lock)); g_cond_wait(render_thread->cond, render_thread->lock);
g_thread_yield();
continue;
} }
zathura_page_t* page = (zathura_page_t*) girara_list_nth(render_thread->list, 0); zathura_page_t* page = (zathura_page_t*) girara_list_nth(render_thread->list, 0);
girara_list_remove(render_thread->list, page); girara_list_remove(render_thread->list, page);
g_static_mutex_unlock(&(render_thread->lock)); g_mutex_unlock(render_thread->lock);
printf("Rendered %d\n", page->number); printf("Rendered %d\n", page->number);
@ -39,6 +37,12 @@ render_init(void)
goto error_ret; goto error_ret;
} }
/* init */
render_thread->list = NULL;
render_thread->thread = NULL;
render_thread->cond = NULL;
/* setup */
render_thread->list = girara_list_new(); render_thread->list = girara_list_new();
if(!render_thread->list) { if(!render_thread->list) {
@ -51,12 +55,34 @@ render_init(void)
goto error_free; goto error_free;
} }
g_static_mutex_init(&(render_thread->lock)); render_thread->cond = g_cond_new();
if(!render_thread->cond) {
goto error_free;
}
render_thread->lock = g_mutex_new();
if(!render_thread->lock) {
goto error_free;
}
return render_thread; return render_thread;
error_free: error_free:
if(render_thread->list) {
girara_list_free(render_thread->list);
}
if(render_thread->cond) {
g_cond_free(render_thread->cond);
}
if(render_thread->lock) {
g_mutex_free(render_thread->lock);
}
free(render_thread); free(render_thread);
error_ret: error_ret:
@ -71,8 +97,17 @@ render_free(render_thread_t* render_thread)
return; return;
} }
girara_list_free(render_thread->list); if(render_thread->list) {
g_static_mutex_free(&(render_thread->lock)); girara_list_free(render_thread->list);
}
if(render_thread->cond) {
g_cond_free(render_thread->cond);
}
if(render_thread->lock) {
g_mutex_free(render_thread->lock);
}
} }
bool bool
@ -82,11 +117,12 @@ render_page(render_thread_t* render_thread, zathura_page_t* page)
return false; return false;
} }
g_static_mutex_lock(&(render_thread->lock)); g_mutex_lock(render_thread->lock);
if(!girara_list_contains(render_thread->list, page)) { if(!girara_list_contains(render_thread->list, page)) {
girara_list_append(render_thread->list, page); girara_list_append(render_thread->list, page);
} }
g_static_mutex_unlock(&(render_thread->lock)); g_cond_signal(render_thread->cond);
g_mutex_unlock(render_thread->lock);
return true; return true;
} }

View File

@ -13,7 +13,8 @@ typedef struct render_thread_s
{ {
girara_list_t* list; girara_list_t* list;
GThread* thread; GThread* thread;
GStaticMutex lock; GMutex* lock;
GCond* cond;
} render_thread_t; } render_thread_t;
render_thread_t* render_init(void); render_thread_t* render_init(void);