[FFmpeg-devel] [PATCH] avutil/eval: make av_expr_eval more thread safe

Michael Niedermayer michael at niedermayer.cc
Sun Dec 15 16:34:03 EET 2019


On Sun, Dec 15, 2019 at 01:52:52PM +0100, Marton Balint wrote:
> 
> 
> On Sun, 15 Dec 2019, Michael Niedermayer wrote:
> 
> >On Sun, Dec 15, 2019 at 02:16:49AM +0100, Marton Balint wrote:
> >>Unfortunately the ld() and st() operations store the variables in the AVExpr
> >>context and not in the parser in order to keep the stored values between
> >>evaluations (which is a rarely(?) used undocumented(?) feature of AVExpr).
> >>
> >>This causes data race issues when the same expression is evaluated from
> >>multiple threads. Let's use the Parser as variable store during evaluation and
> >>only sync the AVExpr context variables with the Parser before and after
> >>evaluation. This keeps the feature working for single threaded cases and fixes
> >>using ld() and st() usage for the "normal" use case when the ld()-s only
> >>reference st()-d values from the same evaluation.
> >>
> >
> >>Maybe we should just remove this feature instead?
> >
> >having some variable that is not local to the current point evaluation is
> >needed for random() as it uses it as state
> >ld/st are also documented in doc/utils.texi and are the basis for multiple
> >features so they themselfs cant be removed either
> 
> lt() and st() is documented, but it is not documented that they keep their
> values between evaluations.
> 
> >> {
> >>     Parser p = { 0 };
> >>-    p.var= e->var;
> >>+    double ret;
> >>
> >>+    memcpy(p.var, e->var, sizeof(p.var));
> >>     p.const_values = const_values;
> >>     p.opaque     = opaque;
> >>-    return eval_expr(&p, e);
> >>+    ret = eval_expr(&p, e);
> >>+    memcpy(e->var, p.var, sizeof(p.var));
> >
> >this does not look thread safe to me
> 
> It is thread safe if the expression is deterministic in a sense that it does
> not use the state variables without setting them first during a single
> evaluation.

I do not think writing different values from 2 threads without synchronization
or any atomicity onto the same array of doubles and afterwards using this
is safe.
Even if we ignore C, you do not know if the frankenstein double value this
could produce will function the same way a normal value would. Strictly it
could crash on read i think


> 
> >also it slows the code down by extra memcopies
> >
> >I think the main question is, do we need to share a expression evaluator
> >between threads ?
> >Is that useful to any feature or usecase ?
> >If it has no use case then the obvious solution is not to share the expression
> >evaluator or at least not the things written to per evaluation
> 
> The ticket this patch fix is a pretty good example where it matters.

i think you misunderstand, the example does not require inter thread 
communication and should be fine with a AVExpr per thread unless iam
missing something.


> 
> The confusion is caused by mixing the state with the parsed expression.
> Maybe we should decouple them, or at least add the possibility to the user
> to use a custom state.

yes decoupling them / giving the user more control over the state makes sense


> 
> Anyway, I am inclined to accept what Gyan proposed, to determine if the
> expression uses lt() st() or rand() and if it does fall back to 1 thread. Or
> implement something like av_expr_is_stateless() to do this in a helper
> function.

This solution kind of sucks because it forces a heap of cases to run
single threaded which would work find with multiple threads
OTOH with one AVExpr or AVExprState per thread it would work multi threaded

The one case the state per thread will need to take a bit of care of is the
random seed, which we would want to be different per thread

Thanks


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Does the universe only have a finite lifespan? No, its going to go on
forever, its just that you wont like living in it. -- Hiranya Peiri
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20191215/6ba73288/attachment.sig>


More information about the ffmpeg-devel mailing list