2014-03-27 13:15:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-12-03 20:18:37 +01:00
|
|
|
* @ingroup sys_pipe
|
2014-03-27 13:15:52 +01:00
|
|
|
* @{
|
|
|
|
* @file
|
|
|
|
* @brief Implementation for statically allocated pipes.
|
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "irq.h"
|
|
|
|
#include "pipe.h"
|
|
|
|
#include "sched.h"
|
|
|
|
|
|
|
|
typedef unsigned (*ringbuffer_op_t)(ringbuffer_t *restrict rb, char *buf, unsigned n);
|
|
|
|
|
|
|
|
static ssize_t pipe_rw(ringbuffer_t *rb,
|
|
|
|
void *buf,
|
|
|
|
size_t n,
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t **other_op_blocked,
|
|
|
|
thread_t **this_op_blocked,
|
2014-03-27 13:15:52 +01:00
|
|
|
ringbuffer_op_t ringbuffer_op)
|
|
|
|
{
|
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2014-03-27 13:15:52 +01:00
|
|
|
|
|
|
|
unsigned count = ringbuffer_op(rb, buf, n);
|
|
|
|
|
|
|
|
if (count > 0) {
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *other_thread = *other_op_blocked;
|
2014-03-27 13:15:52 +01:00
|
|
|
int other_prio = -1;
|
|
|
|
if (other_thread) {
|
|
|
|
*other_op_blocked = NULL;
|
|
|
|
other_prio = other_thread->priority;
|
|
|
|
sched_set_status(other_thread, STATUS_PENDING);
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-03-27 13:15:52 +01:00
|
|
|
|
|
|
|
if (other_prio >= 0) {
|
|
|
|
sched_switch(other_prio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
else if (*this_op_blocked || irq_is_in()) {
|
|
|
|
irq_restore(old_state);
|
2014-03-27 13:15:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2020-08-23 21:25:54 +02:00
|
|
|
*this_op_blocked = thread_get_active();
|
2014-03-27 13:15:52 +01:00
|
|
|
|
2020-08-23 21:25:54 +02:00
|
|
|
sched_set_status(thread_get_active(), STATUS_SLEEPING);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2014-03-27 13:15:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t pipe_read(pipe_t *pipe, void *buf, size_t n)
|
|
|
|
{
|
|
|
|
return pipe_rw(pipe->rb, (char *) buf, n,
|
|
|
|
&pipe->write_blocked, &pipe->read_blocked, ringbuffer_get);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t pipe_write(pipe_t *pipe, const void *buf, size_t n)
|
|
|
|
{
|
|
|
|
return pipe_rw(pipe->rb, (char *) buf, n,
|
|
|
|
&pipe->read_blocked, &pipe->write_blocked, (ringbuffer_op_t) ringbuffer_add);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pipe_init(pipe_t *pipe, ringbuffer_t *rb, void (*free)(void *))
|
|
|
|
{
|
|
|
|
*pipe = (pipe_t) {
|
|
|
|
.rb = rb,
|
|
|
|
.read_blocked = NULL,
|
|
|
|
.write_blocked = NULL,
|
|
|
|
.free = free,
|
|
|
|
};
|
|
|
|
}
|