summaryrefslogtreecommitdiff
path: root/src/threadengines/threadengine_pthread.cpp
blob: 9fc9cc4b4f0a5b3d39ec17fbadd8a4c4e85f16da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
 * See: http://wiki.inspircd.org/Credits
 *
 * This program is free but copyrighted software; see
 *	    the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "inspircd.h"
#include "threadengines/threadengine_pthread.h"
#include <pthread.h>
#include <signal.h>

PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
{
}

static void* entry_point(void* parameter)
{
	/* Recommended by nenolod, signal safety on a per-thread basis */
	sigset_t set;
	sigemptyset(&set);
	sigaddset(&set, SIGPIPE);
	pthread_sigmask(SIG_BLOCK, &set, NULL);

	Thread* pt = reinterpret_cast<Thread*>(parameter);
	pt->Run();
	return parameter;
}


void PThreadEngine::Start(Thread* thread)
{
	PThreadData* data = new PThreadData;
	thread->state = data;

	if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
	{
		thread->state = NULL;
		delete data;
		throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
	}
}

PThreadEngine::~PThreadEngine()
{
}

void PThreadData::FreeThread(Thread* thread)
{
	thread->SetExitFlag(true);
	pthread_join(pthread_id, NULL);
}

MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
{
}

Mutex* MutexFactory::CreateMutex()
{
	return new PosixMutex();
}

PosixMutex::PosixMutex() : Mutex()
{
	pthread_mutex_init(&putex, NULL);
}

PosixMutex::~PosixMutex()
{
	pthread_mutex_destroy(&putex);
}

void PosixMutex::Enable(bool enable)
{
	if (enable)
		pthread_mutex_lock(&putex);
	else
		pthread_mutex_unlock(&putex);
}