libresman

changeset 12:84f55eab27cb

ok now it sortof works, probably something is failing in the done callback mechanism
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 08 Feb 2014 04:21:08 +0200
parents bebc065a941f
children a42888d26839
files examples/imgthumbs/src/thumbs.c src/resman.c src/threadpool.c
diffstat 3 files changed, 46 insertions(+), 38 deletions(-) [+]
line diff
     1.1 --- a/examples/imgthumbs/src/thumbs.c	Fri Feb 07 07:50:02 2014 +0200
     1.2 +++ b/examples/imgthumbs/src/thumbs.c	Sat Feb 08 04:21:08 2014 +0200
     1.3 @@ -67,7 +67,7 @@
     1.4  
     1.5  		node->aspect = 1.0;/*(float)xsz / (float)ysz;*/
     1.6  
     1.7 -		resman_lookup(texman, node->fname, 0);
     1.8 +		resman_lookup(texman, node->fname, node);
     1.9  
    1.10  		/*if(!(pixels = img_load_pixels(node->fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
    1.11  			free(node->fname);
    1.12 @@ -131,11 +131,6 @@
    1.13  	glMatrixMode(GL_MODELVIEW);
    1.14  
    1.15  	while(thumbs) {
    1.16 -		if(!thumbs->tex) {
    1.17 -			thumbs = thumbs->next;
    1.18 -			continue;
    1.19 -		}
    1.20 -
    1.21  		glPushMatrix();
    1.22  		glTranslatef(x, y, 0);
    1.23  
    1.24 @@ -149,28 +144,34 @@
    1.25  		glTexCoord2f(0, 1); glVertex2f(0, 1);
    1.26  		glEnd();
    1.27  
    1.28 -		if(thumbs->aspect >= 1.0) {
    1.29 -			glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
    1.30 -			glScalef(1, 1.0 / thumbs->aspect, 1);
    1.31 -		} else {
    1.32 -			glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
    1.33 -			glScalef(thumbs->aspect, 1, 1);
    1.34 +		if(thumbs->tex) {
    1.35 +			if(thumbs->aspect >= 1.0) {
    1.36 +				glTranslatef(0, 0.5 - 0.5 / thumbs->aspect, 0);
    1.37 +				glScalef(1, 1.0 / thumbs->aspect, 1);
    1.38 +			} else {
    1.39 +				glTranslatef(0.5 - thumbs->aspect / 2.0, 0, 0);
    1.40 +				glScalef(thumbs->aspect, 1, 1);
    1.41 +			}
    1.42 +
    1.43 +			if(glIsTexture(thumbs->tex)) {
    1.44 +				glEnable(GL_TEXTURE_2D);
    1.45 +				glBindTexture(GL_TEXTURE_2D, thumbs->tex);
    1.46 +
    1.47 +				glBegin(GL_QUADS);
    1.48 +				glColor3f(1, 1, 1);
    1.49 +				glTexCoord2f(0, 0); glVertex2f(0, 0);
    1.50 +				glTexCoord2f(1, 0); glVertex2f(1, 0);
    1.51 +				glTexCoord2f(1, 1); glVertex2f(1, 1);
    1.52 +				glTexCoord2f(0, 1); glVertex2f(0, 1);
    1.53 +				glEnd();
    1.54 +			} else {
    1.55 +				fprintf(stderr, "invalid texture: %u\n", thumbs->tex);
    1.56 +			}
    1.57 +
    1.58 +			glPopMatrix();
    1.59 +			glDisable(GL_TEXTURE_2D);
    1.60  		}
    1.61  
    1.62 -		glEnable(GL_TEXTURE_2D);
    1.63 -		glBindTexture(GL_TEXTURE_2D, thumbs->tex);
    1.64 -
    1.65 -		glBegin(GL_QUADS);
    1.66 -		glColor3f(1, 1, 1);
    1.67 -		glTexCoord2f(0, 0); glVertex2f(0, 0);
    1.68 -		glTexCoord2f(1, 0); glVertex2f(1, 0);
    1.69 -		glTexCoord2f(1, 1); glVertex2f(1, 1);
    1.70 -		glTexCoord2f(0, 1); glVertex2f(0, 1);
    1.71 -		glEnd();
    1.72 -
    1.73 -		glPopMatrix();
    1.74 -		glDisable(GL_TEXTURE_2D);
    1.75 -
    1.76  		thumbs->layout_pos[0] = x;
    1.77  		thumbs->layout_pos[1] = y;
    1.78  		thumbs->layout_size[0] = thumb_sz;
    1.79 @@ -192,8 +193,7 @@
    1.80  
    1.81  static int load_res_texture(const char *fname, int id, void *cls)
    1.82  {
    1.83 -	struct resman *rman = cls;
    1.84 -	struct thumbnail *rdata = resman_get_res_data(rman, id);
    1.85 +	struct thumbnail *rdata = resman_get_res_data(texman, id);
    1.86  
    1.87  	assert(rdata);
    1.88  	if(!rdata->img) {
    1.89 @@ -202,7 +202,7 @@
    1.90  		}
    1.91  	}
    1.92  
    1.93 -	if(!img_load(rdata->img, fname) == -1) {
    1.94 +	if(img_load(rdata->img, fname) == -1) {
    1.95  		img_free(rdata->img);
    1.96  		return -1;
    1.97  	}
    1.98 @@ -210,21 +210,20 @@
    1.99  
   1.100  	/* set the resource's data to the loaded image, so that we can use
   1.101  	 * it in the done callback */
   1.102 -	resman_set_res_data(rman, id, rdata);
   1.103 +	resman_set_res_data(texman, id, rdata);
   1.104  	return 0;
   1.105  }
   1.106  
   1.107  static int done_res_texture(int id, void *cls)
   1.108  {
   1.109  	struct thumbnail *rdata;
   1.110 -	struct resman *rman = cls;
   1.111  
   1.112 -	rdata = resman_get_res_data(rman, id);
   1.113 +	rdata = resman_get_res_data(texman, id);
   1.114  
   1.115 -	if(resman_get_res_result(rman, id) != 0 || !rdata) {
   1.116 -		fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(rman, id));
   1.117 +	if(resman_get_res_result(texman, id) != 0 || !rdata) {
   1.118 +		fprintf(stderr, "failed to load resource %d (%s)\n", id, resman_get_res_name(texman, id));
   1.119  	} else {
   1.120 -		printf("done loading resource %d (%s)\n", id, resman_get_res_name(rman, id));
   1.121 +		printf("done loading resource %d (%s)\n", id, resman_get_res_name(texman, id));
   1.122  	}
   1.123  
   1.124  	if(!rdata->tex) {
   1.125 @@ -241,8 +240,7 @@
   1.126  
   1.127  static void free_res_texture(int id, void *cls)
   1.128  {
   1.129 -	struct resman *rman = cls;
   1.130 -	struct thumbnail *rdata = resman_get_res_data(rman, id);
   1.131 +	struct thumbnail *rdata = resman_get_res_data(texman, id);
   1.132  
   1.133  	if(rdata) {
   1.134  		if(rdata->tex) {
     2.1 --- a/src/resman.c	Fri Feb 07 07:50:02 2014 +0200
     2.2 +++ b/src/resman.c	Sat Feb 08 04:21:08 2014 +0200
     2.3 @@ -132,8 +132,10 @@
     2.4  	for(i=0; i<num_res; i++) {
     2.5  		struct resource *res = rman->res[i];
     2.6  
     2.7 +		printf("locking mutex %d\n", res->id);
     2.8  		pthread_mutex_lock(&res->done_lock);
     2.9  		if(!res->done_pending) {
    2.10 +			printf("  unlocking mutex %d\n", res->id);
    2.11  			pthread_mutex_unlock(&res->done_lock);
    2.12  			continue;
    2.13  		}
    2.14 @@ -141,6 +143,7 @@
    2.15  		/* so a done callback *is* pending... */
    2.16  		res->done_pending = 0;
    2.17  		rman->done_func(i, rman->done_func_cls);
    2.18 +		printf("  unlocking mutex %d\n", res->id);
    2.19  		pthread_mutex_unlock(&res->done_lock);
    2.20  	}
    2.21  	return 0;
    2.22 @@ -203,6 +206,8 @@
    2.23  	assert(res->name);
    2.24  	res->data = data;
    2.25  
    2.26 +	pthread_mutex_init(&res->done_lock, 0);
    2.27 +
    2.28  	if(!(tmparr = dynarr_push(rman->res, &res))) {
    2.29  		free(res);
    2.30  		return -1;
    2.31 @@ -223,7 +228,10 @@
    2.32  	struct resman *rman = cls;
    2.33  
    2.34  	res->result = rman->load_func(res->name, res->id, rman->load_func_cls);
    2.35 +
    2.36 +	printf("locking mutex %d\n", res->id);
    2.37  	pthread_mutex_lock(&res->done_lock);
    2.38  	res->done_pending = 1;
    2.39 +	printf("  unlocking mutex %d\n", res->id);
    2.40  	pthread_mutex_unlock(&res->done_lock);
    2.41  }
     3.1 --- a/src/threadpool.c	Fri Feb 07 07:50:02 2014 +0200
     3.2 +++ b/src/threadpool.c	Sat Feb 08 04:21:08 2014 +0200
     3.3 @@ -122,8 +122,10 @@
     3.4  		tpool->work_list_tail->next = node;
     3.5  		tpool->work_list_tail = node;
     3.6  	}
     3.7 +	pthread_mutex_unlock(&tpool->work_lock);
     3.8  
     3.9 -	pthread_mutex_unlock(&tpool->work_lock);
    3.10 +	/* wakeup all threads, there's work to do */
    3.11 +	pthread_cond_broadcast(&tpool->work_cond);
    3.12  	return 0;
    3.13  }
    3.14