libresman

diff src/resman.c @ 11:bebc065a941f

doesn't work yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 07 Feb 2014 07:50:02 +0200
parents 4d18498a0078
children 84f55eab27cb
line diff
     1.1 --- a/src/resman.c	Wed Feb 05 02:01:49 2014 +0200
     1.2 +++ b/src/resman.c	Fri Feb 07 07:50:02 2014 +0200
     1.3 @@ -8,6 +8,7 @@
     1.4  #include "threadpool.h"
     1.5  
     1.6  struct resource {
     1.7 +	int id;
     1.8  	char *name;
     1.9  	void *data;
    1.10  	int result;	/* last callback-reported success/fail code */
    1.11 @@ -17,7 +18,7 @@
    1.12  };
    1.13  
    1.14  struct resman {
    1.15 -	struct resource *res;
    1.16 +	struct resource **res;
    1.17  	struct thread_pool *tpool;
    1.18  
    1.19  	resman_load_func load_func;
    1.20 @@ -74,8 +75,9 @@
    1.21  
    1.22  	for(i=0; i<dynarr_size(rman->res); i++) {
    1.23  		if(rman->destroy_func) {
    1.24 -			rman->destroy_func(rman->res[i].data, rman->destroy_func_cls);
    1.25 +			rman->destroy_func(i, rman->destroy_func_cls);
    1.26  		}
    1.27 +		free(rman->res[i]);
    1.28  	}
    1.29  	dynarr_free(rman->res);
    1.30  
    1.31 @@ -128,8 +130,7 @@
    1.32  
    1.33  	num_res = dynarr_size(rman->res);
    1.34  	for(i=0; i<num_res; i++) {
    1.35 -		struct resource *res = rman->res + i;
    1.36 -		int last_result;
    1.37 +		struct resource *res = rman->res[i];
    1.38  
    1.39  		pthread_mutex_lock(&res->done_lock);
    1.40  		if(!res->done_pending) {
    1.41 @@ -139,34 +140,39 @@
    1.42  
    1.43  		/* so a done callback *is* pending... */
    1.44  		res->done_pending = 0;
    1.45 -		last_result = res->result;
    1.46 +		rman->done_func(i, rman->done_func_cls);
    1.47  		pthread_mutex_unlock(&res->done_lock);
    1.48 -
    1.49 -		rman->done_func(last_result, res->data, rman->done_func_cls);
    1.50  	}
    1.51  	return 0;
    1.52  }
    1.53  
    1.54 +const char *resman_get_res_name(struct resman *rman, int res_id)
    1.55 +{
    1.56 +	if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
    1.57 +		return rman->res[res_id]->name;
    1.58 +	}
    1.59 +	return 0;
    1.60 +}
    1.61  
    1.62  void resman_set_res_data(struct resman *rman, int res_id, void *data)
    1.63  {
    1.64  	if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
    1.65 -		rman->res[res_id].data = data;
    1.66 +		rman->res[res_id]->data = data;
    1.67  	}
    1.68  }
    1.69  
    1.70  void *resman_get_res_data(struct resman *rman, int res_id)
    1.71  {
    1.72  	if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
    1.73 -		return rman->res[res_id].data;
    1.74 +		return rman->res[res_id]->data;
    1.75  	}
    1.76  	return 0;
    1.77  }
    1.78  
    1.79 -int resman_get_res_error(struct resman *rman, int res_id)
    1.80 +int resman_get_res_result(struct resman *rman, int res_id)
    1.81  {
    1.82  	if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
    1.83 -		return rman->res[res_id].result;
    1.84 +		return rman->res[res_id]->result;
    1.85  	}
    1.86  	return -1;
    1.87  }
    1.88 @@ -176,7 +182,7 @@
    1.89  	int i, sz = dynarr_size(rman->res);
    1.90  
    1.91  	for(i=0; i<sz; i++) {
    1.92 -		if(strcmp(rman->res[i].name, fname) == 0) {
    1.93 +		if(strcmp(rman->res[i]->name, fname) == 0) {
    1.94  			return i;
    1.95  		}
    1.96  	}
    1.97 @@ -186,21 +192,25 @@
    1.98  static int add_resource(struct resman *rman, const char *fname, void *data)
    1.99  {
   1.100  	int idx = dynarr_size(rman->res);
   1.101 +	struct resource *res;
   1.102 +	struct resource **tmparr;
   1.103  
   1.104 -	struct resource *tmp = dynarr_push(rman->res, 0);
   1.105 -	if(!tmp) {
   1.106 +	if(!(res = malloc(sizeof *res))) {
   1.107  		return -1;
   1.108  	}
   1.109 -	rman->res = tmp;
   1.110 +	res->id = idx;
   1.111 +	res->name = strdup(fname);
   1.112 +	assert(res->name);
   1.113 +	res->data = data;
   1.114  
   1.115 -	rman->res[idx].name = strdup(fname);
   1.116 -	assert(rman->res[idx].name);
   1.117 -
   1.118 -	rman->res[idx].data = data;
   1.119 +	if(!(tmparr = dynarr_push(rman->res, &res))) {
   1.120 +		free(res);
   1.121 +		return -1;
   1.122 +	}
   1.123 +	rman->res = tmparr;
   1.124  
   1.125  	/* start a loading job ... */
   1.126 -	tpool_add_work(rman->tpool, rman->res + idx);
   1.127 -
   1.128 +	tpool_add_work(rman->tpool, rman->res[idx]);
   1.129  	return idx;
   1.130  }
   1.131  
   1.132 @@ -212,7 +222,7 @@
   1.133  	struct resource *res = data;
   1.134  	struct resman *rman = cls;
   1.135  
   1.136 -	res->result = rman->load_func(res->name, res->data, rman->load_func_cls);
   1.137 +	res->result = rman->load_func(res->name, res->id, rman->load_func_cls);
   1.138  	pthread_mutex_lock(&res->done_lock);
   1.139  	res->done_pending = 1;
   1.140  	pthread_mutex_unlock(&res->done_lock);