VLC  3.0.15
mta_holder.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * mta_holder.c: Hold a MTA from another thread
3  *****************************************************************************
4  * Copyright (C) 2002-2017 the VideoLAN and AUTHORS
5  *
6  * Author: Hugo BeauzĂ©e-Luyssen <hugo@beauzee.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 #ifndef MTA_HOLDER_H
24 # define MTA_HOLDER_H
25 
26 #include <vlc_common.h>
27 
28 #include <assert.h>
29 #include <windows.h>
30 #include <objbase.h>
31 
32 typedef struct vlc_mta_holder
33 {
35  int i_refcount;
39 
40 static inline void* MtaMainLoop( void* opaque )
41 {
42  vlc_mta_holder* p_mta = (vlc_mta_holder*)opaque;
43  CoInitializeEx( NULL, COINIT_MULTITHREADED );
44 
45  vlc_sem_post( &p_mta->ready_sem );
46 
47  vlc_sem_wait( &p_mta->release_sem );
48 
49  CoUninitialize();
50  return NULL;
51 }
52 
53 /**
54  * Ensure an MTA context will be available until vlc_mta_release gets called.
55  *
56  * In the background, this will create a thread that does nothing but to keep the MTA
57  * refcount greater than 0.
58  *
59  * This is usefull in order not to commit a thread to a specific concurrency model.
60  * This function is win32 specific.
61  */
62 static inline bool vlc_mta_acquire( vlc_object_t *p_parent )
63 {
64  vlc_global_lock( VLC_MTA_MUTEX );
65  vlc_mta_holder* p_mta = (vlc_mta_holder*)var_CreateGetAddress( p_parent->obj.libvlc, "mta-holder" );
66  if ( p_mta == NULL )
67  {
68  p_mta = (vlc_mta_holder*)malloc( sizeof( *p_mta ) );
69  if ( unlikely( p_mta == NULL ) )
70  {
71  vlc_global_unlock( VLC_MTA_MUTEX );
72  return false;
73  }
74  vlc_sem_init( &p_mta->ready_sem, 0 );
75  vlc_sem_init( &p_mta->release_sem, 0 );
76  p_mta->i_refcount = 1;
77  if ( vlc_clone( &p_mta->thread, MtaMainLoop, p_mta, VLC_THREAD_PRIORITY_LOW ) )
78  {
79  vlc_sem_destroy( &p_mta->release_sem );
80  vlc_sem_destroy( &p_mta->ready_sem );
81  free( p_mta );
82  p_mta = NULL;
83  vlc_global_unlock( VLC_MTA_MUTEX );
84  return false;
85  }
86  var_SetAddress( p_parent->obj.libvlc, "mta-holder", p_mta );
87  vlc_sem_wait( &p_mta->ready_sem );
88  }
89  else
90  ++p_mta->i_refcount;
91  vlc_global_unlock( VLC_MTA_MUTEX );
92  return true;
93 }
94 
95 /**
96  * Releases a reference to the MTA holder.
97  *
98  * When its refcount reaches 0, the thread created by
99  */
100 static inline void vlc_mta_release( vlc_object_t* p_parent )
101 {
102  vlc_global_lock( VLC_MTA_MUTEX );
103  vlc_mta_holder *p_mta = (vlc_mta_holder*)var_InheritAddress( p_parent->obj.libvlc, "mta-holder" );
104  assert( p_mta != NULL );
105  int i_refcount = --p_mta->i_refcount;
106  if ( i_refcount == 0 )
107  var_SetAddress( p_parent->obj.libvlc, "mta-holder", NULL );
108  vlc_global_unlock( VLC_MTA_MUTEX );
109  if ( i_refcount == 0 )
110  {
111  vlc_sem_post( &p_mta->release_sem );
112 
113  vlc_join( p_mta->thread, NULL );
114 
115  vlc_sem_destroy( &p_mta->release_sem );
116  vlc_sem_destroy( &p_mta->ready_sem );
117  free( p_mta );
118  }
119 }
120 
121 #endif
vlc_sem_init
void vlc_sem_init(vlc_sem_t *sem, unsigned value)
Initializes a semaphore.
Definition: thread.c:325
var_SetAddress
#define var_SetAddress(o, n, p)
Definition: vlc_variables.h:260
vlc_global_lock
#define vlc_global_lock(n)
Acquires a global mutex.
Definition: vlc_threads.h:1074
vlc_mta_acquire
static bool vlc_mta_acquire(vlc_object_t *p_parent)
Ensure an MTA context will be available until vlc_mta_release gets called.
Definition: mta_holder.h:61
vlc_mta_holder::release_sem
vlc_sem_t release_sem
Definition: mta_holder.h:55
vlc_sem_destroy
void vlc_sem_destroy(vlc_sem_t *sem)
Deinitializes a semaphore.
Definition: thread.c:331
vlc_common.h
VLC_THREAD_PRIORITY_LOW
#define VLC_THREAD_PRIORITY_LOW
Definition: vlc_threads.h:321
vlc_mta_release
static void vlc_mta_release(vlc_object_t *p_parent)
Releases a reference to the MTA holder.
Definition: mta_holder.h:99
vlc_mta_holder
struct vlc_mta_holder vlc_mta_holder
vlc_global_unlock
#define vlc_global_unlock(n)
Releases a global mutex.
Definition: vlc_threads.h:1079
vlc_sem_post
int vlc_sem_post(vlc_sem_t *sem)
Increments the value of a semaphore.
Definition: thread.c:343
vlc_common_members::libvlc
libvlc_int_t * libvlc
LibVLC instance.
Definition: vlc_common.h:441
vlc_clone
int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:263
vlc_mta_holder::ready_sem
vlc_sem_t ready_sem
Definition: mta_holder.h:54
vlc_mta_holder::thread
vlc_thread_t thread
Definition: mta_holder.h:52
MtaMainLoop
static void * MtaMainLoop(void *opaque)
Definition: mta_holder.h:39
vlc_sem_wait
void vlc_sem_wait(vlc_sem_t *sem)
Waits on a semaphore.
Definition: thread.c:357
vlc_thread_t
Thread handle.
Definition: vlc_threads.h:252
vlc_mta_holder
Definition: mta_holder.h:31
vlc_object_t
The main vlc_object_t structure.
Definition: vlc_objects.h:39
var_InheritAddress
#define var_InheritAddress(o, n)
Definition: vlc_variables.h:650
vlc_mta_holder::i_refcount
int i_refcount
Definition: mta_holder.h:53
var_CreateGetAddress
#define var_CreateGetAddress(a, b)
Definition: vlc_variables.h:497
unlikely
#define unlikely(p)
Definition: vlc_common.h:114
vlc_sem_t
sem_t vlc_sem_t
Semaphore.
Definition: vlc_threads.h:297
vlc_object_t::obj
struct vlc_common_members obj
Definition: vlc_objects.h:42
vlc_join
void vlc_join(vlc_thread_t handle, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:270