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"
19 pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
21 PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
25 void PThreadEngine::Create(Thread* thread_to_init)
27 pthread_attr_t attribs;
28 pthread_attr_init(&attribs);
29 pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
30 pthread_t* MyPThread = new pthread_t;
32 /* Create a thread in a mutex. This prevents whacking the member value NewThread,
33 * and also prevents recursive creation of threads by mistake (instead, the thread
34 * will just deadlock itself)
38 if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
42 throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
45 pthread_attr_destroy(&attribs);
47 NewThread = thread_to_init;
48 NewThread->Creator = this;
49 NewThread->Extend("pthread", MyPThread);
51 /* Always unset a mutex if you set it */
54 /* Wait for the PThreadEngine::Run method to take a copy of the
55 * pointer and clear this member value
61 PThreadEngine::~PThreadEngine()
65 void PThreadEngine::Run()
67 /* Take a copy of the member value, then clear it. Do this
68 * in a mutex so that we can be sure nothing else is looking
72 Thread* nt = NewThread;
75 /* Now we have our own safe copy, call the object on it */
79 bool PThreadEngine::Mutex(bool enable)
82 pthread_mutex_lock(&MyMutex);
84 pthread_mutex_unlock(&MyMutex);
89 void* PThreadEngine::Entry(void* parameter)
91 /* Recommended by nenolod, signal safety on a per-thread basis */
94 sigaddset(&set, SIGPIPE);
95 if(pthread_sigmask(SIG_BLOCK, &set, NULL))
96 signal(SIGPIPE, SIG_IGN);
98 ThreadEngine * pt = (ThreadEngine*)parameter;
103 void PThreadEngine::FreeThread(Thread* thread)
105 pthread_t* pthread = NULL;
106 if (thread->GetExt("pthread", pthread))
108 thread->SetExitFlag();
111 rc = pthread_join(*pthread, &status);
116 MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
120 Mutex* MutexFactory::CreateMutex()
122 return new PosixMutex(this->ServerInstance);
125 PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance)
127 pthread_mutex_init(&putex, NULL);
130 PosixMutex::~PosixMutex()
132 pthread_mutex_destroy(&putex);
135 void PosixMutex::Enable(bool enable)
138 pthread_mutex_lock(&putex);
140 pthread_mutex_unlock(&putex);