libresman

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <pthread.h>
6 #include "resman.h"
7 #include "dynarr.h"
8 #include "threadpool.h"
10 struct resource {
11 int id;
12 char *name;
13 void *data;
14 int result; /* last callback-reported success/fail code */
16 int done_pending;
17 pthread_mutex_t done_lock;
18 };
20 struct resman {
21 struct resource **res;
22 struct thread_pool *tpool;
24 resman_load_func load_func;
25 resman_done_func done_func;
26 resman_destroy_func destroy_func;
28 void *load_func_cls;
29 void *done_func_cls;
30 void *destroy_func_cls;
31 };
34 static int find_resource(struct resman *rman, const char *fname);
35 static int add_resource(struct resman *rman, const char *fname, void *data);
36 static void work_func(void *data, void *cls);
38 struct resman *resman_create(void)
39 {
40 struct resman *rman = malloc(sizeof *rman);
41 if(resman_init(rman) == -1) {
42 free(rman);
43 return 0;
44 }
45 return rman;
46 }
48 void resman_free(struct resman *rman)
49 {
50 resman_destroy(rman);
51 free(rman);
52 }
54 int resman_init(struct resman *rman)
55 {
56 memset(rman, 0, sizeof *rman);
58 if(!(rman->tpool = tpool_create(TPOOL_AUTO))) {
59 return -1;
60 }
61 tpool_set_work_func(rman->tpool, work_func, rman);
63 if(!(rman->res = dynarr_alloc(0, sizeof *rman->res))) {
64 tpool_free(rman->tpool);
65 return -1;
66 }
68 return 0;
69 }
71 void resman_destroy(struct resman *rman)
72 {
73 int i;
74 if(!rman) return;
76 for(i=0; i<dynarr_size(rman->res); i++) {
77 if(rman->destroy_func) {
78 rman->destroy_func(i, rman->destroy_func_cls);
79 }
80 free(rman->res[i]);
81 }
82 dynarr_free(rman->res);
84 tpool_free(rman->tpool);
85 }
88 void resman_set_load_func(struct resman *rman, resman_load_func func, void *cls)
89 {
90 rman->load_func = func;
91 rman->load_func_cls = cls;
92 }
94 void resman_set_done_func(struct resman *rman, resman_done_func func, void *cls)
95 {
96 rman->done_func = func;
97 rman->done_func_cls = cls;
98 }
100 void resman_set_destroy_func(struct resman *rman, resman_destroy_func func, void *cls)
101 {
102 rman->destroy_func = func;
103 rman->destroy_func_cls = cls;
104 }
106 int resman_lookup(struct resman *rman, const char *fname, void *data)
107 {
108 int ridx;
110 if((ridx = find_resource(rman, fname)) != -1) {
111 return ridx;
112 }
114 /* resource not found, create a new one and start a loading job */
115 return add_resource(rman, fname, data);
116 }
118 void resman_wait(struct resman *rman, int id)
119 {
120 /* TODO */
121 }
123 int resman_poll(struct resman *rman)
124 {
125 int i, num_res;
127 if(!rman->done_func) {
128 return 0; /* no done callback; there's no point in checking anything */
129 }
131 num_res = dynarr_size(rman->res);
132 for(i=0; i<num_res; i++) {
133 struct resource *res = rman->res[i];
135 pthread_mutex_lock(&res->done_lock);
136 if(!res->done_pending) {
137 pthread_mutex_unlock(&res->done_lock);
138 continue;
139 }
141 /* so a done callback *is* pending... */
142 res->done_pending = 0;
143 rman->done_func(i, rman->done_func_cls);
144 pthread_mutex_unlock(&res->done_lock);
145 }
146 return 0;
147 }
149 const char *resman_get_res_name(struct resman *rman, int res_id)
150 {
151 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
152 return rman->res[res_id]->name;
153 }
154 return 0;
155 }
157 void resman_set_res_data(struct resman *rman, int res_id, void *data)
158 {
159 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
160 rman->res[res_id]->data = data;
161 }
162 }
164 void *resman_get_res_data(struct resman *rman, int res_id)
165 {
166 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
167 return rman->res[res_id]->data;
168 }
169 return 0;
170 }
172 int resman_get_res_result(struct resman *rman, int res_id)
173 {
174 if(res_id >= 0 && res_id < dynarr_size(rman->res)) {
175 return rman->res[res_id]->result;
176 }
177 return -1;
178 }
180 static int find_resource(struct resman *rman, const char *fname)
181 {
182 int i, sz = dynarr_size(rman->res);
184 for(i=0; i<sz; i++) {
185 if(strcmp(rman->res[i]->name, fname) == 0) {
186 return i;
187 }
188 }
189 return -1;
190 }
192 static int add_resource(struct resman *rman, const char *fname, void *data)
193 {
194 int idx = dynarr_size(rman->res);
195 struct resource *res;
196 struct resource **tmparr;
198 if(!(res = malloc(sizeof *res))) {
199 return -1;
200 }
201 res->id = idx;
202 res->name = strdup(fname);
203 assert(res->name);
204 res->data = data;
206 if(!(tmparr = dynarr_push(rman->res, &res))) {
207 free(res);
208 return -1;
209 }
210 rman->res = tmparr;
212 /* start a loading job ... */
213 tpool_add_work(rman->tpool, rman->res[idx]);
214 return idx;
215 }
217 /* this is the background work function which handles all the
218 * first-stage resource loading...
219 */
220 static void work_func(void *data, void *cls)
221 {
222 struct resource *res = data;
223 struct resman *rman = cls;
225 res->result = rman->load_func(res->name, res->id, rman->load_func_cls);
226 pthread_mutex_lock(&res->done_lock);
227 res->done_pending = 1;
228 pthread_mutex_unlock(&res->done_lock);
229 }