/* * Main driver */ struct context *server_start(struct server_options *opt) { struct context *ctx = calloc(1, sizeof(*ctx)); ctx->opt = opt; ctx->stop_flag = 0; if (pipe(ctx->stop_pipe) != 0) { perror("pipe"); return NULL; } // Allocate socket queue. Adding 1 to number of threads because we // keep one slot open to distinguish between full and empty buffers ctx->q_size = opt->num_threads + 1; ctx->queue = ecalloc(ctx->q_size, sizeof(ctx->queue[0])); ctx->http_worker_args = emalloc(opt->num_threads * sizeof(struct http_worker_arg_t)); ctx->request_processor_args = emalloc(opt->num_threads * sizeof(struct request_processor_arg_t)); pthread_mutex_init(&ctx->mutex, NULL); pthread_cond_init(&ctx->sq_deq, NULL); pthread_cond_init(&ctx->sq_enq, NULL); pthread_cond_init(&ctx->term, NULL); start_worker_threads(ctx); start_master_thread(ctx); return ctx; }
int main(int argc, char* argv[]) { printf("Hello world from %s!\n", PACKAGE_STRING); struct gengetopt_args_info args; int res = cmdline_parser(argc, argv, &args_info); if (res != 0) exit(EXIT_FAILURE); printf("listening on %s:%hu\n", args.listen_address_arg, args.listen_port_arg); int server_sock; if (start_server(&args, &server_sock) != 0) err(EXIT_FAILURE, "Could not start server"); pthread_t workers[args.worker_count_arg]; start_worker_threads(&args_info, workers, args.worker_count_arg); join_worker_threads(workers, args.worker_count_args); return 0; }