1 /* 2 * Copyright 2011, Ben Langmead <langmea@cs.jhu.edu> 3 * 4 * This file is part of Bowtie 2. 5 * 6 * Bowtie 2 is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Bowtie 2 is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef THREADING_H_ 21 #define THREADING_H_ 22 23 #include <iostream> 24 #include "tinythread.h" 25 #include "fast_mutex.h" 26 27 #ifdef NO_SPINLOCK 28 # define MUTEX_T tthread::mutex 29 #else 30 # define MUTEX_T tthread::fast_mutex 31 #endif /* NO_SPINLOCK */ 32 33 34 /** 35 * Wrap a lock; obtain lock upon construction, release upon destruction. 36 */ 37 class ThreadSafe { 38 public: 39 ThreadSafe(MUTEX_T* ptr_mutex, bool locked = true) { 40 if(locked) { 41 this->ptr_mutex = ptr_mutex; 42 ptr_mutex->lock(); 43 } 44 else 45 this->ptr_mutex = NULL; 46 } 47 ~ThreadSafe()48 ~ThreadSafe() { 49 if (ptr_mutex != NULL) 50 ptr_mutex->unlock(); 51 } 52 53 private: 54 MUTEX_T *ptr_mutex; 55 }; 56 57 #endif 58