1
0
mirror of https://github.com/anope/anope.git synced 2026-07-03 14:03:12 +02:00

Switch Thread to use the C++11 equivalent, remove win32/pthread.

This commit is contained in:
Sadie Powell
2024-02-11 20:17:48 +00:00
parent 2c5b84bd1d
commit 274bb19d03
6 changed files with 19 additions and 184 deletions
+3 -1
View File
@@ -14,6 +14,8 @@
#include "sockets.h"
#include "extensible.h"
#include <thread>
class CoreExport Thread
: public Pipe
, public Extensible
@@ -24,7 +26,7 @@ private:
public:
/* Handle for this thread */
pthread_t handle;
std::thread *handle = nullptr;
/** Threads destructor
*/
-1
View File
@@ -6,7 +6,6 @@ if(WIN32)
list(APPEND SRC_SRCS win32/windows.cpp)
list(APPEND SRC_SRCS win32/dl/dl.cpp)
list(APPEND SRC_SRCS win32/pipe/pipe.cpp)
list(APPEND SRC_SRCS win32/pthread/pthread.cpp)
list(APPEND SRC_SRCS win32/sigaction/sigaction.cpp)
endif()
+16 -27
View File
@@ -13,41 +13,21 @@
#include "threadengine.h"
#include "anope.h"
#ifndef _WIN32
#include <pthread.h>
#endif
static inline pthread_attr_t *get_engine_attr()
{
/* Threadengine attributes used by this thread engine */
static pthread_attr_t attr;
static bool inited = false;
if (inited == false)
{
if (pthread_attr_init(&attr))
throw CoreException("Error calling pthread_attr_init");
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))
throw CoreException("Unable to mark threads as joinable");
inited = true;
}
return &attr;
}
static void *entry_point(void *parameter)
{
Thread *thread = static_cast<Thread *>(parameter);
thread->Run();
thread->SetExitState();
pthread_exit(0);
delete thread->handle;
thread->handle = nullptr;
return NULL;
}
void Thread::Join()
{
this->SetExitState();
pthread_join(handle, NULL);
if (this->handle)
this->handle->join();
}
void Thread::SetExitState()
@@ -59,15 +39,24 @@ void Thread::SetExitState()
void Thread::Exit()
{
this->SetExitState();
pthread_exit(0);
if (this->handle)
{
delete this->handle;
this->handle = nullptr;
}
}
void Thread::Start()
{
if (pthread_create(&this->handle, get_engine_attr(), entry_point, this))
try
{
if (!this->handle)
this->handle = new std::thread(entry_point, this);
}
catch (const std::system_error& err)
{
this->flags[SF_DEAD] = true;
throw CoreException("Unable to create thread: " + Anope::LastError());
throw CoreException("Unable to create thread: " + std::string(err.what()));
}
}
-1
View File
@@ -53,7 +53,6 @@
#include "socket.h"
#include "dl/dl.h"
#include "pipe/pipe.h"
#include "pthread/pthread.h"
#include "sigaction/sigaction.h"
typedef int ssize_t;
-119
View File
@@ -1,119 +0,0 @@
/* POSIX emulation layer for Windows.
*
* (C) 2008-2024 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*/
#include "pthread.h"
struct ThreadInfo final
{
void *(*entry)(void *);
void *param;
};
static DWORD WINAPI entry_point(void *parameter)
{
ThreadInfo *ti = static_cast<ThreadInfo *>(parameter);
ti->entry(ti->param);
delete ti;
return 0;
}
int pthread_attr_init(pthread_attr_t *)
{
/* No need for this */
return 0;
}
int pthread_attr_setdetachstate(pthread_attr_t *, int)
{
/* No need for this */
return 0;
}
int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param)
{
auto *ti = new ThreadInfo;
ti->entry = entry;
ti->param = param;
*thread = CreateThread(NULL, 0, entry_point, ti, 0, NULL);
if (!*thread)
{
delete ti;
return -1;
}
return 0;
}
int pthread_join(pthread_t thread, void **)
{
if (WaitForSingleObject(thread, INFINITE) == WAIT_FAILED)
return -1;
CloseHandle(thread);
return 0;
}
void pthread_exit(int i)
{
ExitThread(i);
}
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *)
{
InitializeCriticalSection(mutex);
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
DeleteCriticalSection(mutex);
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
EnterCriticalSection(mutex);
return 0;
}
int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
return !TryEnterCriticalSection(mutex);
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
return 0;
}
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *)
{
*cond = CreateEvent(NULL, false, false, NULL);
if (*cond == NULL)
return -1;
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
return !CloseHandle(*cond);
}
int pthread_cond_signal(pthread_cond_t *cond)
{
return !PulseEvent(*cond);
}
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
WaitForSingleObject(*cond, INFINITE);
EnterCriticalSection(mutex);
return 0;
}
-35
View File
@@ -1,35 +0,0 @@
/* POSIX emulation layer for Windows.
*
* (C) 2008-2024 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*/
#include <Windows.h>
typedef HANDLE pthread_t;
typedef CRITICAL_SECTION pthread_mutex_t;
typedef HANDLE pthread_cond_t;
typedef int pthread_attr_t;
typedef void pthread_mutexattr_t;
typedef void pthread_condattr_t;
#define PTHREAD_CREATE_JOINABLE 0
extern int pthread_attr_init(pthread_attr_t *);
extern int pthread_attr_setdetachstate(pthread_attr_t *, int);
extern int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
extern int pthread_join(pthread_t, void **);
extern void pthread_exit(int);
extern int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
extern int pthread_mutex_destroy(pthread_mutex_t *);
extern int pthread_mutex_lock(pthread_mutex_t *);
extern int pthread_mutex_trylock(pthread_mutex_t *);
extern int pthread_mutex_unlock(pthread_mutex_t *);
extern int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
extern int pthread_cond_destroy(pthread_cond_t *);
extern int pthread_cond_signal(pthread_cond_t *);
extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);