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

View File

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

View File

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

View File

@ -32,24 +32,24 @@
#define SOCKET_HPP #define SOCKET_HPP
namespace Libsockthread { namespace Libsockthread {
class Socket { class Socket {
public: public:
Socket(int type); // throws Socket error Socket(int type); // throws Socket error
void func(void); void func(void);
private: private:
#ifdef WINSOCK #ifdef WINSOCK
void winsock_cleanup(void); void winsock_cleanup(void);
void winsock_startup(void); // throws Socket_error void winsock_startup(void); // throws Socket_error
const char* Socket::winsock_strerror(int code); const char* winsock_strerror(int code);
#endif #endif
}; };
class Socket_error : public runtime_error { 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 #endif // MUTEX_HPP

View File

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

View File

@ -34,39 +34,33 @@
#define THREAD_HPP #define THREAD_HPP
namespace Libsockthread { namespace Libsockthread {
class Thread { class Thread {
public: public:
Thread(void) // throws Mutex_error (a mutex is created) Thread(void)
: retval(0), running(false) { } : retval(0), running(false) { }
virtual ~Thread(void) virtual ~Thread(void)
{ kill(); } { kill(); }
virtual void *execute(void) = 0; virtual void *execute(void) = 0;
void* get_retval(void); void* get_retval(void);
bool is_running(void); bool is_running(void);
bool kill(void); // throws Thread_error void kill(void);
void start(void); // throws Thread_error void start(void);
private: private:
#ifdef WINTHREAD #ifdef WINTHREAD
static DWORD WINAPI the_thread(void* param); static DWORD WINAPI the_thread(void* param);
HANDLE handle; HANDLE handle;
DWORD id; DWORD id;
#else #else
static void* the_thread(void* param); static void* the_thread(void* param);
pthread_t id; pthread_t id;
#endif #endif
void *retval; void *retval;
bool running; bool running;
Mutex running_m; Mutex running_m;
Mutex continue_m; Mutex continue_m;
}; };
class Thread_error : public runtime_error {
Thread_error(const string& s) : runtime_error(s) { }
};
} }
#endif // THREAD_HPP #endif // THREAD_HPP