void afb_session_init (int max_session_count, int timeout, const char *initok)
{
// let's create as store as hashtable does not have any
- sessions.store = calloc (1 + (unsigned)max_session_count, sizeof(struct afb_session));
+ sessions.store = calloc (1 + (unsigned)max_session_count, sizeof *sessions.store);
pthread_mutex_init(&sessions.mutex, NULL);
sessions.max = max_session_count;
sessions.timeout = timeout;
}
}
+const char *afb_session_initial_token()
+{
+ return sessions.initok;
+}
+
static struct afb_session *search (const char* uuid)
{
int idx;
/* search for an existing one not too old */
if (uuid != NULL) {
session = search(uuid);
+ if (!created)
+ return session;
if (session != NULL) {
*created = 0;
session->access = now;
}
}
- *created = 1;
+ if (created)
+ *created = 1;
+
return make_session(uuid, sessions.timeout, now);
}
struct afb_session *afb_session_addref(struct afb_session *session)
{
if (session != NULL)
- session->refcount++;
+ __atomic_add_fetch(&session->refcount, 1, __ATOMIC_RELAXED);
return session;
}
{
if (session != NULL) {
assert(session->refcount != 0);
- --session->refcount;
- if (session->refcount == 0 && session->uuid[0] == 0) {
- destroy (session);
- pthread_mutex_destroy(&session->mutex);
- free(session);
+ if (!__atomic_sub_fetch(&session->refcount, 1, __ATOMIC_RELAXED)) {
+ if (session->uuid[0] == 0) {
+ destroy (session);
+ pthread_mutex_destroy(&session->mutex);
+ free(session);
+ }
}
}
}
cookie = cookie_add(session, idx, key, value, freecb);
if (!cookie) {
if (makecb && freecb)
- free(value);
+ freecb(value);
value = NULL;
}
}