libresman
diff src/resman.c @ 14:2fcd1fbb0d18
buggy piece of shit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 10 Feb 2014 12:11:02 +0200 |
parents | a42888d26839 |
children | c6073bf9fd38 |
line diff
1.1 --- a/src/resman.c Sat Feb 08 07:41:39 2014 +0200 1.2 +++ b/src/resman.c Mon Feb 10 12:11:02 2014 +0200 1.3 @@ -14,6 +14,7 @@ 1.4 int result; /* last callback-reported success/fail code */ 1.5 1.6 int done_pending; 1.7 + int delete_pending; 1.8 pthread_mutex_t done_lock; 1.9 }; 1.10 1.11 @@ -56,9 +57,16 @@ 1.12 1.13 int resman_init(struct resman *rman) 1.14 { 1.15 + const char *env; 1.16 + int num_threads = TPOOL_AUTO; 1.17 + 1.18 memset(rman, 0, sizeof *rman); 1.19 1.20 - if(!(rman->tpool = tpool_create(TPOOL_AUTO))) { 1.21 + if((env = getenv("RESMAN_THREADS"))) { 1.22 + num_threads = atoi(env); 1.23 + } 1.24 + 1.25 + if(!(rman->tpool = tpool_create(num_threads))) { 1.26 return -1; 1.27 } 1.28 tpool_set_work_func(rman->tpool, work_func, rman); 1.29 @@ -131,11 +139,27 @@ 1.30 { 1.31 int i, num_res; 1.32 1.33 + /* first check all the resources to see if any is pending deletion */ 1.34 + num_res = dynarr_size(rman->res); 1.35 + for(i=0; i<num_res; i++) { 1.36 + struct resource *res = rman->res[i]; 1.37 + if(!res) { 1.38 + continue; 1.39 + } 1.40 + 1.41 + if(res->delete_pending) { 1.42 + if(rman->destroy_func) { 1.43 + rman->destroy_func(i, rman->destroy_func_cls); 1.44 + } 1.45 + remove_resource(rman, i); 1.46 + } 1.47 + } 1.48 + 1.49 + 1.50 if(!rman->done_func) { 1.51 return 0; /* no done callback; there's no point in checking anything */ 1.52 } 1.53 1.54 - num_res = dynarr_size(rman->res); 1.55 for(i=0; i<num_res; i++) { 1.56 struct resource *res = rman->res[i]; 1.57 if(!res) { 1.58 @@ -278,8 +302,10 @@ 1.59 1.60 res->result = rman->load_func(res->name, res->id, rman->load_func_cls); 1.61 if(res->result == -1 && !rman->done_func) { 1.62 - /* if there's no done function and we got an error, remove the resource now */ 1.63 - remove_resource(rman, res->id); 1.64 + /* if there's no done function and we got an error, mark this 1.65 + * resource for deletion in the caller context 1.66 + */ 1.67 + res->delete_pending = 1; 1.68 return; 1.69 } 1.70