Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit b28a02f

Browse files
authored
Merge pull request #140 from gao-feng/event2
[RFC] handle ttyfd out event priorly when write buffer is full
2 parents d3cfa23 + 9a6738d commit b28a02f

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/event.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ int hyper_modify_event(int efd, struct hyper_event *he, int flag)
9696
return 0;
9797
}
9898

99+
int hyper_requeue_event(int efd, struct hyper_event *ev)
100+
{
101+
struct epoll_event event = {
102+
.events = ev->flag,
103+
.data.ptr = ev,
104+
};
105+
106+
if (epoll_ctl(efd, EPOLL_CTL_DEL, ev->fd, NULL) < 0) {
107+
perror("epoll_ctl del fd failed");
108+
return -1;
109+
}
110+
111+
if (epoll_ctl(efd, EPOLL_CTL_ADD, ev->fd, &event) < 0) {
112+
perror("epoll_ctl add fd failed");
113+
return -1;
114+
}
115+
116+
return 0;
117+
}
118+
99119
static int hyper_getmsg_len(struct hyper_event *he, uint32_t *len)
100120
{
101121
struct hyper_buf *buf = &he->rbuf;
@@ -210,7 +230,9 @@ int hyper_event_write(struct hyper_event *he, int efd)
210230
memmove(buf->data, buf->data + len, buf->get);
211231

212232
if (buf->get == 0) {
213-
hyper_modify_event(ctl.efd, he, EPOLLIN);
233+
hyper_modify_event(ctl.efd, he, he->flag & ~(EPOLLOUT| EPOLLPRI));
234+
} else if (!FULL(buf)) {
235+
hyper_modify_event(ctl.efd, he, he->flag & ~EPOLLPRI);
214236
}
215237

216238
return 0;

src/event.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ struct hyper_event {
3232
void *ptr;
3333
};
3434

35+
#define FULL(buf) \
36+
(buf->size - buf->get <= 12)
37+
3538
int hyper_add_event(int efd, struct hyper_event *de, int flag);
3639
int hyper_modify_event(int efd, struct hyper_event *de, int flag);
40+
int hyper_requeue_event(int efd, struct hyper_event *ev);
3741
int hyper_init_event(struct hyper_event *de, struct hyper_event_ops *ops,
3842
void *arg);
3943
int hyper_handle_event(int efd, struct epoll_event *event);

src/exec.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,15 @@ static void stderr_hup(struct hyper_event *de, int efd)
106106
static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_exec *exec)
107107
{
108108
int size = -1;
109+
int flag = de->flag | EPOLLOUT;
109110
struct hyper_buf *buf = &ctl.tty.wbuf;
110111

111-
while ((buf->get + 12 < buf->size) && size) {
112+
if (FULL(buf)) {
113+
flag |= EPOLLPRI;
114+
goto out;
115+
}
116+
117+
do {
112118
size = read(de->fd, buf->data + buf->get + 12, buf->size - buf->get - 12);
113119
fprintf(stdout, "%s: read %d data\n", __func__, size);
114120
if (size < 0) {
@@ -124,16 +130,23 @@ static int pts_loop(struct hyper_event *de, uint64_t seq, int efd, struct hyper_
124130
}
125131
if (size == 0) { // eof
126132
pts_hup(de, efd, exec);
127-
break;
133+
return 0;
128134
}
129135

130136
hyper_set_be64(buf->data + buf->get, seq);
131137
hyper_set_be32(buf->data + buf->get + 8, size + 12);
132138
buf->get += size + 12;
133-
}
139+
} while (!FULL(buf));
134140

135-
if (hyper_modify_event(ctl.efd, &ctl.tty, EPOLLIN | EPOLLOUT) < 0) {
136-
fprintf(stderr, "modify ctl tty event to in & out failed\n");
141+
if (FULL(buf)) {
142+
flag |= EPOLLPRI;
143+
/* del & add event to move event to tail, this gives
144+
* other event a chance to write data to wbuf of tty. */
145+
hyper_requeue_event(ctl.efd, de);
146+
}
147+
out:
148+
if (hyper_modify_event(ctl.efd, &ctl.tty, flag) < 0) {
149+
fprintf(stderr, "modify ctl tty event to %d failed\n", flag);
137150
return -1;
138151
}
139152

0 commit comments

Comments
 (0)