[PATCH] avcodec/utils: use a default lock manager that uses a spinlock
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager. Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavcodec/utils.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 3832b81..cf7be5e 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -56,9 +56,33 @@ # include <iconv.h> #endif +static int default_lockmgr_cb(void **mutex, enum AVLockOp op) +{ + void * volatile *state = mutex; + + switch (op) { + case AV_LOCK_CREATE: + *state = NULL; + return 0; + case AV_LOCK_OBTAIN: + while (avpriv_atomic_ptr_cas(state, NULL, mutex)) + ; + + return 0; + case AV_LOCK_RELEASE: + if (avpriv_atomic_ptr_cas(state, mutex, NULL) != mutex) + av_assert0(0); + + return 0; + case AV_LOCK_DESTROY: + return 0; + } + return 1; +} + volatile int ff_avcodec_locked; static int volatile entangled_thread_counter = 0; -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op); +static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb; static void *codec_mutex; static void *avformat_mutex; -- 1.7.9.5
On 10/17/13, Michael Niedermayer <michaelni@gmx.at> wrote:
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavcodec/utils.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 3832b81..cf7be5e 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -56,9 +56,33 @@ # include <iconv.h> #endif
+static int default_lockmgr_cb(void **mutex, enum AVLockOp op) +{ + void * volatile *state = mutex; + + switch (op) { + case AV_LOCK_CREATE: + *state = NULL; + return 0; + case AV_LOCK_OBTAIN: + while (avpriv_atomic_ptr_cas(state, NULL, mutex)) + ; + + return 0; + case AV_LOCK_RELEASE: + if (avpriv_atomic_ptr_cas(state, mutex, NULL) != mutex) + av_assert0(0); + + return 0; + case AV_LOCK_DESTROY: + return 0; + } + return 1; +} + volatile int ff_avcodec_locked; static int volatile entangled_thread_counter = 0; -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op); +static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb; static void *codec_mutex; static void *avformat_mutex;
-- 1.7.9.5
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Why this was not done before?
On Thu, Oct 17, 2013 at 01:48:05PM +0000, Paul B Mahol wrote:
On 10/17/13, Michael Niedermayer <michaelni@gmx.at> wrote:
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavcodec/utils.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 3832b81..cf7be5e 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -56,9 +56,33 @@ # include <iconv.h> #endif
+static int default_lockmgr_cb(void **mutex, enum AVLockOp op) +{ + void * volatile *state = mutex; + + switch (op) { + case AV_LOCK_CREATE: + *state = NULL; + return 0; + case AV_LOCK_OBTAIN: + while (avpriv_atomic_ptr_cas(state, NULL, mutex)) + ; + + return 0; + case AV_LOCK_RELEASE: + if (avpriv_atomic_ptr_cas(state, mutex, NULL) != mutex) + av_assert0(0); + + return 0; + case AV_LOCK_DESTROY: + return 0; + } + return 1; +} + volatile int ff_avcodec_locked; static int volatile entangled_thread_counter = 0; -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op); +static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb; static void *codec_mutex; static void *avformat_mutex;
-- 1.7.9.5
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Why this was not done before?
the avpriv_atomic_ stuff didnt exist before [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct answer.
----- Original Message ----- From: "Paul B Mahol" <onemda@gmail.com> To: "FFmpeg development discussions and patches" <ffmpeg-devel@ffmpeg.org> Sent: Thursday, October 17, 2013 9:48 AM Subject: Re: [FFmpeg-devel] [PATCH] avcodec/utils: use a default lock manager that uses a spinlock
On 10/17/13, Michael Niedermayer <michaelni@gmx.at> wrote:
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager.
Signed-off-by: Michael Niedermayer <michaelni@gmx.at> --- libavcodec/utils.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 3832b81..cf7be5e 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -56,9 +56,33 @@ # include <iconv.h> #endif
+static int default_lockmgr_cb(void **mutex, enum AVLockOp op) +{ + void * volatile *state = mutex; + + switch (op) { + case AV_LOCK_CREATE: + *state = NULL; + return 0; + case AV_LOCK_OBTAIN: + while (avpriv_atomic_ptr_cas(state, NULL, mutex)) + ; + + return 0; + case AV_LOCK_RELEASE: + if (avpriv_atomic_ptr_cas(state, mutex, NULL) != mutex) + av_assert0(0); + + return 0; + case AV_LOCK_DESTROY: + return 0; + } + return 1; +} + volatile int ff_avcodec_locked; static int volatile entangled_thread_counter = 0; -static int (*lockmgr_cb)(void **mutex, enum AVLockOp op); +static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb; static void *codec_mutex; static void *avformat_mutex;
+ case AV_LOCK_OBTAIN: + while (avpriv_atomic_ptr_cas(state, NULL, mutex)) + ; + + return 0; Seems the while loop here will cause CPU to spike high. Suggest whatever you use for Sleep be incorporated in loop.
On Thu, 17 Oct 2013 15:40:36 +0200 Michael Niedermayer <michaelni@gmx.at> wrote:
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager.
So what again is the reason that you can't do one of these things: 1. Don't use global state, but recreate it every time the user creates a demuxer or whatever. So there's nothing to synchronize. 2. Use pthreads, and on Windows, one of the popular pthreads wrappers. I figure it's not quite as simple as it looks like, but badly reimplementing the operating system/libc provided locking primitives doesn't sound like a nice solution at all.
On Fri, Oct 18, 2013 at 12:01:55AM +0200, wm4 wrote:
On Thu, 17 Oct 2013 15:40:36 +0200 Michael Niedermayer <michaelni@gmx.at> wrote:
That makes avformat & avcodec thread safe without the need to explicitly register a lock manager.
So what again is the reason that you can't do one of these things:
1. Don't use global state, but recreate it every time the user creates a demuxer or whatever. So there's nothing to synchronize.
we where always trying to avoid code that initialized globals in thread unsafe ways. so yes we & i are / am trying that sort of since a while and most code shuld just dumbly init the tables on every init and always write the same data to each byte, making them inherently safe ... also there are cases where global state just exists and synchronization simply is needed. the codec/format/filter/parser/... registration some external libraries which are not thread safe themselfs some APIs which are not thread safe (parts of OpenCL IIRC fall in this category) av_log()
2. Use pthreads, and on Windows, one of the popular pthreads wrappers.
Last time this idea came up someone said it doesnt work because theres no gurantee that the pthread mutex you then use is compatible with the threads its trying to sychronize which may be something else than pthread based. IIRC
I figure it's not quite as simple as it looks like, but badly reimplementing the operating system/libc provided locking primitives doesn't sound like a nice solution at all.
[...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope
On 18.10.2013, at 01:47, Michael Niedermayer <michaelni@gmx.at> wrote:
2. Use pthreads, and on Windows, one of the popular pthreads wrappers.
Last time this idea came up someone said it doesnt work because theres no gurantee that the pthread mutex you then use is compatible with the threads its trying to sychronize which may be something else than pthread based. IIRC
To be honest, neither of these approaches work properly. Your atomics approach will completely break (as in cause hangs) in an environment using cooperative scheduling if the user yields in a FFmpeg callback. It also will burn the CPU for a whole time-slice in single-core environments if there is any congestion. Note these also apply to the av_log changes. Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses. Or in short: we don't do it currently, because it simply doesn't work in general. Whether we should support (either by default or with a simpler way that requires less code) a pthreads based application is a different question, but it's likely to break some use case, and probably needs to be considered and API change.
On Fri, Oct 18, 2013 at 08:41:36AM +0200, Reimar Döffinger wrote:
On 18.10.2013, at 01:47, Michael Niedermayer <michaelni@gmx.at> wrote:
2. Use pthreads, and on Windows, one of the popular pthreads wrappers.
Last time this idea came up someone said it doesnt work because theres no gurantee that the pthread mutex you then use is compatible with the threads its trying to sychronize which may be something else than pthread based. IIRC
To be honest, neither of these approaches work properly. Your atomics approach will completely break (as in cause hangs) in an environment using cooperative scheduling if the user yields in a FFmpeg callback.
i hoped that such platforms dont exist anymore
It also will burn the CPU for a whole time-slice in single-core environments if there is any congestion.
that sounds nasty but if theres congestion (which for the cases where it would be used i would not expect) you sure prefer burning cpu over it somehow mysteriously failing in hard to reproduce ways.
Note these also apply to the av_log changes.
the second patchset doesnt have the lock over the user callback but just the default, so the question would be if any function in there can cause a "cooperative schedulin environment" to yield
Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses. Or in short: we don't do it currently, because it simply doesn't work in general. Whether we should support (either by default or with a simpler way that requires less code) a pthreads based application is a different question, but it's likely to break some use case, and probably needs to be considered and API change.
You miss a problem here the current code does not work, its not that my patches implement merely a convenient default ... the lock manager registration itself is completely unsafe consider 2 libs or an application with plugins, or an application and a lib loading libavcodec & libavformat or in case of 2 libs libavcodec twice how could they set a lock manager ? each might require a different lock manager and even if they use the same the registration still is not thread safe as it would unregister the old at a random non synchronized point. And to make it worse the 2 libs/app using libavcodec could use different thread systems considering above iam not sure the spinlock (in environments where its safe) would be that bad of a solution. but iam happy to implement something else [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Breaking DRM is a little like attempting to break through a door even though the window is wide open and the only thing in the house is a bunch of things you dont want and which you would get tomorrow for free anyway
On 18.10.2013, at 11:27, Michael Niedermayer <michaelni@gmx.at> wrote:
On Fri, Oct 18, 2013 at 08:41:36AM +0200, Reimar Döffinger wrote:
On 18.10.2013, at 01:47, Michael Niedermayer <michaelni@gmx.at> wrote:
2. Use pthreads, and on Windows, one of the popular pthreads wrappers.
Last time this idea came up someone said it doesnt work because theres no gurantee that the pthread mutex you then use is compatible with the threads its trying to sychronize which may be something else than pthread based. IIRC
To be honest, neither of these approaches work properly. Your atomics approach will completely break (as in cause hangs) in an environment using cooperative scheduling if the user yields in a FFmpeg callback.
i hoped that such platforms dont exist anymore
I believe many embedded an real-time systems still behave like that.
It also will burn the CPU for a whole time-slice in single-core environments if there is any congestion.
that sounds nasty but if theres congestion (which for the cases where it would be used i would not expect) you sure prefer burning cpu over it somehow mysteriously failing in hard to reproduce ways.
But as long as it is such rarely called functions, the question is if there's an advantage over using pthreads.
Note these also apply to the av_log changes.
the second patchset doesnt have the lock over the user callback but just the default, so the question would be if any function in there can cause a "cooperative schedulin environment" to yield
Hm, that might be too much for us to care about, but it's not unusual for IO to trigger a yield I believe.
Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses. Or in short: we don't do it currently, because it simply doesn't work in general. Whether we should support (either by default or with a simpler way that requires less code) a pthreads based application is a different question, but it's likely to break some use case, and probably needs to be considered and API change.
You miss a problem here the current code does not work, its not that my patches implement merely a convenient default ...
the lock manager registration itself is completely unsafe consider 2 libs or an application with plugins, or an application and a lib loading libavcodec & libavformat or in case of 2 libs libavcodec twice how could they set a lock manager ? each might require a different lock manager and even if they use the same the registration still is not thread safe as it would unregister the old at a random non synchronized point. And to make it worse the 2 libs/app using libavcodec could use different thread systems
considering above iam not sure the spinlock (in environments where its safe) would be that bad of a solution. but iam happy to implement something else
Well, I just kind of thing that pthreads would be a better solution. If compiled without threads, we'd automatically default to not using this (and thus admittedly not being thread safe, that's probably not fully ideal if an application cannot know that). A pthread implementation could do a yield for systems requiring it. Another option would possibly be to use a spinlock only for setting the lock manager? Still seems like it will be tricky to use though, and might just combine the worst of all options.
On Fri, 18 Oct 2013 08:41:36 +0200 Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses.
Well, ffmpeg is doing the multithreading, so of course pthreads will be available? Even if it's in form of the internal windows pthreads wrapper found in compat/w32pthreads.h. Why should there be any "compatibility" be necessary? As far as I can see, ffmpeg will always use the system's standard threading API, so what exactly is the problem?
On 18.10.2013, at 14:17, wm4 <nfxjfg@googlemail.com> wrote:
On Fri, 18 Oct 2013 08:41:36 +0200 Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses.
Well, ffmpeg is doing the multithreading, so of course pthreads will be available?
"The multithreading"? None of this is required or in any way relevant for the FFmpeg internal threading. So no, pthreads might not be available, or FFmpeg might have been compiled without threading support.
Why should there be any "compatibility" be necessary? As far as I can see, ffmpeg will always use the system's standard threading API, so what exactly is the problem?
There have been pthread implementations that could not "see" the native threads (as a stupid example, a generic pthread implementation based on cooperative scheduling which only uses one single native thread). In those cases a pthread lock will fail to affect the applications native threads, i.e. the lock will simply not work for threads not created by pthreads. There are many variants of this. There is no general way to know if the applications threading method is compatible with what FFmpeg used, which is the reason to have the lock manager in the first place.
On Fri, 18 Oct 2013 18:20:20 +0200 Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
On 18.10.2013, at 14:17, wm4 <nfxjfg@googlemail.com> wrote:
On Fri, 18 Oct 2013 08:41:36 +0200 Reimar Döffinger <Reimar.Doeffinger@gmx.de> wrote:
Pthreads is not always available, and even if it is there is no guarantee it will be compatible with the threading system the application uses.
Well, ffmpeg is doing the multithreading, so of course pthreads will be available?
"The multithreading"? None of this is required or in any way relevant for the FFmpeg internal threading. So no, pthreads might not be available, or FFmpeg might have been compiled without threading support.
If no threading implementation is available, you can just assume that the target platform doesn't have threads, or if it does, that it's in the user's responsibility to compensate for the lack of porting. Or in other words: ffmpeg properly ported to a platform should either 1. use the platform's threading API, either directly, or via libpthread, 2. or assume there is no threading at all. Everything else is insane. You don't need to make everything worse for everyone just because someone decides he wants to do insane things.
Why should there be any "compatibility" be necessary? As far as I can see, ffmpeg will always use the system's standard threading API, so what exactly is the problem?
There have been pthread implementations that could not "see" the native threads (as a stupid example, a generic pthread implementation based on cooperative scheduling which only uses one single native thread). In those cases a pthread lock will fail to affect the applications native threads, i.e. the lock will simply not work for threads not created by pthreads. There are many variants of this.
How is this not complete non-sense? Is every library that needs internal locking supposed to have such a "lock manager"? ffmpeg sure is the first library I've seen that does this. Do you have an example for a non-theoretical platform where your assumption holds up? I'm not entirely sure why a threading implementation would need to know ("see") about a thread just for locks if the underlying primitives (i.e. syscalls) are the same anyway. Are you thinking about a case where cooperative scheduling and kernel threads are somehow mixed? Why would ffmpeg need to have to support such a broken setup?
There is no general way to know if the applications threading method is compatible with what FFmpeg used, which is the reason to have the lock manager in the first place.
You've got to make some reasonable assumptions about your platform, or you enter hell. And again, I haven't heard of a real-world example yet where a lock manager is really needed, and also where you couldn't decide this at compile time.
participants (5)
-
Don Moir -
Michael Niedermayer -
Paul B Mahol -
Reimar Döffinger -
wm4