aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/server_initialize.c')
-rw-r--r--src/server/server_initialize.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/server/server_initialize.c b/src/server/server_initialize.c
new file mode 100644
index 0000000..4b94fc6
--- /dev/null
+++ b/src/server/server_initialize.c
@@ -0,0 +1,106 @@
+#include <signal.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "../core/index.h"
+#include "../parameters/parameters.h"
+
+#include "server.h"
+
+static int initialize_worker_collection
+(
+ struct JH_server_thread_collection c [const restrict static 1],
+ const struct JH_parameters params [const restrict static 1]
+)
+{
+ int error;
+
+ c->threads = (struct JH_server_thread_data *) NULL;
+ c->threads_capacity = 0;
+ c->currently_running = 0;
+
+ error =
+ pthread_mutex_init
+ (
+ &(c->mutex),
+ (const pthread_mutexattr_t *) NULL
+ );
+
+ if (error != 0)
+ {
+ JH_FATAL
+ (
+ stderr,
+ "Unable to initialize worker collection's mutex (error: %d): %s.",
+ error,
+ strerror(error)
+ );
+
+ return -1;
+ }
+
+ error =
+ pthread_barrier_init
+ (
+ &(c->barrier),
+ (const pthread_barrierattr_t *) NULL,
+ 2
+ );
+
+ if (error != 0)
+ {
+ pthread_mutex_destroy(&(c->mutex));
+
+ JH_FATAL
+ (
+ stderr,
+ "Unable to initialize worker collection's barrier (error: %d): %s.",
+ error,
+ strerror(error)
+ );
+
+ return -1;
+ }
+
+ return 0;
+}
+
+void initialize_thread_parameters
+(
+ struct JH_server server [const restrict static 1],
+ const struct JH_parameters params [const restrict static 1]
+)
+{
+ server->thread_params.thread_collection = &(server->workers);
+ server->thread_params.server_params = params;
+ server->thread_params.socket = -1;
+}
+
+int JH_server_initialize
+(
+ struct JH_server server [const restrict static 1],
+ const struct JH_parameters params [const restrict static 1]
+)
+{
+ if (initialize_worker_collection(&(server->workers), params) < 0)
+ {
+ return -1;
+ }
+
+ if
+ (
+ JH_server_socket_open
+ (
+ &(server->socket),
+ JH_parameters_get_socket_name(params)
+ ) < 0
+ )
+ {
+ return -2;
+ }
+
+ initialize_thread_parameters(server, params);
+
+ return 0;
+}