Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core refactor #155

Merged
merged 7 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move shared data structure setup/teardown to corresponding file
  • Loading branch information
Yao Yue committed Jul 3, 2017
commit ab5a666ccebc7e192d83b301441924e3906e1c6f
35 changes: 2 additions & 33 deletions src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
#include <channel/cc_pipe.h>

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sysexits.h>
#include <unistd.h>

struct buf_sock;
bool core_init = false;
Expand All @@ -31,35 +23,13 @@ core_setup(admin_options_st *opt_admin,
server_options_st *opt_server, worker_options_st *opt_worker,
server_metrics_st *smetrics, worker_metrics_st *wmetrics)
{
pipe_c = pipe_conn_create();
if (pipe_c == NULL) {
log_error("Could not create connection for pipe, abort");
goto error;
}

if (!pipe_open(NULL, pipe_c)) {
log_error("Could not open pipe connection: %s", strerror(pipe_c->err));
goto error;
}

pipe_set_nonblocking(pipe_c);

conn_arr = ring_array_create(sizeof(struct buf_sock *), RING_ARRAY_DEFAULT_CAP);
if (conn_arr == NULL) {
log_error("core setup failed: could not allocate conn array");
goto error;
}
core_shared_setup();

core_server_setup(opt_server, smetrics);
core_worker_setup(opt_worker, wmetrics);
core_admin_setup(opt_admin);

core_init = true;

return;

error:
exit(EX_CONFIG);
}

void
Expand All @@ -69,8 +39,7 @@ core_teardown(void)
core_worker_teardown();
core_server_teardown();

ring_array_destroy(conn_arr);
pipe_conn_destroy(&pipe_c);
core_shared_teardown();

core_init = false;
}
Expand Down
43 changes: 42 additions & 1 deletion src/core/data/shared.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
#include "shared.h"

#include <stdlib.h> /* for NULL */
#include <cc_ring_array.h>
#include <channel/cc_pipe.h>

#include <errno.h>
#include <string.h>
#include <sysexits.h>


/* needs to be initialized to avoid linker issues due to being optimized out */
struct pipe_conn *pipe_c = NULL;

struct ring_array *conn_arr = NULL;

void
core_shared_setup(void)
{
pipe_c = pipe_conn_create();
if (pipe_c == NULL) {
log_error("Could not create connection for pipe, abort");
goto error;
}

if (!pipe_open(NULL, pipe_c)) {
log_error("Could not open pipe connection: %s", strerror(pipe_c->err));
goto error;
}

pipe_set_nonblocking(pipe_c);

conn_arr = ring_array_create(sizeof(struct buf_sock *), RING_ARRAY_DEFAULT_CAP);
if (conn_arr == NULL) {
log_error("core setup failed: could not allocate conn array");
goto error;
}

error:
exit(EX_CONFIG);
}


void
core_shared_teardown(void)
{
ring_array_destroy(conn_arr);
pipe_conn_destroy(&pipe_c);
}

3 changes: 3 additions & 0 deletions src/core/data/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ extern struct pipe_conn *pipe_c;

/* array holding accepted connections */
extern struct ring_array *conn_arr;

void core_shared_setup(void);
void core_shared_teardown(void);