Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit 77bf7be

Browse files
committed
adjusted zlib module to lock only individual objects, not the whole module, when compressing and decompressing. This effectively allows it to be multi-threaded
1 parent 8050691 commit 77bf7be

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
*.pyc
2+
*.o
3+
*.so

mod/zlibmodule.c

+25-9
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@
2323
there was an de/compress object-specific lock. However, for the
2424
moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
2525
de/compress objects.
26+
27+
S.T.
28+
And this is exactly what we do, have one lock per object. This should allow
29+
multi-threaded compression and decompression
2630
*/
2731

28-
static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
29-
3032
#define ENTER_ZLIB \
3133
Py_BEGIN_ALLOW_THREADS \
32-
PyThread_acquire_lock(zlib_lock, 1); \
34+
PyThread_acquire_lock(self->zlib_lock, 1); \
3335
Py_END_ALLOW_THREADS
3436

3537
#define LEAVE_ZLIB \
36-
PyThread_release_lock(zlib_lock);
38+
PyThread_release_lock(self->zlib_lock);
3739

3840
#else
3941

4042
#define ENTER_ZLIB
4143
#define LEAVE_ZLIB
4244

43-
#endif
45+
#endif /* WITH THREAD */
4446

4547
/* The following parameters are copied from zutil.h, version 0.95 */
4648
#define DEFLATED 8
@@ -67,6 +69,10 @@ typedef struct
6769
PyObject *unused_data;
6870
PyObject *unconsumed_tail;
6971
int is_initialised;
72+
73+
#ifdef WITH_THREAD
74+
PyThread_type_lock zlib_lock;
75+
#endif
7076
} compobject;
7177

7278
static void
@@ -106,6 +112,10 @@ newcompobject(PyTypeObject *type)
106112
Py_DECREF(self);
107113
return NULL;
108114
}
115+
116+
#ifdef WITH_THREAD
117+
self->zlib_lock = PyThread_allocate_lock();
118+
#endif /* WITH_THREAD */
109119
return self;
110120
}
111121

@@ -368,6 +378,11 @@ Comp_dealloc(compobject *self)
368378
deflateEnd(&self->zst);
369379
Py_XDECREF(self->unused_data);
370380
Py_XDECREF(self->unconsumed_tail);
381+
382+
#ifdef WITH_THREAD
383+
PyThread_free_lock(self->zlib_lock);
384+
#endif /* WITH_THREAD */
385+
371386
PyObject_Del(self);
372387
}
373388

@@ -378,6 +393,11 @@ Decomp_dealloc(compobject *self)
378393
inflateEnd(&self->zst);
379394
Py_XDECREF(self->unused_data);
380395
Py_XDECREF(self->unconsumed_tail);
396+
397+
#ifdef WITH_THREAD
398+
PyThread_free_lock(self->zlib_lock);
399+
#endif /* WITH_THREAD */
400+
381401
PyObject_Del(self);
382402
}
383403

@@ -1032,8 +1052,4 @@ PyInit_zlib(void)
10321052
PyModule_AddObject(m, "ZLIB_VERSION", ver);
10331053

10341054
PyModule_AddStringConstant(m, "__version__", "1.0");
1035-
1036-
#ifdef WITH_THREAD
1037-
zlib_lock = PyThread_allocate_lock();
1038-
#endif /* WITH_THREAD */
10391055
}

0 commit comments

Comments
 (0)