mirror of
https://github.com/sigmasternchen/CShore
synced 2025-03-15 16:18:56 +00:00
added basic synchronization for session; still probablematic if two requests use the same session object at the same time; maybe add thread specific buffer or something
This commit is contained in:
parent
bfd04560ed
commit
e8a1c2dd46
1 changed files with 12 additions and 4 deletions
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <uuid/uuid.h>
|
#include <uuid/uuid.h>
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include <headers.h>
|
#include <headers.h>
|
||||||
|
|
||||||
// SESSION_PTR_TYPE doesn't matter for this file
|
// SESSION_PTR_TYPE doesn't matter for this file
|
||||||
|
@ -20,26 +22,28 @@ static struct session {
|
||||||
void* data;
|
void* data;
|
||||||
}* sessions = NULL;
|
}* sessions = NULL;
|
||||||
static size_t sessionno = 0;
|
static size_t sessionno = 0;
|
||||||
|
static pthread_mutex_t globalLock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
#define SESSION_BLOCK_SIZE (128)
|
#define SESSION_BLOCK_SIZE (128)
|
||||||
|
|
||||||
int resizeSessionList() {
|
int resizeSessionList() {
|
||||||
// TODO synchronization
|
|
||||||
struct session* tmp = realloc(sessions, sizeof(struct session) * (sessionno + SESSION_BLOCK_SIZE));
|
struct session* tmp = realloc(sessions, sizeof(struct session) * (sessionno + SESSION_BLOCK_SIZE));
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memset(tmp + sizeof(struct session) * sessionno, 0, sizeof(struct session) * SESSION_BLOCK_SIZE);
|
for (size_t i = 0; i < SESSION_BLOCK_SIZE; i++) {
|
||||||
|
struct session* session = &tmp[sessionno + i];
|
||||||
|
session->inUse = 0;
|
||||||
|
}
|
||||||
sessions = tmp;
|
sessions = tmp;
|
||||||
sessionno += SESSION_BLOCK_SIZE;
|
sessionno += SESSION_BLOCK_SIZE;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct session* newSession(size_t size) {
|
struct session* newSession(size_t size) {
|
||||||
for (size_t i = 0; i < sessionno; i++) {
|
for (size_t i = 0; i < sessionno; i++) {
|
||||||
if (!sessions[i].inUse) {
|
if (!sessions[i].inUse) {
|
||||||
// TODO synchronization
|
|
||||||
sessions[i].inUse = true;
|
sessions[i].inUse = true;
|
||||||
sessions[i].data = malloc(size);
|
sessions[i].data = malloc(size);
|
||||||
if (sessions[i].data == NULL) {
|
if (sessions[i].data == NULL) {
|
||||||
|
@ -89,6 +93,8 @@ void* _session_start(ctx_t* ctx, const char* cookie, size_t size) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&globalLock);
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
session = findSession(id);
|
session = findSession(id);
|
||||||
}
|
}
|
||||||
|
@ -107,6 +113,8 @@ void* _session_start(ctx_t* ctx, const char* cookie, size_t size) {
|
||||||
setCookie(ctx, cookie, buffer, cookieSettingsNull());
|
setCookie(ctx, cookie, buffer, cookieSettingsNull());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&globalLock);
|
||||||
|
|
||||||
session->lastAccess = time(NULL);
|
session->lastAccess = time(NULL);
|
||||||
|
|
||||||
return session->data;
|
return session->data;
|
||||||
|
|
Loading…
Reference in a new issue