# HG changeset patch # User John Tsiombikas # Date 1392137304 -7200 # Node ID 43a9fe4a80eed7f51a05a31e09fd7c89f8880445 # Parent 0a789208498dc9e38de534727140a39c23647b6b# Parent 2b8281a146af8125325b0dc89d9a2742b2bfc52e merged diff -r 0a789208498d -r 43a9fe4a80ee examples/imgthumbs/src/thumbs.c --- a/examples/imgthumbs/src/thumbs.c Tue Feb 11 18:47:33 2014 +0200 +++ b/examples/imgthumbs/src/thumbs.c Tue Feb 11 18:48:24 2014 +0200 @@ -16,6 +16,7 @@ #endif struct resman *texman; +struct thumbnail *dbg; static int load_res_texture(const char *fname, int id, void *cls); static int done_res_texture(int id, void *cls); @@ -28,6 +29,7 @@ struct dirent *dent; /* allocate dummy head node */ struct thumbnail *list = calloc(1, sizeof *list); + dbg = list; if(!texman) { texman = resman_create(); @@ -89,6 +91,7 @@ node->next = list->next; node->prev = list; + list->next = node; } closedir(dir); @@ -123,7 +126,7 @@ float view_aspect; glGetIntegerv(GL_VIEWPORT, vp); - view_aspect = (float)(vp[2] - vp[0]) / (vp[3] - vp[1]); + view_aspect = (float)(vp[2] - vp[0]) / (float)(vp[3] - vp[1]); glMatrixMode(GL_PROJECTION); glPushMatrix(); @@ -134,6 +137,7 @@ thumbs = thumbs->next; /* skip dummy node */ while(thumbs) { + printf("drawing thumb: %s\n", thumbs->fname); glPushMatrix(); glTranslatef(x, y, 0); @@ -253,6 +257,8 @@ { struct thumbnail *thumb = resman_get_res_data(texman, id); + printf("deleting thumb %d: %s\n", id, thumb->fname); + if(thumb) { if(thumb->tex) { glDeleteTextures(1, &thumb->tex); diff -r 0a789208498d -r 43a9fe4a80ee src/resman.c --- a/src/resman.c Tue Feb 11 18:47:33 2014 +0200 +++ b/src/resman.c Tue Feb 11 18:48:24 2014 +0200 @@ -14,6 +14,7 @@ int result; /* last callback-reported success/fail code */ int done_pending; + int delete_pending; pthread_mutex_t done_lock; }; @@ -56,9 +57,16 @@ int resman_init(struct resman *rman) { + const char *env; + int num_threads = TPOOL_AUTO; + memset(rman, 0, sizeof *rman); - if(!(rman->tpool = tpool_create(TPOOL_AUTO))) { + if((env = getenv("RESMAN_THREADS"))) { + num_threads = atoi(env); + } + + if(!(rman->tpool = tpool_create(num_threads))) { return -1; } tpool_set_work_func(rman->tpool, work_func, rman); @@ -131,11 +139,27 @@ { int i, num_res; + /* first check all the resources to see if any is pending deletion */ + num_res = dynarr_size(rman->res); + for(i=0; ires[i]; + if(!res) { + continue; + } + + if(res->delete_pending) { + if(rman->destroy_func) { + rman->destroy_func(i, rman->destroy_func_cls); + } + remove_resource(rman, i); + } + } + + if(!rman->done_func) { return 0; /* no done callback; there's no point in checking anything */ } - num_res = dynarr_size(rman->res); for(i=0; ires[i]; if(!res) { @@ -278,8 +302,10 @@ res->result = rman->load_func(res->name, res->id, rman->load_func_cls); if(res->result == -1 && !rman->done_func) { - /* if there's no done function and we got an error, remove the resource now */ - remove_resource(rman, res->id); + /* if there's no done function and we got an error, mark this + * resource for deletion in the caller context + */ + res->delete_pending = 1; return; } diff -r 0a789208498d -r 43a9fe4a80ee src/threadpool.c --- a/src/threadpool.c Tue Feb 11 18:47:33 2014 +0200 +++ b/src/threadpool.c Tue Feb 11 18:48:24 2014 +0200 @@ -5,6 +5,7 @@ #include "threadpool.h" struct work_item { + int id; /* just for debugging messages */ void *data; struct work_item *next; }; @@ -106,6 +107,7 @@ int tpool_add_work(struct thread_pool *tpool, void *data) { struct work_item *node; + static int jcounter; if(!(node = alloc_node())) { fprintf(stderr, "%s: failed to allocate new work item node\n", __FUNCTION__); @@ -115,6 +117,9 @@ node->next = 0; pthread_mutex_lock(&tpool->work_lock); + node->id = jcounter++; + + printf("TPOOL: adding work item: %d\n", node->id); if(!tpool->work_list) { tpool->work_list = tpool->work_list_tail = node; @@ -132,8 +137,17 @@ static void *thread_func(void *tp) { + int i, tidx = -1; struct work_item *job; struct thread_pool *tpool = tp; + pthread_t tid = pthread_self(); + + for(i=0; inum_workers; i++) { + if(tpool[i].workers[i] == tid) { + tidx = i; + break; + } + } pthread_mutex_lock(&tpool->work_lock); for(;;) { @@ -143,11 +157,14 @@ continue; /* spurious wakeup, go back to sleep */ } + printf("TPOOL: worker %d start job: %d\n", tidx, job->id); + job = tpool->work_list; tpool->work_list = tpool->work_list->next; tpool->work_func(job->data, tpool->cls); + printf("TPOOL: worker %d completed job: %d\n", tidx, job->id); free_node(job); } pthread_mutex_unlock(&tpool->work_lock);