ref: 2b292d4de35874ea46074ab8aa18d71d515d844b
parent: 2d093d2651d333cbd4eec7747595da947eb4952b
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Mar 19 09:32:27 EDT 2021
sdl2: move threads logic into a separate file, implement SDL_SetThreadPriority, add SDL_CreateThreadWithStackSize
--- a/include/npe/SDL2/SDL_thread.h
+++ b/include/npe/SDL2/SDL_thread.h
@@ -2,7 +2,7 @@
#define _npe_SDL_thread_h_
enum {
- SDL_THREAD_PRIORITY_LOW = -1,
+ SDL_THREAD_PRIORITY_LOW,
SDL_THREAD_PRIORITY_NORMAL,
SDL_THREAD_PRIORITY_HIGH,
SDL_THREAD_PRIORITY_TIME_CRITICAL,
@@ -13,8 +13,11 @@
#pragma incomplete SDL_Thread
SDL_Thread *SDL_CreateThread(SDL_ThreadFunction, char *, void *);
+SDL_Thread *SDL_CreateThreadWithStackSize(SDL_ThreadFunction, const char *, size_t, void *);
+
void SDL_DetachThread(SDL_Thread *);
+void SDL_WaitThread(SDL_Thread *, int *);
+
void SDL_SetThreadPriority(int);
-void SDL_WaitThread(SDL_Thread *thread, int *status);
#endif
--- a/libnpe_sdl2/_sdl.h
+++ b/libnpe_sdl2/_sdl.h
@@ -1,3 +1,14 @@
+#include <SDL2/SDL.h>
+#include <sys/stat.h>
+#include <stdint.h>
+#include <tos.h>
+#include <draw.h>
+#include <memdraw.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <plumb.h>
+#include "_npe.h"
+
struct npe_sdl {
Mousectl *mctl;
Mouse m, om;
--- a/libnpe_sdl2/audio.c
+++ b/libnpe_sdl2/audio.c
@@ -1,4 +1,4 @@
-#include <SDL2/SDL.h>
+#include "_sdl.h"
enum {
Aout = 2,
--- a/libnpe_sdl2/events.c
+++ b/libnpe_sdl2/events.c
@@ -1,6 +1,3 @@
-#include <SDL2/SDL.h>
-#include <draw.h>
-#include <mouse.h>
#include "_sdl.h"
enum {
--- a/libnpe_sdl2/mkfile
+++ b/libnpe_sdl2/mkfile
@@ -9,6 +9,7 @@
audio.$O\
events.$O\
sdl2.$O\
+ threads.$O\
UPDATE=\
mkfile\
--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -1,14 +1,3 @@
-#include <npe.h>
-#include <stdint.h>
-#include <tos.h>
-#include <draw.h>
-#include <memdraw.h>
-#include <mouse.h>
-#include <cursor.h>
-#include <plumb.h>
-#include <SDL2/SDL.h>
-#include <sys/stat.h>
-#include "_npe.h"
#include "_sdl.h"
struct SDL_Window {
@@ -33,13 +22,6 @@
Point hot;
};
-struct SDL_Thread {
- SDL_ThreadFunction f;
- char *name;
- void *userdata;
- Channel *wait;
-};
-
static SDL_PixelFormat argb8888 = {
.format = SDL_PIXELFORMAT_ARGB8888,
};
@@ -136,63 +118,6 @@
return err;
}
-static void
-sdlthread(void *p)
-{
- SDL_Thread *t;
- int res;
-
- t = p;
- if(t->name != nil)
- threadsetname(t->name);
-
- res = t->f(t->userdata);
- send(t->wait, &res);
- chanfree(t->wait);
- free(t);
-
- threadexits(res == 0 ? nil : "error");
-}
-
-SDL_Thread *
-SDL_CreateThread(SDL_ThreadFunction f, char *name, void *userdata)
-{
- SDL_Thread *t;
-
- if((t = calloc(1, sizeof(*t))) == nil)
- return nil;
-
- t->f = f;
- t->name = name;
- t->userdata = userdata;
- t->wait = chancreate(sizeof(int), 0);
-
- if(proccreate(sdlthread, t, mainstacksize) < 0){
- chanfree(t->wait);
- free(t);
- t = nil;
- }
-
- return t;
-}
-
-void
-SDL_DetachThread(SDL_Thread *t)
-{
- chanclose(t->wait);
-}
-
-
-void
-SDL_WaitThread(SDL_Thread *t, int *status)
-{
- int r;
-
- recv(t->wait, &r);
- if(status != nil)
- *status = r;
-}
-
static void *
readfile(char *path, int *got)
{
@@ -964,12 +889,6 @@
{
/* most def */
return SDL_TRUE;
-}
-
-void
-SDL_SetThreadPriority(int)
-{
- /* nothing to do here */
}
void
--- /dev/null
+++ b/libnpe_sdl2/threads.c
@@ -1,0 +1,95 @@
+#include "_sdl.h"
+
+struct SDL_Thread {
+ SDL_ThreadFunction f;
+ const char *name;
+ void *userdata;
+ Channel *wait;
+};
+
+static int prio[] = {
+ [SDL_THREAD_PRIORITY_LOW] = 5,
+ [SDL_THREAD_PRIORITY_NORMAL] = 10,
+ [SDL_THREAD_PRIORITY_HIGH] = 13,
+ [SDL_THREAD_PRIORITY_TIME_CRITICAL] = 100,
+};
+
+static void sdlthread(void *p);
+
+SDL_Thread *
+SDL_CreateThreadWithStackSize(SDL_ThreadFunction f, const char *name, size_t stacksz, void *data)
+{
+ SDL_Thread *t;
+
+ if((t = calloc(1, sizeof(*t))) == nil)
+ return nil;
+
+ t->f = f;
+ t->name = name;
+ t->userdata = data;
+ t->wait = chancreate(sizeof(int), 0);
+
+ if(proccreate(sdlthread, t, stacksz) < 0){
+ chanfree(t->wait);
+ free(t);
+ t = nil;
+ }
+
+ return t;
+}
+
+SDL_Thread *
+SDL_CreateThread(SDL_ThreadFunction f, char *name, void *data)
+{
+ return SDL_CreateThreadWithStackSize(f, name, mainstacksize, data);
+}
+
+void
+SDL_DetachThread(SDL_Thread *t)
+{
+ chanclose(t->wait);
+}
+
+void
+SDL_WaitThread(SDL_Thread *t, int *status)
+{
+ int r;
+
+ recv(t->wait, &r);
+ if(status != nil)
+ *status = r;
+}
+
+void
+SDL_SetThreadPriority(int p)
+{
+ char t[32];
+ int f;
+
+ if(p < 0 || p >= nelem(prio))
+ return;
+
+ snprint(t, sizeof(t), "/proc/%d/ctl", getpid());
+ if((f = open(t, OWRITE)) >= 0){
+ fprint(f, "pri %d", prio[p]);
+ close(f);
+ }
+}
+
+static void
+sdlthread(void *p)
+{
+ SDL_Thread *t;
+ int res;
+
+ t = p;
+ if(t->name != nil)
+ threadsetname(t->name);
+
+ res = t->f(t->userdata);
+ send(t->wait, &res);
+ chanfree(t->wait);
+ free(t);
+
+ threadexits(res == 0 ? nil : "error");
+}