aboutsummaryrefslogtreecommitdiff
blob: 4b94fc631740c2a48f1a4a68a0ce38d579f40e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
}