Skip to content

Commit a71f6c8

Browse files
committed
using more glib2 and ZTS mutexes
1 parent 7355ed4 commit a71f6c8

1 file changed

Lines changed: 31 additions & 41 deletions

File tree

ext/session/mod_mm.c

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
#define PS_MM_FILE "session_mm_"
3434

35+
#ifdef ZTS
36+
MUTEX_T session_mm_lock;
37+
#endif
38+
3539
/* This list holds all data associated with one session. */
3640

3741
typedef struct ps_sd {
@@ -63,28 +67,14 @@ static ps_mm *ps_mm_instance = NULL;
6367
# define ps_mm_debug(a)
6468
#endif
6569

66-
static inline uint32_t ps_sd_hash(const zend_string *data)
67-
{
68-
uint32_t h;
69-
const char *data_char = ZSTR_VAL(data);
70-
const char *e = ZSTR_VAL(data) + ZSTR_LEN(data);
71-
72-
for (h = 2166136261U; data_char < e; ) {
73-
h *= 16777619;
74-
h ^= *data_char++;
75-
}
76-
77-
return h;
78-
}
79-
8070
static ps_sd *ps_sd_new(ps_mm *data, zend_string *key)
8171
{
8272
ps_sd *sd;
8373

84-
sd = g_malloc(sizeof(ps_sd) + ZSTR_LEN(key));
74+
sd = g_try_malloc(sizeof(ps_sd) + ZSTR_LEN(key));
8575
if (!sd) {
8676

87-
//php_error_docref(NULL, E_WARNING, "g_malloc failed, avail %ld, err %s", mm_available(data->mm), mm_error());
77+
php_error_docref(NULL, E_WARNING, "g_malloc failed");
8878
return NULL;
8979
}
9080

@@ -110,24 +100,6 @@ static void ps_sd_destroy(ps_mm *data, zend_string *key, ps_sd *sd)
110100
g_hash_table_remove(data->hash, key);
111101
}
112102

113-
static ps_sd *ps_sd_lookup(ps_mm *data, const zend_string *key, bool _rw)
114-
{
115-
return (ps_sd *)g_hash_table_lookup(data->hash, key);
116-
}
117-
118-
static zend_result ps_mm_key_exists(ps_mm *data, const zend_string *key)
119-
{
120-
ps_sd *sd;
121-
if (!key) {
122-
return FAILURE;
123-
}
124-
sd = ps_sd_lookup(data, key, false);
125-
if (sd) {
126-
return SUCCESS;
127-
}
128-
return FAILURE;
129-
}
130-
131103
const ps_module ps_mod_mm = {
132104
PS_MOD_SID(mm)
133105
};
@@ -244,13 +216,21 @@ PHP_MINIT_FUNCTION(ps_mm)
244216
return FAILURE;
245217
}
246218

219+
#ifdef ZTS
220+
session_mm_lock = tsrm_mutex_alloc();
221+
#endif
222+
247223
php_session_register_module(&ps_mod_mm);
248224
return SUCCESS;
249225
}
250226

251227
PHP_MSHUTDOWN_FUNCTION(ps_mm)
252228
{
253229
if (ps_mm_instance) {
230+
#ifdef ZTS
231+
tsrm_mutex_free(session_mm_lock);
232+
session_mm_lock = NULL;
233+
#endif
254234
ps_mm_destroy(ps_mm_instance);
255235
return SUCCESS;
256236
}
@@ -284,7 +264,7 @@ PS_READ_FUNC(mm)
284264

285265
/* If there is an ID and strict mode, verify existence */
286266
if (PS(use_strict_mode)
287-
&& ps_mm_key_exists(data, key) == FAILURE) {
267+
&& g_hash_table_contains(data->hash, key) == FAILURE) {
288268
/* key points to PS(id), but cannot change here. */
289269
if (key) {
290270
efree(PS(id));
@@ -301,7 +281,7 @@ PS_READ_FUNC(mm)
301281
PS(session_status) = php_session_active;
302282
}
303283

304-
sd = ps_sd_lookup(data, PS(id), false);
284+
sd = g_hash_table_lookup(data->hash, PS(id));
305285
if (sd) {
306286
*val = zend_string_init(sd->data, sd->datalen, false);
307287
ret = SUCCESS;
@@ -315,9 +295,11 @@ PS_WRITE_FUNC(mm)
315295
PS_MM_DATA;
316296
ps_sd *sd;
317297

318-
mm_lock(data->mm, MM_LOCK_RW);
298+
#ifdef ZTS
299+
tsrm_mutex_lock(session_mm_lock);
300+
#endif
319301

320-
sd = ps_sd_lookup(data, key, true);
302+
sd = g_hash_table_lookup(data->hash, key);
321303
if (!sd) {
322304
sd = ps_sd_new(data, key);
323305
ps_mm_debug(("new entry for %s\n", ZSTR_VAL(key)));
@@ -341,6 +323,9 @@ PS_WRITE_FUNC(mm)
341323
}
342324
}
343325

326+
#ifdef ZTS
327+
tsrm_mutex_unlock(session_mm_lock);
328+
#endif
344329
return sd ? SUCCESS : FAILURE;
345330
}
346331

@@ -349,13 +334,18 @@ PS_DESTROY_FUNC(mm)
349334
PS_MM_DATA;
350335
ps_sd *sd;
351336

352-
mm_lock(data->mm, MM_LOCK_RW);
337+
#ifdef ZTS
338+
tsrm_mutex_lock(session_mm_lock);
339+
#endif
353340

354-
sd = ps_sd_lookup(data, key, false);
341+
sd = g_hash_table_lookup(data->hash, key);
355342
if (sd) {
356343
ps_sd_destroy(data, key, sd);
357344
}
358345

346+
#ifdef ZTS
347+
tsrm_mutex_unlock(session_mm_lock);
348+
#endif
359349
return SUCCESS;
360350
}
361351

@@ -387,7 +377,7 @@ PS_CREATE_SID_FUNC(mm)
387377
do {
388378
sid = php_session_create_id((void **)&data);
389379
/* Check collision */
390-
if (ps_mm_key_exists(data, sid) == SUCCESS) {
380+
if (g_hash_table_contains(data->hash, sid) == SUCCESS) {
391381
if (sid) {
392382
zend_string_release_ex(sid, 0);
393383
sid = NULL;

0 commit comments

Comments
 (0)