@@ -26,6 +26,7 @@
struct ptvar {
pthread_key_t key;
pthread_mutex_t lock;
+ ptvar_init_fn init_fn;
size_t nr_used;
size_t nr_counters;
size_t data_size;
@@ -38,6 +39,7 @@ int
ptvar_alloc(
size_t nr,
size_t size,
+ ptvar_init_fn init_fn,
struct ptvar **pptv)
{
struct ptvar *ptv;
@@ -58,6 +60,7 @@ ptvar_alloc(
ptv->data_size = size;
ptv->nr_counters = nr;
ptv->nr_used = 0;
+ ptv->init_fn = init_fn;
memset(ptv->data, 0, nr * size);
ret = -pthread_mutex_init(&ptv->lock, NULL);
if (ret)
@@ -98,11 +101,15 @@ ptvar_get(
if (!p) {
pthread_mutex_lock(&ptv->lock);
assert(ptv->nr_used < ptv->nr_counters);
- p = &ptv->data[(ptv->nr_used++) * ptv->data_size];
+ p = &ptv->data[ptv->nr_used * ptv->data_size];
ret = -pthread_setspecific(ptv->key, p);
if (ret)
goto out_unlock;
+ ptv->nr_used++;
pthread_mutex_unlock(&ptv->lock);
+
+ if (ptv->init_fn)
+ ptv->init_fn(p);
}
*retp = 0;
return p;
@@ -8,7 +8,9 @@
struct ptvar;
-int ptvar_alloc(size_t nr, size_t size, struct ptvar **pptv);
+typedef void (*ptvar_init_fn)(void *data);
+int ptvar_alloc(size_t nr, size_t size, ptvar_init_fn init_fn,
+ struct ptvar **pptv);
void ptvar_free(struct ptvar *ptv);
void *ptvar_get(struct ptvar *ptv, int *ret);
@@ -38,7 +38,7 @@ ptcounter_alloc(
p = malloc(sizeof(struct ptcounter));
if (!p)
return errno;
- ret = -ptvar_alloc(nr, sizeof(uint64_t), &p->var);
+ ret = -ptvar_alloc(nr, sizeof(uint64_t), NULL, &p->var);
if (ret) {
free(p);
return ret;
@@ -89,7 +89,7 @@ descr_init_phase(
int ret;
assert(descr_ptvar == NULL);
- ret = -ptvar_alloc(nr_threads, DESCR_BUFSZ, &descr_ptvar);
+ ret = -ptvar_alloc(nr_threads, DESCR_BUFSZ, NULL, &descr_ptvar);
if (ret)
str_liberror(ctx, ret, _("creating description buffer"));
@@ -136,7 +136,7 @@ phase7_func(
}
error = -ptvar_alloc(scrub_nproc(ctx), sizeof(struct summary_counts),
- &ptvar);
+ NULL, &ptvar);
if (error) {
str_liberror(ctx, error, _("setting up block counter"));
return error;
@@ -120,7 +120,7 @@ read_verify_pool_alloc(
rvp->disk = disk;
rvp->ioerr_fn = ioerr_fn;
ret = -ptvar_alloc(submitter_threads, sizeof(struct read_verify),
- &rvp->rvstate);
+ NULL, &rvp->rvstate);
if (ret)
goto out_counter;
ret = -workqueue_create(&rvp->wq, (struct xfs_mount *)rvp,