2011-09-21 14:52:29 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
|
|
|
thread.h
|
|
|
|
Describes a thread belonging to a process.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef SORTIX_THREAD_H
|
|
|
|
#define SORTIX_THREAD_H
|
|
|
|
|
2011-09-15 16:38:40 -04:00
|
|
|
#include "signal.h"
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
namespace Sortix
|
|
|
|
{
|
2012-01-18 11:10:44 -05:00
|
|
|
class Event;
|
2011-09-21 14:52:29 -04:00
|
|
|
class Process;
|
|
|
|
class Thread;
|
|
|
|
|
|
|
|
// Adds a thread to the current process.
|
|
|
|
Thread* CreateThread(addr_t entry, size_t stacksize = 0);
|
|
|
|
Thread* CurrentThread();
|
|
|
|
|
2011-09-15 16:38:40 -04:00
|
|
|
typedef void (*sighandler_t)(int);
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
friend Thread* CreateThread(addr_t entry, size_t stacksize);
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum State { NONE, RUNNABLE, BLOCKING };
|
|
|
|
|
2011-09-15 16:38:40 -04:00
|
|
|
public:
|
|
|
|
static void Init();
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
private:
|
|
|
|
Thread();
|
|
|
|
Thread(const Thread* forkfrom);
|
2011-11-06 16:00:29 -05:00
|
|
|
void ResetCallbacks();
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
~Thread();
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t id;
|
|
|
|
Process* process;
|
2012-02-10 07:27:11 -05:00
|
|
|
pid_t pidbackup;
|
2012-03-02 07:51:03 -05:00
|
|
|
addr_t addrspacebackup;
|
2012-02-10 07:27:11 -05:00
|
|
|
bool terminated;
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* prevsibling;
|
|
|
|
Thread* nextsibling;
|
|
|
|
|
2012-01-18 11:10:44 -05:00
|
|
|
// These are used internally when a thread is waiting for an Event to
|
|
|
|
// happen. Consider them private.
|
|
|
|
public:
|
|
|
|
Event* event;
|
|
|
|
Thread* eventnextwaiting;
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
// These are some things used internally by the scheduler and should not be
|
|
|
|
// touched by anything but it. Consider it private.
|
|
|
|
public:
|
|
|
|
Thread* schedulerlistprev;
|
|
|
|
Thread* schedulerlistnext;
|
|
|
|
State state;
|
|
|
|
uintmax_t sleepuntil;
|
|
|
|
Thread* nextsleepingthread;
|
|
|
|
|
|
|
|
public:
|
|
|
|
addr_t stackpos;
|
|
|
|
size_t stacksize;
|
2011-09-15 16:38:40 -04:00
|
|
|
Signal* currentsignal;
|
|
|
|
SignalQueue signalqueue;
|
|
|
|
sighandler_t sighandler;
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
// After being created/forked, a thread is not inserted into the scheduler.
|
|
|
|
// Whenever the thread has been safely established within a process, then
|
|
|
|
// call Ready() to finalize the creation and insert it into the scheduler.
|
|
|
|
private:
|
|
|
|
bool ready;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void Ready();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPU::InterruptRegisters registers;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Thread* Fork();
|
|
|
|
void SaveRegisters(const CPU::InterruptRegisters* src);
|
|
|
|
void LoadRegisters(CPU::InterruptRegisters* dest);
|
2011-09-15 16:38:40 -04:00
|
|
|
void HandleSignal(CPU::InterruptRegisters* regs);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void HandleSignalCPU(CPU::InterruptRegisters* regs);
|
2011-11-06 17:06:32 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
void* scfunc;
|
|
|
|
size_t scsize;
|
|
|
|
size_t scstate[8];
|
2011-11-06 16:00:29 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
void (*onchildprocessexit)(Thread*, Process*);
|
2012-03-21 19:52:29 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|