1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "threadengines/threadengine_pthread.h"
18 pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
20 PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
24 void PThreadEngine::Create(Thread* thread_to_init)
26 pthread_attr_t attribs;
27 pthread_attr_init(&attribs);
28 pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
29 pthread_t* MyPThread = new pthread_t;
31 /* Create a thread in a mutex. This prevents whacking the member value NewThread,
32 * and also prevents recursive creation of threads by mistake (instead, the thread
33 * will just deadlock itself)
37 if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
41 throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
44 pthread_attr_destroy(&attribs);
46 NewThread = thread_to_init;
47 NewThread->Creator = this;
48 NewThread->Extend("pthread", MyPThread);
50 /* Always unset a mutex if you set it */
53 /* Wait for the PThreadEngine::Run method to take a copy of the
54 * pointer and clear this member value
60 PThreadEngine::~PThreadEngine()
64 void PThreadEngine::Run()
66 /* Take a copy of the member value, then clear it. Do this
67 * in a mutex so that we can be sure nothing else is looking
71 Thread* nt = NewThread;
74 /* Now we have our own safe copy, call the object on it */
78 bool PThreadEngine::Mutex(bool enable)
81 pthread_mutex_lock(&MyMutex);
83 pthread_mutex_unlock(&MyMutex);
88 void* PThreadEngine::Entry(void* parameter)
90 ThreadEngine * pt = (ThreadEngine*)parameter;
95 void PThreadEngine::FreeThread(Thread* thread)
97 pthread_t* pthread = NULL;
98 if (thread->GetExt("pthread", pthread))
100 thread->SetExitFlag();
103 rc = pthread_join(*pthread, &status);
108 MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
112 Mutex* MutexFactory::CreateMutex()
114 return new PosixMutex(this->ServerInstance);
117 PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance)
119 pthread_mutex_init(&putex, NULL);
122 PosixMutex::~PosixMutex()
124 pthread_mutex_destroy(&putex);
127 void PosixMutex::Enable(bool enable)
130 pthread_mutex_lock(&putex);
132 pthread_mutex_unlock(&putex);