Last active
July 4, 2023 02:16
-
-
Save SealtielFreak/10f3a7ed1c3f4fd3e72e635b369163cf to your computer and use it in GitHub Desktop.
uscheduler testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <setjmp.h> | |
#define MAX_AWAIT_JMP 8 | |
static int throw_signal; | |
static jmp_buf main_jmp; | |
static int current_index = -1; | |
static jmp_buf all_await_jmp[MAX_AWAIT_JMP]; | |
#define startjmp(signal) { signal = setjmp(main_jmp); } | |
#define restorelastawait(signal) { \ | |
if(current_index >= 0) { \ | |
int last_index = current_index; \ | |
current_index = -1; \ | |
longjmp(all_await_jmp[last_index], signal); \ | |
}} | |
#define getsignal() throw_signal | |
#define throwsignal(signal) longjmp(main_jmp, signal) | |
#define awaitsignal(index, signal) { \ | |
current_index = index; \ | |
int received_signal = setjmp(all_await_jmp[index]); \ | |
if(current_index >= 0) { \ | |
throw_signal = signal; \ | |
longjmp(main_jmp, signal); \ | |
} \ | |
if(received_signal >= 0) { \ | |
signal = received_signal; \ | |
} \ | |
} | |
#define TYPE_STATE unsigned short | |
#define MAX_STATE 32 | |
typedef TYPE_STATE (*FuncType)(void); | |
static FuncType all_state_array[MAX_STATE]; | |
#define changestate(_state) { return (TYPE_STATE) _state; } | |
#define CALL_STATE(_index) all_state_array[_index](); | |
#define DEFINE_STATE(_index) \ | |
TYPE_STATE state_##_index(void); \ | |
__attribute__((constructor)) \ | |
void register_state_##_index(void) { \ | |
all_state_array[_index] = state_##_index; \ | |
} \ | |
TYPE_STATE state_##_index(void) | |
enum MyState { | |
INIT, WAITING, REPEAT, STOP | |
}; | |
DEFINE_STATE(INIT) { | |
printf("Init...\n"); | |
changestate(WAITING); | |
} | |
DEFINE_STATE(WAITING) { | |
printf("Waiting\n"); | |
changestate(REPEAT); | |
} | |
DEFINE_STATE(REPEAT) { | |
printf("Repeat\n"); | |
changestate(STOP); | |
} | |
DEFINE_STATE(STOP) { | |
printf("Stop\n"); | |
changestate(STOP); | |
} | |
int main(void) { | |
int index = 0; | |
while (true) { | |
index = CALL_STATE(index); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <setjmp.h> | |
#define MAX_AWAIT_JMP 8 | |
static int throw_signal; | |
static jmp_buf main_jmp; | |
static int current_index = -1; | |
static jmp_buf all_await_jmp[MAX_AWAIT_JMP]; | |
#define startjmp(signal) { signal = setjmp(main_jmp); } | |
#define restorelastawait(signal) { \ | |
if(current_index >= 0) { \ | |
int last_index = current_index; \ | |
current_index = -1; \ | |
longjmp(all_await_jmp[last_index], signal); \ | |
}} | |
#define getsignal() throw_signal | |
#define throwsignal(signal) longjmp(main_jmp, signal) | |
#define awaitsignal(index, signal) { \ | |
current_index = index; \ | |
int received_signal = setjmp(all_await_jmp[index]); \ | |
if(current_index >= 0) { \ | |
throw_signal = signal; \ | |
longjmp(main_jmp, signal); \ | |
} \ | |
if(received_signal >= 0) { \ | |
signal = received_signal; \ | |
} \ | |
} | |
int main(void) { | |
startjmp(getsignal()); | |
printf("Current throw value: %i\n", getsignal()); | |
restorelastawait(5); | |
if (getsignal() == 0) { | |
throwsignal(1); | |
} else if (getsignal() == 1) { | |
throwsignal(2); | |
} else if (getsignal() == 2) { | |
throwsignal(3); | |
} else { | |
int my_signal = 4; | |
printf("Await signal...\n"); | |
awaitsignal(0, my_signal); | |
printf("Restore main thread, throw value: %i\n", my_signal); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <setjmp.h> | |
static int throw_signal; | |
static jmp_buf main_jmp; | |
#define startjmp() setjmp(main_jmp) | |
#define getsignal() throw_signal | |
#define throwsignal(signal) longjmp(main_jmp, signal) | |
int main(void) { | |
throw_signal = startjmp(); | |
printf("Current throw value: %i\n", getsignal()); | |
if (getsignal() == 0) { | |
throwsignal(1); | |
} else if (getsignal() == 1) { | |
throwsignal(2); | |
} else if (getsignal() == 2) { | |
throwsignal(3); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment