summaryrefslogtreecommitdiff
path: root/src/threadengines/threadengine_pthread.cpp
blob: 8bdcd6d96cad3e3183848c6ca042cd85f829c406 (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
86
87
88
89
90
91
92
93
94
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *	    the file COPYING for details.
 *
 * ---------------------------------------------------
 */

/* $Core: libIRCDthreadengine */

/*********        DEFAULTS       **********/
/* $ExtraSources: socketengines/socketengine_pthread.cpp */
/* $ExtraObjects: socketengine_pthread.o */

/* $If: USE_WIN32 */
/* $ExtraSources: socketengines/socketengine_win32.cpp */
/* $ExtraObjects: socketengine_win32.o */
/* $EndIf */

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

pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;

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

void PThreadEngine::Create(Thread* thread_to_init)
{
	pthread_attr_t attribs;
	pthread_attr_init(&attribs);
	pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
	pthread_t* MyPThread = new pthread_t;

	if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
	{
		delete MyPThread;
		throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
	}

	pthread_attr_destroy(&attribs);

	NewThread = thread_to_init;
	NewThread->Creator = this;
	NewThread->Extend("pthread", MyPThread);
}

PThreadEngine::~PThreadEngine()
{
	//pthread_kill(this->MyPThread, SIGKILL);
}

void PThreadEngine::Run()
{
	NewThread->Run();
}

bool PThreadEngine::Mutex(bool enable)
{
	if (enable)
		pthread_mutex_lock(&MyMutex);
	else
		pthread_mutex_unlock(&MyMutex);

	return false;
}

void* PThreadEngine::Entry(void* parameter)
{
	ThreadEngine * pt = (ThreadEngine*)parameter;
	pt->Run();
	return NULL;
}

void PThreadEngine::FreeThread(Thread* thread)
{
	pthread_t* pthread = NULL;
	if (thread->GetExt("pthread", pthread))
	{
		thread->SetExitFlag();
		int rc;
		void* status;
		rc = pthread_join(*pthread, &status);
		delete pthread;
	}
}