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  free( p_mta );
82  p_mta = NULL;
83  vlc_global_unlock( VLC_MTA_MUTEX );
84  return false;
85  }
86  var_SetAddress( vlc, "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_object_t *vlc = VLC_OBJECT(vlc_object_instance(p_parent));
103 
104  vlc_global_lock( VLC_MTA_MUTEX );
105  vlc_mta_holder *p_mta = (vlc_mta_holder*)var_InheritAddress( vlc, "mta-holder" );
106  assert( p_mta != NULL );
107  int i_refcount = --p_mta->i_refcount;
108  if ( i_refcount == 0 )
109  var_SetAddress( vlc, "mta-holder", NULL );
110  vlc_global_unlock( VLC_MTA_MUTEX );
111  if ( i_refcount == 0 )
112  {
113  vlc_sem_post( &p_mta->release_sem );
114 
115  vlc_join( p_mta->thread, NULL );
116 
117  free( p_mta );
118  }
119 }
120 
121 #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
Semaphore.
Definition: vlc_threads.h:498
#define vlc_global_lock(n)
Acquires a global mutex.
Definition: vlc_threads.h:1147
static void vlc_mta_release(vlc_object_t *p_parent)
Releases a reference to the MTA holder.
Definition: mta_holder.h:100
vlc_sem_t ready_sem
Definition: mta_holder.h:36
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:167
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:1152
Thread handle.
Definition: vlc_threads.h:208
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:223
struct vlc_mta_holder vlc_mta_holder
int vlc_sem_post(vlc_sem_t *sem)
Increments the value of a semaphore.
Definition: threads.c:484
#define VLC_OBJECT(x)
Type-safe vlc_object_t cast.
Definition: vlc_objects.h:70
void vlc_sem_wait(vlc_sem_t *sem)
Waits on a semaphore.
Definition: threads.c:500
#define vlc_object_instance(o)
Definition: vlc_objects.h:196
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
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_sem_init(vlc_sem_t *sem, unsigned value)
Initializes a semaphore.
Definition: threads.c:479
void vlc_join(vlc_thread_t handle, void **result)
Waits for a thread to complete (if needed), then destroys it.
Definition: thread.c:174
#define VLC_THREAD_PRIORITY_LOW
Definition: vlc_threads.h:248