Fix last commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
---
|
||||
BasedOnStyle: WebKit
|
||||
IndentWidth: 4
|
||||
---
|
||||
Language: Cpp
|
||||
AlignConsecutiveMacros: true
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: true
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
ColumnLimit: 100
|
||||
IncludeBlocks: Regroup
|
||||
IndentCaseLabels: false
|
||||
IndentWrappedFunctionNames: true
|
||||
PointerAlignment: Right
|
||||
ReflowComments: false
|
||||
SortIncludes: false
|
||||
SpaceAfterCStyleCast: true
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceAfterControlStatementKeyword: true
|
||||
BreakBeforeBraces: Attach
|
||||
---
|
||||
@@ -11,9 +11,9 @@ httpserver: $(OBJS)
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
# Auto-rebuild if Makefile changes
|
||||
bin/%.o: src/%.c Makefile
|
||||
bin/%.o: %.c Makefile
|
||||
@mkdir -p bin/deps/
|
||||
$(CC) $(CFLAGS) -MD -MF $(patsubst src/%.c,bin/deps/%.d,$<) -c -o $@ $<
|
||||
$(CC) $(CFLAGS) -MD -MF $(patsubst %.c,bin/deps/%.d,$<) -c -o $@ $<
|
||||
|
||||
include $(SRCS:%.c/bin/deps/%.d)
|
||||
|
||||
|
||||
+34
-26
@@ -5,11 +5,12 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char *EMPTY_STRING = "";
|
||||
static const char* EMPTY_STRING = "";
|
||||
|
||||
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key,
|
||||
const char *value) {
|
||||
HTTPHeaderList *new = malloc(sizeof(HTTPHeaderList));
|
||||
HTTPHeaderList* http_header_list_push(HTTPHeaderList* list, const char* key,
|
||||
const char* value)
|
||||
{
|
||||
HTTPHeaderList* new = malloc(sizeof(HTTPHeaderList));
|
||||
if (!new) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -32,9 +33,10 @@ HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key,
|
||||
return new;
|
||||
}
|
||||
|
||||
void free_http_header_list(HTTPHeaderList *list) {
|
||||
void free_http_header_list(HTTPHeaderList* list)
|
||||
{
|
||||
while (list) {
|
||||
HTTPHeaderList *next = list->next;
|
||||
HTTPHeaderList* next = list->next;
|
||||
free(list->key);
|
||||
free(list->value);
|
||||
free(list);
|
||||
@@ -42,8 +44,9 @@ void free_http_header_list(HTTPHeaderList *list) {
|
||||
}
|
||||
}
|
||||
|
||||
const char *http_header_list_search(HTTPHeaderList *list, const char *key,
|
||||
const char *def) {
|
||||
const char* http_header_list_search(HTTPHeaderList* list, const char* key,
|
||||
const char* def)
|
||||
{
|
||||
while (list) {
|
||||
if (strcmp(list->key, key) == 0) {
|
||||
return list->value;
|
||||
@@ -66,9 +69,10 @@ const char *http_header_list_search(HTTPHeaderList *list, const char *key,
|
||||
}
|
||||
|
||||
// if an error occured, req->uri is not allocated
|
||||
static HTTPRequestParseResult parse_method_uri_line(FILE *stream,
|
||||
size_t *restrict bytes_read,
|
||||
HTTPRequest *restrict req) {
|
||||
static HTTPRequestParseResult parse_method_uri_line(FILE* stream,
|
||||
size_t* restrict bytes_read,
|
||||
HTTPRequest* restrict req)
|
||||
{
|
||||
// allow for some leeway in passing incorrect methods
|
||||
char method[MAX_METHOD_LENGTH + 1];
|
||||
char uri[MAX_URI_LENGTH + 1];
|
||||
@@ -113,9 +117,10 @@ static HTTPRequestParseResult parse_method_uri_line(FILE *stream,
|
||||
|
||||
// return true if there are more headers and no error occurred, false otherwise
|
||||
// this will *not* free LIST if an error occurs
|
||||
static bool next_header(FILE *stream, size_t *restrict bytes_read,
|
||||
HTTPRequestParseResult *restrict res,
|
||||
HTTPHeaderList *restrict *restrict list) {
|
||||
static bool next_header(FILE* stream, size_t* restrict bytes_read,
|
||||
HTTPRequestParseResult* restrict res,
|
||||
HTTPHeaderList* restrict* restrict list)
|
||||
{
|
||||
char c = fgetc(stream);
|
||||
RETURN_IF_READ_ERROR(stream);
|
||||
if (c == '\r') {
|
||||
@@ -136,8 +141,7 @@ static bool next_header(FILE *stream, size_t *restrict bytes_read,
|
||||
ssize_t signed_bytes_read;
|
||||
#define S1(s) #s
|
||||
#define S(s) S1(s)
|
||||
int nconv =
|
||||
fscanf(stream,
|
||||
int nconv = fscanf(stream,
|
||||
// clang-format off
|
||||
"%" S(MAX_HEADER_KEY_LENGTH) "[a-zA-Z0-9.-]"
|
||||
":%c"
|
||||
@@ -161,8 +165,9 @@ static bool next_header(FILE *stream, size_t *restrict bytes_read,
|
||||
return true;
|
||||
}
|
||||
|
||||
HTTPRequestParseResult parse_http_request(FILE *stream,
|
||||
HTTPRequest *restrict out) {
|
||||
HTTPRequestParseResult parse_http_request(FILE* stream,
|
||||
HTTPRequest* restrict out)
|
||||
{
|
||||
out->uri = EMPTY_STRING;
|
||||
out->path = EMPTY_STRING;
|
||||
out->method = EMPTY_STRING;
|
||||
@@ -182,23 +187,25 @@ HTTPRequestParseResult parse_http_request(FILE *stream,
|
||||
return res;
|
||||
}
|
||||
|
||||
void free_http_request(HTTPRequest *restrict req) {
|
||||
void free_http_request(HTTPRequest* restrict req)
|
||||
{
|
||||
if (req->method != EMPTY_STRING) {
|
||||
free((char *) req->method);
|
||||
free((char*)req->method);
|
||||
}
|
||||
if (req->uri != EMPTY_STRING) {
|
||||
free((char *) req->uri);
|
||||
free((char*)req->uri);
|
||||
}
|
||||
free_http_header_list(req->headers);
|
||||
}
|
||||
|
||||
const char *status_code_to_message(int status, size_t *restrict length) {
|
||||
const char* status_code_to_message(int status, size_t* restrict length)
|
||||
{
|
||||
static const struct {
|
||||
int code;
|
||||
const char *msg;
|
||||
const char* msg;
|
||||
size_t size;
|
||||
} CODES[] = {
|
||||
#define P(s, m) {s, m, sizeof(m) - 1}
|
||||
#define P(s, m) { s, m, sizeof(m) - 1 }
|
||||
P(200, "OK"),
|
||||
P(201, "Created"),
|
||||
P(400, "Bad Request"),
|
||||
@@ -221,12 +228,13 @@ const char *status_code_to_message(int status, size_t *restrict length) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void format_http_response(FILE *stream, HTTPResponse *restrict resp) {
|
||||
void format_http_response(FILE* stream, HTTPResponse* restrict resp)
|
||||
{
|
||||
assert(status_code_to_message(resp->status, NULL));
|
||||
fprintf(stream, "HTTP/1.1 %d %s\r\n", resp->status,
|
||||
status_code_to_message(resp->status, NULL));
|
||||
fprintf(stream, "Content-Length: %zu\r\n", resp->body_length);
|
||||
for (HTTPHeaderList *h = resp->headers; h; h = h->next) {
|
||||
for (HTTPHeaderList* h = resp->headers; h; h = h->next) {
|
||||
fwrite(h->key, 1, h->key_length, stream);
|
||||
fwrite(": ", 1, 2, stream);
|
||||
fwrite(h->value, 1, h->value_length, stream);
|
||||
+3
-6
@@ -14,13 +14,11 @@ struct _HTTPHeaderList {
|
||||
HTTPHeaderList *next;
|
||||
};
|
||||
|
||||
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key,
|
||||
const char *value);
|
||||
HTTPHeaderList *http_header_list_push(HTTPHeaderList *list, const char *key, const char *value);
|
||||
|
||||
void free_http_header_list(HTTPHeaderList *list);
|
||||
|
||||
const char *http_header_list_search(HTTPHeaderList *list, const char *key,
|
||||
const char *def);
|
||||
const char *http_header_list_search(HTTPHeaderList *list, const char *key, const char *def);
|
||||
|
||||
typedef enum {
|
||||
HRPR_OK,
|
||||
@@ -39,8 +37,7 @@ typedef struct {
|
||||
HTTPHeaderList *headers;
|
||||
} HTTPRequest;
|
||||
|
||||
HTTPRequestParseResult parse_http_request(FILE *stream,
|
||||
HTTPRequest *restrict out);
|
||||
HTTPRequestParseResult parse_http_request(FILE *stream, HTTPRequest *restrict out);
|
||||
|
||||
void free_http_request(HTTPRequest *restrict req);
|
||||
|
||||
+44
-32
@@ -28,7 +28,7 @@ typedef struct {
|
||||
bool help_flag;
|
||||
uint32_t port;
|
||||
size_t parallelism;
|
||||
const char *address;
|
||||
const char* address;
|
||||
} GlobalFlags;
|
||||
|
||||
static const GlobalFlags DEFAULT_FLAGS = {
|
||||
@@ -40,14 +40,15 @@ static const GlobalFlags DEFAULT_FLAGS = {
|
||||
};
|
||||
|
||||
// Return false on failure, true on success
|
||||
static bool parse_uint(const char *str, uintmax_t min, uintmax_t max,
|
||||
uintmax_t *output) {
|
||||
static bool parse_uint(const char* str, uintmax_t min, uintmax_t max,
|
||||
uintmax_t* output)
|
||||
{
|
||||
if (isspace(*str) || *str == '+' || *str == '-') {
|
||||
log_error("malformed number: \"%s\"", str);
|
||||
return false;
|
||||
}
|
||||
errno = 0;
|
||||
char *endptr;
|
||||
char* endptr;
|
||||
uintmax_t conv = strtoumax(str, &endptr, 10);
|
||||
if (!*str || *endptr) {
|
||||
log_error("malformed number: \"%s\"", str);
|
||||
@@ -61,13 +62,14 @@ static bool parse_uint(const char *str, uintmax_t min, uintmax_t max,
|
||||
return true;
|
||||
}
|
||||
|
||||
static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
||||
static void parse_cli_options(int argc, const char** argv, GlobalFlags* flags)
|
||||
{
|
||||
constexpr size_t MAX_PARALLELISM = SIZE_MAX;
|
||||
constexpr uint32_t MAX_PORT = 65535;
|
||||
opterr = false;
|
||||
int c;
|
||||
char pretty_flag[8];
|
||||
while ((c = getopt(argc, (char *const *) argv, ":ha:p:")) >= 0) {
|
||||
while ((c = getopt(argc, (char* const*)argv, ":ha:p:")) >= 0) {
|
||||
if (isprint(c)) {
|
||||
snprintf(pretty_flag, sizeof(pretty_flag), "%c", optopt);
|
||||
} else {
|
||||
@@ -118,7 +120,8 @@ static void parse_cli_options(int argc, const char **argv, GlobalFlags *flags) {
|
||||
}
|
||||
}
|
||||
|
||||
static void print_help(FILE *file) {
|
||||
static void print_help(FILE* file)
|
||||
{
|
||||
fprintf(file,
|
||||
"usage: httpserver [-h] [-a ADDRESS] [-p PARALLELISM] <PORT>\n");
|
||||
fprintf(file, " -h print this message, then exit\n");
|
||||
@@ -128,19 +131,20 @@ static void print_help(FILE *file) {
|
||||
fprintf(file, " -a bind to ADDRESS (default: 127.0.0.1)\n");
|
||||
}
|
||||
|
||||
constexpr int WORKER_BLOCKED_SIGNALS[] = {SIGTERM, SIGINT, SIGHUP};
|
||||
constexpr size_t N_WORKER_BLOCKED_SIGNALS =
|
||||
sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
|
||||
constexpr int WORKER_BLOCKED_SIGNALS[] = { SIGTERM, SIGINT, SIGHUP };
|
||||
constexpr size_t N_WORKER_BLOCKED_SIGNALS = sizeof(WORKER_BLOCKED_SIGNALS) / sizeof(int);
|
||||
static bool shutdown_flag = false;
|
||||
|
||||
static void signal_handler(int signal) {
|
||||
static void signal_handler(int signal)
|
||||
{
|
||||
if (shutdown_flag) {
|
||||
_exit(signal == SIGTERM ? 0 : EXIT_FAILURE);
|
||||
}
|
||||
shutdown_flag = true;
|
||||
}
|
||||
|
||||
static void setup_signals() {
|
||||
static void setup_signals()
|
||||
{
|
||||
struct sigaction act = {
|
||||
.sa_handler = signal_handler,
|
||||
.sa_flags = 0,
|
||||
@@ -154,7 +158,8 @@ static void setup_signals() {
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_result_to_status(HTTPRequestParseResult res) {
|
||||
static int parse_result_to_status(HTTPRequestParseResult res)
|
||||
{
|
||||
switch (res) {
|
||||
case HRPR_OK:
|
||||
return 200;
|
||||
@@ -170,9 +175,10 @@ static int parse_result_to_status(HTTPRequestParseResult res) {
|
||||
}
|
||||
}
|
||||
|
||||
static void send_simple_response(FILE *stream, int status) {
|
||||
static void send_simple_response(FILE* stream, int status)
|
||||
{
|
||||
size_t status_msg_len;
|
||||
const char *status_msg = status_code_to_message(status, &status_msg_len);
|
||||
const char* status_msg = status_code_to_message(status, &status_msg_len);
|
||||
HTTPResponse resp = {
|
||||
.status = status,
|
||||
.headers = NULL,
|
||||
@@ -183,16 +189,18 @@ static void send_simple_response(FILE *stream, int status) {
|
||||
fputc('\n', stream);
|
||||
}
|
||||
|
||||
static void write_audit_log_entry(HTTPRequest *restrict req, int status) {
|
||||
static void write_audit_log_entry(HTTPRequest* restrict req, int status)
|
||||
{
|
||||
fprintf(stderr, "%s,%s,%d,%s\n", req->method, req->uri, status,
|
||||
http_header_list_search(req->headers, "Request-ID", "0"));
|
||||
}
|
||||
|
||||
static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
|
||||
static void handle_get_request(FILE* conn, HTTPRequest* restrict req)
|
||||
{
|
||||
int status = 200;
|
||||
struct stat statbuf;
|
||||
flockfile(stderr);
|
||||
FILE *file_handle = fopen(req->path, "r");
|
||||
FILE* file_handle = fopen(req->path, "r");
|
||||
if (!file_handle) {
|
||||
if (errno == ENOENT) {
|
||||
status = 404;
|
||||
@@ -234,15 +242,15 @@ static void handle_get_request(FILE *conn, HTTPRequest *restrict req) {
|
||||
fclose(file_handle);
|
||||
}
|
||||
|
||||
static ssize_t get_content_length(HTTPRequest *restrict req) {
|
||||
const char *text =
|
||||
http_header_list_search(req->headers, "Content-Length", NULL);
|
||||
static ssize_t get_content_length(HTTPRequest* restrict req)
|
||||
{
|
||||
const char* text = http_header_list_search(req->headers, "Content-Length", NULL);
|
||||
if (!text) {
|
||||
return 0;
|
||||
} else if (isspace(*text) || *text == '-' || *text == '+') {
|
||||
return -1;
|
||||
}
|
||||
char *endptr;
|
||||
char* endptr;
|
||||
uintmax_t conv = strtoumax(text, &endptr, 10);
|
||||
if (*endptr || (conv == UINTMAX_MAX && errno == ERANGE)
|
||||
|| conv > SIZE_MAX) {
|
||||
@@ -251,7 +259,8 @@ static ssize_t get_content_length(HTTPRequest *restrict req) {
|
||||
return conv;
|
||||
}
|
||||
|
||||
static void handle_put_request(FILE *conn, HTTPRequest *restrict req) {
|
||||
static void handle_put_request(FILE* conn, HTTPRequest* restrict req)
|
||||
{
|
||||
ssize_t content_length = get_content_length(req);
|
||||
if (content_length < 0) {
|
||||
write_audit_log_entry(req, 400);
|
||||
@@ -269,8 +278,8 @@ static void handle_put_request(FILE *conn, HTTPRequest *restrict req) {
|
||||
char read_buff[4096];
|
||||
while (content_length) {
|
||||
ssize_t read_size = fread(read_buff, 1,
|
||||
(size_t) content_length < sizeof(read_buff)
|
||||
? (size_t) content_length
|
||||
(size_t)content_length < sizeof(read_buff)
|
||||
? (size_t)content_length
|
||||
: sizeof(read_buff),
|
||||
conn);
|
||||
if (ferror(conn) || write(temp_fd, read_buff, read_size) < 0) {
|
||||
@@ -313,8 +322,9 @@ write_status_and_unlock:
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_connection(void *arg) {
|
||||
FILE *conn = arg;
|
||||
static void handle_connection(void* arg)
|
||||
{
|
||||
FILE* conn = arg;
|
||||
HTTPRequest req;
|
||||
HTTPRequestParseResult res = parse_http_request(conn, &req);
|
||||
if (res != HRPR_OK) {
|
||||
@@ -336,11 +346,13 @@ static void handle_connection(void *arg) {
|
||||
fclose(conn);
|
||||
}
|
||||
|
||||
static void fclose_free_func(void *file) {
|
||||
static void fclose_free_func(void* file)
|
||||
{
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
setenv("POSIXLY_CORRECT", "1", true);
|
||||
GlobalFlags flags = DEFAULT_FLAGS;
|
||||
parse_cli_options(argc, argv, &flags);
|
||||
@@ -355,11 +367,11 @@ int main(int argc, const char **argv) {
|
||||
for (size_t i = 0; i < N_WORKER_BLOCKED_SIGNALS; ++i) {
|
||||
sigaddset(&worker_block_set, WORKER_BLOCKED_SIGNALS[i]);
|
||||
}
|
||||
Server *server = make_server(flags.address, flags.port);
|
||||
Server* server = make_server(flags.address, flags.port);
|
||||
if (!server) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
ThreadPool *pool = make_thread_pool(flags.parallelism, worker_block_set);
|
||||
ThreadPool* pool = make_thread_pool(flags.parallelism, worker_block_set);
|
||||
if (!pool) {
|
||||
destroy_server(server);
|
||||
return EXIT_FAILURE;
|
||||
@@ -375,7 +387,7 @@ int main(int argc, const char **argv) {
|
||||
} else if (conn_fd < 0) {
|
||||
continue;
|
||||
}
|
||||
FILE *conn = fdopen(conn_fd, "w+");
|
||||
FILE* conn = fdopen(conn_fd, "w+");
|
||||
if (!conn) {
|
||||
close(conn_fd);
|
||||
continue;
|
||||
@@ -12,7 +12,8 @@ struct _Server {
|
||||
int socket;
|
||||
};
|
||||
|
||||
Server *make_server(const char *text_addr, uint32_t port) {
|
||||
Server* make_server(const char* text_addr, uint32_t port)
|
||||
{
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_port = htons(port),
|
||||
@@ -21,7 +22,7 @@ Server *make_server(const char *text_addr, uint32_t port) {
|
||||
log_error("bad IPv4 address: \"%s\"", text_addr);
|
||||
return NULL;
|
||||
}
|
||||
Server *server = malloc_safe(sizeof(Server));
|
||||
Server* server = malloc_safe(sizeof(Server));
|
||||
server->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server->socket < 0) {
|
||||
log_errno("socket");
|
||||
@@ -36,7 +37,7 @@ Server *make_server(const char *text_addr, uint32_t port) {
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
if (bind(server->socket, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
if (bind(server->socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
log_errno("bind");
|
||||
close(server->socket);
|
||||
free(server);
|
||||
@@ -51,11 +52,13 @@ Server *make_server(const char *text_addr, uint32_t port) {
|
||||
return server;
|
||||
}
|
||||
|
||||
void destroy_server(Server *server) {
|
||||
void destroy_server(Server* server)
|
||||
{
|
||||
close(server->socket);
|
||||
free(server);
|
||||
}
|
||||
|
||||
int server_accept(Server *server) {
|
||||
int server_accept(Server* server)
|
||||
{
|
||||
return accept(server->socket, NULL, NULL);
|
||||
}
|
||||
@@ -7,24 +7,25 @@
|
||||
|
||||
struct thread_pool_queue {
|
||||
Task task;
|
||||
void *arg;
|
||||
void* arg;
|
||||
FreeFunc ff;
|
||||
struct thread_pool_queue *next;
|
||||
struct thread_pool_queue* next;
|
||||
};
|
||||
|
||||
struct _ThreadPool {
|
||||
bool running;
|
||||
size_t nthreads;
|
||||
sigset_t thread_sig_mask;
|
||||
pthread_t *threads;
|
||||
pthread_t* threads;
|
||||
|
||||
pthread_cond_t queue_cnd;
|
||||
pthread_mutex_t queue_mtx;
|
||||
struct thread_pool_queue *queue;
|
||||
struct thread_pool_queue* queue;
|
||||
};
|
||||
|
||||
// return false if we need to stop
|
||||
static bool get_task(ThreadPool *pool, Task *task, void **task_arg) {
|
||||
static bool get_task(ThreadPool* pool, Task* task, void** task_arg)
|
||||
{
|
||||
pthread_mutex_lock(&pool->queue_mtx);
|
||||
if (!pool->running) {
|
||||
pthread_mutex_unlock(&pool->queue_mtx);
|
||||
@@ -36,7 +37,7 @@ static bool get_task(ThreadPool *pool, Task *task, void **task_arg) {
|
||||
pthread_mutex_unlock(&pool->queue_mtx);
|
||||
return false;
|
||||
}
|
||||
struct thread_pool_queue *ent = pool->queue;
|
||||
struct thread_pool_queue* ent = pool->queue;
|
||||
if (ent) {
|
||||
pool->queue = pool->queue->next;
|
||||
pthread_mutex_unlock(&pool->queue_mtx);
|
||||
@@ -49,19 +50,21 @@ static bool get_task(ThreadPool *pool, Task *task, void **task_arg) {
|
||||
abort();
|
||||
}
|
||||
|
||||
static void *pool_thread_function(void *arg) {
|
||||
ThreadPool *pool = arg;
|
||||
static void* pool_thread_function(void* arg)
|
||||
{
|
||||
ThreadPool* pool = arg;
|
||||
pthread_sigmask(SIG_SETMASK, &pool->thread_sig_mask, NULL);
|
||||
Task task;
|
||||
void *task_arg;
|
||||
void* task_arg;
|
||||
while (get_task(pool, &task, &task_arg)) {
|
||||
task(task_arg);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ThreadPool *make_thread_pool(size_t parallelism, sigset_t sig_mask) {
|
||||
ThreadPool *pool = malloc_safe(sizeof(ThreadPool));
|
||||
ThreadPool* make_thread_pool(size_t parallelism, sigset_t sig_mask)
|
||||
{
|
||||
ThreadPool* pool = malloc_safe(sizeof(ThreadPool));
|
||||
pthread_mutex_init(&pool->queue_mtx, NULL);
|
||||
pthread_cond_init(&pool->queue_cnd, NULL);
|
||||
pool->running = true;
|
||||
@@ -81,7 +84,8 @@ ThreadPool *make_thread_pool(size_t parallelism, sigset_t sig_mask) {
|
||||
return pool;
|
||||
}
|
||||
|
||||
void destroy_thread_pool(ThreadPool *pool) {
|
||||
void destroy_thread_pool(ThreadPool* pool)
|
||||
{
|
||||
pthread_mutex_lock(&pool->queue_mtx);
|
||||
pool->running = false;
|
||||
pthread_cond_broadcast(&pool->queue_cnd);
|
||||
@@ -92,9 +96,9 @@ void destroy_thread_pool(ThreadPool *pool) {
|
||||
free(pool->threads);
|
||||
pthread_mutex_destroy(&pool->queue_mtx);
|
||||
pthread_cond_destroy(&pool->queue_cnd);
|
||||
struct thread_pool_queue *queue = pool->queue;
|
||||
struct thread_pool_queue* queue = pool->queue;
|
||||
while (queue) {
|
||||
struct thread_pool_queue *next = queue->next;
|
||||
struct thread_pool_queue* next = queue->next;
|
||||
if (queue->ff) {
|
||||
queue->ff(queue->arg);
|
||||
}
|
||||
@@ -104,10 +108,10 @@ void destroy_thread_pool(ThreadPool *pool) {
|
||||
free(pool);
|
||||
}
|
||||
|
||||
void thread_pool_enqueue(ThreadPool *pool, Task task, void *arg, FreeFunc ff) {
|
||||
void thread_pool_enqueue(ThreadPool* pool, Task task, void* arg, FreeFunc ff)
|
||||
{
|
||||
pthread_mutex_lock(&pool->queue_mtx);
|
||||
struct thread_pool_queue *new =
|
||||
malloc_safe(sizeof(struct thread_pool_queue));
|
||||
struct thread_pool_queue* new = malloc_safe(sizeof(struct thread_pool_queue));
|
||||
new->task = task;
|
||||
new->arg = arg;
|
||||
new->ff = ff;
|
||||
+11
-6
@@ -7,9 +7,10 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
void *realloc_safe(void *oldptr, size_t size) {
|
||||
void* realloc_safe(void* oldptr, size_t size)
|
||||
{
|
||||
static const char OOM_MSG[] = "fatal: out of memory\n";
|
||||
void *ptr = realloc(oldptr, size);
|
||||
void* ptr = realloc(oldptr, size);
|
||||
if (size && !ptr) {
|
||||
fwrite(OOM_MSG, 1, sizeof(OOM_MSG) - 1, stderr);
|
||||
abort();
|
||||
@@ -17,12 +18,14 @@ void *realloc_safe(void *oldptr, size_t size) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *malloc_safe(size_t size) {
|
||||
void* malloc_safe(size_t size)
|
||||
{
|
||||
return realloc_safe(NULL, size);
|
||||
}
|
||||
|
||||
// asprintf is not POSIX
|
||||
int alloc_sprintf(char *restrict *restrict out, const char *restrict fmt, ...) {
|
||||
int alloc_sprintf(char* restrict* restrict out, const char* restrict fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
va_list args2;
|
||||
@@ -35,7 +38,8 @@ int alloc_sprintf(char *restrict *restrict out, const char *restrict fmt, ...) {
|
||||
return written;
|
||||
}
|
||||
|
||||
void log_error(const char *restrict fmt, ...) {
|
||||
void log_error(const char* restrict fmt, ...)
|
||||
{
|
||||
time_t cur_time = time(NULL);
|
||||
struct tm tm;
|
||||
localtime_r(&cur_time, &tm);
|
||||
@@ -49,6 +53,7 @@ void log_error(const char *restrict fmt, ...) {
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
|
||||
void log_errno(const char *detail) {
|
||||
void log_errno(const char* detail)
|
||||
{
|
||||
log_error("%s: %s", detail, strerror(errno));
|
||||
}
|
||||
+2
-2
@@ -4,9 +4,9 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#if __has_attribute(format)
|
||||
# define PRINTF_LIKE(i, j) __attribute__((format(printf, i, j)))
|
||||
#define PRINTF_LIKE(i, j) __attribute__((format(printf, i, j)))
|
||||
#else
|
||||
# define PRINTF_LIKE(i, j)
|
||||
#define PRINTF_LIKE(i, j)
|
||||
#endif
|
||||
|
||||
void *realloc_safe(void *oldptr, size_t size);
|
||||
Reference in New Issue
Block a user