Add sleeping on accept & read
This commit is contained in:
parent
bb79c9c090
commit
a245c21d71
@ -254,7 +254,7 @@ void schedule(struct regs *r) {
|
||||
kprintf("PID %d Corrupted CR3 %p Current Task %d\n", current_task->next_task->pid, current_task->next_task->cr3, current_task->pid);
|
||||
}
|
||||
if (current_task->state == TASK_ZOMBIE) {
|
||||
if (current_task->parent_task->state = TASK_WAITING) {
|
||||
if (current_task->parent_task->state == TASK_WAITING) {
|
||||
wait_return = (unsigned int *)(current_task->parent_task->esp + 44);
|
||||
status = (unsigned int *)(current_task->parent_task->esp + 32);
|
||||
|
||||
|
@ -14,6 +14,10 @@
|
||||
#define TASK_SEM_WAIT 6
|
||||
#define TASK_ZOMBIE 7
|
||||
|
||||
#define SLEEP_USER 1
|
||||
#define SLEEP_TCP_ACCEPT 2
|
||||
#define SLEEP_TCP_READ 3
|
||||
|
||||
#ifndef KRNL_STACK_SIZE
|
||||
# define KRNL_STACK_SIZE 0x4000
|
||||
#endif
|
||||
@ -66,6 +70,7 @@ struct task_t {
|
||||
struct socket_t *waiting_sockets[25];
|
||||
int waiting_socket_count;
|
||||
unsigned int exit_status;
|
||||
int sleep_reason;
|
||||
char zombie_ttl;
|
||||
};
|
||||
|
||||
|
9
socket.c
9
socket.c
@ -114,6 +114,9 @@ struct socket_t *socket_find(unsigned int dport, unsigned int sport, unsigned in
|
||||
|
||||
new_socket->status = 0;
|
||||
task->waiting_sockets[task->waiting_socket_count++] = new_socket;
|
||||
if (task->state == TASK_SLEEPING && task->sleep_reason == SLEEP_TCP_ACCEPT) {
|
||||
task->state = TASK_RUNNING;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -211,6 +214,8 @@ int socket_read(struct socket_t *sock, char *buffer, int len) {
|
||||
return 0;
|
||||
}
|
||||
sock->offset = offset;
|
||||
((struct task_t *)sock->data)->sleep_reason = SLEEP_TCP_READ;
|
||||
((struct task_t *)sock->data)->state = TASK_SLEEPING;
|
||||
return -1; // return -1 to signal retry.
|
||||
}
|
||||
|
||||
@ -478,7 +483,9 @@ struct socket_t *socket_accept(struct socket_t *socket, struct inet_addr *client
|
||||
}
|
||||
return new_socket;
|
||||
}
|
||||
|
||||
task->state = TASK_SLEEPING;
|
||||
task->sleep_reason = SLEEP_TCP_ACCEPT;
|
||||
|
||||
return (void *)0;
|
||||
}
|
||||
|
||||
|
@ -229,6 +229,9 @@ void syscall_isr(struct regs *r) {
|
||||
break;
|
||||
case SYS_SOCK_READ:
|
||||
r->eax = socket_read((struct socket_t *)r->ebx, (char *)r->ecx, r->edx);
|
||||
if (r->eax == -1) {
|
||||
schedule(r);
|
||||
}
|
||||
break;
|
||||
case SYS_SOCK_WRITE:
|
||||
r->eax = socket_write((struct socket_t*)r->ebx, (unsigned char*)r->ecx, r->edx);
|
||||
@ -238,6 +241,9 @@ void syscall_isr(struct regs *r) {
|
||||
break;
|
||||
case SYS_SOCK_ACCEPT:
|
||||
r->eax = (unsigned int)socket_accept((struct socket_t *)r->ebx, (struct inet_addr *)r->ecx);
|
||||
if (r->eax == (void *)0) {
|
||||
schedule(r);
|
||||
}
|
||||
break;
|
||||
case SYS_MEM_INFO:
|
||||
r->eax = mem_get_info((struct mem_info *)r->ebx);
|
||||
|
4
tcp.c
4
tcp.c
@ -84,6 +84,10 @@ void tcp_process_packet(struct ether_t *ether, unsigned int dest, unsigned int s
|
||||
|
||||
|
||||
if (sock != (void *)0) {
|
||||
if (((struct task_t *)sock->data)->state == TASK_SLEEPING && ((struct task_t *)sock->data)->sleep_reason == SLEEP_TCP_READ) {
|
||||
((struct task_t *)sock->data)->state = TASK_RUNNING;
|
||||
}
|
||||
|
||||
if ((htons(packet->flags) & TCP_FLAGS_FIN) && (htons(packet->flags) & TCP_FLAGS_ACK)) {
|
||||
sock->tcp_sock.ack_number = htonl(packet->seq_number) + 1;
|
||||
tcp_send(ether, sock, TCP_FLAGS_ACK, (void *)0, 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user