-
Notifications
You must be signed in to change notification settings - Fork 1
/
basicthread.cpp
72 lines (58 loc) · 2.24 KB
/
basicthread.cpp
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
#include "includes.h"
extern CLogger g_cSystemLogger;
RET_CODE CBasicThread::Start() {
m_bShutdownFlag = false;
m_bIsExitComplete = false;
int lnReturnValue = pthread_create(&m_nThreadId,
&m_stThreadAttributes,
ThreadProc,
this);
if (THREAD_API_SUCCESS != lnReturnValue) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: ThreadStart failed(%s)", LOG_LOCATION, strerror(lnReturnValue));
return FAILURE;
}
if (SYSTEM_CALL_ERROR == sem_wait(&m_cThreadStartWait)) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: Wait for ThreadStart failed(%s)", LOG_LOCATION, strerror(errno));
return FAILURE;
}
return SUCCESS;
}
RET_CODE CBasicThread::Join() {
if (m_bIsExitComplete) {
return SUCCESS;
}
if (false == m_bShutdownFlag) {
SetShutdown();
}
int lnReturnValue = pthread_join(m_nThreadId, NULL);
if (THREAD_API_SUCCESS != lnReturnValue) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: Thread Join failed(%s)", LOG_LOCATION, strerror(lnReturnValue));
//return FAILURE;
}
m_bIsExitComplete = true;
return SUCCESS;
}
void* CBasicThread::ThreadProc(void* pcBasicThread) {
CBasicThread* lpcBasicThread = static_cast<CBasicThread*> (pcBasicThread);
lpcBasicThread->NotifyThreadStartEvent();
void* lpcThreadParam = lpcBasicThread->GetThreadParam();
if (FAILURE == lpcBasicThread->Process(lpcThreadParam)) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: Thread Process failed", LOG_LOCATION);
}
pthread_exit(NULL);
}
void CBasicThread::NotifyThreadStartEvent() {
if (SYSTEM_CALL_ERROR == sem_post(&m_cThreadStartWait)) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: semaphore post failed(%s)", LOG_LOCATION, strerror(errno));
}
}
CBasicThread::~CBasicThread() {
Join();
int lnReturnValue = pthread_attr_destroy(&m_stThreadAttributes);
if (THREAD_API_SUCCESS != lnReturnValue) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: Thread attribute destroy failed(%s)", LOG_LOCATION, strerror(lnReturnValue));
}
if (SYSTEM_CALL_ERROR == sem_destroy(&m_cThreadStartWait)) {
g_cSystemLogger.LogMessage("(%s:%s:%d):: semaphore destroy failed(%s)", LOG_LOCATION, strerror(errno));
}
}