-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.c
91 lines (81 loc) · 2.26 KB
/
session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include "session.h"
#include "vector.h"
#include "writer.h"
#include "sql.h"
typedef struct session_t {
status_t status;
time_t time_created,
time_updated;
} session_t;
static vector sessions;
static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
extern volatile time_t db_updated;
void sessions_init() {
pthread_mutex_lock(&sessions_mutex);
vector_new(&sessions, 10);
pthread_mutex_unlock(&sessions_mutex);
}
int sessions_add() {
int id;
time_t now;
time(&now);
session_t *s = malloc(sizeof(session_t));
s->status = ST_DIRTY;// born dirty, i.e. needs an update
s->time_created = now;
s->time_updated = 0; // never been updated yet
pthread_mutex_lock(&sessions_mutex);
id = sessions.used;
vector_pushback(&sessions, s);
pthread_mutex_unlock(&sessions_mutex);
return id;
}
void sessions_touch(size_t s) {
time_t now;
time(&now);
pthread_mutex_lock(&sessions_mutex);
if (s < sessions.used) {
session_t *sess = sessions.data[s];
if (sess->status != ST_DEAD) {
sess->time_updated = now;
sess->status = ST_CLEAN;
}
}
pthread_mutex_unlock(&sessions_mutex);
}
void sessions_kill(size_t s) {
time_t now;
time(&now);
pthread_mutex_lock(&sessions_mutex);
if (s < sessions.used) {
session_t *sess = sessions.data[s];
if (sess->status != ST_DEAD) {
sess->time_updated = now;
sess->status = ST_DEAD;
}
}
pthread_mutex_unlock(&sessions_mutex);
}
status_t session_get_status(size_t s) {
status_t result = ST_DEAD;
time_t db = db_last_update_time();
pthread_mutex_lock(&sessions_mutex);
if (s < sessions.used) {
session_t *sess = sessions.data[s];
if (sess->status == ST_CLEAN && difftime(db, sess->time_updated) > 0)
sess->status = ST_DIRTY;
result = sess->status;
}
pthread_mutex_unlock(&sessions_mutex);
return result;
}
void sessions_cleanup() {
pthread_mutex_lock(&sessions_mutex);
for (size_t i = 0; i < sessions.used; i++) {
session_t *s = sessions.data[i];
free(s);
}
vector_free(&sessions);
}