Skip to content

Commit

Permalink
Initializing Repo. EPOS 00 - base
Browse files Browse the repository at this point in the history
Co-routines: The minimal OS
  • Loading branch information
Leonardo Passig Horstmann committed Aug 21, 2024
1 parent 0910418 commit 1d428d6
Show file tree
Hide file tree
Showing 373 changed files with 45,458 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/hello/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <utility/ostream.h>

using namespace EPOS;

OStream cout;

int main()
{
cout << "Hello world!" << endl;

return 0;
}
136 changes: 136 additions & 0 deletions app/hello/hello_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#ifndef __traits_h
#define __traits_h

#include <system/config.h>

__BEGIN_SYS

// Build
template<> struct Traits<Build>: public Traits_Tokens
{
// Basic configuration
static const unsigned int SMOD = LIBRARY;
static const unsigned int ARCHITECTURE = ARMv8;
static const unsigned int MACHINE = Cortex;
static const unsigned int MODEL = Raspberry_Pi3;
static const unsigned int CPUS = 1;
static const unsigned int NETWORKING = STANDALONE;
static const unsigned int EXPECTED_SIMULATION_TIME = 60;

// Default flags
static const bool enabled = true;
static const bool debugged = true;
static const bool hysterically_debugged = false;
};


// Utilities
template<> struct Traits<Debug>: public Traits<Build>
{
static const bool error = true;
static const bool warning = true;
static const bool info = false;
static const bool trace = false;
};

template<> struct Traits<Lists>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Spin>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Heaps>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};

template<> struct Traits<Observers>: public Traits<Build>
{
// Some observed objects are created before initializing the Display
// Enabling debug may cause trouble in some Machines
static const bool debugged = false;
};


// System Parts (mostly to fine control debugging)
template<> struct Traits<Boot>: public Traits<Build>
{
};

template<> struct Traits<Setup>: public Traits<Build>
{
};

template<> struct Traits<Init>: public Traits<Build>
{
};

template<> struct Traits<Framework>: public Traits<Build>
{
};

template<> struct Traits<Aspect>: public Traits<Build>
{
static const bool debugged = hysterically_debugged;
};


__END_SYS

// Mediators
#include __ARCHITECTURE_TRAITS_H
#include __MACHINE_TRAITS_H

__BEGIN_SYS


// API Components
template<> struct Traits<Application>: public Traits<Build>
{
static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = Traits<Machine>::HEAP_SIZE;
static const unsigned int MAX_THREADS = Traits<Machine>::MAX_THREADS;
};

template<> struct Traits<System>: public Traits<Build>
{
static const bool multithread = (Traits<Application>::MAX_THREADS > 1);

static const unsigned long LIFE_SPAN = 1 * YEAR; // s
static const unsigned int DUTY_CYCLE = 1000000; // ppm

static const bool reboot = true;

static const unsigned int STACK_SIZE = Traits<Machine>::STACK_SIZE;
static const unsigned int HEAP_SIZE = (Traits<Application>::MAX_THREADS + 1) * Traits<Application>::STACK_SIZE;
};

template<> struct Traits<Thread>: public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
static const bool trace_idle = hysterically_debugged;
static const unsigned int QUANTUM = 500000; // us
};

template<> struct Traits<Scheduler<Thread>>: public Traits<Build>
{
static const bool debugged = Traits<Thread>::trace_idle || hysterically_debugged;
};

template<> struct Traits<Synchronizer>: public Traits<Build>
{
static const bool enabled = Traits<System>::multithread;
};

template<> struct Traits<Alarm>: public Traits<Build>
{
static const bool visible = hysterically_debugged;
};

__END_SYS

#endif
17 changes: 17 additions & 0 deletions app/hello/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EPOS Application Makefile

include ../../makedefs

all: install

$(APPLICATION): $(APPLICATION).o $(LIB)/*
$(ALD) $(ALDFLAGS) -o $@ $(APPLICATION).o

$(APPLICATION).o: $(APPLICATION).cc $(SRC)
$(ACC) $(ACCFLAGS) -o $@ $<

install: $(APPLICATION)
$(INSTALL) $(APPLICATION) $(IMG)

clean:
$(CLEAN) *.o $(APPLICATION)
14 changes: 14 additions & 0 deletions app/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EPOS Application Makefile

include ../makedefs

all: $(APPLICATION)

$(APPLICATION): FORCE
(cd $@ && $(MAKE))

clean:
make MAKE:="$(MAKECLEAN)" $(APPLICATION)

FORCE:

17 changes: 17 additions & 0 deletions app/philosophers_dinner/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EPOS Application Makefile

include ../../makedefs

all: install

$(APPLICATION): $(APPLICATION).o $(LIB)/*
$(ALD) $(ALDFLAGS) -o $@ $(APPLICATION).o

$(APPLICATION).o: $(APPLICATION).cc $(SRC)
$(ACC) $(ACCFLAGS) -o $@ $<

install: $(APPLICATION)
$(INSTALL) $(APPLICATION) $(IMG)

clean:
$(CLEAN) *.o $(APPLICATION)
116 changes: 116 additions & 0 deletions app/philosophers_dinner/philosophers_dinner.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// EPOS Scheduler Test Program

#include <machine/display.h>
#include <time.h>
#include <synchronizer.h>
#include <process.h>

using namespace EPOS;

const int iterations = 10;

Mutex table;

Thread * phil[5];
Semaphore * chopstick[5];

OStream cout;

int philosopher(int n, int l, int c);

int main()
{
table.lock();
Display::clear();
Display::position(0, 0);
cout << "The Philosopher's Dinner:" << endl;

for(int i = 0; i < 5; i++)
chopstick[i] = new Semaphore;

phil[0] = new Thread(&philosopher, 0, 5, 32);
phil[1] = new Thread(&philosopher, 1, 10, 44);
phil[2] = new Thread(&philosopher, 2, 16, 39);
phil[3] = new Thread(&philosopher, 3, 16, 24);
phil[4] = new Thread(&philosopher, 4, 10, 20);

cout << "Philosophers are alive and hungry!" << endl;

Display::position(7, 44);
cout << '/';
Display::position(13, 44);
cout << '\\';
Display::position(16, 35);
cout << '|';
Display::position(13, 27);
cout << '/';
Display::position(7, 27);
cout << '\\';
Display::position(19, 0);

cout << "The dinner is served ..." << endl;
table.unlock();

for(int i = 0; i < 5; i++) {
int ret = phil[i]->join();
table.lock();
Display::position(20 + i, 0);
cout << "Philosopher " << i << " ate " << ret << " times " << endl;
table.unlock();
}

for(int i = 0; i < 5; i++)
delete chopstick[i];
for(int i = 0; i < 5; i++)
delete phil[i];

cout << "The end!" << endl;

return 0;
}

int philosopher(int n, int l, int c)
{
int first = (n < 4)? n : 0;
int second = (n < 4)? n + 1 : 4;

for(int i = iterations; i > 0; i--) {

table.lock();
Display::position(l, c);
cout << "thinking";
table.unlock();

Delay thinking(1000000);

table.lock();
Display::position(l, c);
cout << " hungry ";
table.unlock();

chopstick[first]->p(); // get first chopstick
chopstick[second]->p(); // get second chopstick

table.lock();
Display::position(l, c);
cout << " eating ";
table.unlock();

Delay eating(500000);

table.lock();
Display::position(l, c);
cout << " sate ";
table.unlock();

chopstick[first]->v(); // release first chopstick
chopstick[second]->v(); // release second chopstick
}

table.lock();
Display::position(l, c);
cout << " done ";
table.unlock();

return iterations;
}
Loading

0 comments on commit 1d428d6

Please sign in to comment.