usermode/library/pmalloc/src/txmutex.h

00001 /*
00002     Copyright (C) 2011 Computer Sciences Department, 
00003     University of Wisconsin -- Madison
00004 
00005     ----------------------------------------------------------------------
00006 
00007     This file is part of Mnemosyne: Lightweight Persistent Memory, 
00008     originally developed at the University of Wisconsin -- Madison.
00009 
00010     Mnemosyne was originally developed primarily by Haris Volos
00011     with contributions from Andres Jaan Tack.
00012 
00013     ----------------------------------------------------------------------
00014 
00015     Mnemosyne is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU General Public License
00017     as published by the Free Software Foundation, version 2
00018     of the License.
00019  
00020     Mnemosyne is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU General Public License for more details.
00024 
00025     You should have received a copy of the GNU General Public License
00026     along with this program; if not, write to the Free Software
00027     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00028     Boston, MA  02110-1301, USA.
00029 
00030 ### END HEADER ###
00031 */
00032 
00033 /* 
00034  * This is a subset of the txlock.[h|c] defined by the MTM library.
00035  * We don't use the txlock.[h|c] to avoid depending on the MTM
00036  * library. 
00037  *
00038  * Note: We already depend on MTM as we use transactions along
00039  * the memory allocators generic slow path but we would like to
00040  * remove this dependency in the future.
00041  */
00042 
00043 #ifndef _M_TXMUTEX_H_GHA983
00044 #define _M_TXMUTEX_H_GHA983
00045 
00046 #undef pthread_mutex_t
00047 #undef pthread_mutex_init
00048 #undef pthread_mutex_destroy
00049 #undef pthread_mutex_lock
00050 #undef pthread_mutex_unlock
00051 
00052 #undef pthread_rwlock_t
00053 #undef pthread_rwlock_init
00054 #undef pthread_rwlock_destroy
00055 #undef pthread_rwlock_rdlock
00056 #undef pthread_rwlock_wrlock
00057 #undef pthread_rwlock_unlock
00058 
00059 
00060 #include <itm.h>
00061 #include <pthread.h>
00062 
00063 #define DEBUG_PRINTF(format, ...)
00064 //#define DEBUG_PRINTF(format, a, b) printf(format, a, b)
00065 
00066 /* MUTEX TXSAFE LOCKS */
00067 
00068 typedef pthread_mutex_t txmutex_t;
00069 
00070 
00071 static
00072 void _ITM_CALL_CONVENTION txmutex_unlock_commit_action(void *arg)
00073 {
00074         txmutex_t *txmutex = (txmutex_t *) arg;
00075         pthread_mutex_t *mutex = (pthread_mutex_t *) txmutex;
00076 
00077         //printf("[%d] COMMIT: MUTEX  UNLOCK %p\n", pthread_self(), mutex);
00078         pthread_mutex_unlock(mutex);
00079 
00080         return;
00081 }
00082 
00083 __attribute__((tm_pure))
00084 static inline 
00085 int txmutex_init(txmutex_t *txmutex)
00086 {
00087         pthread_mutex_t *mutex = (pthread_mutex_t *) txmutex;
00088         pthread_mutexattr_t attr;
00089         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00090 
00091         return pthread_mutex_init(mutex, &attr);
00092 }
00093 
00094 
00095 __attribute__((tm_pure))
00096 static inline 
00097 int txmutex_destroy(txmutex_t *txmutex)
00098 {
00099         pthread_mutex_t *mutex = (pthread_mutex_t *) txmutex;
00100 
00101         return pthread_mutex_destroy(mutex);
00102 }
00103 
00104 
00105 __attribute__((tm_pure))
00106 static inline 
00107 int txmutex_lock(txmutex_t *txmutex)
00108 {
00109         pthread_mutex_t *mutex = (pthread_mutex_t *) txmutex;
00110         int ret;
00111 
00112         DEBUG_PRINTF("[%d] NOW  : MUTEX  LOCK   %p\n", pthread_self(), mutex);
00113         ret = pthread_mutex_lock(mutex);
00114         DEBUG_PRINTF("[%d] NOW  : MUTEX  LOCK   %p DONE\n", pthread_self(), mutex);
00115 
00116         return ret;
00117 }
00118 
00119 
00120 __attribute__((tm_pure))
00121 static inline 
00122 int txmutex_unlock(txmutex_t *txmutex)
00123 {
00124         int             ret;
00125         pthread_mutex_t *mutex = (pthread_mutex_t *) txmutex;
00126 
00127         _ITM_transaction * td;
00128         td = _ITM_getTransaction();
00129 
00130         if (_ITM_inTransaction(td) > 0) {
00131                 DEBUG_PRINTF("[%d] DEFER: MUTEX  UNLOCK %p\n", pthread_self(), mutex);
00132                 _ITM_addUserCommitAction (td, txmutex_unlock_commit_action, 2, txmutex);
00133                 ret = 0;
00134         } else {
00135                 DEBUG_PRINTF("[%d] NOW  : MUTEX  UNLOCK %p\n", pthread_self(), mutex);
00136                 ret = pthread_mutex_unlock (mutex);
00137         }
00138 
00139         return ret;
00140 }
00141 
00142 
00143 #endif

Generated on Sat Apr 23 11:43:36 2011 for Mnemosyne by  doxygen 1.4.7