Change throws to asserts. If any of this stuff happens it means a code logic error or a retarded computer, so throwing it is just a waste of time.

This commit is contained in:
mpc
2004-07-01 09:17:17 +00:00
committed by zzz
parent f51e064cf6
commit 2b951e3f61
6 changed files with 71 additions and 106 deletions

View File

@ -40,14 +40,10 @@ Mutex::Mutex(void)
{
#ifdef WINTHREAD
mutex = CreateMutex(NULL, FALSE, NULL);
if (mutex == NULL) {
TCHAR str[80];
throw Mutex_error(win_strerror(str, sizeof str));
}
assert(mutex != NULL);
#else
int rc = pthread_mutex_init(&mutex, NULL);
if (!rc)
throw Mutex_error(strerror(rc));
assert(!rc);
#endif
}
@ -57,14 +53,11 @@ Mutex::Mutex(void)
Mutex::~Mutex(void)
{
#ifdef WINTHREAD
if (!CloseHandle(mutex)) {
TCHAR str[80];
throw Mutex_error(win_strerror(str, sizeof str)); // TODO: log instead
}
BOOL rc = CloseHandle(mutex);
assert(!rc);
#else
int rc = pthread_mutex_destroy(&mutex);
if (!rc)
throw Mutex_error(strerror(rc)); // TODO: log instead
assert(!rc);
#endif
}
@ -74,14 +67,11 @@ Mutex::~Mutex(void)
void Mutex::lock(void)
{
#ifdef WINTHREAD
if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) {
TCHAR str[80];
throw Mutex_error(win_strerror(str, sizeof str));
}
DWORD rc = WaitForSingleObject(mutex, INFINITE);
assert(rc != WAIT_FAILED);
#else
int rc = pthread_mutex_lock(&mutex);
if (!rc)
throw Mutex_error(strerror(rc));
assert(!rc);
#endif
}
@ -91,13 +81,10 @@ void Mutex::lock(void)
void Mutex::unlock(void)
{
#ifdef WINTHREAD
if (!ReleaseMutex(mutex)) {
TCHAR str[80];
throw Mutex_error(win_strerror(str, sizeof str));
}
BOOL rc = ReleaseMutex(mutex);
assert(!rc);
#else
int rc = pthread_mutex_unlock(&mutex);
if (!rc)
throw Mutex_error(strerror(rc));
assert(!rc);
#endif
}

View File

@ -34,14 +34,13 @@
#define MUTEX_HPP
namespace Libsockthread {
class Mutex {
public:
Mutex(void); // throws Mutex_error
Mutex(void);
~Mutex(void);
void lock(void); // throws Mutex_error
void unlock(void); // throws Mutex_error
void lock(void);
void unlock(void);
private:
#ifdef WINTHREAD
HANDLE mutex;
@ -49,11 +48,6 @@ namespace Libsockthread {
pthread_mutex_t mutex;
#endif
};
class Mutex_error : public runtime_error {
Mutex_error(const string& s) : runtime_error(s) { }
};
}
#endif // MUTEX_HPP

View File

@ -40,7 +40,7 @@ Socket::Socket(int type)
{
#ifdef WINSOCK
winsock_startup();
#endif
}
#ifdef WINSOCK
@ -49,9 +49,8 @@ Socket::Socket(int type)
*/
void Socket::winsock_cleanup(void)
{
if (WSACleanup() == SOCKET_ERROR)
throw Socket_error("WSACleanup() failed (" +
winsock_strerror(WSAGetLastError()) + ")"); // TODO: log instead
int rc = WSACleanup();
assert(rc != SOCKET_ERROR);
}
/*

View File

@ -32,7 +32,6 @@
#define SOCKET_HPP
namespace Libsockthread {
class Socket {
public:
Socket(int type); // throws Socket error
@ -42,14 +41,15 @@ namespace Libsockthread {
#ifdef WINSOCK
void winsock_cleanup(void);
void winsock_startup(void); // throws Socket_error
const char* Socket::winsock_strerror(int code);
const char* winsock_strerror(int code);
#endif
};
class Socket_error : public runtime_error {
Socket_error(const string& s) : runtime_error(s) { }
public:
Socket_error(const string& s)
: runtime_error(s) { }
};
}
#endif // MUTEX_HPP

View File

@ -62,29 +62,26 @@ bool Thread::is_running(void)
/*
* Stops the thread
* Generally NOT a good idea
*
* Returns: true if the thread was killed, or false if it was already dead
*/
bool Thread::kill(void)
void Thread::kill(void)
{
running_m.lock();
#ifndef NDEBUG
// make sure it as actually running first
if (!running) {
running_m.unlock();
return false;
assert(false);
}
#endif
#ifdef WINTHREAD
if (!TerminateThread(handle, 0)) {
TCHAR str[80];
throw Thread_error(win_strerror(str, sizeof str)); // TODO: log instead
}
BOOL rc = TerminateThread(handle, 0);
assert(!rc);
#else
int rc = pthread_cancel(id);
if (!rc)
throw Thread_error(strerror(rc)); // TODO: log instead
assert(!rc);
#endif
running = false;
running_m.unlock();
return true;
}
/*
@ -101,16 +98,10 @@ void Thread::start(void)
continue_m.lock();
#ifdef WINTHREAD
handle = CreateThread(NULL, 0, &the_thread, this, 0, &id);
if (handle == NULL) {
TCHAR str[80];
throw Thread_error(win_strerror(str, sizeof str));
}
assert(handle != NULL);
#else
int rc = pthread_create(&id, NULL, &the_thread, this);
if (!rc) {
continue_m.unlock();
throw Thread_error(strerror(rc));
}
assert(!rc);
#endif
// Wait until `running' is set
running_m.lock();

View File

@ -34,10 +34,9 @@
#define THREAD_HPP
namespace Libsockthread {
class Thread {
public:
Thread(void) // throws Mutex_error (a mutex is created)
Thread(void)
: retval(0), running(false) { }
virtual ~Thread(void)
{ kill(); }
@ -45,8 +44,8 @@ namespace Libsockthread {
virtual void *execute(void) = 0;
void* get_retval(void);
bool is_running(void);
bool kill(void); // throws Thread_error
void start(void); // throws Thread_error
void kill(void);
void start(void);
private:
#ifdef WINTHREAD
@ -62,11 +61,6 @@ namespace Libsockthread {
Mutex running_m;
Mutex continue_m;
};
class Thread_error : public runtime_error {
Thread_error(const string& s) : runtime_error(s) { }
};
}
#endif // THREAD_HPP