Nice wrappers around process management
All checks were successful
Build documentation / build-and-deploy (push) Successful in 34s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 34s
This commit is contained in:
57
init/init.c
57
init/init.c
@@ -1,45 +1,15 @@
|
|||||||
#include <alloc/liballoc.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <m/status.h>
|
#include <proc/local.h>
|
||||||
#include <m/system.h>
|
#include <proc/proc.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string/string.h>
|
#include <string/string.h>
|
||||||
|
|
||||||
#define MUTEX 2000
|
#define MUTEX 2000
|
||||||
|
|
||||||
__thread char letter = 'c';
|
LOCAL char letter = 'c';
|
||||||
|
|
||||||
void app_thread1 (void);
|
void app_proc1 (void) {
|
||||||
|
|
||||||
int spawn (void (*fn) (void)) {
|
|
||||||
size_t stack_size = 256 * PAGE_SIZE;
|
|
||||||
void* stack = malloc (stack_size);
|
|
||||||
if (stack == NULL)
|
|
||||||
return -ST_OOM_ERROR;
|
|
||||||
|
|
||||||
uintptr_t stack_top = (uintptr_t)stack + stack_size;
|
|
||||||
return clone (stack_top, fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_main (void) {
|
|
||||||
mutex_create (MUTEX);
|
|
||||||
|
|
||||||
letter = 'a';
|
|
||||||
|
|
||||||
spawn (&app_thread1);
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
mutex_lock (MUTEX);
|
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++)
|
|
||||||
test (letter);
|
|
||||||
|
|
||||||
mutex_unlock (MUTEX);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_thread1 (void) {
|
|
||||||
letter = 'b';
|
letter = 'b';
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -51,5 +21,22 @@ void app_thread1 (void) {
|
|||||||
mutex_unlock (MUTEX);
|
mutex_unlock (MUTEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
quit ();
|
process_quit ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_main (void) {
|
||||||
|
mutex_create (MUTEX);
|
||||||
|
|
||||||
|
letter = 'a';
|
||||||
|
|
||||||
|
process_spawn (&app_proc1);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
mutex_lock (MUTEX);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
test (letter);
|
||||||
|
|
||||||
|
mutex_unlock (MUTEX);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
libmsl/proc/.gitignore
vendored
Normal file
1
libmsl/proc/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.o
|
||||||
6
libmsl/proc/local.h
Normal file
6
libmsl/proc/local.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef _LIBMSL_PROC_TLS_H
|
||||||
|
#define _LIBMSL_PROC_TLS_H
|
||||||
|
|
||||||
|
#define LOCAL __thread
|
||||||
|
|
||||||
|
#endif // _LIBMSL_PROC_TLS_H
|
||||||
17
libmsl/proc/proc.c
Normal file
17
libmsl/proc/proc.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <alloc/liballoc.h>
|
||||||
|
#include <m/status.h>
|
||||||
|
#include <m/system.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int process_spawn (process_func_t func) {
|
||||||
|
void* stack = malloc (PROC_STACK_SIZE);
|
||||||
|
if (stack == NULL)
|
||||||
|
return -ST_OOM_ERROR;
|
||||||
|
|
||||||
|
uintptr_t top = (uintptr_t)stack + PROC_STACK_SIZE;
|
||||||
|
return clone (top, func);
|
||||||
|
}
|
||||||
|
|
||||||
|
int process_quit (void) { return quit (); }
|
||||||
13
libmsl/proc/proc.h
Normal file
13
libmsl/proc/proc.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _LIBMSL_PROC_PROC_H
|
||||||
|
#define _LIBMSL_PROC_PROC_H
|
||||||
|
|
||||||
|
#include <m/system.h>
|
||||||
|
|
||||||
|
#define PROC_STACK_SIZE 256 * PAGE_SIZE
|
||||||
|
|
||||||
|
typedef void (*process_func_t) (void);
|
||||||
|
|
||||||
|
int process_spawn (process_func_t func);
|
||||||
|
int process_quit (void);
|
||||||
|
|
||||||
|
#endif // _LIBMSL_PROC_PROC_H
|
||||||
3
libmsl/proc/src.mk
Normal file
3
libmsl/proc/src.mk
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
c += proc/proc.c
|
||||||
|
|
||||||
|
o += proc/proc.o
|
||||||
@@ -3,3 +3,4 @@ include init/src.mk
|
|||||||
include m/src.mk
|
include m/src.mk
|
||||||
include string/src.mk
|
include string/src.mk
|
||||||
include alloc/src.mk
|
include alloc/src.mk
|
||||||
|
include proc/src.mk
|
||||||
|
|||||||
Reference in New Issue
Block a user