2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CMutex.h"
|
2001-10-14 18:29:43 +00:00
|
|
|
#include "CLog.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CMutex
|
|
|
|
//
|
|
|
|
|
|
|
|
CMutex::CMutex()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMutex::CMutex(const CMutex&)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMutex::~CMutex()
|
|
|
|
{
|
|
|
|
fini();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CMutex&
|
2002-06-17 13:31:21 +00:00
|
|
|
CMutex::operator=(const CMutex&)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if HAVE_PTHREAD
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cerrno>
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::init()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = new pthread_mutex_t;
|
|
|
|
int status = pthread_mutex_init(mutex, NULL);
|
|
|
|
assert(status == 0);
|
|
|
|
// status = pthread_mutexattr_settype(mutex, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
// assert(status == 0);
|
|
|
|
m_mutex = reinterpret_cast<void*>(mutex);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::fini()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_destroy(mutex);
|
2002-10-15 21:29:44 +00:00
|
|
|
LOGC(status != 0, (CLOG_ERR "pthread_mutex_destroy status %d", status));
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(status == 0);
|
|
|
|
delete mutex;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::lock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_lock(mutex);
|
|
|
|
|
|
|
|
switch (status) {
|
2002-04-29 14:40:01 +00:00
|
|
|
case 0:
|
2001-10-06 14:13:28 +00:00
|
|
|
// success
|
|
|
|
return;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case EDEADLK:
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "lock already owned");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case EAGAIN:
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "too many recursive locks");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
default:
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_ERR "pthread_mutex_lock status %d", status));
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "unexpected error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::unlock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_unlock(mutex);
|
|
|
|
|
|
|
|
switch (status) {
|
2002-04-29 14:40:01 +00:00
|
|
|
case 0:
|
2001-10-06 14:13:28 +00:00
|
|
|
// success
|
|
|
|
return;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
case EPERM:
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "thread doesn't own a lock");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 14:40:01 +00:00
|
|
|
default:
|
2002-10-15 21:29:44 +00:00
|
|
|
LOG((CLOG_ERR "pthread_mutex_unlock status %d", status));
|
2001-10-06 14:13:28 +00:00
|
|
|
assert(0 && "unexpected error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#endif // HAVE_PTHREAD
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-10 16:49:46 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2001-10-06 14:13:28 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::init()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CRITICAL_SECTION* mutex = new CRITICAL_SECTION;
|
2001-10-25 22:17:17 +00:00
|
|
|
InitializeCriticalSection(mutex);
|
2001-10-06 14:13:28 +00:00
|
|
|
m_mutex = reinterpret_cast<void*>(mutex);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::fini()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CRITICAL_SECTION* mutex = reinterpret_cast<CRITICAL_SECTION*>(m_mutex);
|
2001-10-25 22:17:17 +00:00
|
|
|
DeleteCriticalSection(mutex);
|
2001-10-06 14:13:28 +00:00
|
|
|
delete mutex;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::lock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2001-10-25 22:17:17 +00:00
|
|
|
EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CMutex::unlock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2001-10-25 22:17:17 +00:00
|
|
|
LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#endif // WINDOWS_LIKE
|