Skip to content

Commit

Permalink
oscore: Add thread abstractions.
Browse files Browse the repository at this point in the history
Should allow for some more flexibility regarding OSes. FreeRTOS port should benefit from this. Theoretically.
  • Loading branch information
vifino committed Jun 4, 2018
1 parent 54374a4 commit f991962
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 71 deletions.
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ clean:

# --- Generic object conversion rule begins here ---
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ `cat $(@:.o=.incs) 2>/dev/null` $^
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ `cat $(@:.o=.incs) 2>/dev/null` $^

# --- Module compile info begins here ---
ifeq ($(STATIC),0)
# To build modules/X.so, link src/modules/X.o with information in an optional .libs file
modules/%.so: src/modules/%.o $(ML_OBJECTS)
mkdir -p modules
$(CC) $(CFLAGS) $(LDSOFLAGS) -o $@ $^ `cat src/modules/$*.libs 2>/dev/null`
$(CC) $(CFLAGS) $(LDFLAGS) $(LDSOFLAGS) -o $@ $^ `cat src/modules/$*.libs 2>/dev/null`
else
# To build all modwraps, run kslink
$(MODULES_WC) src/slloadcore.gen.c: $(MODULES_C) static/kslink
Expand Down
21 changes: 5 additions & 16 deletions src/modules/bgm_fish.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <oscore.h>
#include <string.h>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif

#include "timers.h"
#include "matrix.h"
#include "main.h"
#include "modloader.h"
#include "asl.h"

static int fish_fifo;
static pthread_t fish_thread;
static oscore_task fish_task;
// NOTE: fish_shutdown is maintained by the FISh thread,
// and is set by fish_pollshutdown.
static int fish_getch_buffer, fish_shutdown, fish_moduleno;
Expand All @@ -34,7 +30,7 @@ static char fish_getch_bufferval;

static void fish_panic(char * reason) {
printf("FISh died: %s\n", reason);
pthread_exit(0);
oscore_task_exit(0);
}

static void fish_pollshutdown() {
Expand Down Expand Up @@ -247,14 +243,7 @@ int init(int moduleno, char* argstr) {
fish_shutdown_mt = tmp[1];
fish_shutdown_ot = tmp[0];

pthread_create(&fish_thread, NULL, fish_thread_func, NULL);

// Name our thread.
#if defined(__linux__) || defined(__NetBSD__)
pthread_setname_np(fish_thread, "bgm_fish");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(fish_thread, "bgm_fish");
#endif
fish_task = oscore_task_create("bgm_fish", fish_thread_func, NULL);

return 0;
}
Expand Down Expand Up @@ -285,7 +274,7 @@ void reset(void) {
int deinit() {
char ch = 0;
write(fish_shutdown_mt, &ch, 1);
pthread_join(fish_thread, NULL);
oscore_task_join(fish_task);
close(fish_fifo);
close(fish_shutdown_mt);
close(fish_shutdown_ot);
Expand Down
1 change: 0 additions & 1 deletion src/modules/bgm_fish.libs

This file was deleted.

19 changes: 5 additions & 14 deletions src/modules/bgm_opc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <oscore.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif


#include "timers.h"
#include "matrix.h"
Expand All @@ -44,7 +40,7 @@ static ulong opc_mtlastframe;
#define OPC_MTCOUNTDOWN_MAX 100
#define FRAMETIME 10000
#define OPC_SNAKE
static pthread_t opc_thread;
static oscore_task opc_task;

typedef struct {
byte channel;
Expand Down Expand Up @@ -209,14 +205,9 @@ int init(int moduleno, char* argstr) {
opc_shutdown_fd_mt = tmp[1];
opc_shutdown_fd_ot = tmp[0];
opc_moduleno = moduleno;
pthread_create(&opc_thread, NULL, opc_thread_func, NULL);

// Name our thread.
#if defined(__linux__) || defined(__NetBSD__)
pthread_setname_np(opc_thread, "bgm_opc");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(opc_thread, "bgm_opc");
#endif
opc_task = oscore_task_create("bgm_opc", opc_thread_func, NULL);

return 0;
}

Expand Down Expand Up @@ -262,7 +253,7 @@ void reset(void) {
int deinit() {
char blah = 0;
if (write(opc_shutdown_fd_mt, &blah, 1) != -1)
pthread_join(opc_thread, NULL);
oscore_task_join(opc_task);
close(opc_shutdown_fd_mt);
close(opc_shutdown_fd_ot);
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/modules/bgm_opc.libs

This file was deleted.

19 changes: 4 additions & 15 deletions src/modules/bgm_pixelflut.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <oscore.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif


#include "timers.h"
#include "matrix.h"
#include "main.h"
Expand All @@ -42,7 +37,7 @@ static int px_pixelcount, px_clientcount;

static int px_mx, px_my;
static ulong px_mtlastframe;
static pthread_t px_thread;
static oscore_task px_task;

#define PX_MTCOUNTDOWN_MAX 100
#define FRAMETIME 10000
Expand Down Expand Up @@ -367,14 +362,8 @@ int init(int moduleno, char* argstr) {
px_shutdown_fd_mt = tmp[1];
px_shutdown_fd_ot = tmp[0];
px_moduleno = moduleno;
pthread_create(&px_thread, NULL, px_thread_func, NULL);
px_task = oscore_task_create("bgm_pixelflut", px_thread_func, NULL);

// Name our thread.
#if defined(__linux__) || defined(__NetBSD__)
pthread_setname_np(px_thread, "bgm_pixelflut");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(px_thread, "bgm_pixelflut");
#endif
return 0;
}

Expand Down Expand Up @@ -420,7 +409,7 @@ void reset(void) {
int deinit() {
char blah = 0;
if (write(px_shutdown_fd_mt, &blah, 1) != -1)
pthread_join(px_thread, NULL);
oscore_task_join(px_task);
close(px_shutdown_fd_mt);
close(px_shutdown_fd_ot);
return 0;
Expand Down
1 change: 0 additions & 1 deletion src/modules/bgm_pixelflut.libs

This file was deleted.

15 changes: 5 additions & 10 deletions src/modules/bgm_xyscope.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include <matrix.h>
#include <timers.h>
#include <graphics.h>
#include <oscore.h>
#include <stdio.h>
#include <random.h>
#include <alsa/asoundlib.h>
#include <math.h>
#include <pthread.h>

#define SAMPLE_RATE 48000
#define BUFFER_FRAMES 4096
Expand Down Expand Up @@ -53,7 +53,7 @@ static int moduleno;
static int doshutdown;
static int dotimeout;

static pthread_t scope_thread;
static oscore_task scope_task;

#define SM_ALGORITHM(sample, shr, sub) (((byte) (sample >> shr)) - sub)
#define LD_ALGORITHM(typ, shr, sub) \
Expand Down Expand Up @@ -277,13 +277,8 @@ int init(int modulen, char* argstr) {
free(bufferC);
return 1;
}
pthread_create(&scope_thread, NULL, thread_func, NULL);
// Name our thread.
#if defined(__linux__) || defined(__NetBSD__)
pthread_setname_np(scope_thread, "bgm_xyscope");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(scope_thread, "bgm_xyscope");
#endif

scope_thread = oscore_task_create("bgm_xyscope", thread_func, NULL);
return 0;
}

Expand Down Expand Up @@ -333,6 +328,6 @@ int draw(int argc, char* argv[]) {

int deinit(void) {
doshutdown = 1;
pthread_join(scope_thread, NULL);
oscore_task_join(scope_task);
return 0;
}
20 changes: 10 additions & 10 deletions src/modules/gfx_candyflow.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This is basically a slightly modified version of the affinematrix port,
/*
* This is basically a slightly modified version of the affinematrix port,
* much donated by @orithena from https://github.com/orithena/Arduino-LED-experiments/blob/master/MatrixDemo/XAffineFields.ino
*
* This is an effect that basically paints a "base canvas" via a function and then defines a "camera" that
Expand Down Expand Up @@ -51,7 +51,7 @@ static float runmod[runvar_count] = {
};

// the actual run variables
float runvar[runvar_count] = {
static float runvar[runvar_count] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
Expand Down Expand Up @@ -122,8 +122,8 @@ static inline int _min(int x, int y) {
int draw(int argc, char* argv[]) {
perf_start(modno);
increment_runvars();
// compose transformation matrix out of 9 input matrices

// compose transformation matrix out of 9 input matrices
// which are calculated from some of the run variables
matrix3_3 m = composem3( 9,
rotation3(cos(runvar[12]) * M_PI),
Expand All @@ -136,14 +136,14 @@ int draw(int argc, char* argv[]) {
rotation3(runvar[15]),
scale3(0.6+sin(runvar[8])/4.0, 0.6+cos(runvar[9])/4.0)
);

// pre-calculate some variables outside the loop
float pc1 = cosf(runvar[1]);
float pc01 = runvar[0] + pc1;
float pc10 = (mx2*sinf(runvar[10]));

perf_print(modno, "Composition");

// actual pixel loop
for( int x = 0; x < mx; x++ ) {
vec2 kernel_x = multm3v2_partx(m, x-(mx2));
Expand Down Expand Up @@ -171,12 +171,12 @@ int draw(int argc, char* argv[]) {
matrix_set(x,y, &color);
}
}

perf_print(modno, "Drawing");

// render it out
matrix_render();

perf_print(modno, "Rendering");

// manage framework variables
Expand Down
1 change: 1 addition & 0 deletions src/modules/gfx_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include "text.h"

Expand Down
19 changes: 19 additions & 0 deletions src/os/os_3ds.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ ulong oscore_udate(void) {
return T_SECOND * tv.tv_sec + tv.tv_usec;
}

// Threading
oscore_task oscore_thread_create(char* name, oscore_task_function func, void* ctx) {
// uuh
return NULL;
}

void oscore_task_yield(void) {
// nothing.
};

void oscore_task_exit(int status) {
// nope
};

int oscore_task_join(oscore_task task) {
// ye ok
return 0;
};

// -- mutex
// TODO: fill this in.
oscore_mutex oscore_mutex_new(void) {
Expand Down
20 changes: 20 additions & 0 deletions src/os/os_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ ulong oscore_udate(void) {
return T_SECOND * tv.tv_sec + tv.tv_usec;
}

// Threading
oscore_task oscore_thread_create(char* name, oscore_task_function func, void* ctx) {
// uuh
return NULL;
}

void oscore_task_yield(void) {
// nothing.
};

void oscore_task_exit(int status) {
// nope
};

int oscore_task_join(oscore_task task) {
// ye ok
return 0;
};


// -- mutex
oscore_mutex oscore_mutex_new(void) {
return NULL;
Expand Down
Loading

0 comments on commit f991962

Please sign in to comment.