summaryrefslogtreecommitdiff
path: root/os
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2012-08-07 16:54:36 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2012-08-07 16:54:36 +0200
commit09e89c867c8dc14adee298176a0a4c72d8f32245 (patch)
tree8d125de0b4a1c9d608ddae6ff927fc3624663bd0 /os
parent2fc77d271db27ecb140191c2dfafdba835962ffb (diff)
Split out reusable parts into the laks project.
Diffstat (limited to 'os')
-rw-r--r--os/mutex.h42
-rw-r--r--os/pool.cpp5
-rw-r--r--os/pool.h146
-rw-r--r--os/thread.cpp4
-rw-r--r--os/thread.h63
-rw-r--r--os/time.cpp3
-rw-r--r--os/time.h23
7 files changed, 0 insertions, 286 deletions
diff --git a/os/mutex.h b/os/mutex.h
deleted file mode 100644
index d12331d..0000000
--- a/os/mutex.h
+++ /dev/null
@@ -1,42 +0,0 @@
-#ifndef MUTEX_H
-#define MUTEX_H
-
-class Mutex {
- private:
- uint8_t locked;
- public:
- Mutex() : locked(0) {}
- Mutex(uint8_t l) : locked(l) {}
-
- bool trylock() {
- uint8_t val;
-
- // Check if mutex is locked.
- asm volatile ("ldrexb %0, [%1]" : "=r" (val) : "r" (&locked));
- if(val) {
- return false;
- }
-
- // Try taking the lock.
- asm volatile ("strexb %0, %1, [%2]" : "=r" (val) : "r" (1), "r" (&locked));
- if(val) {
- return false;
- }
-
- asm volatile("dmb");
- return true;
- }
-
- void lock() {
- while(!trylock()) {
- Thread::yield();
- }
- }
-
- void unlock() {
- asm volatile("dmb");
- locked = 0;
- }
-};
-
-#endif
diff --git a/os/pool.cpp b/os/pool.cpp
deleted file mode 100644
index f059b05..0000000
--- a/os/pool.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "pool.h"
-
-void* operator new(unsigned int, char* buf) {
- return (void*)buf;
-}
diff --git a/os/pool.h b/os/pool.h
deleted file mode 100644
index 1cb0a71..0000000
--- a/os/pool.h
+++ /dev/null
@@ -1,146 +0,0 @@
-#ifndef POOL_H
-#define POOL_H
-
-#include "stdint.h"
-
-template<class T>
-class BasePool {
- public:
- struct Element {
- unsigned int use_count;
- BasePool* pool;
-
- char data[sizeof(T)];
- };
-
- virtual void free(Element* e) = 0;
-};
-
-template<class T>
-class P {
- private:
- typedef typename BasePool<T>::Element Element;
-
- Element* e;
-
- void inc() {
- e->use_count++;
- }
-
- void dec() {
- e->use_count--;
- if(!e->use_count) {
- T* p = (T*)e->data;
- p->~T();
- e->pool->free(e);
- }
- }
-
- public:
- P() : e(0) {}
-
- explicit P(Element* ep) : e(ep) {
- inc();
- }
-
- P(const P& p) : e(p.e) {
- inc();
- }
-
- ~P() {
- if(e) {
- dec();
- }
- }
-
- void operator=(const P& p) {
- if(e) {
- dec();
- }
-
- e = p.e;
-
- if(e) {
- inc();
- }
- }
-
- void reset() {
- if(e) {
- dec();
- }
-
- e = 0;
- }
-
- T* operator->() {
- return (T*)e->data;
- }
-
- T* operator*() {
- return (T*)e->data;
- }
-
- operator bool() {
- return bool(e);
- }
-};
-
-template<class T, unsigned int size>
-class Pool : public BasePool<T> {
- private:
- typedef typename BasePool<T>::Element Element;
-
- union Node {
- Element e;
- Node* next;
- };
-
- Node elements[size];
-
- Node* next_free;
-
- void free(Element* e) {
- Node* n = (Node*)e;
-
- n->next = next_free;
- next_free = n;
- }
-
- Element* alloc() {
- if(!next_free) {
- return 0;
- }
-
- Element* e = &next_free->e;
- next_free = next_free->next;
-
- e->use_count = 0;
- e->pool = this;
-
- return e;
- }
-
- public:
- Pool() : next_free(0) {
- for(unsigned int i = 0; i < size; i++) {
- free(&elements[i].e);
- }
- }
-
- P<T> create() {
- Element* e = alloc();
-
- if(!e) {
- return P<T>();
- }
-
- new (e->data) T;
-
- return P<T>(e);
- }
-};
-
-void* operator new(unsigned int, char* buf);
-
-#endif
diff --git a/os/thread.cpp b/os/thread.cpp
deleted file mode 100644
index 426fffd..0000000
--- a/os/thread.cpp
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "thread.h"
-
-Thread Thread::main_thread __attribute__ ((init_priority (1000)));
-Thread* Thread::active_thread = &Thread::main_thread;
diff --git a/os/thread.h b/os/thread.h
deleted file mode 100644
index 2213d6f..0000000
--- a/os/thread.h
+++ /dev/null
@@ -1,63 +0,0 @@
-#ifndef THREAD_H
-#define THREAD_H
-
-#include <stdint.h>
-
-class Thread {
- friend void switch_context();
-
- private:
- struct int_frame_t {
- // Software saved.
- uint32_t r4;
- uint32_t r5;
- uint32_t r6;
- uint32_t r7;
- uint32_t r8;
- uint32_t r9;
- uint32_t r10;
- uint32_t r11;
- uint32_t lr_ex;
-
- // Hardware saved.
- uint32_t r0;
- uint32_t r1;
- uint32_t r2;
- uint32_t r3;
- uint32_t r12;
- uint32_t lr;
- uint32_t pc;
- uint32_t psr;
- };
-
- int_frame_t* sp;
-
- Thread* next;
-
- static Thread* active_thread;
- static Thread main_thread;
-
- Thread() : next(this) {}
-
- public:
- Thread(void* stack, uint32_t stack_size, void (*func)()) {
- sp = (int_frame_t*)((uint8_t*)stack + stack_size - sizeof(int_frame_t));
-
- sp->lr_ex = 0xfffffff9;
-
- // frame->lr = thread exit handler
- sp->pc = (uint32_t)func;
- sp->psr = 0x01000000;
- }
-
- void start() {
- next = active_thread->next;
- active_thread->next = this;
- }
-
- static inline void yield() {
- asm volatile("svc 0");
- }
-};
-
-#endif
diff --git a/os/time.cpp b/os/time.cpp
deleted file mode 100644
index 6c96027..0000000
--- a/os/time.cpp
+++ /dev/null
@@ -1,3 +0,0 @@
-#include "time.h"
-
-volatile uint32_t Time::systime;
diff --git a/os/time.h b/os/time.h
deleted file mode 100644
index f4706e0..0000000
--- a/os/time.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef TIME_H
-#define TIME_H
-
-#include "thread.h"
-
-class Time {
- private:
- static volatile uint32_t systime;
-
- public:
- inline static void tick() {
- systime++;
- }
-
- inline static void sleep(uint32_t ms) {
- ms += systime;
- while(systime < ms) {
- Thread::yield();
- }
- }
-};
-
-#endif