1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ptmutex.h"
// GThread-based implementation -
// Not currently used - needs to be migrated to GStaticRecMutex
// and since it's no more guaranteed than pthreads to be available on Win32
// it might make more sense to forget about the GLib implementation and
// use Win32 thread functions instead.
//#ifdef G_THREADS_ENABLED
#if 0
PTMutex::PTMutex() : mutex(NULL)
{
if (!g_thread_supported ())
g_thread_init (NULL);
mutex=g_mutex_new();
}
PTMutex::~PTMutex()
{
if(mutex)
g_mutex_free(mutex);
}
void PTMutex::ObtainMutex()
{
g_mutex_lock(mutex);
}
bool PTMutex::AttemptMutex()
{
return(g_mutex_trylock(mutex));
}
void PTMutex::ReleaseMutex()
{
g_mutex_unlock(mutex);
}
#elif defined HAVE_LIBPTHREAD
// pthreads-based implementation
PTMutex::PTMutex()
{
pthread_mutexattr_t pmi;
pthread_mutexattr_init(&pmi);
pthread_mutexattr_settype(&pmi,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex,&pmi);
}
PTMutex::~PTMutex()
{
pthread_mutex_destroy(&mutex);
}
void PTMutex::ObtainMutex()
{
pthread_mutex_lock(&mutex);
}
bool PTMutex::AttemptMutex()
{
int result=pthread_mutex_trylock(&mutex);
if(result==0)
return(true);
else
return(false);
}
void PTMutex::ReleaseMutex()
{
pthread_mutex_unlock(&mutex);
}
#else
// Dummy implementation. Obtaining the mutex always succeeds.
PTMutex::PTMutex()
{
cerr << "Warning - building a dummy mutex" << endl;
}
PTMutex::~PTMutex()
{
}
void PTMutex::ObtainMutex()
{
cerr << "Warning - obtaining a dummy mutex" << endl;
}
bool PTMutex::AttemptMutex()
{
cerr << "Warning - attempting a dummy mutex" << endl;
return(true);
}
void PTMutex::ReleaseMutex()
{
cerr << "Warning - releasing a dummy mutex" << endl;
}
#endif
|