DWORD WINAPI ThreadFunc(void* thrd) {
tls_alloc_once::instance();
if(current_thread_tls_key == TLS_OUT_OF_INDEXES)
throw std::exception("tls alloc error");
if(!::TlsSetValue(current_thread_tls_key, thrd))
throw std::exception("tls setvalue error");
try {
static_cast<thread*>(thrd)->_data.run();
} catch(interrupt_exception&) {}
return 0;
}
void interruption_point() {
thread* thrd = get_current_thread_data();
if(!thrd) throw std::exception("no thread, wth");
if(thrd->_data.interrupted) {
thrd->_data.interrupted = false;
throw interrupt_exception();
}
}
thread* get_current_thread_data() {
if(current_thread_tls_key == TLS_OUT_OF_INDEXES) {
return NULL;
}
return (thread*)TlsGetValue(current_thread_tls_key);
}
}; // end of namespace thread
locker.h
[cpp]
#pragma once
#include "windows.h"
class locker {
public:
locker() {
::InitializeCriticalSection(&cs);
}
~locker() {
::DeleteCriticalSection(&cs);
}
void lock() const {
::EnterCriticalSection(&cs);
}
void unlock() const {
::LeaveCriticalSection(&cs);
}
private:
mutable ::CRITICAL_SECTION cs;
};
singleton.h