-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathptask.cpp
More file actions
182 lines (146 loc) · 2.77 KB
/
Copy pathptask.cpp
File metadata and controls
182 lines (146 loc) · 2.77 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "plog.h"
#include "ptask.h"
static void * task_start(void *arg)
{
PTask* task = static_cast<PTask*>(arg);
task->OnRun();
task->OnExit();
task->DelRef();
return NULL;
}
PTask::PTask()
: m_ref(1)
, m_efd(-1)
, m_exit(false)
{
pthread_mutex_init(&m_mutex, NULL);
m_eventfd = eventfd(0, EFD_NONBLOCK);;
m_efd = epoll_create(1);
this->AddInEvent(m_eventfd);
}
PTask::~PTask()
{
close(m_efd);
close(m_eventfd);
pthread_mutex_destroy(&m_mutex);
}
void PTask::Start()
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
int ret = pthread_create(&tid, &attr, task_start, this);
if (ret)
{
P_LOG("start thread err:%d", ret);
}
pthread_attr_destroy(&attr);
}
void PTask::OnExit()
{
pthread_mutex_lock(&m_mutex);
m_exit = true;
pthread_mutex_unlock(&m_mutex);
}
void PTask::AddRef()
{
pthread_mutex_lock(&m_mutex);
++m_ref;
pthread_mutex_unlock(&m_mutex);
}
void PTask::DelRef()
{
pthread_mutex_lock(&m_mutex);
--m_ref;
if (m_ref <= 0)
{
pthread_mutex_unlock(&m_mutex);
delete this;
}
else
{
pthread_mutex_unlock(&m_mutex);
}
}
int PTask::EnqueMsg(PTaskMsg& msg)
{
pthread_mutex_lock(&m_mutex);
if (m_exit)
{
pthread_mutex_unlock(&m_mutex);
return -1;
}
m_msgQue.push(msg);
pthread_mutex_unlock(&m_mutex);
uint64_t c = 1;
write(m_eventfd, &c, sizeof(c));
return 0;
}
void PTask::DequeMsg(std::vector<PTaskMsg>& msgs)
{
pthread_mutex_lock(&m_mutex);
while (m_msgQue.size())
{
msgs.push_back(m_msgQue.front());
m_msgQue.pop();
}
pthread_mutex_unlock(&m_mutex);
}
int PTask::AddInEvent(int sock)
{
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = sock;
int ret = epoll_ctl(m_efd, EPOLL_CTL_ADD, sock, &ev);
if (ret < 0)
{
P_LOG("add in event err:%d", errno);
return -1;
}
return 0;
}
int PTask::WaitEvent(struct epoll_event* outEvents, std::vector<PTaskMsg>& msgs, int timeout)
{
msgs.clear();
int ret = epoll_wait(m_efd, m_events, DEF_MAX_EVENTS, timeout);
int n = 0;
if (ret > 0)
{
for (int i = 0; i < ret; ++i)
{
struct epoll_event& ev = m_events[i];
if (ev.data.fd == m_eventfd)
{
uint64_t c;
if (-1 != read(m_eventfd, &c, sizeof(c)))
{
this->DequeMsg(msgs);
}
}
else
{
outEvents[n] = ev;
++n;
}
}
}
else if (ret < 0)
{
P_LOG("epoll wait err:%d", errno);
return -1;
}
return n;
}
void PTask::aquire_lock()
{
pthread_mutex_lock(&m_mutex);
}
void PTask::release_lock()
{
pthread_mutex_unlock(&m_mutex);
}