00001 /***************************************************************************** 00002 * vlc_gcrypt.h: VLC thread support for gcrypt 00003 ***************************************************************************** 00004 * Copyright (C) 2004-2010 Rémi Denis-Courmont 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. 00019 *****************************************************************************/ 00020 00021 /** 00022 * \file 00023 * This file implements gcrypt support functions in vlc 00024 */ 00025 00026 #include <errno.h> 00027 00028 #ifdef LIBVLC_USE_PTHREAD 00029 /** 00030 * If possible, use gcrypt-provided thread implementation. This is so that 00031 * other non-VLC components (inside the process) can also use gcrypt safely. 00032 */ 00033 GCRY_THREAD_OPTION_PTHREAD_IMPL; 00034 # define gcry_threads_vlc gcry_threads_pthread 00035 #else 00036 00037 /** 00038 * gcrypt thread option VLC implementation 00039 */ 00040 00041 static int gcry_vlc_mutex_init( void **p_sys ) 00042 { 00043 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) ); 00044 if( p_lock == NULL) 00045 return ENOMEM; 00046 00047 vlc_mutex_init( p_lock ); 00048 *p_sys = p_lock; 00049 return VLC_SUCCESS; 00050 } 00051 00052 static int gcry_vlc_mutex_destroy( void **p_sys ) 00053 { 00054 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys; 00055 vlc_mutex_destroy( p_lock ); 00056 free( p_lock ); 00057 return VLC_SUCCESS; 00058 } 00059 00060 static int gcry_vlc_mutex_lock( void **p_sys ) 00061 { 00062 vlc_mutex_lock( (vlc_mutex_t *)*p_sys ); 00063 return VLC_SUCCESS; 00064 } 00065 00066 static int gcry_vlc_mutex_unlock( void **lock ) 00067 { 00068 vlc_mutex_unlock( (vlc_mutex_t *)*lock ); 00069 return VLC_SUCCESS; 00070 } 00071 00072 static const struct gcry_thread_cbs gcry_threads_vlc = 00073 { 00074 GCRY_THREAD_OPTION_USER, 00075 NULL, 00076 gcry_vlc_mutex_init, 00077 gcry_vlc_mutex_destroy, 00078 gcry_vlc_mutex_lock, 00079 gcry_vlc_mutex_unlock 00080 }; 00081 #endif 00082 00083 /** 00084 * Initializes gcrypt with proper locking. 00085 */ 00086 static inline void vlc_gcrypt_init (void) 00087 { 00088 /* This would need a process-wide static mutex with all libraries linking 00089 * to a given instance of libgcrypt. We cannot do this as we have different 00090 * plugins linking with gcrypt, and some underlying libraries may use it 00091 * behind our back. Only way is to always link gcrypt statically (ouch!) or 00092 * have upstream gcrypt provide one shared object per threading system. */ 00093 static bool done = false; 00094 00095 vlc_global_lock (VLC_GCRYPT_MUTEX); 00096 if (!done) 00097 { 00098 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc); 00099 done = true; 00100 } 00101 vlc_global_unlock (VLC_GCRYPT_MUTEX); 00102 }
1.5.6