/
ThreadManager.cpp
executable file
·159 lines (149 loc) · 3.6 KB
/
ThreadManager.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "ThreadManager.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <queue>
#include <sys/time.h>
using namespace std;
extern pthread_cond_t gEvent;
extern pthread_mutex_t gMutex;
namespace KIDD {
void WorkerTask::Run() {
while (!TestStop()) {
printf("WorkerTask::Run\n");
sleep(10);
} printf("out of WorkerTask::Run\n");
}
WorkerThread::WorkerThread(WorkerTask * ipTask):mSignaled(false) {
mpTask = ipTask;
//Init();
// gSignaled = true;
}
void WorkerThread::CancelTask() {
if (mpTask) {
mpTask->SetStop();
cout << "set stop" << endl;
} else
cout << "??\n";
pthread_join(mThreadID, NULL);
}
void WorkerThread::SetTask(WorkerTask* ipTask)
{
if(mpTask != NULL)
{
delete mpTask;
}
mpTask = ipTask;
}
bool WorkerThread::Init() {
int hr = 0;
hr = pthread_mutex_init(&mMutex, NULL);
if (hr) {
printf("mutex init failure\n");
return false;
}
pthread_cond_init(&mEvent, NULL);
if (hr) {
printf("cond init failure\n");
return false;
}
if (hr = pthread_create(&mThreadID, NULL, ThreadFunction, this)) {
printf("failed in creating a worker thread\n");
return false;
}
return true;
}
void WorkerThread::WaitForever() {
pthread_mutex_lock(&mMutex);
//sleep(1);
//cout << "start waiting forever" << pthread_self() << endl ;
if (!mSignaled) // if not using mSignaled, it seems the below function will block if pthread_cond_signal is triggered before this thread comes here
pthread_cond_wait(&mEvent, &mMutex);
//cout << "finished waiting forever in thread: " << pthread_self() << endl;
pthread_mutex_unlock(&mMutex);
}
void *WorkerThread::ThreadFunction(void *ipThread) {
WorkerThread *lpThread =
reinterpret_cast < WorkerThread * >(ipThread);
lpThread->Run();
}
void WorkerThread::Run() {
WaitForever();
mpTask->Run();
}
void WorkerThread::Start() {
Init();
pthread_mutex_lock(&mMutex);
pthread_cond_signal(&mEvent);
mSignaled = true;
pthread_mutex_unlock(&mMutex);
}
WorkerThread::~WorkerThread() {
Destroy();
}
void WorkerThread::Destroy() {
::pthread_cond_destroy(&mEvent);
::pthread_mutex_destroy(&mMutex);
if (!mpTask) {
delete mpTask;
mpTask = NULL;
cout << "mpTask now NULL \n";
}
}
void WorkerThread::ThreadJoin() {
if (mThreadID)
pthread_join(mThreadID, NULL);
}
typedef queue < char *>ServerAddrQ;
ServerAddrQ gServerQ;
typedef queue <short>ConnectionQ;
ConnectionQ gConnectionQ;
// below mutex must be initialized before calling the following 2 functions
pthread_mutex_t gServerQMutex;
char *PopServerQ() {
pthread_mutex_lock(&gServerQMutex);
if (gServerQ.empty()) {
pthread_mutex_unlock(&gServerQMutex);
return NULL;
}
char *lpServer = gServerQ.front();
gServerQ.pop();
pthread_mutex_unlock(&gServerQMutex);
return lpServer;
}
void PushServerQ(char *ipServer) {
pthread_mutex_lock(&gServerQMutex);
gServerQ.push(ipServer);
pthread_mutex_unlock(&gServerQMutex);
}
void InitGlobals() {
int hr = pthread_mutex_init(&gServerQMutex, NULL);
if (hr) {
perror("gServerQMutex");
}
}
void DestroyGlobals() {
::pthread_mutex_destroy(&gServerQMutex);
}
// maintaining a Q of connected sockets
int PopConnectionQ()
{
pthread_mutex_lock(&gServerQMutex);
if(gConnectionQ.empty())
{
pthread_mutex_unlock(&gServerQMutex);
return -1;
}
short lSockfd = gConnectionQ.front();
gConnectionQ.pop();
pthread_mutex_unlock(&gServerQMutex);
return lSockfd;
}
void PushConnectionQ(int iSockfd)
{
pthread_mutex_lock(&gServerQMutex);
gConnectionQ.push(iSockfd);
pthread_mutex_unlock(&gServerQMutex);
}
}