mutex.c (2407B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2001, 2002, 2003, 2004, 2012 GNUnet e.V. 4 5 GNUnet is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with GNUnet; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 21 /** 22 * @file src/fuse/mutex.c 23 * @brief implementation of mutual exclusion 24 */ 25 #include "gnunet-fuse.h" 26 #include "mutex.h" 27 28 #include <pthread.h> 29 30 #ifndef PTHREAD_MUTEX_NORMAL 31 #ifdef PTHREAD_MUTEX_TIMED_NP 32 #define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_TIMED_NP 33 #else 34 #define PTHREAD_MUTEX_NORMAL NULL 35 #endif 36 #endif 37 38 39 /** 40 * @brief Structure for MUTual EXclusion (Mutex). 41 */ 42 struct GNUNET_Mutex 43 { 44 pthread_mutex_t pt; 45 }; 46 47 48 struct GNUNET_Mutex * 49 GNUNET_mutex_create (int isRecursive) 50 { 51 pthread_mutexattr_t attr; 52 struct GNUNET_Mutex *mut; 53 #if WINDOWS 54 attr = NULL; 55 #endif 56 57 pthread_mutexattr_init (&attr); 58 if (isRecursive) 59 { 60 GNUNET_assert (0 == pthread_mutexattr_settype 61 (&attr, PTHREAD_MUTEX_RECURSIVE)); 62 } 63 else 64 { 65 GNUNET_assert (0 == pthread_mutexattr_settype 66 (&attr, PTHREAD_MUTEX_ERRORCHECK)); 67 } 68 mut = GNUNET_new (struct GNUNET_Mutex); 69 GNUNET_assert (0 == pthread_mutex_init (&mut->pt, &attr)); 70 return mut; 71 } 72 73 74 void 75 GNUNET_mutex_destroy (struct GNUNET_Mutex * mutex) 76 { 77 GNUNET_assert (0 == pthread_mutex_destroy (&mutex->pt)); 78 GNUNET_free (mutex); 79 } 80 81 82 void 83 GNUNET_mutex_lock (struct GNUNET_Mutex * mutex) 84 { 85 if (0 != (errno = pthread_mutex_lock (&mutex->pt))) 86 { 87 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pthread_mutex_unlock"); 88 GNUNET_assert (0); 89 } 90 } 91 92 93 void 94 GNUNET_mutex_unlock (struct GNUNET_Mutex * mutex) 95 { 96 if (0 != (errno = pthread_mutex_unlock (&mutex->pt))) 97 { 98 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pthread_mutex_unlock"); 99 GNUNET_assert (0); 100 } 101 } 102 103 104 /* end of mutex.c */