dthread-unix.C

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #include "common/h/dthread.h"
00032 #include <pthread.h>
00033 #include <assert.h>
00034 
00035 DThread::DThread() :
00036    live(false)
00037 {
00038 }
00039 
00040 DThread::~DThread()
00041 {
00042 }
00043 
00044 typedef struct {
00045    DThread::initial_func_t func;
00046    void *param;
00047 } initial_data;
00048 
00049 static void *thread_init(void *d) {
00050    assert(d);
00051    initial_data *data = (initial_data *) d;
00052    DThread::initial_func_t func = data->func;
00053    void *param = data->param;
00054    delete data;
00055    func(param);
00056    return NULL;
00057 }
00058 
00059 bool DThread::spawn(initial_func_t func, void *param)
00060 {
00061    initial_data *data = new initial_data();
00062    data->func = func;
00063    data->param = param;
00064    int result = pthread_create(&thrd, NULL, thread_init, data);
00065    assert(result == 0);
00066    live = true;
00067    return true;
00068 }
00069 
00070 bool DThread::join()
00071 {
00072    assert(live && pthread_self() != thrd);
00073    int result = pthread_join(thrd, NULL);
00074    return (result == 0);
00075 }
00076 
00077 long DThread::id()
00078 {
00079    if (!live) return 0;
00080    return (long) thrd;
00081 }
00082 
00083 long DThread::self()
00084 {
00085    return (long) pthread_self();
00086 }
00087 
00088 Mutex::Mutex(bool recursive)
00089 {
00090    pthread_mutexattr_t attr;
00091    pthread_mutexattr_init(&attr);
00092    if (recursive) {
00093       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00094    }
00095    int result = pthread_mutex_init(&mutex, &attr);
00096    assert(result == 0);
00097 
00098    pthread_mutexattr_destroy(&attr);
00099 }
00100 
00101 #include <stdio.h>
00102 #include <errno.h>
00103 #include <string.h>
00104 
00105 
00106 Mutex::~Mutex()
00107 {
00108    int result = pthread_mutex_destroy(&mutex);
00109    assert(result == 0);
00110 }
00111 
00112 bool Mutex::lock()
00113 {
00114    int result = pthread_mutex_lock(&mutex);
00115    return (result == 0);
00116 }
00117 
00118 bool Mutex::trylock()
00119 {
00120   int result = pthread_mutex_trylock(&mutex);
00121   return (result == 0);
00122 }
00123 
00124 bool Mutex::unlock()
00125 {
00126    int result = pthread_mutex_unlock(&mutex);
00127    return (result == 0);
00128 }
00129 
00130 CondVar::CondVar(Mutex *m)
00131 {
00132    if (!m) {
00133       created_mutex = true;
00134       mutex = new Mutex();
00135    }
00136    else {
00137       created_mutex = false;
00138       mutex = static_cast<Mutex *>(m);
00139    }
00140    pthread_cond_init(&cond, NULL);
00141 }
00142 
00143 CondVar::~CondVar()
00144 {
00145    if (created_mutex)
00146       delete mutex;
00147    int result = pthread_cond_destroy(&cond);
00148    assert(result == 0);
00149 }
00150 
00151 bool CondVar::unlock()
00152 {
00153    return mutex->unlock();
00154 }
00155 
00156 bool CondVar::lock()
00157 {
00158    return mutex->lock();
00159 }
00160 
00161 bool CondVar::trylock()
00162 {
00163   return mutex->trylock();
00164 }
00165 
00166 bool CondVar::signal()
00167 {
00168    int result = pthread_cond_signal(&cond);
00169    return result == 0;
00170 }
00171 
00172 bool CondVar::broadcast()
00173 {
00174    int result = pthread_cond_broadcast(&cond);
00175    return result == 0;
00176 }
00177 
00178 bool CondVar::wait()
00179 {
00180    int result = pthread_cond_wait(&cond, &mutex->mutex);
00181    return result == 0;
00182 }
00183 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1