kernel: More lock fixes around pipes...

This commit is contained in:
K. Lange 2022-03-04 22:01:13 +09:00
parent 63fbbec928
commit e07291a0b1
2 changed files with 15 additions and 13 deletions

View File

@ -97,13 +97,12 @@ size_t ring_buffer_read(ring_buffer_t * ring_buffer, size_t size, uint8_t * buff
} }
wakeup_queue(ring_buffer->wait_queue_writers); wakeup_queue(ring_buffer->wait_queue_writers);
if (collected == 0) { if (collected == 0) {
if (ring_buffer->internal_stop) {
spin_unlock(ring_buffer->lock);
return 0;
}
if (sleep_on_unlocking(ring_buffer->wait_queue_readers, &ring_buffer->lock)) { if (sleep_on_unlocking(ring_buffer->wait_queue_readers, &ring_buffer->lock)) {
if (ring_buffer->internal_stop) { return -ERESTARTSYS;
ring_buffer->internal_stop = 0;
} else {
if (!collected) return -ERESTARTSYS;
}
break;
} }
} else { } else {
spin_unlock(ring_buffer->lock); spin_unlock(ring_buffer->lock);
@ -132,11 +131,10 @@ size_t ring_buffer_write(ring_buffer_t * ring_buffer, size_t size, uint8_t * buf
break; break;
} }
if (sleep_on_unlocking(ring_buffer->wait_queue_writers, &ring_buffer->lock)) { if (sleep_on_unlocking(ring_buffer->wait_queue_writers, &ring_buffer->lock)) {
if (ring_buffer->internal_stop) { if (!written) return -ERESTARTSYS;
ring_buffer->internal_stop = 0; break;
} else { }
if (!written) return -ERESTARTSYS; if (ring_buffer->internal_stop) {
}
break; break;
} }
} else { } else {
@ -198,7 +196,7 @@ void ring_buffer_destroy(ring_buffer_t * ring_buffer) {
void ring_buffer_interrupt(ring_buffer_t * ring_buffer) { void ring_buffer_interrupt(ring_buffer_t * ring_buffer) {
ring_buffer->internal_stop = 1; ring_buffer->internal_stop = 1;
wakeup_queue_interrupted(ring_buffer->wait_queue_readers); wakeup_queue(ring_buffer->wait_queue_readers);
wakeup_queue_interrupted(ring_buffer->wait_queue_writers); wakeup_queue(ring_buffer->wait_queue_writers);
} }

View File

@ -57,15 +57,18 @@ static ssize_t write_unixpipe(fs_node_t * node, off_t offset, size_t size, uint8
static void close_read_pipe(fs_node_t * node) { static void close_read_pipe(fs_node_t * node) {
struct unix_pipe * self = node->device; struct unix_pipe * self = node->device;
spin_lock(self->buffer->lock);
self->read_closed = 1; self->read_closed = 1;
if (!self->write_closed) { if (!self->write_closed) {
ring_buffer_interrupt(self->buffer); ring_buffer_interrupt(self->buffer);
} }
spin_unlock(self->buffer->lock);
} }
static void close_write_pipe(fs_node_t * node) { static void close_write_pipe(fs_node_t * node) {
struct unix_pipe * self = node->device; struct unix_pipe * self = node->device;
spin_lock(self->buffer->lock);
self->write_closed = 1; self->write_closed = 1;
if (!self->read_closed) { if (!self->read_closed) {
ring_buffer_interrupt(self->buffer); ring_buffer_interrupt(self->buffer);
@ -73,6 +76,7 @@ static void close_write_pipe(fs_node_t * node) {
ring_buffer_alert_waiters(self->buffer); ring_buffer_alert_waiters(self->buffer);
} }
} }
spin_unlock(self->buffer->lock);
} }
static int check_pipe(fs_node_t * node) { static int check_pipe(fs_node_t * node) {