Reworking threads. Thread states

This commit is contained in:
max 2025-02-03 14:58:50 +01:00
parent 282d93a97e
commit a07a3236e6
7 changed files with 61 additions and 14 deletions

7
.vscode/tasks.json vendored
View File

@ -3,10 +3,15 @@
{
"label": "build_start_server",
"dependsOn": [
"CMake: clean rebuild",
"cmake-build",
"netex_srv-gdbserver"
]
},
{
"type": "shell",
"label": "cmake-build",
"command": "cmake --build ."
},
{
"type": "shell",
"label": "netex_srv-gdbserver",

View File

@ -11,7 +11,7 @@
#define CHAT_PREFIX_COLOR 4
int execute_ncurses(void);
unsigned long start_ui(void);
typedef struct scr_size
{

View File

@ -1,10 +1,22 @@
#include <pthread.h>
#include "netex.h"
#include "nx_curses.h"
bool running;
pthread_t ui_thread;
void* ui_routine(void* args);
int execute_ncurses(void)
unsigned long start_ui(void)
{
const int thread_result = pthread_create(&ui_thread, NULL, ui_routine, NULL);
return thread_result;
}
void* ui_routine(void* args)
{
(void)args;
initscr();
noecho(); // Stop printing key presses
if (has_colors() && can_change_color())
@ -38,5 +50,6 @@ int execute_ncurses(void)
}
endwin();
return 0;
pthread_exit(0);
}

View File

@ -1,3 +1,5 @@
#include <pthread.h>
#include "netex.h"
#include "server.h"
@ -6,32 +8,48 @@
// https://stackoverflow.com/questions/40812836/how-to-create-chat-server-using-openssl-in-c
// https://www.cs.cmu.edu/~srini/15-441/F02/Projects/lab01/reference/part1.pdf
int shutdown(int code);
int main(int argc, char *argv[])
{
netex_init();
const unsigned long ui_thread = start_ui();
if (ui_thread != 0)
{
PRINT_LINE("Failed to start ui thread!");
return shutdown(-1);
}
// Start server thread
const int init_result = initialize_server(argc, argv);
if (init_result)
{
WARN("Failed to initialize server. Exiting...");
return init_result;
}
const int srv_start_result = server_start(); // Is now holding
if (!srv_start_result)
{
netex_shutdown();
return srv_start_result;
WARN("Error running server...");
}
if (!execute_ui())
// Join server thread
int ui_thread_result;
const int join_ui = pthread_join(ui_thread, (void**)&ui_thread_result);
if (join_ui != 0)
{
PRINT_LINE("Failed to setup srv windows!");
getch();
return 0;
// Something went wrong
WARN("Error exiting ui thread! Code: %i", ui_thread_result);
}
return shutdown(0);
}
int shutdown(int code)
{
netex_shutdown();
return 0;
return code;
}

View File

@ -0,0 +1,6 @@
#include <stdbool.h>
#include "server_state.h"
static bool is_server_running;
static bool shutdown_requested;

View File

@ -0,0 +1,7 @@
#ifndef SERVER_STATE
#define SERVER_STATE
extern bool is_server_running;
extern bool shutdown_requested;
#endif // SERVER_STATE

View File

@ -13,8 +13,6 @@ void calculate_window_sizes(void);
bool execute_ui(void)
{
// setup_curses(resize_func, keypressed_func, onexit_func);
execute_ncurses();
calculate_window_sizes();
if (!setup_info_window())
return false;