1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Multiple threads can now wait on the same pipe.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-01-18 17:10:44 +01:00
parent f676cf75f4
commit 83aeec2514
6 changed files with 135 additions and 24 deletions

View file

@ -82,6 +82,7 @@ elf.o \
process.o \ process.o \
initrd.o \ initrd.o \
thread.o \ thread.o \
event.o \
io.o \ io.o \
pipe.o \ pipe.o \
filesystem.o \ filesystem.o \

67
sortix/event.cpp Normal file
View file

@ -0,0 +1,67 @@
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along
with Sortix. If not, see <http://www.gnu.org/licenses/>.
event.cpp
Each thread can wait for an event to happen and be signaled when it does.
******************************************************************************/
#include "platform.h"
#include "thread.h"
#include "syscall.h"
#include "event.h"
namespace Sortix
{
Event::Event()
{
waiting = NULL;
}
Event::~Event()
{
if ( waiting )
{
Panic("Thread was waiting on event, but it went out of scope");
}
}
void Event::Register()
{
Thread* thread = CurrentThread();
if ( thread->event )
{
Panic("Thread tried to wait on an event, but was already waiting");
}
thread->event = this;
thread->eventnextwaiting = waiting;
waiting = thread;
}
void Event::Signal()
{
while ( waiting )
{
waiting->event = NULL;
Syscall::ScheduleResumption(waiting);
waiting = waiting->eventnextwaiting;
}
}
}

49
sortix/event.h Normal file
View file

@ -0,0 +1,49 @@
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along
with Sortix. If not, see <http://www.gnu.org/licenses/>.
event.h
Each thread can wait for an event to happen and be signaled when it does.
******************************************************************************/
#ifndef SORTIX_EVENT_H
#define SORTIX_EVENT_H
namespace Sortix
{
class Thread;
class Event
{
public:
Event();
~Event();
public:
void Register();
void Signal();
private:
Thread* waiting;
};
}
#endif

View file

@ -25,6 +25,7 @@
#include "platform.h" #include "platform.h"
#include <libmaxsi/error.h> #include <libmaxsi/error.h>
#include <libmaxsi/memory.h> #include <libmaxsi/memory.h>
#include "event.h"
#include "thread.h" #include "thread.h"
#include "process.h" #include "process.h"
#include "syscall.h" #include "syscall.h"
@ -48,8 +49,8 @@ namespace Sortix
size_t buffersize; size_t buffersize;
size_t bufferoffset; size_t bufferoffset;
size_t bufferused; size_t bufferused;
Thread* readwaiting; Event readevent;
Thread* writewaiting; Event writeevent;
public: public:
virtual ssize_t Read(byte* dest, size_t count); virtual ssize_t Read(byte* dest, size_t count);
@ -65,14 +66,10 @@ namespace Sortix
this->buffersize = buffersize; this->buffersize = buffersize;
this->bufferoffset = 0; this->bufferoffset = 0;
this->bufferused = 0; this->bufferused = 0;
this->readwaiting = NULL;
this->writewaiting = NULL;
} }
DevPipeStorage::~DevPipeStorage() DevPipeStorage::~DevPipeStorage()
{ {
if ( readwaiting ) { Syscall::ScheduleResumption(readwaiting); }
if ( writewaiting ) { Syscall::ScheduleResumption(writewaiting); }
delete[] buffer; delete[] buffer;
} }
@ -91,20 +88,13 @@ namespace Sortix
Memory::Copy(dest, buffer + bufferoffset, amount); Memory::Copy(dest, buffer + bufferoffset, amount);
bufferoffset = (bufferoffset + amount) % buffersize; bufferoffset = (bufferoffset + amount) % buffersize;
bufferused -= amount; bufferused -= amount;
if ( writewaiting ) writeevent.Signal();
{
Syscall::ScheduleResumption(writewaiting);
writewaiting = NULL;
}
if ( bufferused == 0 || amount == count ) { return amount; } if ( bufferused == 0 || amount == count ) { return amount; }
return amount + Read(dest + amount, count - amount); return amount + Read(dest + amount, count - amount);
} }
Error::Set(EWOULDBLOCK); Error::Set(EWOULDBLOCK);
readevent.Register();
// TODO: Only one thread can wait on a pipe at the same time.
ASSERT(readwaiting == NULL);
readwaiting = CurrentThread();
return -1; return -1;
} }
@ -120,20 +110,13 @@ namespace Sortix
if ( linear < amount ) { amount = linear; } if ( linear < amount ) { amount = linear; }
Memory::Copy(buffer + writeoffset, src, amount); Memory::Copy(buffer + writeoffset, src, amount);
bufferused += amount; bufferused += amount;
if ( readwaiting ) readevent.Signal();
{
Syscall::ScheduleResumption(readwaiting);
readwaiting = NULL;
}
if ( buffersize == bufferused || amount == count ) { return amount; } if ( buffersize == bufferused || amount == count ) { return amount; }
return amount + Write(src + amount, count - amount); return amount + Write(src + amount, count - amount);
} }
Error::Set(EWOULDBLOCK); Error::Set(EWOULDBLOCK);
writeevent.Register();
// TODO: Only one thread can wait on a pipe at the same time.
ASSERT(writewaiting == NULL);
writewaiting = CurrentThread();
return -1; return -1;
} }

View file

@ -42,6 +42,8 @@ namespace Sortix
process = NULL; process = NULL;
prevsibling = NULL; prevsibling = NULL;
nextsibling = NULL; nextsibling = NULL;
event = NULL;
eventnextwaiting = NULL;
sleepuntil = 0; sleepuntil = 0;
nextsleepingthread = NULL; nextsleepingthread = NULL;
schedulerlistprev = NULL; schedulerlistprev = NULL;
@ -62,6 +64,8 @@ namespace Sortix
prevsibling = NULL; prevsibling = NULL;
nextsibling = NULL; nextsibling = NULL;
state = forkfrom->state; state = forkfrom->state;
event = NULL;
eventnextwaiting = NULL;
sleepuntil = forkfrom->sleepuntil; sleepuntil = forkfrom->sleepuntil;
Maxsi::Memory::Copy(&registers, &forkfrom->registers, sizeof(registers)); Maxsi::Memory::Copy(&registers, &forkfrom->registers, sizeof(registers));
ready = false; ready = false;

View file

@ -29,6 +29,7 @@
namespace Sortix namespace Sortix
{ {
class Event;
class Process; class Process;
class Thread; class Thread;
@ -62,6 +63,12 @@ namespace Sortix
Thread* prevsibling; Thread* prevsibling;
Thread* nextsibling; Thread* nextsibling;
// These are used internally when a thread is waiting for an Event to
// happen. Consider them private.
public:
Event* event;
Thread* eventnextwaiting;
// These are some things used internally by the scheduler and should not be // These are some things used internally by the scheduler and should not be
// touched by anything but it. Consider it private. // touched by anything but it. Consider it private.
public: public: