[FFmpeg-devel] Patch: Enable OpenCL with Win32 threads

Matt Oliver protogonoi at gmail.com
Tue Apr 15 15:44:27 CEST 2014

> Using undocumented implementation specific things seems like a terrible
> hack.
> Windows thread API has no static initialization, and thats the end of
> the story, doing anything else seems error prone and evil, and should
> be avoided at all cost.

This is why this wasn't my first choice. But CONDITION_VARIABLE in win32 is
just a struct of int/HANDLES etc. so this just initialises them to their
default values. The initial values are not documented well but people have
actually been using them directly for years. In fact the -1 thing has been
around for ages so from my understanding when Microsoft updated the thread
api in Vista they actually left it internally as -1 because so many coders
had been previously explicitly using it as -1. So its been supported for
years. Given minGWs pthread implementation of the define is "((void *)
(size_t) -1)" I figured it wasnt that far off base.

If you want this to work with the Win32 Thread API, you should
> re-write it to be able to use init.

This was my initial thought. But it would require adding an extra function
that needs calling before any opencl functions get called (even before the
opencl_init function) and would serve no other purpose than to initalise
the mutex (which does nothing if threads are disabled). This function would
have to be added to all existing code that uses opencl functions so I was
trying to cause minimal changes. I thought of using something
like InitOnceExecuteOnce to run the init at startup but that is only
supported on Vista or later so I assumed that was not a good idea. The only
other option would be to add the mutex to the OpenCl context object with an
'is initialised' flag. For the functions that can be called before the
_init function they would need an atomic check on the initalise flag that
if it failed would then call the _init function on the mutex. This would
work but seems unnecessarily costly.

Or, just build with pthreads, it doesn't really have any downsides.

Tell msvc that ;)

As always open to suggestions.

More information about the ffmpeg-devel mailing list