[vlc-devel] [PATCH 1/6] os2: implement vlc_once()
Kamil Rytarowski
n54 at gmx.com
Mon Apr 2 23:12:53 CEST 2018
On 23.03.2018 09:54, Rémi Denis-Courmont wrote:
> Le jeudi 22 mars 2018, 16:41:29 EET KO Myung-Hun a écrit :
>> ---
>> include/vlc_threads.h | 6 ++++++
>> src/os2/thread.c | 17 +++++++++++++++++
>> 2 files changed, 23 insertions(+)
>>
>> diff --git a/include/vlc_threads.h b/include/vlc_threads.h
>> index 37054a3ded..b7505da266 100644
>> --- a/include/vlc_threads.h
>> +++ b/include/vlc_threads.h
>> @@ -125,6 +125,12 @@ typedef struct
>> #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
>> #define LIBVLC_NEED_SEMAPHORE
>> #define LIBVLC_NEED_RWLOCK
>> +typedef struct
>> +{
>> + unsigned done;
>> + vlc_mutex_t mutex;
>> +} vlc_once_t;
>> +#define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }
>> typedef struct vlc_threadvar *vlc_threadvar_t;
>> typedef struct vlc_timer *vlc_timer_t;
>>
>> diff --git a/src/os2/thread.c b/src/os2/thread.c
>> index 8f7b335ea4..52f58023b8 100644
>> --- a/src/os2/thread.c
>> +++ b/src/os2/thread.c
>> @@ -427,6 +427,23 @@ int vlc_cond_timedwait_daytime (vlc_cond_t *p_condvar,
>> vlc_mutex_t *p_mutex, return vlc_cond_wait_common (p_condvar, p_mutex,
>> ulTimeout);
>> }
>>
>> +void vlc_once(vlc_once_t *once, void (*cb)(void))
>> +{
>> + if( !once->done )
>
> This looks like a violation of the memory model.
>
This patch is correct.
We set done to 0 before using vlc_once() and in every scenario
afterwards when the variable done is set to 1, it's a correct and
defined behavior.
There is just need to assume that there is a memory barrier after
initialization (using VLC_STATIC_ONCE). Hardening it does not solve any
world problems. Someone should misuse or bypass API in the first place
to generate a potential violation.
>> + {
>> + vlc_mutex_lock( &once->mutex );
>> +
>> + if( !once->done )
>> + {
>> + cb();
>> +
>> + once->done = 1;
>> + }
>> +
>> + vlc_mutex_unlock( &once->mutex );
>> + }
>> +}
>> +
>> /*** Thread-specific variables (TLS) ***/
>> struct vlc_threadvar
>> {
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 850 bytes
Desc: OpenPGP digital signature
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20180402/878d7a6b/attachment.sig>
More information about the vlc-devel
mailing list