# HG changeset patch # User John Tsiombikas # Date 1392235528 -7200 # Node ID fe0dbdfbe403b0f2bdd6ec9d6dafe55d6880eb20 # Parent c6073bf9fd38131d39cbf5184753a4325137baa8 file modification monitoring and reload done on linux diff -r c6073bf9fd38 -r fe0dbdfbe403 examples/imgthumbs/src/thumbs.c --- a/examples/imgthumbs/src/thumbs.c Wed Feb 12 16:02:12 2014 +0200 +++ b/examples/imgthumbs/src/thumbs.c Wed Feb 12 22:05:28 2014 +0200 @@ -22,6 +22,11 @@ struct dirent *dent; /* allocate dummy head node */ struct thumbnail *list = calloc(1, sizeof *list); + char *env; + + if((env = getenv("RESMAN_LOAD_ASYNC"))) { + dbg_load_async = atoi(env); + } if(!texman) { texman = resman_create(); @@ -85,6 +90,7 @@ list->next = node; } node->list = list; + node->load_count = 0; } closedir(dir); @@ -243,11 +249,13 @@ thumb->img->width, thumb->img->height, 0, img_glfmt(thumb->img), img_gltype(thumb->img), thumb->img->pixels); - /* and add it to the list of thumbnails */ - thumb->prev = thumb->list; - thumb->next = thumb->list->next; - if(thumb->list->next) thumb->list->next->prev = thumb; - thumb->list->next = thumb; + /* and add it to the list of thumbnails (if it's the first loading) */ + if(resman_get_res_load_count(texman, id) == 0) { + thumb->prev = thumb->list; + thumb->next = thumb->list->next; + if(thumb->list->next) thumb->list->next->prev = thumb; + thumb->list->next = thumb; + } return 0; } diff -r c6073bf9fd38 -r fe0dbdfbe403 examples/imgthumbs/src/thumbs.h --- a/examples/imgthumbs/src/thumbs.h Wed Feb 12 16:02:12 2014 +0200 +++ b/examples/imgthumbs/src/thumbs.h Wed Feb 12 22:05:28 2014 +0200 @@ -15,6 +15,8 @@ struct thumbnail *next, *prev; struct thumbnail *list; /* pointer to the list this thumbnail belongs to */ + + int load_count; }; struct thumbnail *create_thumbs(const char *dirpath); diff -r c6073bf9fd38 -r fe0dbdfbe403 resman.def --- a/resman.def Wed Feb 12 16:02:12 2014 +0200 +++ b/resman.def Wed Feb 12 22:05:28 2014 +0200 @@ -18,4 +18,5 @@ resman_get_res_name resman_set_res_data resman_get_res_data - resman_get_res_result \ No newline at end of file + resman_get_res_result + resman_get_res_load_count diff -r c6073bf9fd38 -r fe0dbdfbe403 src/rbtree.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rbtree.c Wed Feb 12 22:05:28 2014 +0200 @@ -0,0 +1,494 @@ +#include +#include +#include +#include +#include "rbtree.h" + +#define INT2PTR(x) ((void*)(intptr_t)(x)) +#define PTR2INT(x) ((int)(intptr_t)(x)) + +struct rbtree { + struct rbnode *root; + + rb_alloc_func_t alloc; + rb_free_func_t free; + + rb_cmp_func_t cmp; + rb_del_func_t del; + void *del_cls; + + struct rbnode *rstack, *iter; +}; + +static int cmpaddr(const void *ap, const void *bp); +static int cmpint(const void *ap, const void *bp); + +static int count_nodes(struct rbnode *node); +static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls); +static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data); +static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key); +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/ +static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls); + +struct rbtree *rb_create(rb_cmp_func_t cmp_func) +{ + struct rbtree *rb; + + if(!(rb = malloc(sizeof *rb))) { + return 0; + } + if(rb_init(rb, cmp_func) == -1) { + free(rb); + return 0; + } + return rb; +} + +void rb_free(struct rbtree *rb) +{ + rb_destroy(rb); + free(rb); +} + + +int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func) +{ + memset(rb, 0, sizeof *rb); + + if(cmp_func == RB_KEY_INT) { + rb->cmp = cmpint; + } else if(cmp_func == RB_KEY_STRING) { + rb->cmp = (rb_cmp_func_t)strcmp; + } else { + rb->cmp = cmpaddr; + } + + rb->alloc = malloc; + rb->free = free; + return 0; +} + +void rb_destroy(struct rbtree *rb) +{ + del_tree(rb->root, rb->del, rb->del_cls); +} + +void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free) +{ + rb->alloc = alloc; + rb->free = free; +} + + +void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func) +{ + rb->cmp = func; +} + +void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls) +{ + rb->del = func; + rb->del_cls = cls; +} + + +void rb_clear(struct rbtree *rb) +{ + del_tree(rb->root, rb->del, rb->del_cls); + rb->root = 0; +} + +int rb_copy(struct rbtree *dest, struct rbtree *src) +{ + struct rbnode *node; + + rb_clear(dest); + rb_begin(src); + while((node = rb_next(src))) { + if(rb_insert(dest, node->key, node->data) == -1) { + return -1; + } + } + return 0; +} + +int rb_size(struct rbtree *rb) +{ + return count_nodes(rb->root); +} + +int rb_insert(struct rbtree *rb, void *key, void *data) +{ + rb->root = insert(rb, rb->root, key, data); + rb->root->red = 0; + return 0; +} + +int rb_inserti(struct rbtree *rb, int key, void *data) +{ + rb->root = insert(rb, rb->root, INT2PTR(key), data); + rb->root->red = 0; + return 0; +} + + +int rb_delete(struct rbtree *rb, void *key) +{ + rb->root = delete(rb, rb->root, key); + rb->root->red = 0; + return 0; +} + +int rb_deletei(struct rbtree *rb, int key) +{ + rb->root = delete(rb, rb->root, INT2PTR(key)); + rb->root->red = 0; + return 0; +} + + +void *rb_find(struct rbtree *rb, void *key) +{ + struct rbnode *node = rb->root; + + while(node) { + int cmp = rb->cmp(key, node->key); + if(cmp == 0) { + return node->data; + } + node = cmp < 0 ? node->left : node->right; + } + return 0; +} + +void *rb_findi(struct rbtree *rb, int key) +{ + return rb_find(rb, INT2PTR(key)); +} + + +void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls) +{ + traverse(rb->root, func, cls); +} + + +struct rbnode *rb_root(struct rbtree *rb) +{ + return rb->root; +} + +void rb_begin(struct rbtree *rb) +{ + rb->rstack = 0; + rb->iter = rb->root; +} + +#define push(sp, x) ((x)->next = (sp), (sp) = (x)) +#define pop(sp) ((sp) = (sp)->next) +#define top(sp) (sp) + +struct rbnode *rb_next(struct rbtree *rb) +{ + struct rbnode *res = 0; + + while(rb->rstack || rb->iter) { + if(rb->iter) { + push(rb->rstack, rb->iter); + rb->iter = rb->iter->left; + } else { + rb->iter = top(rb->rstack); + pop(rb->rstack); + res = rb->iter; + rb->iter = rb->iter->right; + break; + } + } + return res; +} + +void *rb_node_key(struct rbnode *node) +{ + return node ? node->key : 0; +} + +int rb_node_keyi(struct rbnode *node) +{ + return node ? PTR2INT(node->key) : 0; +} + +void *rb_node_data(struct rbnode *node) +{ + return node ? node->data : 0; +} + +static int cmpaddr(const void *ap, const void *bp) +{ + return ap < bp ? -1 : (ap > bp ? 1 : 0); +} + +static int cmpint(const void *ap, const void *bp) +{ + return PTR2INT(ap) - PTR2INT(bp); +} + + +/* ---- left-leaning 2-3 red-black implementation ---- */ + +/* helper prototypes */ +static int is_red(struct rbnode *tree); +static void color_flip(struct rbnode *tree); +static struct rbnode *rot_left(struct rbnode *a); +static struct rbnode *rot_right(struct rbnode *a); +static struct rbnode *find_min(struct rbnode *tree); +static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree); +/*static struct rbnode *move_red_right(struct rbnode *tree);*/ +static struct rbnode *move_red_left(struct rbnode *tree); +static struct rbnode *fix_up(struct rbnode *tree); + +static int count_nodes(struct rbnode *node) +{ + if(!node) + return 0; + + return 1 + count_nodes(node->left) + count_nodes(node->right); +} + +static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls) +{ + if(!node) + return; + + del_tree(node->left, delfunc, cls); + del_tree(node->right, delfunc, cls); + + if(delfunc) { + delfunc(node, cls); + } + free(node); +} + +static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data) +{ + int cmp; + + if(!tree) { + struct rbnode *node = rb->alloc(sizeof *node); + node->red = 1; + node->key = key; + node->data = data; + node->left = node->right = 0; + return node; + } + + cmp = rb->cmp(key, tree->key); + + if(cmp < 0) { + tree->left = insert(rb, tree->left, key, data); + } else if(cmp > 0) { + tree->right = insert(rb, tree->right, key, data); + } else { + tree->data = data; + } + + /* fix right-leaning reds */ + if(is_red(tree->right)) { + tree = rot_left(tree); + } + /* fix two reds in a row */ + if(is_red(tree->left) && is_red(tree->left->left)) { + tree = rot_right(tree); + } + + /* if 4-node, split it by color inversion */ + if(is_red(tree->left) && is_red(tree->right)) { + color_flip(tree); + } + + return tree; +} + +static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key) +{ + int cmp; + + if(!tree) { + return 0; + } + + cmp = rb->cmp(key, tree->key); + + if(cmp < 0) { + if(!is_red(tree->left) && !is_red(tree->left->left)) { + tree = move_red_left(tree); + } + tree->left = delete(rb, tree->left, key); + } else { + /* need reds on the right */ + if(is_red(tree->left)) { + tree = rot_right(tree); + } + + /* found it at the bottom (XXX what certifies left is null?) */ + if(cmp == 0 && !tree->right) { + if(rb->del) { + rb->del(tree, rb->del_cls); + } + rb->free(tree); + return 0; + } + + if(!is_red(tree->right) && !is_red(tree->right->left)) { + tree = move_red_left(tree); + } + + if(key == tree->key) { + struct rbnode *rmin = find_min(tree->right); + tree->key = rmin->key; + tree->data = rmin->data; + tree->right = del_min(rb, tree->right); + } else { + tree->right = delete(rb, tree->right, key); + } + } + + return fix_up(tree); +} + +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) +{ + int cmp; + + if(!node) + return 0; + + if((cmp = rb->cmp(key, node->key)) == 0) { + return node; + } + return find(rb, cmp < 0 ? node->left : node->right, key); +}*/ + +static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls) +{ + if(!node) + return; + + traverse(node->left, func, cls); + func(node, cls); + traverse(node->right, func, cls); +} + +/* helpers */ + +static int is_red(struct rbnode *tree) +{ + return tree && tree->red; +} + +static void color_flip(struct rbnode *tree) +{ + tree->red = !tree->red; + tree->left->red = !tree->left->red; + tree->right->red = !tree->right->red; +} + +static struct rbnode *rot_left(struct rbnode *a) +{ + struct rbnode *b = a->right; + a->right = b->left; + b->left = a; + b->red = a->red; + a->red = 1; + return b; +} + +static struct rbnode *rot_right(struct rbnode *a) +{ + struct rbnode *b = a->left; + a->left = b->right; + b->right = a; + b->red = a->red; + a->red = 1; + return b; +} + +static struct rbnode *find_min(struct rbnode *tree) +{ + struct rbnode *node; + + if(!tree) + return 0; + + while(node->left) { + node = node->left; + } + return node; +} + +static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree) +{ + if(!tree->left) { + if(rb->del) { + rb->del(tree->left, rb->del_cls); + } + rb->free(tree->left); + return 0; + } + + /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */ + if(!is_red(tree->left) && !is_red(tree->left->left)) { + tree = move_red_left(tree); + } + tree->left = del_min(rb, tree->left); + + /* fix right-reds, red-reds, and split 4-nodes on the way up */ + return fix_up(tree); +} + +#if 0 +/* push a red link on this node to the right */ +static struct rbnode *move_red_right(struct rbnode *tree) +{ + /* flipping it makes both children go red, so we have a red to the right */ + color_flip(tree); + + /* if after the flip we've got a red-red situation to the left, fix it */ + if(is_red(tree->left->left)) { + tree = rot_right(tree); + color_flip(tree); + } + return tree; +} +#endif + +/* push a red link on this node to the left */ +static struct rbnode *move_red_left(struct rbnode *tree) +{ + /* flipping it makes both children go red, so we have a red to the left */ + color_flip(tree); + + /* if after the flip we've got a red-red on the right-left, fix it */ + if(is_red(tree->right->left)) { + tree->right = rot_right(tree->right); + tree = rot_left(tree); + color_flip(tree); + } + return tree; +} + +static struct rbnode *fix_up(struct rbnode *tree) +{ + /* fix right-leaning */ + if(is_red(tree->right)) { + tree = rot_left(tree); + } + /* change invalid red-red pairs into a proper 4-node */ + if(is_red(tree->left) && is_red(tree->left->left)) { + tree = rot_right(tree); + } + /* split 4-nodes */ + if(is_red(tree->left) && is_red(tree->right)) { + color_flip(tree); + } + return tree; +} diff -r c6073bf9fd38 -r fe0dbdfbe403 src/rbtree.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rbtree.h Wed Feb 12 22:05:28 2014 +0200 @@ -0,0 +1,71 @@ +#ifndef RBTREE_H_ +#define RBTREE_H_ + +struct rbtree; + + +struct rbnode { + void *key, *data; + int red; + struct rbnode *left, *right; + struct rbnode *next; /* for iterator stack */ +}; + + +typedef void *(*rb_alloc_func_t)(size_t); +typedef void (*rb_free_func_t)(void*); + +typedef int (*rb_cmp_func_t)(const void*, const void*); +typedef void (*rb_del_func_t)(struct rbnode*, void*); + +#define RB_KEY_ADDR (rb_cmp_func_t)(0) +#define RB_KEY_INT (rb_cmp_func_t)(1) +#define RB_KEY_STRING (rb_cmp_func_t)(3) + + +#ifdef __cplusplus +extern "C" { +#endif + +struct rbtree *rb_create(rb_cmp_func_t cmp_func); +void rb_free(struct rbtree *rb); + +int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func); +void rb_destroy(struct rbtree *rb); + +void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free); +void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func); +void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls); +/* TODO add user deep copy function */ + +void rb_clear(struct rbtree *rb); +int rb_copy(struct rbtree *dest, struct rbtree *src); + +int rb_size(struct rbtree *rb); + +int rb_insert(struct rbtree *rb, void *key, void *data); +int rb_inserti(struct rbtree *rb, int key, void *data); + +int rb_delete(struct rbtree *rb, void *key); +int rb_deletei(struct rbtree *rb, int key); + +void *rb_find(struct rbtree *rb, void *key); +void *rb_findi(struct rbtree *rb, int key); + +void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls); + +struct rbnode *rb_root(struct rbtree *rb); + +void rb_begin(struct rbtree *rb); +struct rbnode *rb_next(struct rbtree *rb); + +void *rb_node_key(struct rbnode *node); +int rb_node_keyi(struct rbnode *node); +void *rb_node_data(struct rbnode *node); + +#ifdef __cplusplus +} +#endif + + +#endif /* RBTREE_H_ */ diff -r c6073bf9fd38 -r fe0dbdfbe403 src/resman.c --- a/src/resman.c Wed Feb 12 16:02:12 2014 +0200 +++ b/src/resman.c Wed Feb 12 22:05:28 2014 +0200 @@ -5,8 +5,15 @@ #include #include "resman.h" #include "dynarr.h" +#include "rbtree.h" #include "threadpool.h" +#ifdef __linux__ +#include +#include +#include +#endif + struct resource { int id; char *name; @@ -15,7 +22,17 @@ int done_pending; int delete_pending; - pthread_mutex_t done_lock; + pthread_mutex_t lock; + + int num_loads; /* number of loads up to now */ + + /* file change monitoring */ +#ifdef __WIN32__ + HANDLE nhandle; +#endif +#ifdef __linux__ + int nfd; +#endif }; struct resman { @@ -31,6 +48,13 @@ void *load_func_cls; void *done_func_cls; void *destroy_func_cls; + + /* file change monitoring */ + struct rbtree *nresmap; + struct rbtree *modset; +#ifdef __linux__ + int inotify_fd; +#endif }; @@ -39,6 +63,15 @@ static void remove_resource(struct resman *rman, int idx); static void work_func(void *data, void *cls); +/* file modification watching */ +static int init_file_monitor(struct resman *rman); +static void destroy_file_monitor(struct resman *rman); +static int start_watch(struct resman *rman, struct resource *res); +static void stop_watch(struct resman *rman, struct resource *res); +static void check_watch(struct resman *rman); +static void reload_modified(struct rbnode *node, void *cls); + + struct resman *resman_create(void) { struct resman *rman = malloc(sizeof *rman); @@ -66,6 +99,10 @@ num_threads = atoi(env); } + if(init_file_monitor(rman) == -1) { + return -1; + } + if(!(rman->tpool = tpool_create(num_threads))) { return -1; } @@ -96,6 +133,8 @@ tpool_free(rman->tpool); + destroy_file_monitor(rman); + pthread_mutex_destroy(&rman->lock); } @@ -156,6 +195,10 @@ } + /* then check for modified files */ + check_watch(rman); + + if(!rman->done_func) { return 0; /* no done callback; there's no point in checking anything */ } @@ -166,21 +209,29 @@ continue; } - pthread_mutex_lock(&res->done_lock); + pthread_mutex_lock(&res->lock); if(!res->done_pending) { - pthread_mutex_unlock(&res->done_lock); + pthread_mutex_unlock(&res->lock); continue; } /* so a done callback *is* pending... */ res->done_pending = 0; if(rman->done_func(i, rman->done_func_cls) == -1) { - /* done-func returned -1, so let's remove the resource */ - pthread_mutex_unlock(&res->done_lock); - remove_resource(rman, i); - continue; + /* done-func returned -1, so let's remove the resource + * but only if this was the first load. Otherwise keep it + * around in case it gets valid again... + */ + if(res->num_loads == 0) { + pthread_mutex_unlock(&res->lock); + remove_resource(rman, i); + continue; + } } - pthread_mutex_unlock(&res->done_lock); + res->num_loads++; + + start_watch(rman, res); /* start watching the file for modifications */ + pthread_mutex_unlock(&res->lock); } return 0; } @@ -216,6 +267,14 @@ return -1; } +int resman_get_res_load_count(struct resman *rman, int res_id) +{ + if(res_id >= 0 && res_id < dynarr_size(rman->res)) { + return rman->res[res_id]->num_loads; + } + return -1; +} + static int find_resource(struct resman *rman, const char *fname) { int i, sz = dynarr_size(rman->res); @@ -243,8 +302,7 @@ res->name = strdup(fname); assert(res->name); res->data = data; - pthread_mutex_init(&res->done_lock, 0); - + pthread_mutex_init(&res->lock, 0); /* check to see if there's an emtpy (previously erased) slot */ for(i=0; ires[idx]); + if(rman->destroy_func) { rman->destroy_func(idx, rman->destroy_func_cls); } - pthread_mutex_destroy(&rman->res[idx]->done_lock); + pthread_mutex_destroy(&rman->res[idx]->lock); free(rman->res[idx]); rman->res[idx] = 0; @@ -296,16 +356,124 @@ struct resource *res = data; struct resman *rman = cls; + pthread_mutex_lock(&res->lock); + 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, mark this - * resource for deletion in the caller context - */ - res->delete_pending = 1; + if(!rman->done_func) { + if(res->result == -1) { + /* if there's no done function and we got an error, mark this + * resource for deletion in the caller context. But only if this + * is the first load of this resource. + */ + if(res->num_loads == 0) { + res->delete_pending = 1; + } + } else { + /* succeded, start a watch */ + if(res->nfd <= 0) { + start_watch(rman, res); + } + } + } else { + /* if we have a done_func, mark this resource as done */ + res->done_pending = 1; + } + pthread_mutex_unlock(&res->lock); +} + +static int init_file_monitor(struct resman *rman) +{ + int fd; + + if((fd = inotify_init()) == -1) { + return -1; + } + /* set non-blocking flag, to allow polling by reading */ + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); + rman->inotify_fd = fd; + + /* create the fd->resource map */ + rman->nresmap = rb_create(RB_KEY_INT); + /* create the modified set */ + rman->modset = rb_create(RB_KEY_INT); + return 0; +} + +static void destroy_file_monitor(struct resman *rman) +{ + rb_free(rman->nresmap); + rb_free(rman->modset); + + if(rman->inotify_fd >= 0) { + close(rman->inotify_fd); + rman->inotify_fd = -1; + } +} + +static int start_watch(struct resman *rman, struct resource *res) +{ + int fd; + + if((fd = inotify_add_watch(rman->inotify_fd, res->name, IN_MODIFY)) == -1) { + return -1; + } + printf("started watching file \"%s\" for modification (fd %d)\n", res->name, fd); + rb_inserti(rman->nresmap, fd, res); + + res->nfd = fd; + return 0; +} + +static void stop_watch(struct resman *rman, struct resource *res) +{ + if(res->nfd > 0) { + rb_deletei(rman->nresmap, res->nfd); + inotify_rm_watch(rman->inotify_fd, res->nfd); + } +} + +static void check_watch(struct resman *rman) +{ + char buf[512]; + struct inotify_event *ev; + int sz, evsize; + + while((sz = read(rman->inotify_fd, buf, sizeof buf)) > 0) { + ev = (struct inotify_event*)buf; + while(sz > 0) { + if(ev->mask & IN_MODIFY) { + /* add the file descriptor to the modified set */ + rb_inserti(rman->modset, ev->wd, 0); + } + + evsize = sizeof *ev + ev->len; + sz -= evsize; + ev += evsize; + } + } + + /* for each item in the modified set, start a new job to reload it */ + rb_foreach(rman->modset, reload_modified, rman); + rb_clear(rman->modset); +} + +/* this is called for each item in the modified set (see above) */ +static void reload_modified(struct rbnode *node, void *cls) +{ + int watch_fd; + struct resource *res; + struct resman *rman = cls; + + watch_fd = rb_node_keyi(node); + + if(!(res = rb_findi(rman->nresmap, watch_fd))) { + fprintf(stderr, "%s: can't find resource for watch descriptor: %d\n", + __FUNCTION__, watch_fd); return; } + assert(watch_fd == res->nfd); - pthread_mutex_lock(&res->done_lock); - res->done_pending = 1; - pthread_mutex_unlock(&res->done_lock); + printf("file \"%s\" modified (fd %d)\n", res->name, rb_node_keyi(node)); + + tpool_add_work(rman->tpool, res); } diff -r c6073bf9fd38 -r fe0dbdfbe403 src/resman.h --- a/src/resman.h Wed Feb 12 16:02:12 2014 +0200 +++ b/src/resman.h Wed Feb 12 22:05:28 2014 +0200 @@ -37,6 +37,8 @@ int resman_get_res_result(struct resman *rman, int res_id); +int resman_get_res_load_count(struct resman *rman, int res_id); + #ifdef __cplusplus } #endif diff -r c6073bf9fd38 -r fe0dbdfbe403 src/threadpool.c --- a/src/threadpool.c Wed Feb 12 16:02:12 2014 +0200 +++ b/src/threadpool.c Wed Feb 12 22:05:28 2014 +0200 @@ -164,23 +164,28 @@ } } - pthread_mutex_lock(&tpool->work_lock); for(;;) { + int job_id; + void *data; + + pthread_mutex_lock(&tpool->work_lock); /* while there aren't any work items to do go to sleep on the condvar */ - pthread_cond_wait(&tpool->work_cond, &tpool->work_lock); - if(!tpool->work_list) { - continue; /* spurious wakeup, go back to sleep */ + while(!tpool->work_list) { + pthread_cond_wait(&tpool->work_cond, &tpool->work_lock); } job = tpool->work_list; tpool->work_list = tpool->work_list->next; - printf("TPOOL: worker %d start job: %d\n", tidx, job->id); - tpool->work_func(job->data, tpool->cls); - printf("TPOOL: worker %d completed job: %d\n", tidx, job->id); + job_id = job->id; + data = job->data; free_node(job); + pthread_mutex_unlock(&tpool->work_lock); + + printf("TPOOL: worker %d start job: %d\n", tidx, job_id); + tpool->work_func(data, tpool->cls); + printf("TPOOL: worker %d completed job: %d\n", tidx, job_id); } - pthread_mutex_unlock(&tpool->work_lock); return 0; }