Skip to content

Commit

Permalink
Folded platform specific code into master branch
Browse files Browse the repository at this point in the history
Using branches for ports, while flexible, was debatably a
questionable abuse of git branches. The model proved to be
unhelpfully annoying to manage and made the codebase
unnecessarily distributed.
  • Loading branch information
geky committed Jul 1, 2016
1 parent 6830476 commit 7586f7d
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 12 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
script:
- git config user.email "test"
- git config user.name "test"
- git fetch origin posix:posix
- git merge posix --no-commit
- make test
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
TARGET = libevents.a


CC = gcc
AR = ar
SIZE = size

SRC += $(wildcard *.c sys/*.c)
SRC += $(wildcard *.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
ASM := $(SRC:.c=.s)

TESTS = tests/tests
TSRC += $(wildcard tests/*.c)
TOBJ := $(TSRC:.c=.o)
TDEP := $(TSRC:.c=.d)

ifdef DEBUG
CFLAGS += -O0 -g3 -DMU_DEBUG
CFLAGS += -fkeep-inline-functions
Expand All @@ -19,16 +23,20 @@ endif
ifdef WORD
CFLAGS += -m$(WORD)
endif
CFLAGS += -I.
CFLAGS += -std=c99
CFLAGS += -Wall -Winline
CFLAGS += -D_POSIX_C_SOURCE=200112L
CFLAGS += -D_XOPEN_SOURCE=500

LFLAGS += -lpthread
LFLAGS += -lrt


all: $(TARGET)

test: $(TOBJ) $(OBJ)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $(TESTS)
$(TESTS)

asm: $(ASM)

size: $(OBJ)
Expand All @@ -47,6 +55,8 @@ size: $(OBJ)

clean:
rm -f $(TARGET)
rm -f $(TESTS)
rm -f $(TOBJ) $(TDEP)
rm -f $(OBJ)
rm -f $(DEP)
rm -f $(ASM)
6 changes: 3 additions & 3 deletions events.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extern "C" {
#endif

// System specific files
#include "sys/events_tick.h"
#include "sys/events_mutex.h"
#include "sys/events_sema.h"
#include "events_tick.h"
#include "events_mutex.h"
#include "events_sema.h"


// Event/queue structures
Expand Down
102 changes: 102 additions & 0 deletions events_mbed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#if defined(__MBED__)

#include "events_tick.h"
#include "events_sema.h"
#include "events_mutex.h"

#include <stdbool.h>
#include "mbed.h"
#ifdef MBED_CONF_RTOS_PRESENT
#include "rtos.h"
#endif


// Ticker operations
static class GlobalTicker {
public:
GlobalTicker() {
_tick = 0;
_timer.start();
_ticker.attach_us(this, &GlobalTicker::step, (1 << 16) * 1000);
};

void step() {
_timer.reset();
_tick += 1 << 16;
}

unsigned tick() {
return _tick + (unsigned)_timer.read_ms();
}

private:
unsigned _tick;
Timer _timer;
Ticker _ticker;
} gticker;

unsigned events_tick() {
return gticker.tick();
}


// Mutex operations
int events_mutex_create(events_mutex_t *m) { return 0; }
void events_mutex_destroy(events_mutex_t *m) { }

void events_mutex_lock(events_mutex_t *m) {
*m = __get_PRIMASK();
__disable_irq();
}

void events_mutex_unlock(events_mutex_t *m) {
__set_PRIMASK(*m);
}


// Semaphore operations
#ifdef MBED_CONF_RTOS_PRESENT

static inline Semaphore *sema(events_sema_t *s) {
return static_cast<Semaphore*>(*s);
}

int events_sema_create(events_sema_t *s) {
*s = new Semaphore(0);
return sema(s) ? 0 : -1;
}

void events_sema_destroy(events_sema_t *s) {
delete sema(s);
}

void events_sema_release(events_sema_t *s) {
sema(s)->release();
}

bool events_sema_wait(events_sema_t *s, int ms) {
int t = sema(s)->wait(ms < 0 ? osWaitForever : ms);
return t > 0;
}

#else

// Semaphore operations
int events_sema_create(events_sema_t *s) { return 0; }
void events_sema_destroy(events_sema_t *s) {}
void events_sema_release(events_sema_t *s) {}

static void events_sema_wakeup() {}

bool events_sema_wait(events_sema_t *s, int ms) {
Timeout timeout;
timeout.attach_us(events_sema_wakeup, ms*1000);

__WFI();

return true;
}

#endif

#endif
8 changes: 7 additions & 1 deletion sys/events_mutex.h → events_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ extern "C" {
// If this type is safe in interrupt contexts, then
// the associated event queue will also be safe in
// interrupt contexts.
typedef struct {} events_mutex_t;
#if defined(__unix__)
#include <pthread.h>
typedef pthread_mutex_t events_mutex_t;
#elif defined(__MBED__)
typedef unsigned events_mutex_t;
#endif


// Mutex operations
int events_mutex_create(events_mutex_t *m);
Expand Down
91 changes: 91 additions & 0 deletions events_posix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#if defined(__unix__)

#include "events_tick.h"
#include "events_sema.h"
#include "events_mutex.h"

#include <time.h>


// Tick operations
#ifdef _POSIX_TIMERS

unsigned events_tick(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned)(ts.tv_sec*1000 + ts.tv_nsec/1000000);
}

#else
#include <sys/time.h>

unsigned events_tick(void) {
struct timeval tv;
gettimeofday(&tv, 0);
return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000);
}

#endif


// Mutex operations
int events_mutex_create(events_mutex_t *m) {
return pthread_mutex_init(m, 0);
}

void events_mutex_destroy(events_mutex_t *m) {
pthread_mutex_destroy(m);
}

void events_mutex_lock(events_mutex_t *m) {
pthread_mutex_lock(m);
}

void events_mutex_unlock(events_mutex_t *m) {
pthread_mutex_unlock(m);
}


int events_sema_create(events_sema_t *s) {
int err = pthread_mutex_init(&s->mutex, 0);
if (err) {
return err;
}

err = pthread_cond_init(&s->cond, 0);
if (err) {
return err;
}

return 0;
}

void events_sema_destroy(events_sema_t *s) {
pthread_mutex_destroy(&s->mutex);
pthread_cond_destroy(&s->cond);
}

void events_sema_release(events_sema_t *s) {
pthread_cond_signal(&s->cond);
}

bool events_sema_wait(events_sema_t *s, int ms) {
int err;
pthread_mutex_lock(&s->mutex);

if (ms < 0) {
err = pthread_cond_wait(&s->cond, &s->mutex);
} else {
ms += events_tick();
struct timespec ts = {
.tv_sec = ms/1000,
.tv_nsec = ms*1000000,
};
err = pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
}

pthread_mutex_unlock(&s->mutex);
return !err;
}

#endif
13 changes: 13 additions & 0 deletions sys/events_sema.h → events_sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ extern "C" {
//
// Optimal implementation is a binary semaphore,
// however a regular semaphore is sufficient.
#if defined(__unix__)
#include <pthread.h>
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
} events_sema_t;
#elif defined(__MBED__)
#ifdef MBED_CONF_RTOS_PRESENT
typedef void *events_sema_t;
#else
typedef struct {} events_sema_t;
#endif
#endif


// Semaphore operations
int events_sema_create(events_sema_t *s);
Expand Down
File renamed without changes.
Loading

0 comments on commit 7586f7d

Please sign in to comment.