mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-24 21:30:39 +08:00
commit
2ece24e077
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -71,7 +71,7 @@ jobs:
|
|||||||
- if: ${{ env.GO == 1 }}
|
- if: ${{ env.GO == 1 }}
|
||||||
run: make test upload-coverage TFLAGS=-DNO_SNTP_CHECK SSL=OPENSSL ASAN_OPTIONS= OPENSSL=`echo /usr/local/Cellar/openssl*/*`
|
run: make test upload-coverage TFLAGS=-DNO_SNTP_CHECK SSL=OPENSSL ASAN_OPTIONS= OPENSSL=`echo /usr/local/Cellar/openssl*/*`
|
||||||
- if: ${{ env.GO == 1 }}
|
- if: ${{ env.GO == 1 }}
|
||||||
run: make test SSL=MBEDTLS TFLAGS="-DNO_SNTP_CHECK -DMG_ENABLE_ATOMIC=1" ASAN_OPTIONS= MBEDTLS=`echo /usr/local/Cellar/mbedtls*/*`
|
run: make test SSL=MBEDTLS TFLAGS=-DNO_SNTP_CHECK ASAN_OPTIONS= MBEDTLS=`echo /usr/local/Cellar/mbedtls*/*`
|
||||||
- if: ${{ env.GO == 1 }}
|
- if: ${{ env.GO == 1 }}
|
||||||
run: make mg_prefix
|
run: make mg_prefix
|
||||||
windows:
|
windows:
|
||||||
|
8
Makefile
8
Makefile
@ -1,6 +1,6 @@
|
|||||||
SRCS = mongoose.c test/unit_test.c test/packed_fs.c
|
SRCS = mongoose.c test/unit_test.c test/packed_fs.c
|
||||||
HDRS = $(wildcard src/*.h) $(wildcard src/tcpip/*.h)
|
HDRS = $(wildcard src/*.h) $(wildcard src/tcpip/*.h)
|
||||||
DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_SSI=1
|
DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_SSI=1 -DMG_ENABLE_ASSERT=1
|
||||||
WARN ?= -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common -Wconversion -Wundef
|
WARN ?= -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common -Wconversion -Wundef
|
||||||
OPTS ?= -O3 -g3
|
OPTS ?= -O3 -g3
|
||||||
INCS ?= -Isrc -I.
|
INCS ?= -Isrc -I.
|
||||||
@ -15,10 +15,10 @@ ASAN_OPTIONS ?= detect_leaks=1
|
|||||||
EXAMPLES := $(dir $(wildcard examples/*/Makefile))
|
EXAMPLES := $(dir $(wildcard examples/*/Makefile))
|
||||||
PREFIX ?= /usr/local
|
PREFIX ?= /usr/local
|
||||||
VERSION ?= $(shell cut -d'"' -f2 src/version.h)
|
VERSION ?= $(shell cut -d'"' -f2 src/version.h)
|
||||||
COMMON_CFLAGS ?= $(C_WARN) $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS)
|
COMMON_CFLAGS ?= $(C_WARN) $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS) -pthread
|
||||||
CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS) -pthread
|
CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS)
|
||||||
VALGRIND_CFLAGS ?= $(OPTS) $(COMMON_CFLAGS)
|
VALGRIND_CFLAGS ?= $(OPTS) $(COMMON_CFLAGS)
|
||||||
VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes
|
VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes --fair-sched=yes
|
||||||
.PHONY: examples test valgrind mip_test
|
.PHONY: examples test valgrind mip_test
|
||||||
|
|
||||||
ifeq "$(findstring ++,$(CC))" ""
|
ifeq "$(findstring ++,$(CC))" ""
|
||||||
|
@ -2,7 +2,7 @@ SOURCES = main.c mongoose.c # Source code files
|
|||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
||||||
|
|
||||||
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
|
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
|
||||||
CFLAGS_MONGOOSE += -DMG_ENABLE_ATOMIC=1
|
CFLAGS_MONGOOSE +=
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
# Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD
|
# Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD
|
||||||
|
@ -15,7 +15,7 @@ struct thread_data {
|
|||||||
struct mg_str body; // Copy of message body
|
struct mg_str body; // Copy of message body
|
||||||
};
|
};
|
||||||
|
|
||||||
static void start_thread(void (*f)(void *), void *p) {
|
static void start_thread(void *(*f)(void *), void *p) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define usleep(x) Sleep((x) / 1000)
|
#define usleep(x) Sleep((x) / 1000)
|
||||||
_beginthread((void(__cdecl *)(void *)) f, 0, p);
|
_beginthread((void(__cdecl *)(void *)) f, 0, p);
|
||||||
@ -25,18 +25,17 @@ static void start_thread(void (*f)(void *), void *p) {
|
|||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
(void) pthread_attr_init(&attr);
|
(void) pthread_attr_init(&attr);
|
||||||
(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||||
pthread_create(&thread_id, &attr, (void *(*) (void *) ) f, p);
|
pthread_create(&thread_id, &attr, f, p);
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void worker_thread(void *param) {
|
static void *worker_thread(void *param) {
|
||||||
struct thread_data *d = (struct thread_data *) param;
|
struct thread_data *d = (struct thread_data *) param;
|
||||||
char buf[100]; // On-stack buffer for the message queue
|
char buf[100]; // On-stack buffer for the message queue
|
||||||
|
|
||||||
d->queue.buf = buf; // Caller passed us an empty queue with NULL
|
mg_queue_init(&d->queue, buf, sizeof(buf)); // Init queue
|
||||||
d->queue.len = sizeof(buf); // buffer. Initialise it now
|
usleep(1 * 1000 * 1000); // Simulate long execution time
|
||||||
usleep(1 * 1000 * 1000); // Simulate long execution time
|
|
||||||
|
|
||||||
// Send a response to the connection
|
// Send a response to the connection
|
||||||
if (d->body.len == 0) {
|
if (d->body.len == 0) {
|
||||||
@ -48,9 +47,10 @@ static void worker_thread(void *param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait until connection reads our message, then it is safe to quit
|
// Wait until connection reads our message, then it is safe to quit
|
||||||
while (mg_queue_next(&d->queue, NULL) != MG_QUEUE_EMPTY) usleep(1000);
|
while (d->queue.tail != d->queue.head) usleep(1000);
|
||||||
MG_INFO(("done, cleaning up..."));
|
MG_INFO(("done, cleaning up..."));
|
||||||
free(d);
|
free(d);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTP request callback
|
// HTTP request callback
|
||||||
@ -68,10 +68,11 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|||||||
size_t len;
|
size_t len;
|
||||||
char *buf;
|
char *buf;
|
||||||
// Check if we have a message from the worker
|
// Check if we have a message from the worker
|
||||||
if (d != NULL && (len = mg_queue_next(&d->queue, &buf)) != MG_QUEUE_EMPTY) {
|
if (d != NULL && (len = mg_queue_next(&d->queue, &buf)) > 0) {
|
||||||
// Got message from worker. Send a response and cleanup
|
// Got message from worker. Send a response and cleanup
|
||||||
mg_http_reply(c, 200, "", "%.*s\n", (int) len, buf);
|
mg_http_reply(c, 200, "", "%.*s\n", (int) len, buf);
|
||||||
mg_queue_del(&d->queue); // Delete message: signal worker that we're done
|
mg_queue_del(&d->queue, len); // Delete message
|
||||||
|
*(void **) c->data = NULL; // Forget about thread data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(void) fn_data;
|
(void) fn_data;
|
||||||
|
98
mongoose.c
98
mongoose.c
@ -3496,7 +3496,7 @@ void mg_mgr_init(struct mg_mgr *mgr) {
|
|||||||
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
|
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
|
||||||
size_t len = mg_snprintf(NULL, 0, fmt, ap);
|
size_t len = mg_snprintf(NULL, 0, fmt, ap);
|
||||||
char *buf;
|
char *buf;
|
||||||
if (mg_queue_space(q, &buf) < len + 1) {
|
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1) {
|
||||||
len = 0; // Nah. Not enough space
|
len = 0; // Nah. Not enough space
|
||||||
} else {
|
} else {
|
||||||
len = mg_vsnprintf((char *)buf, len + 1, fmt, ap);
|
len = mg_vsnprintf((char *)buf, len + 1, fmt, ap);
|
||||||
@ -3612,43 +3612,85 @@ size_t mg_print_mac(void (*out)(char, void *), void *arg, va_list *ap) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// Every message in the queue is prepended by the message length (ML)
|
|
||||||
// ML is sizeof(size_t) in size
|
|
||||||
// Tail points to the message data
|
|
||||||
//
|
|
||||||
// |------| ML | message1 | ML | message2 |--- free space ---|
|
|
||||||
// ^ ^ ^ ^
|
|
||||||
// buf tail head len
|
|
||||||
|
|
||||||
size_t mg_queue_space(struct mg_queue *q, char **buf) {
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
size_t ofs;
|
#define MG_MEMORY_BARRIER() __sync_synchronize()
|
||||||
if (q->head > 0 && q->tail >= q->head) { // All messages read?
|
#elif defined(_MSC_VER) && _MSC_VER >= 1700
|
||||||
q->head = 0; // Yes. Reset head first
|
#define MG_MEMORY_BARRIER() MemoryBarrier()
|
||||||
q->tail = 0; // Now reset the tail
|
#elif !defined(MG_MEMORY_BARRIER)
|
||||||
|
#define MG_MEMORY_BARRIER()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Every message in a queue is prepended by a 32-bit message length (ML).
|
||||||
|
// If ML is 0, then it is the end, and reader must wrap to the beginning.
|
||||||
|
//
|
||||||
|
// Queue when q->tail <= q->head:
|
||||||
|
// |----- free -----| ML | message1 | ML | message2 | ----- free ------|
|
||||||
|
// ^ ^ ^ ^
|
||||||
|
// buf tail head len
|
||||||
|
//
|
||||||
|
// Queue when q->tail > q->head:
|
||||||
|
// | ML | message2 |----- free ------| ML | message1 | 0 |---- free ----|
|
||||||
|
// ^ ^ ^ ^
|
||||||
|
// buf head tail len
|
||||||
|
|
||||||
|
void mg_queue_init(struct mg_queue *q, char *buf, size_t size) {
|
||||||
|
q->size = size;
|
||||||
|
q->buf = buf;
|
||||||
|
q->head = q->tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t mg_queue_read_len(struct mg_queue *q) {
|
||||||
|
uint32_t n = 0;
|
||||||
|
MG_MEMORY_BARRIER();
|
||||||
|
memcpy(&n, q->buf + q->tail, sizeof(n));
|
||||||
|
assert(q->tail + n + sizeof(n) <= q->size);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_queue_write_len(struct mg_queue *q, size_t len) {
|
||||||
|
uint32_t n = (uint32_t) len;
|
||||||
|
memcpy(q->buf + q->head, &n, sizeof(n));
|
||||||
|
MG_MEMORY_BARRIER();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mg_queue_book(struct mg_queue *q, char **buf, size_t len) {
|
||||||
|
size_t space = 0, hs = sizeof(uint32_t) * 2; // *2 is for the 0 marker
|
||||||
|
if (q->head >= q->tail && q->head + len + hs <= q->size) {
|
||||||
|
space = q->size - q->head - hs; // There is enough space
|
||||||
|
} else if (q->head >= q->tail && q->tail > hs) {
|
||||||
|
mg_queue_write_len(q, 0); // Not enough space ahead
|
||||||
|
q->head = 0; // Wrap head to the beginning
|
||||||
}
|
}
|
||||||
ofs = q->head + sizeof(size_t);
|
if (q->head + hs + len < q->tail) space = q->tail - q->head - hs;
|
||||||
if (buf != NULL) *buf = q->buf + ofs;
|
if (buf != NULL) *buf = q->buf + q->head + sizeof(uint32_t);
|
||||||
return ofs > q->len ? 0 : q->len - ofs;
|
return space;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_queue_next(struct mg_queue *q, char **buf) {
|
size_t mg_queue_next(struct mg_queue *q, char **buf) {
|
||||||
size_t len = MG_QUEUE_EMPTY;
|
size_t len = 0;
|
||||||
if (q->tail < q->head) memcpy(&len, &q->buf[q->tail], sizeof(len));
|
if (q->tail != q->head) {
|
||||||
if (buf != NULL) *buf = &q->buf[q->tail + sizeof(len)];
|
len = mg_queue_read_len(q);
|
||||||
|
if (len == 0) { // Zero (head wrapped) ?
|
||||||
|
q->tail = 0; // Reset tail to the start
|
||||||
|
if (q->head > q->tail) len = mg_queue_read_len(q); // Read again
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (buf != NULL) *buf = q->buf + q->tail + sizeof(uint32_t);
|
||||||
|
assert(q->tail + len <= q->size);
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mg_queue_add(struct mg_queue *q, size_t len) {
|
void mg_queue_add(struct mg_queue *q, size_t len) {
|
||||||
size_t head = q->head + len + (size_t) sizeof(head); // New head
|
assert(len > 0);
|
||||||
if (head <= q->len) { // Have space ?
|
mg_queue_write_len(q, len);
|
||||||
memcpy(q->buf + q->head, &len, sizeof(len)); // Yes. Store ML
|
assert(q->head + sizeof(uint32_t) * 2 + len <= q->size);
|
||||||
q->head = head; // Advance head
|
q->head += len + sizeof(uint32_t);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mg_queue_del(struct mg_queue *q) {
|
void mg_queue_del(struct mg_queue *q, size_t len) {
|
||||||
size_t len = mg_queue_next(q, NULL), tail = q->tail + len + sizeof(size_t);
|
q->tail += len + sizeof(uint32_t);
|
||||||
if (len != MG_QUEUE_EMPTY) q->tail = tail;
|
assert(q->tail + sizeof(uint32_t) <= q->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MG_ENABLE_LINES
|
#ifdef MG_ENABLE_LINES
|
||||||
@ -3806,7 +3848,7 @@ static uint32_t blk0(union char64long16 *block, int i) {
|
|||||||
w = rol(w, 30);
|
w = rol(w, 30);
|
||||||
|
|
||||||
static void mg_sha1_transform(uint32_t state[5],
|
static void mg_sha1_transform(uint32_t state[5],
|
||||||
const unsigned char buffer[64]) {
|
const unsigned char *buffer) {
|
||||||
uint32_t a, b, c, d, e;
|
uint32_t a, b, c, d, e;
|
||||||
union char64long16 block[1];
|
union char64long16 block[1];
|
||||||
|
|
||||||
|
56
mongoose.h
56
mongoose.h
@ -223,7 +223,6 @@ static inline int mg_mkdir(const char *path, mode_t mode) {
|
|||||||
#define MG_PATH_MAX 100
|
#define MG_PATH_MAX 100
|
||||||
#define MG_ENABLE_SOCKET 0
|
#define MG_ENABLE_SOCKET 0
|
||||||
#define MG_ENABLE_DIRLIST 0
|
#define MG_ENABLE_DIRLIST 0
|
||||||
#define MG_ENABLE_ATOMIC 1
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -240,7 +239,6 @@ static inline int mg_mkdir(const char *path, mode_t mode) {
|
|||||||
|
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
int mkdir(const char *, mode_t);
|
int mkdir(const char *, mode_t);
|
||||||
#define MG_ENABLE_ATOMIC 1
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -599,10 +597,6 @@ struct timeval {
|
|||||||
#define MG_ENABLE_TCPIP 0 // Mongoose built-in network stack
|
#define MG_ENABLE_TCPIP 0 // Mongoose built-in network stack
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_ATOMIC
|
|
||||||
#define MG_ENABLE_ATOMIC 0 // Required by mg_queue
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef MG_ENABLE_LWIP
|
#ifndef MG_ENABLE_LWIP
|
||||||
#define MG_ENABLE_LWIP 0 // lWIP network stack
|
#define MG_ENABLE_LWIP 0 // lWIP network stack
|
||||||
#endif
|
#endif
|
||||||
@ -676,6 +670,10 @@ struct timeval {
|
|||||||
#define MG_ENABLE_PACKED_FS 0
|
#define MG_ENABLE_PACKED_FS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_ASSERT
|
||||||
|
#define MG_ENABLE_ASSERT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_IO_SIZE
|
#ifndef MG_IO_SIZE
|
||||||
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
||||||
#endif
|
#endif
|
||||||
@ -792,40 +790,32 @@ char *mg_remove_double_dots(char *s);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if MG_ENABLE_ATOMIC
|
|
||||||
#include <stdatomic.h>
|
|
||||||
#elif !defined(_Atomic)
|
|
||||||
#define _Atomic
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Single producer, single consumer non-blocking queue
|
// Single producer, single consumer non-blocking queue
|
||||||
//
|
//
|
||||||
// Producer:
|
// Producer:
|
||||||
// void *buf;
|
// char *buf;
|
||||||
// while (mg_queue_space(q, &buf) < len) WAIT(); // Wait for free space
|
// while (mg_queue_book(q, &buf) < len) WAIT(); // Wait for space
|
||||||
// memcpy(buf, data, len); // Copy data to the queue
|
// memcpy(buf, my_data, len); // Copy data to the queue
|
||||||
// mg_queue_add(q, len); // Advance q->head
|
// mg_queue_add(q, len);
|
||||||
//
|
//
|
||||||
// Consumer:
|
// Consumer:
|
||||||
// void *buf;
|
// char *buf;
|
||||||
// while ((len = mg_queue_next(q, &buf)) == MG_QUEUE_EMPTY) WAIT();
|
// while ((len = mg_queue_get(q, &buf)) == 0) WAIT();
|
||||||
// mg_hexdump(buf, len); // Handle message
|
// mg_hexdump(buf, len); // Handle message
|
||||||
// mg_queue_del(q); // Delete message
|
// mg_queue_del(q, len);
|
||||||
//
|
//
|
||||||
struct mg_queue {
|
struct mg_queue {
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t len;
|
size_t size;
|
||||||
volatile _Atomic size_t tail;
|
volatile size_t tail;
|
||||||
volatile _Atomic size_t head;
|
volatile size_t head;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MG_QUEUE_EMPTY ((size_t) ~0ul) // Next message size when queue is empty
|
void mg_queue_init(struct mg_queue *, char *, size_t); // Init queue
|
||||||
|
size_t mg_queue_book(struct mg_queue *, char **buf, size_t); // Reserve space
|
||||||
void mg_queue_add(struct mg_queue *, size_t len); // Advance head
|
void mg_queue_add(struct mg_queue *, size_t); // Add new message
|
||||||
void mg_queue_del(struct mg_queue *); // Advance tail
|
size_t mg_queue_next(struct mg_queue *, char **); // Get oldest message
|
||||||
size_t mg_queue_space(struct mg_queue *, char **); // Get free space
|
void mg_queue_del(struct mg_queue *, size_t); // Delete oldest message
|
||||||
size_t mg_queue_next(struct mg_queue *, char **); // Get next message size
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -958,6 +948,12 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if MG_ENABLE_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#elif !defined(assert)
|
||||||
|
#define assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
void mg_random(void *buf, size_t len);
|
void mg_random(void *buf, size_t len);
|
||||||
char *mg_random_str(char *buf, size_t len);
|
char *mg_random_str(char *buf, size_t len);
|
||||||
uint16_t mg_ntohs(uint16_t net);
|
uint16_t mg_ntohs(uint16_t net);
|
||||||
|
@ -19,6 +19,5 @@
|
|||||||
#define MG_PATH_MAX 100
|
#define MG_PATH_MAX 100
|
||||||
#define MG_ENABLE_SOCKET 0
|
#define MG_ENABLE_SOCKET 0
|
||||||
#define MG_ENABLE_DIRLIST 0
|
#define MG_ENABLE_DIRLIST 0
|
||||||
#define MG_ENABLE_ATOMIC 1
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -12,5 +12,4 @@
|
|||||||
|
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
int mkdir(const char *, mode_t);
|
int mkdir(const char *, mode_t);
|
||||||
#define MG_ENABLE_ATOMIC 1
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,10 +8,6 @@
|
|||||||
#define MG_ENABLE_TCPIP 0 // Mongoose built-in network stack
|
#define MG_ENABLE_TCPIP 0 // Mongoose built-in network stack
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_ATOMIC
|
|
||||||
#define MG_ENABLE_ATOMIC 0 // Required by mg_queue
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef MG_ENABLE_LWIP
|
#ifndef MG_ENABLE_LWIP
|
||||||
#define MG_ENABLE_LWIP 0 // lWIP network stack
|
#define MG_ENABLE_LWIP 0 // lWIP network stack
|
||||||
#endif
|
#endif
|
||||||
@ -85,6 +81,10 @@
|
|||||||
#define MG_ENABLE_PACKED_FS 0
|
#define MG_ENABLE_PACKED_FS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_ASSERT
|
||||||
|
#define MG_ENABLE_ASSERT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_IO_SIZE
|
#ifndef MG_IO_SIZE
|
||||||
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
#define MG_IO_SIZE 2048 // Granularity of the send/recv IO buffer growth
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
|
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
|
||||||
size_t len = mg_snprintf(NULL, 0, fmt, ap);
|
size_t len = mg_snprintf(NULL, 0, fmt, ap);
|
||||||
char *buf;
|
char *buf;
|
||||||
if (mg_queue_space(q, &buf) < len + 1) {
|
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1) {
|
||||||
len = 0; // Nah. Not enough space
|
len = 0; // Nah. Not enough space
|
||||||
} else {
|
} else {
|
||||||
len = mg_vsnprintf((char *)buf, len + 1, fmt, ap);
|
len = mg_vsnprintf((char *)buf, len + 1, fmt, ap);
|
||||||
|
92
src/queue.c
92
src/queue.c
@ -1,40 +1,82 @@
|
|||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
// Every message in the queue is prepended by the message length (ML)
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
// ML is sizeof(size_t) in size
|
#define MG_MEMORY_BARRIER() __sync_synchronize()
|
||||||
// Tail points to the message data
|
#elif defined(_MSC_VER) && _MSC_VER >= 1700
|
||||||
|
#define MG_MEMORY_BARRIER() MemoryBarrier()
|
||||||
|
#elif !defined(MG_MEMORY_BARRIER)
|
||||||
|
#define MG_MEMORY_BARRIER()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Every message in a queue is prepended by a 32-bit message length (ML).
|
||||||
|
// If ML is 0, then it is the end, and reader must wrap to the beginning.
|
||||||
//
|
//
|
||||||
// |------| ML | message1 | ML | message2 |--- free space ---|
|
// Queue when q->tail <= q->head:
|
||||||
// ^ ^ ^ ^
|
// |----- free -----| ML | message1 | ML | message2 | ----- free ------|
|
||||||
// buf tail head len
|
// ^ ^ ^ ^
|
||||||
|
// buf tail head len
|
||||||
|
//
|
||||||
|
// Queue when q->tail > q->head:
|
||||||
|
// | ML | message2 |----- free ------| ML | message1 | 0 |---- free ----|
|
||||||
|
// ^ ^ ^ ^
|
||||||
|
// buf head tail len
|
||||||
|
|
||||||
size_t mg_queue_space(struct mg_queue *q, char **buf) {
|
void mg_queue_init(struct mg_queue *q, char *buf, size_t size) {
|
||||||
size_t ofs;
|
q->size = size;
|
||||||
if (q->head > 0 && q->tail >= q->head) { // All messages read?
|
q->buf = buf;
|
||||||
q->head = 0; // Yes. Reset head first
|
q->head = q->tail = 0;
|
||||||
q->tail = 0; // Now reset the tail
|
}
|
||||||
|
|
||||||
|
static size_t mg_queue_read_len(struct mg_queue *q) {
|
||||||
|
uint32_t n = 0;
|
||||||
|
MG_MEMORY_BARRIER();
|
||||||
|
memcpy(&n, q->buf + q->tail, sizeof(n));
|
||||||
|
assert(q->tail + n + sizeof(n) <= q->size);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_queue_write_len(struct mg_queue *q, size_t len) {
|
||||||
|
uint32_t n = (uint32_t) len;
|
||||||
|
memcpy(q->buf + q->head, &n, sizeof(n));
|
||||||
|
MG_MEMORY_BARRIER();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mg_queue_book(struct mg_queue *q, char **buf, size_t len) {
|
||||||
|
size_t space = 0, hs = sizeof(uint32_t) * 2; // *2 is for the 0 marker
|
||||||
|
if (q->head >= q->tail && q->head + len + hs <= q->size) {
|
||||||
|
space = q->size - q->head - hs; // There is enough space
|
||||||
|
} else if (q->head >= q->tail && q->tail > hs) {
|
||||||
|
mg_queue_write_len(q, 0); // Not enough space ahead
|
||||||
|
q->head = 0; // Wrap head to the beginning
|
||||||
}
|
}
|
||||||
ofs = q->head + sizeof(size_t);
|
if (q->head + hs + len < q->tail) space = q->tail - q->head - hs;
|
||||||
if (buf != NULL) *buf = q->buf + ofs;
|
if (buf != NULL) *buf = q->buf + q->head + sizeof(uint32_t);
|
||||||
return ofs > q->len ? 0 : q->len - ofs;
|
return space;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mg_queue_next(struct mg_queue *q, char **buf) {
|
size_t mg_queue_next(struct mg_queue *q, char **buf) {
|
||||||
size_t len = MG_QUEUE_EMPTY;
|
size_t len = 0;
|
||||||
if (q->tail < q->head) memcpy(&len, &q->buf[q->tail], sizeof(len));
|
if (q->tail != q->head) {
|
||||||
if (buf != NULL) *buf = &q->buf[q->tail + sizeof(len)];
|
len = mg_queue_read_len(q);
|
||||||
|
if (len == 0) { // Zero (head wrapped) ?
|
||||||
|
q->tail = 0; // Reset tail to the start
|
||||||
|
if (q->head > q->tail) len = mg_queue_read_len(q); // Read again
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (buf != NULL) *buf = q->buf + q->tail + sizeof(uint32_t);
|
||||||
|
assert(q->tail + len <= q->size);
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mg_queue_add(struct mg_queue *q, size_t len) {
|
void mg_queue_add(struct mg_queue *q, size_t len) {
|
||||||
size_t head = q->head + len + (size_t) sizeof(head); // New head
|
assert(len > 0);
|
||||||
if (head <= q->len) { // Have space ?
|
mg_queue_write_len(q, len);
|
||||||
memcpy(q->buf + q->head, &len, sizeof(len)); // Yes. Store ML
|
assert(q->head + sizeof(uint32_t) * 2 + len <= q->size);
|
||||||
q->head = head; // Advance head
|
q->head += len + sizeof(uint32_t);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mg_queue_del(struct mg_queue *q) {
|
void mg_queue_del(struct mg_queue *q, size_t len) {
|
||||||
size_t len = mg_queue_next(q, NULL), tail = q->tail + len + sizeof(size_t);
|
q->tail += len + sizeof(uint32_t);
|
||||||
if (len != MG_QUEUE_EMPTY) q->tail = tail;
|
assert(q->tail + sizeof(uint32_t) <= q->size);
|
||||||
}
|
}
|
||||||
|
42
src/queue.h
42
src/queue.h
@ -1,38 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "arch.h" // For size_t
|
#include "arch.h" // For size_t
|
||||||
#include "config.h" // For MG_ENABLE_ATOMIC
|
|
||||||
|
|
||||||
#if MG_ENABLE_ATOMIC
|
|
||||||
#include <stdatomic.h>
|
|
||||||
#elif !defined(_Atomic)
|
|
||||||
#define _Atomic
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Single producer, single consumer non-blocking queue
|
// Single producer, single consumer non-blocking queue
|
||||||
//
|
//
|
||||||
// Producer:
|
// Producer:
|
||||||
// void *buf;
|
// char *buf;
|
||||||
// while (mg_queue_space(q, &buf) < len) WAIT(); // Wait for free space
|
// while (mg_queue_book(q, &buf) < len) WAIT(); // Wait for space
|
||||||
// memcpy(buf, data, len); // Copy data to the queue
|
// memcpy(buf, my_data, len); // Copy data to the queue
|
||||||
// mg_queue_add(q, len); // Advance q->head
|
// mg_queue_add(q, len);
|
||||||
//
|
//
|
||||||
// Consumer:
|
// Consumer:
|
||||||
// void *buf;
|
// char *buf;
|
||||||
// while ((len = mg_queue_next(q, &buf)) == MG_QUEUE_EMPTY) WAIT();
|
// while ((len = mg_queue_get(q, &buf)) == 0) WAIT();
|
||||||
// mg_hexdump(buf, len); // Handle message
|
// mg_hexdump(buf, len); // Handle message
|
||||||
// mg_queue_del(q); // Delete message
|
// mg_queue_del(q, len);
|
||||||
//
|
//
|
||||||
struct mg_queue {
|
struct mg_queue {
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t len;
|
size_t size;
|
||||||
volatile _Atomic size_t tail;
|
volatile size_t tail;
|
||||||
volatile _Atomic size_t head;
|
volatile size_t head;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MG_QUEUE_EMPTY ((size_t) ~0ul) // Next message size when queue is empty
|
void mg_queue_init(struct mg_queue *, char *, size_t); // Init queue
|
||||||
|
size_t mg_queue_book(struct mg_queue *, char **buf, size_t); // Reserve space
|
||||||
void mg_queue_add(struct mg_queue *, size_t len); // Advance head
|
void mg_queue_add(struct mg_queue *, size_t); // Add new message
|
||||||
void mg_queue_del(struct mg_queue *); // Advance tail
|
size_t mg_queue_next(struct mg_queue *, char **); // Get oldest message
|
||||||
size_t mg_queue_space(struct mg_queue *, char **); // Get free space
|
void mg_queue_del(struct mg_queue *, size_t); // Delete oldest message
|
||||||
size_t mg_queue_next(struct mg_queue *, char **); // Get next message size
|
|
||||||
|
@ -48,7 +48,7 @@ static uint32_t blk0(union char64long16 *block, int i) {
|
|||||||
w = rol(w, 30);
|
w = rol(w, 30);
|
||||||
|
|
||||||
static void mg_sha1_transform(uint32_t state[5],
|
static void mg_sha1_transform(uint32_t state[5],
|
||||||
const unsigned char buffer[64]) {
|
const unsigned char *buffer) {
|
||||||
uint32_t a, b, c, d, e;
|
uint32_t a, b, c, d, e;
|
||||||
union char64long16 block[1];
|
union char64long16 block[1];
|
||||||
|
|
||||||
|
@ -6,6 +6,12 @@
|
|||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
|
#if MG_ENABLE_ASSERT
|
||||||
|
#include <assert.h>
|
||||||
|
#elif !defined(assert)
|
||||||
|
#define assert(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
void mg_random(void *buf, size_t len);
|
void mg_random(void *buf, size_t len);
|
||||||
char *mg_random_str(char *buf, size_t len);
|
char *mg_random_str(char *buf, size_t len);
|
||||||
uint16_t mg_ntohs(uint16_t net);
|
uint16_t mg_ntohs(uint16_t net);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#define MG_ENABLE_PACKED_FS 0
|
#define MG_ENABLE_PACKED_FS 0
|
||||||
#define MG_ENABLE_LINES 1
|
#define MG_ENABLE_LINES 1
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#ifndef __OpenBSD__
|
#ifndef __OpenBSD__
|
||||||
#include <linux/if.h>
|
#include <linux/if.h>
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
#define MG_ENABLE_TCPIP 1
|
#define MG_ENABLE_TCPIP 1
|
||||||
#define MG_ENABLE_PACKED_FS 0
|
#define MG_ENABLE_PACKED_FS 0
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include "mongoose.c"
|
#include "mongoose.c"
|
||||||
#include "driver_mock.c"
|
#include "driver_mock.c"
|
||||||
|
|
||||||
|
117
test/unit_test.c
117
test/unit_test.c
@ -2686,32 +2686,41 @@ static void test_poll(void) {
|
|||||||
mg_mgr_free(&mgr);
|
mg_mgr_free(&mgr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MG_ENABLE_ATOMIC
|
#define NMESSAGES 99999
|
||||||
#define NMESSAGES 49999
|
|
||||||
static uint32_t s_qcrc = 0;
|
static uint32_t s_qcrc = 0;
|
||||||
|
static int s_out, s_in;
|
||||||
static void producer(void *param) {
|
static void producer(void *param) {
|
||||||
struct mg_queue *q = (struct mg_queue *) param;
|
struct mg_queue *q = (struct mg_queue *) param;
|
||||||
volatile size_t i, n, len;
|
char tmp[64 * 1024], *buf;
|
||||||
for (i = 0; i < NMESSAGES; i++) {
|
size_t len, ofs = sizeof(tmp);
|
||||||
char buf[100];
|
for (s_out = 0; s_out < NMESSAGES; s_out++) {
|
||||||
char *p;
|
if (ofs >= sizeof(tmp)) mg_random(tmp, sizeof(tmp)), ofs = 0;
|
||||||
mg_random(buf, sizeof(buf));
|
len = ((uint8_t *) tmp)[ofs] % 55 + 1;
|
||||||
n = ((unsigned char *) buf)[0] % sizeof(buf);
|
if (ofs + len > sizeof(tmp)) len = sizeof(tmp) - ofs;
|
||||||
while ((len = mg_queue_space(q, &p)) < n) (void) 0;
|
while ((mg_queue_book(q, &buf, len)) < len) (void) 0;
|
||||||
memcpy(p, buf, n);
|
memcpy(buf, &tmp[ofs], len);
|
||||||
mg_queue_add(q, n);
|
s_qcrc = mg_crc32(s_qcrc, buf, len);
|
||||||
s_qcrc = mg_crc32(s_qcrc, buf, n);
|
ofs += len;
|
||||||
|
#if 0
|
||||||
|
fprintf(stderr, "-->prod %3d %8x %-3lu %zu/%zu/%lu\n", s_out, s_qcrc, len, q->tail,
|
||||||
|
q->head, buf - q->buf);
|
||||||
|
#endif
|
||||||
|
mg_queue_add(q, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t consumer(struct mg_queue *q) {
|
static uint32_t consumer(struct mg_queue *q) {
|
||||||
uint32_t i, crc = 0;
|
uint32_t crc = 0;
|
||||||
for (i = 0; i < NMESSAGES; i++) {
|
for (s_in = 0; s_in < NMESSAGES; s_in++) {
|
||||||
char *p;
|
char *buf;
|
||||||
volatile size_t len;
|
size_t len;
|
||||||
while ((len = mg_queue_next(q, &p)) == MG_QUEUE_EMPTY) (void) 0;
|
while ((len = mg_queue_next(q, &buf)) == 0) (void) 0;
|
||||||
crc = mg_crc32(crc, (char *) p, len);
|
crc = mg_crc32(crc, buf, len);
|
||||||
mg_queue_del(q);
|
#if 0
|
||||||
|
fprintf(stderr, "-->cons %3u %8x %-3lu %zu/%zu/%lu\n", s_in, crc, len, q->tail,
|
||||||
|
q->head, buf - q->buf);
|
||||||
|
#endif
|
||||||
|
mg_queue_del(q, len);
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
@ -2739,68 +2748,18 @@ static void start_thread(void (*f)(void *), void *p) {
|
|||||||
(void) f, (void) p;
|
(void) f, (void) p;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
|
|
||||||
static void test_queue(void) {
|
static void test_queue(void) {
|
||||||
char buf[512], *p;
|
char buf[512];
|
||||||
size_t size = sizeof(size);
|
struct mg_queue queue;
|
||||||
struct mg_queue queue, *q = &queue;
|
uint32_t crc;
|
||||||
|
memset(buf, 0x55, sizeof(buf));
|
||||||
memset(q, 0, sizeof(*q));
|
mg_queue_init(&queue, buf, sizeof(buf));
|
||||||
q->buf = buf;
|
start_thread(producer, &queue); // Start producer in a separate thread
|
||||||
q->len = sizeof(buf);
|
crc = consumer(&queue); // Consumer eats data in this thread
|
||||||
|
MG_INFO(("CRC1 %8x", s_qcrc)); // Show CRCs
|
||||||
ASSERT(mg_queue_next(q, &p) == MG_QUEUE_EMPTY);
|
MG_INFO(("CRC2 %8x", crc));
|
||||||
|
ASSERT(s_qcrc == crc);
|
||||||
// Write "hi"
|
|
||||||
ASSERT(mg_queue_space(q, &p) == sizeof(buf) - size);
|
|
||||||
ASSERT(mg_queue_printf(q, "hi") == 2);
|
|
||||||
ASSERT(q->head == size + 2);
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 2);
|
|
||||||
|
|
||||||
// Write zero-length message
|
|
||||||
ASSERT(mg_queue_space(q, &p) == sizeof(buf) - 2 - 2 * size);
|
|
||||||
ASSERT(mg_queue_printf(q, "") == 0);
|
|
||||||
ASSERT(q->head == size * 2 + 2);
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 2);
|
|
||||||
|
|
||||||
// Write "dude"
|
|
||||||
ASSERT(mg_queue_space(q, &p) == sizeof(buf) - 2 - 3 * size);
|
|
||||||
ASSERT(mg_queue_printf(q, "dude") == 4);
|
|
||||||
ASSERT(q->head == size * 3 + 2 + 4);
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 2);
|
|
||||||
|
|
||||||
// Read "hi"
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 2);
|
|
||||||
ASSERT(memcmp(p, "hi", 2) == 0);
|
|
||||||
mg_queue_del(q);
|
|
||||||
ASSERT(q->head == size * 3 + 2 + 4);
|
|
||||||
ASSERT(q->tail == size + 2);
|
|
||||||
|
|
||||||
// Read empty message
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 0);
|
|
||||||
mg_queue_del(q);
|
|
||||||
ASSERT(q->tail == size * 2 + 2);
|
|
||||||
|
|
||||||
ASSERT(mg_queue_next(q, &p) == 4);
|
|
||||||
ASSERT(memcmp(p, "dude", 4) == 0);
|
|
||||||
mg_queue_del(q);
|
|
||||||
ASSERT(q->tail == q->head);
|
|
||||||
|
|
||||||
ASSERT(mg_queue_space(q, &p) == sizeof(buf) - size);
|
|
||||||
ASSERT(q->tail == 0 && q->head == 0);
|
|
||||||
q->tail = q->head = 0;
|
|
||||||
|
|
||||||
#if MG_ENABLE_ATOMIC
|
|
||||||
{
|
|
||||||
// Test concurrent queue access
|
|
||||||
uint32_t crc;
|
|
||||||
start_thread(producer, q);
|
|
||||||
crc = consumer(q);
|
|
||||||
MG_INFO(("DONE. %x %x", s_qcrc, crc));
|
|
||||||
ASSERT(s_qcrc == crc);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
Loading…
Reference in New Issue
Block a user