34#include "xbps_api_impl.h"
39 xbps_dictionary_t dict;
42 unsigned int arraycount;
43 unsigned int *reserved;
44 pthread_mutex_t *reserved_lock;
45 unsigned int slicecount;
46 int (*fn)(
struct xbps_handle *, xbps_object_t,
const char *,
void *,
bool *);
59array_foreach_thread(
void *arg)
61 xbps_object_t obj, pkgd;
62 struct thread_data *thd = arg;
65 bool loop_done =
false;
66 unsigned i = thd->start;
67 unsigned int end = i + thd->slicecount;
69 while(i < thd->arraycount) {
71 for (; i < end && i < thd->arraycount; i++) {
72 obj = xbps_array_get(thd->array, i);
73 if (xbps_object_type(thd->dict) == XBPS_TYPE_DICTIONARY) {
74 pkgd = xbps_dictionary_get_keysym(thd->dict, obj);
75 key = xbps_dictionary_keysym_cstring_nocopy(obj);
77 if (strncmp(key,
"_XBPS_", 6) == 0)
83 rv = (*thd->fn)(thd->xhp, pkgd, key, thd->fn_arg, &loop_done);
84 if (rv != 0 || loop_done)
88 pthread_mutex_lock(thd->reserved_lock);
90 end = i + thd->slicecount;
92 pthread_mutex_unlock(thd->reserved_lock);
100 xbps_dictionary_t dict,
101 int (*fn)(
struct xbps_handle *, xbps_object_t,
const char *,
void *,
bool *),
104 struct thread_data *thd;
105 unsigned int arraycount, slicecount;
106 int rv = 0, error = 0, i, maxthreads;
107 unsigned int reserved;
108 pthread_mutex_t reserved_lock = PTHREAD_MUTEX_INITIALIZER;
112 if (xbps_object_type(array) != XBPS_TYPE_ARRAY)
115 arraycount = xbps_array_count(array);
119 maxthreads = (int)sysconf(_SC_NPROCESSORS_ONLN);
120 if (maxthreads <= 1 || arraycount <= 1)
123 thd = calloc(maxthreads,
sizeof(*thd));
127 if((
unsigned int)maxthreads >= arraycount) {
128 maxthreads = arraycount;
131 slicecount = arraycount / maxthreads;
132 if (slicecount > 32) {
137 reserved = slicecount * maxthreads;
139 for (i = 0; i < maxthreads; i++) {
140 thd[i].array = array;
145 thd[i].start = i * slicecount;
146 thd[i].reserved = &reserved;
147 thd[i].reserved_lock = &reserved_lock;
148 thd[i].slicecount = slicecount;
149 thd[i].arraycount = arraycount;
151 if ((rv = pthread_create(&thd[i].thread, NULL, array_foreach_thread, &thd[i])) != 0) {
158 for (
int c = 0; c < i; c++) {
159 if ((rv = pthread_join(thd[c].thread, NULL)) != 0)
164 pthread_mutex_destroy(&reserved_lock);
166 return error ? error : rv;
172 xbps_dictionary_t dict,
173 int (*fn)(
struct xbps_handle *, xbps_object_t,
const char *,
void *,
bool *),
176 xbps_dictionary_t pkgd;
180 bool loop_done =
false;
182 for (
unsigned int i = 0; i < xbps_array_count(array); i++) {
183 obj = xbps_array_get(array, i);
184 if (xbps_object_type(dict) == XBPS_TYPE_DICTIONARY) {
185 pkgd = xbps_dictionary_get_keysym(dict, obj);
186 key = xbps_dictionary_keysym_cstring_nocopy(obj);
188 if (strncmp(key,
"_XBPS_", 6) == 0)
194 rv = (*fn)(xhp, pkgd, key, arg, &loop_done);
195 if (rv != 0 || loop_done)
201xbps_object_iterator_t
206 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
209 array = xbps_dictionary_get(dict, key);
210 if (xbps_object_type(array) != XBPS_TYPE_ARRAY) {
215 return xbps_array_iterator(array);
219array_replace_dict(xbps_array_t array,
220 xbps_dictionary_t dict,
225 const char *pkgver, *pkgname;
227 assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
228 assert(xbps_object_type(dict) == XBPS_TYPE_DICTIONARY);
231 for (
unsigned int i = 0; i < xbps_array_count(array); i++) {
232 obj = xbps_array_get(array, i);
236 if (!xbps_dictionary_get_cstring_nocopy(obj,
"pkgver", &pkgver)) {
242 if (!xbps_array_set(array, i, dict)) {
249 xbps_dictionary_get_cstring_nocopy(obj,
"pkgname", &pkgname);
250 if (strcmp(pkgname, str) == 0) {
251 if (!xbps_array_set(array, i, dict)) {
263xbps_array_replace_dict_by_name(xbps_array_t array,
264 xbps_dictionary_t dict,
267 return array_replace_dict(array, dict, pkgver,
false);
271xbps_array_replace_dict_by_pattern(xbps_array_t array,
272 xbps_dictionary_t dict,
275 return array_replace_dict(array, dict, pattern,
true);
Generic XBPS structure handler for initialization.
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
int xbps_array_foreach_cb_multi(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t obj, const char *, void *arg, bool *done), void *arg)
int xbps_array_foreach_cb(struct xbps_handle *xhp, xbps_array_t array, xbps_dictionary_t dict, int(*fn)(struct xbps_handle *, xbps_object_t obj, const char *, void *arg, bool *done), void *arg)
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)