VLC  4.0.0-dev
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 {
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 {
65 
66  vlc_global_lock( VLC_MTA_MUTEX );
67  vlc_mta_holder* p_mta = (vlc_mta_holder*)var_CreateGetAddress( vlc, "mta-holder" );
68  if ( p_mta == NULL )
69  {
70  p_mta = (vlc_mta_holder*)malloc( sizeof( *p_mta ) );
71  if ( unlikely( p_mta == NULL ) )
72  {
73  vlc_global_unlock( VLC_MTA_MUTEX );
74  return false;
75  }
76  vlc_sem_init( &p_mta->ready_sem, 0 );
77  vlc_sem_init( &p_mta->release_sem, 0 );
78  p_mta->i_refcount = 1;
79  if ( vlc_clone( &p_mta->thread, MtaMainLoop, p_mta, VLC_THREAD_PRIORITY_LOW ) )
80  {
81  vlc_sem_destroy( &p_mta->release_sem );
82  vlc_sem_destroy( &p_mta->ready_sem );
83  free( p_mta );
84  p_mta = NULL;
85  vlc_global_unlock( VLC_MTA_MUTEX );
86  return false;
87  }
88  var_SetAddress( vlc, "mta-holder", p_mta );
89  vlc_sem_wait( &p_mta->ready_sem );
90  }
91  else
92  ++p_mta->i_refcount;
93  vlc_global_unlock( VLC_MTA_MUTEX );
94  return true;
95 }
96 
97 /**
98  * Releases a reference to the MTA holder.
99  *
100  * When its refcount reaches 0, the thread created by
101  */
102 static inline void vlc_mta_release( vlc_object_t* p_parent )
103 {
104  vlc_object_t *vlc = VLC_OBJECT(vlc_object_instance(p_parent));
105 
106  vlc_global_lock( VLC_MTA_MUTEX );
107  vlc_mta_holder *p_mta = (vlc_mta_holder*)var_InheritAddress( vlc, "mta-holder" );
108  assert( p_mta != NULL );
109  int i_refcount = --p_mta->i_refcount;
110  if ( i_refcount == 0 )
111  var_SetAddress( vlc, "mta-holder", NULL );
112  vlc_global_unlock( VLC_MTA_MUTEX );
113  if ( i_refcount == 0 )
114  {
115  vlc_sem_post( &p_mta->release_sem );
116 
117  vlc_join( p_mta->thread, NULL );
118 
119  vlc_sem_destroy( &p_mta->release_sem );
120  vlc_sem_destroy( &p_mta->ready_sem );
121  free( p_mta );
122  }
123 }
124 
125 #endif
static void * var_CreateGetAddress(vlc_object_t *p_obj, const char *psz_name)
Create an address variable with inherit and get its value.
Definition: vlc_variables.h:584
#define vlc_global_lock(n)
Acquires a global mutex.
Definition: vlc_threads.h:1161
static void vlc_mta_release(vlc_object_t *p_parent)
Releases a reference to the MTA holder.
Definition: mta_holder.h:102
vlc_sem_t ready_sem
Definition: mta_holder.h:36
void vlc_sem_destroy(vlc_sem_t *sem)
Deinitializes a semaphore.
Definition: thread.c:294
This file is a collection of common definitions and types.
int vlc_clone(vlc_thread_t *th, void *(*entry)(void *), void *data, int priority)
Creates and starts a new thread.
Definition: thread.c:266
void vlc_sem_init(vlc_sem_t *sem, unsigned value)
Initializes a semaphore.
Definition: thread.c:288
int i_refcount
Definition: mta_holder.h:35
vlc_sem_t release_sem
Definition: mta_holder.h:37
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:62
Definition: mta_holder.h:32
#define vlc_global_unlock(n)
Releases a global mutex.
Definition: vlc_threads.h:1166
Thread handle.
Definition: vlc_threads.h:263
int vlc_sem_post(vlc_sem_t *sem)
Increments the value of a semaphore.
Definition: thread.c:306
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:223
struct vlc_mta_holder vlc_mta_holder
#define VLC_OBJECT(x)
Type-safe vlc_object_t cast.
Definition: vlc_objects.h:70
sem_t vlc_sem_t
Semaphore.
Definition: vlc_threads.h:308
#define vlc_object_instance(o)
Definition: vlc_objects.h:194
vlc_thread_t thread
Definition: mta_holder.h:34
static void * var_InheritAddress(vlc_object_t *obj, const char *name)
Definition: vlc_variables.h:720
void vlc_sem_wait(vlc_sem_t *sem)
Waits on a semaphore.
Definition: thread.c:320
static int var_SetAddress(vlc_object_t *p_obj, const char *psz_name, void *ptr)
Set the value of a pointer variable.
Definition: vlc_variables.h:360
VLC object common members.
Definition: vlc_objects.h:43
static void * MtaMainLoop(void *opaque)
Definition: mta_holder.h:40
void vlc_join(vlc_thread_t handle, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:273
#define VLC_THREAD_PRIORITY_LOW
Definition: vlc_threads.h:345