Add HTTP server

This commit is contained in:
Kamil Trzcinski
2022-04-04 21:35:02 +02:00
parent 932c32576b
commit a3fa4062f7
8 changed files with 353 additions and 79 deletions

41
buffer_lock.c Normal file
View File

@ -0,0 +1,41 @@
#include "buffer_lock.h"
void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{
pthread_mutex_lock(&buf_lock->lock);
buffer_consumed(buf_lock->buf);
buffer_use(buf);
buf_lock->buf = buf;
buf_lock->counter++;
E_LOG_DEBUG(buf_lock, "Captured HTTP snapshot: %d", buf_lock->counter);
pthread_cond_broadcast(&buf_lock->cond_wait);
pthread_mutex_unlock(&buf_lock->lock);
}
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_s, int *counter)
{
buffer_t *buf = NULL;
struct timeval now;
struct timespec timeout;
gettimeofday(&now, NULL);
timeout.tv_nsec = now.tv_usec;
timeout.tv_sec = now.tv_sec + timeout_s;
pthread_mutex_lock(&buf_lock->lock);
if (*counter == buf_lock->counter && timeout_s > 0) {
int ret = pthread_cond_timedwait(&buf_lock->cond_wait, &buf_lock->lock, &timeout);
if (ret == ETIMEDOUT) {
goto ret;
} else if (ret < 0) {
goto ret;
}
}
buf = buf_lock->buf;
buffer_use(buf);
*counter = buf_lock->counter;
ret:
pthread_mutex_unlock(&buf_lock->lock);
return buf;
}