zstd-ruby 0.1.2 → 1.1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/ext/zstdruby/libzstd/BUCK +186 -0
- data/ext/zstdruby/libzstd/Makefile +58 -31
- data/ext/zstdruby/libzstd/common/mem.h +1 -1
- data/ext/zstdruby/libzstd/common/pool.c +194 -0
- data/ext/zstdruby/libzstd/common/pool.h +56 -0
- data/ext/zstdruby/libzstd/common/threading.c +79 -0
- data/ext/zstdruby/libzstd/common/threading.h +104 -0
- data/ext/zstdruby/libzstd/common/zstd_common.c +0 -4
- data/ext/zstdruby/libzstd/common/zstd_errors.h +16 -2
- data/ext/zstdruby/libzstd/common/zstd_internal.h +9 -0
- data/ext/zstdruby/libzstd/compress/zstd_compress.c +94 -51
- data/ext/zstdruby/libzstd/compress/zstd_opt.h +6 -6
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +740 -0
- data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +78 -0
- data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +39 -22
- data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +26 -0
- data/ext/zstdruby/libzstd/dictBuilder/cover.c +1021 -0
- data/ext/zstdruby/libzstd/dictBuilder/zdict.c +60 -12
- data/ext/zstdruby/libzstd/dictBuilder/zdict.h +109 -19
- data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +2 -0
- data/ext/zstdruby/libzstd/legacy/zstd_v04.c +13 -19
- data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4 -2
- data/ext/zstdruby/libzstd/legacy/zstd_v07.c +3 -3
- data/ext/zstdruby/libzstd/zstd.h +53 -25
- data/lib/zstd-ruby/version.rb +1 -1
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 477faa534daab9e4b42a615f87b29a6ef16676b9
|
4
|
+
data.tar.gz: 3ad80717437d8b90e07b3c6c55762fa248a38a14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c35693a0ffc216560e455cc78f5659797b4baefe685e8efe2ff0e37b712543914673fa31879261c3ba8b62a7b2c3589efe75512162c91e5b095ee75bb6f99d26
|
7
|
+
data.tar.gz: 086228081f38a4eabe4de8979fb07c61964e62e233c0233e15872d43deea534d98fa2ab7ef3022703984e3be8dc16b694211edd1b0beb231a8f8a12f6553e6fd
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ See https://github.com/facebook/zstd
|
|
9
9
|
Fork from https://github.com/jarredholman/ruby-zstd.
|
10
10
|
|
11
11
|
## Zstd version
|
12
|
-
v1.1.
|
12
|
+
v1.1.3 (https://github.com/facebook/zstd/releases/tag/v1.1.3)
|
13
13
|
|
14
14
|
## Installation
|
15
15
|
|
@@ -0,0 +1,186 @@
|
|
1
|
+
cxx_library(
|
2
|
+
name='zstd',
|
3
|
+
header_namespace='',
|
4
|
+
visibility=['PUBLIC'],
|
5
|
+
deps=[
|
6
|
+
':common',
|
7
|
+
':compress',
|
8
|
+
':decompress',
|
9
|
+
':deprecated',
|
10
|
+
],
|
11
|
+
)
|
12
|
+
|
13
|
+
cxx_library(
|
14
|
+
name='compress',
|
15
|
+
header_namespace='',
|
16
|
+
visibility=['PUBLIC'],
|
17
|
+
exported_headers=subdir_glob([
|
18
|
+
('compress', 'zstdmt_compress.h'),
|
19
|
+
]),
|
20
|
+
headers=subdir_glob([
|
21
|
+
('compress', 'zstd_opt.h'),
|
22
|
+
]),
|
23
|
+
srcs=[
|
24
|
+
'compress/zstd_compress.c',
|
25
|
+
'compress/zstdmt_compress.c',
|
26
|
+
],
|
27
|
+
deps=[':common'],
|
28
|
+
)
|
29
|
+
|
30
|
+
cxx_library(
|
31
|
+
name='decompress',
|
32
|
+
header_namespace='',
|
33
|
+
visibility=['PUBLIC'],
|
34
|
+
srcs=['decompress/zstd_decompress.c'],
|
35
|
+
deps=[
|
36
|
+
':common',
|
37
|
+
':legacy',
|
38
|
+
],
|
39
|
+
)
|
40
|
+
|
41
|
+
cxx_library(
|
42
|
+
name='deprecated',
|
43
|
+
header_namespace='',
|
44
|
+
visibility=['PUBLIC'],
|
45
|
+
exported_headers=subdir_glob([
|
46
|
+
('decprecated', '*.h'),
|
47
|
+
]),
|
48
|
+
srcs=glob(['deprecated/*.c']),
|
49
|
+
deps=[':common'],
|
50
|
+
)
|
51
|
+
|
52
|
+
cxx_library(
|
53
|
+
name='legacy',
|
54
|
+
header_namespace='',
|
55
|
+
visibility=['PUBLIC'],
|
56
|
+
exported_headers=subdir_glob([
|
57
|
+
('legacy', '*.h'),
|
58
|
+
]),
|
59
|
+
srcs=glob(['legacy/*.c']),
|
60
|
+
deps=[':common'],
|
61
|
+
)
|
62
|
+
|
63
|
+
cxx_library(
|
64
|
+
name='zdict',
|
65
|
+
header_namespace='',
|
66
|
+
visibility=['PUBLIC'],
|
67
|
+
exported_headers=subdir_glob([
|
68
|
+
('dictBuilder', 'zdict.h'),
|
69
|
+
]),
|
70
|
+
headers=subdir_glob([
|
71
|
+
('dictBuilder', 'divsufsort.h'),
|
72
|
+
]),
|
73
|
+
srcs=glob(['dictBuilder/*.c']),
|
74
|
+
deps=[':common'],
|
75
|
+
)
|
76
|
+
|
77
|
+
cxx_library(
|
78
|
+
name='bitstream',
|
79
|
+
header_namespace='',
|
80
|
+
visibility=['PUBLIC'],
|
81
|
+
exported_headers=subdir_glob([
|
82
|
+
('common', 'bitstream.h'),
|
83
|
+
]),
|
84
|
+
)
|
85
|
+
|
86
|
+
cxx_library(
|
87
|
+
name='entropy',
|
88
|
+
header_namespace='',
|
89
|
+
visibility=['PUBLIC'],
|
90
|
+
exported_headers=subdir_glob([
|
91
|
+
('common', 'fse.h'),
|
92
|
+
('common', 'huf.h'),
|
93
|
+
]),
|
94
|
+
srcs=[
|
95
|
+
'common/entropy_common.c',
|
96
|
+
'common/fse_decompress.c',
|
97
|
+
'compress/fse_compress.c',
|
98
|
+
'compress/huf_compress.c',
|
99
|
+
'decompress/huf_decompress.c',
|
100
|
+
],
|
101
|
+
deps=[
|
102
|
+
':bitstream',
|
103
|
+
':errors',
|
104
|
+
':mem',
|
105
|
+
],
|
106
|
+
)
|
107
|
+
|
108
|
+
cxx_library(
|
109
|
+
name='errors',
|
110
|
+
header_namespace='',
|
111
|
+
visibility=['PUBLIC'],
|
112
|
+
exported_headers=subdir_glob([
|
113
|
+
('common', 'error_private.h'),
|
114
|
+
('common', 'zstd_errors.h'),
|
115
|
+
]),
|
116
|
+
srcs=['common/error_private.c'],
|
117
|
+
)
|
118
|
+
|
119
|
+
cxx_library(
|
120
|
+
name='mem',
|
121
|
+
header_namespace='',
|
122
|
+
visibility=['PUBLIC'],
|
123
|
+
exported_headers=subdir_glob([
|
124
|
+
('common', 'mem.h'),
|
125
|
+
]),
|
126
|
+
)
|
127
|
+
|
128
|
+
cxx_library(
|
129
|
+
name='pool',
|
130
|
+
header_namespace='',
|
131
|
+
visibility=['PUBLIC'],
|
132
|
+
exported_headers=subdir_glob([
|
133
|
+
('common', 'pool.h'),
|
134
|
+
]),
|
135
|
+
srcs=['common/pool.c'],
|
136
|
+
deps=[':threading'],
|
137
|
+
)
|
138
|
+
|
139
|
+
cxx_library(
|
140
|
+
name='threading',
|
141
|
+
header_namespace='',
|
142
|
+
visibility=['PUBLIC'],
|
143
|
+
exported_headers=subdir_glob([
|
144
|
+
('common', 'threading.h'),
|
145
|
+
]),
|
146
|
+
srcs=['common/threading.c'],
|
147
|
+
)
|
148
|
+
|
149
|
+
cxx_library(
|
150
|
+
name='xxhash',
|
151
|
+
header_namespace='',
|
152
|
+
visibility=['PUBLIC'],
|
153
|
+
exported_headers=subdir_glob([
|
154
|
+
('common', 'xxhash.h'),
|
155
|
+
]),
|
156
|
+
srcs=['common/xxhash.c'],
|
157
|
+
)
|
158
|
+
|
159
|
+
cxx_library(
|
160
|
+
name='zstd_common',
|
161
|
+
header_namespace='',
|
162
|
+
visibility=['PUBLIC'],
|
163
|
+
exported_headers=subdir_glob([
|
164
|
+
('', 'zstd.h'),
|
165
|
+
('common', 'zstd_internal.h'),
|
166
|
+
]),
|
167
|
+
srcs=['common/zstd_common.c'],
|
168
|
+
deps=[
|
169
|
+
':errors',
|
170
|
+
':mem',
|
171
|
+
],
|
172
|
+
)
|
173
|
+
|
174
|
+
cxx_library(
|
175
|
+
name='common',
|
176
|
+
deps=[
|
177
|
+
':bitstream',
|
178
|
+
':entropy',
|
179
|
+
':errors',
|
180
|
+
':mem',
|
181
|
+
':pool',
|
182
|
+
':threading',
|
183
|
+
':xxhash',
|
184
|
+
':zstd_common',
|
185
|
+
]
|
186
|
+
)
|
@@ -1,11 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# ##########################################################################
|
2
2
|
# Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
3
3
|
# All rights reserved.
|
4
4
|
#
|
5
|
+
# This Makefile is validated for Linux, macOS, *BSD, Hurd, Solaris, MSYS2 targets
|
6
|
+
#
|
5
7
|
# This source code is licensed under the BSD-style license found in the
|
6
8
|
# LICENSE file in the root directory of this source tree. An additional grant
|
7
9
|
# of patent rights can be found in the PATENTS file in the same directory.
|
8
|
-
#
|
10
|
+
# ##########################################################################
|
9
11
|
|
10
12
|
# Version numbers
|
11
13
|
LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
|
@@ -18,17 +20,12 @@ LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
|
|
18
20
|
LIBVER := $(shell echo $(LIBVER_SCRIPT))
|
19
21
|
VERSION?= $(LIBVER)
|
20
22
|
|
21
|
-
DESTDIR?=
|
22
|
-
PREFIX ?= /usr/local
|
23
|
-
LIBDIR ?= $(PREFIX)/lib
|
24
|
-
INCLUDEDIR=$(PREFIX)/include
|
25
|
-
|
26
23
|
CPPFLAGS+= -I. -I./common -DXXH_NAMESPACE=ZSTD_
|
27
24
|
CFLAGS ?= -O3
|
28
|
-
|
29
|
-
-Wswitch-enum -Wdeclaration-after-statement
|
30
|
-
-Wpointer-arith
|
31
|
-
CFLAGS += $(MOREFLAGS)
|
25
|
+
DEBUGFLAGS = -g -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
|
26
|
+
-Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
|
27
|
+
-Wstrict-prototypes -Wundef -Wpointer-arith
|
28
|
+
CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
|
32
29
|
FLAGS = $(CPPFLAGS) $(CFLAGS)
|
33
30
|
|
34
31
|
|
@@ -41,6 +38,8 @@ CPPFLAGS += -I./legacy -DZSTD_LEGACY_SUPPORT=1
|
|
41
38
|
ZSTD_FILES+= $(wildcard legacy/*.c)
|
42
39
|
endif
|
43
40
|
|
41
|
+
ZSTD_OBJ := $(patsubst %.c,%.o,$(ZSTD_FILES))
|
42
|
+
|
44
43
|
# OS X linker doesn't support -soname, and use different extension
|
45
44
|
# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
|
46
45
|
ifeq ($(shell uname), Darwin)
|
@@ -60,15 +59,14 @@ LIBZSTD = libzstd.$(SHARED_EXT_VER)
|
|
60
59
|
|
61
60
|
.PHONY: default all clean install uninstall
|
62
61
|
|
63
|
-
default: lib
|
62
|
+
default: lib-release
|
64
63
|
|
65
64
|
all: lib
|
66
65
|
|
67
66
|
libzstd.a: ARFLAGS = rcs
|
68
|
-
libzstd.a: $(
|
67
|
+
libzstd.a: $(ZSTD_OBJ)
|
69
68
|
@echo compiling static library
|
70
|
-
@$(
|
71
|
-
@$(AR) $(ARFLAGS) $@ *.o
|
69
|
+
@$(AR) $(ARFLAGS) $@ $^
|
72
70
|
|
73
71
|
$(LIBZSTD): LDFLAGS += -shared -fPIC -fvisibility=hidden
|
74
72
|
$(LIBZSTD): $(ZSTD_FILES)
|
@@ -87,14 +85,41 @@ libzstd : $(LIBZSTD)
|
|
87
85
|
|
88
86
|
lib: libzstd.a libzstd
|
89
87
|
|
88
|
+
lib-release: DEBUGFLAGS :=
|
89
|
+
lib-release: lib
|
90
|
+
|
90
91
|
clean:
|
91
|
-
@$(RM)
|
92
|
-
@$(RM)
|
92
|
+
@$(RM) -r *.dSYM # Mac OS-X specific
|
93
|
+
@$(RM) core *.o *.a *.gcda *.$(SHARED_EXT) *.$(SHARED_EXT).* libzstd.pc
|
94
|
+
@$(RM) dll/libzstd.dll dll/libzstd.lib
|
95
|
+
@$(RM) common/*.o compress/*.o decompress/*.o dictBuilder/*.o legacy/*.o deprecated/*.o
|
93
96
|
@echo Cleaning library completed
|
94
97
|
|
95
|
-
|
96
|
-
#make install is validated only for Linux, OSX,
|
97
|
-
|
98
|
+
#-----------------------------------------------------------------------------
|
99
|
+
# make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets
|
100
|
+
#-----------------------------------------------------------------------------
|
101
|
+
ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS))
|
102
|
+
|
103
|
+
ifneq (,$(filter $(shell uname),SunOS))
|
104
|
+
INSTALL ?= ginstall
|
105
|
+
else
|
106
|
+
INSTALL ?= install
|
107
|
+
endif
|
108
|
+
|
109
|
+
PREFIX ?= /usr/local
|
110
|
+
DESTDIR ?=
|
111
|
+
LIBDIR ?= $(PREFIX)/lib
|
112
|
+
INCLUDEDIR ?= $(PREFIX)/include
|
113
|
+
|
114
|
+
ifneq (,$(filter $(shell uname),OpenBSD FreeBSD NetBSD DragonFly))
|
115
|
+
PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
|
116
|
+
else
|
117
|
+
PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig
|
118
|
+
endif
|
119
|
+
|
120
|
+
INSTALL_LIB ?= $(INSTALL) -m 755
|
121
|
+
INSTALL_DATA ?= $(INSTALL) -m 644
|
122
|
+
|
98
123
|
|
99
124
|
libzstd.pc:
|
100
125
|
libzstd.pc: libzstd.pc.in
|
@@ -106,16 +131,18 @@ libzstd.pc: libzstd.pc.in
|
|
106
131
|
$< >$@
|
107
132
|
|
108
133
|
install: libzstd.a libzstd libzstd.pc
|
109
|
-
|
110
|
-
|
111
|
-
@
|
112
|
-
|
113
|
-
|
114
|
-
@
|
115
|
-
@
|
116
|
-
@
|
117
|
-
|
118
|
-
|
134
|
+
@$(INSTALL) -d -m 755 $(DESTDIR)$(PKGCONFIGDIR)/ $(DESTDIR)$(INCLUDEDIR)/
|
135
|
+
@$(INSTALL_DATA) libzstd.pc $(DESTDIR)$(PKGCONFIGDIR)/
|
136
|
+
@echo Installing libraries
|
137
|
+
@$(INSTALL_LIB) libzstd.a $(DESTDIR)$(LIBDIR)
|
138
|
+
@$(INSTALL_LIB) libzstd.$(SHARED_EXT_VER) $(DESTDIR)$(LIBDIR)
|
139
|
+
@ln -sf libzstd.$(SHARED_EXT_VER) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
|
140
|
+
@ln -sf libzstd.$(SHARED_EXT_VER) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
|
141
|
+
@echo Installing includes
|
142
|
+
@$(INSTALL_DATA) zstd.h $(DESTDIR)$(INCLUDEDIR)
|
143
|
+
@$(INSTALL_DATA) common/zstd_errors.h $(DESTDIR)$(INCLUDEDIR)
|
144
|
+
@$(INSTALL_DATA) deprecated/zbuff.h $(DESTDIR)$(INCLUDEDIR) # prototypes generate deprecation warnings
|
145
|
+
@$(INSTALL_DATA) dictBuilder/zdict.h $(DESTDIR)$(INCLUDEDIR)
|
119
146
|
@echo zstd static and shared library installed
|
120
147
|
|
121
148
|
uninstall:
|
@@ -123,7 +150,7 @@ uninstall:
|
|
123
150
|
@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
|
124
151
|
@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
|
125
152
|
@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_VER)
|
126
|
-
@$(RM) $(DESTDIR)$(
|
153
|
+
@$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libzstd.pc
|
127
154
|
@$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd.h
|
128
155
|
@$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd_errors.h
|
129
156
|
@$(RM) $(DESTDIR)$(INCLUDEDIR)/zbuff.h # Deprecated streaming functions
|
@@ -39,7 +39,7 @@ extern "C" {
|
|
39
39
|
#endif
|
40
40
|
|
41
41
|
/* code only tested on 32 and 64 bits systems */
|
42
|
-
#define MEM_STATIC_ASSERT(c) { enum {
|
42
|
+
#define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; }
|
43
43
|
MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
|
44
44
|
|
45
45
|
|
@@ -0,0 +1,194 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2016-present, Facebook, Inc.
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* This source code is licensed under the BSD-style license found in the
|
6
|
+
* LICENSE file in the root directory of this source tree. An additional grant
|
7
|
+
* of patent rights can be found in the PATENTS file in the same directory.
|
8
|
+
*/
|
9
|
+
|
10
|
+
|
11
|
+
/* ====== Dependencies ======= */
|
12
|
+
#include <stddef.h> /* size_t */
|
13
|
+
#include <stdlib.h> /* malloc, calloc, free */
|
14
|
+
#include "pool.h"
|
15
|
+
|
16
|
+
/* ====== Compiler specifics ====== */
|
17
|
+
#if defined(_MSC_VER)
|
18
|
+
# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
|
19
|
+
#endif
|
20
|
+
|
21
|
+
|
22
|
+
#ifdef ZSTD_MULTITHREAD
|
23
|
+
|
24
|
+
#include "threading.h" /* pthread adaptation */
|
25
|
+
|
26
|
+
/* A job is a function and an opaque argument */
|
27
|
+
typedef struct POOL_job_s {
|
28
|
+
POOL_function function;
|
29
|
+
void *opaque;
|
30
|
+
} POOL_job;
|
31
|
+
|
32
|
+
struct POOL_ctx_s {
|
33
|
+
/* Keep track of the threads */
|
34
|
+
pthread_t *threads;
|
35
|
+
size_t numThreads;
|
36
|
+
|
37
|
+
/* The queue is a circular buffer */
|
38
|
+
POOL_job *queue;
|
39
|
+
size_t queueHead;
|
40
|
+
size_t queueTail;
|
41
|
+
size_t queueSize;
|
42
|
+
/* The mutex protects the queue */
|
43
|
+
pthread_mutex_t queueMutex;
|
44
|
+
/* Condition variable for pushers to wait on when the queue is full */
|
45
|
+
pthread_cond_t queuePushCond;
|
46
|
+
/* Condition variables for poppers to wait on when the queue is empty */
|
47
|
+
pthread_cond_t queuePopCond;
|
48
|
+
/* Indicates if the queue is shutting down */
|
49
|
+
int shutdown;
|
50
|
+
};
|
51
|
+
|
52
|
+
/* POOL_thread() :
|
53
|
+
Work thread for the thread pool.
|
54
|
+
Waits for jobs and executes them.
|
55
|
+
@returns : NULL on failure else non-null.
|
56
|
+
*/
|
57
|
+
static void* POOL_thread(void* opaque) {
|
58
|
+
POOL_ctx* const ctx = (POOL_ctx*)opaque;
|
59
|
+
if (!ctx) { return NULL; }
|
60
|
+
for (;;) {
|
61
|
+
/* Lock the mutex and wait for a non-empty queue or until shutdown */
|
62
|
+
pthread_mutex_lock(&ctx->queueMutex);
|
63
|
+
while (ctx->queueHead == ctx->queueTail && !ctx->shutdown) {
|
64
|
+
pthread_cond_wait(&ctx->queuePopCond, &ctx->queueMutex);
|
65
|
+
}
|
66
|
+
/* empty => shutting down: so stop */
|
67
|
+
if (ctx->queueHead == ctx->queueTail) {
|
68
|
+
pthread_mutex_unlock(&ctx->queueMutex);
|
69
|
+
return opaque;
|
70
|
+
}
|
71
|
+
/* Pop a job off the queue */
|
72
|
+
{ POOL_job const job = ctx->queue[ctx->queueHead];
|
73
|
+
ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
|
74
|
+
/* Unlock the mutex, signal a pusher, and run the job */
|
75
|
+
pthread_mutex_unlock(&ctx->queueMutex);
|
76
|
+
pthread_cond_signal(&ctx->queuePushCond);
|
77
|
+
job.function(job.opaque);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
/* Unreachable */
|
81
|
+
}
|
82
|
+
|
83
|
+
POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) {
|
84
|
+
POOL_ctx *ctx;
|
85
|
+
/* Check the parameters */
|
86
|
+
if (!numThreads || !queueSize) { return NULL; }
|
87
|
+
/* Allocate the context and zero initialize */
|
88
|
+
ctx = (POOL_ctx *)calloc(1, sizeof(POOL_ctx));
|
89
|
+
if (!ctx) { return NULL; }
|
90
|
+
/* Initialize the job queue.
|
91
|
+
* It needs one extra space since one space is wasted to differentiate empty
|
92
|
+
* and full queues.
|
93
|
+
*/
|
94
|
+
ctx->queueSize = queueSize + 1;
|
95
|
+
ctx->queue = (POOL_job *)malloc(ctx->queueSize * sizeof(POOL_job));
|
96
|
+
ctx->queueHead = 0;
|
97
|
+
ctx->queueTail = 0;
|
98
|
+
pthread_mutex_init(&ctx->queueMutex, NULL);
|
99
|
+
pthread_cond_init(&ctx->queuePushCond, NULL);
|
100
|
+
pthread_cond_init(&ctx->queuePopCond, NULL);
|
101
|
+
ctx->shutdown = 0;
|
102
|
+
/* Allocate space for the thread handles */
|
103
|
+
ctx->threads = (pthread_t *)malloc(numThreads * sizeof(pthread_t));
|
104
|
+
ctx->numThreads = 0;
|
105
|
+
/* Check for errors */
|
106
|
+
if (!ctx->threads || !ctx->queue) { POOL_free(ctx); return NULL; }
|
107
|
+
/* Initialize the threads */
|
108
|
+
{ size_t i;
|
109
|
+
for (i = 0; i < numThreads; ++i) {
|
110
|
+
if (pthread_create(&ctx->threads[i], NULL, &POOL_thread, ctx)) {
|
111
|
+
ctx->numThreads = i;
|
112
|
+
POOL_free(ctx);
|
113
|
+
return NULL;
|
114
|
+
} }
|
115
|
+
ctx->numThreads = numThreads;
|
116
|
+
}
|
117
|
+
return ctx;
|
118
|
+
}
|
119
|
+
|
120
|
+
/*! POOL_join() :
|
121
|
+
Shutdown the queue, wake any sleeping threads, and join all of the threads.
|
122
|
+
*/
|
123
|
+
static void POOL_join(POOL_ctx *ctx) {
|
124
|
+
/* Shut down the queue */
|
125
|
+
pthread_mutex_lock(&ctx->queueMutex);
|
126
|
+
ctx->shutdown = 1;
|
127
|
+
pthread_mutex_unlock(&ctx->queueMutex);
|
128
|
+
/* Wake up sleeping threads */
|
129
|
+
pthread_cond_broadcast(&ctx->queuePushCond);
|
130
|
+
pthread_cond_broadcast(&ctx->queuePopCond);
|
131
|
+
/* Join all of the threads */
|
132
|
+
{ size_t i;
|
133
|
+
for (i = 0; i < ctx->numThreads; ++i) {
|
134
|
+
pthread_join(ctx->threads[i], NULL);
|
135
|
+
} }
|
136
|
+
}
|
137
|
+
|
138
|
+
void POOL_free(POOL_ctx *ctx) {
|
139
|
+
if (!ctx) { return; }
|
140
|
+
POOL_join(ctx);
|
141
|
+
pthread_mutex_destroy(&ctx->queueMutex);
|
142
|
+
pthread_cond_destroy(&ctx->queuePushCond);
|
143
|
+
pthread_cond_destroy(&ctx->queuePopCond);
|
144
|
+
if (ctx->queue) free(ctx->queue);
|
145
|
+
if (ctx->threads) free(ctx->threads);
|
146
|
+
free(ctx);
|
147
|
+
}
|
148
|
+
|
149
|
+
void POOL_add(void *ctxVoid, POOL_function function, void *opaque) {
|
150
|
+
POOL_ctx *ctx = (POOL_ctx *)ctxVoid;
|
151
|
+
if (!ctx) { return; }
|
152
|
+
|
153
|
+
pthread_mutex_lock(&ctx->queueMutex);
|
154
|
+
{ POOL_job const job = {function, opaque};
|
155
|
+
/* Wait until there is space in the queue for the new job */
|
156
|
+
size_t newTail = (ctx->queueTail + 1) % ctx->queueSize;
|
157
|
+
while (ctx->queueHead == newTail && !ctx->shutdown) {
|
158
|
+
pthread_cond_wait(&ctx->queuePushCond, &ctx->queueMutex);
|
159
|
+
newTail = (ctx->queueTail + 1) % ctx->queueSize;
|
160
|
+
}
|
161
|
+
/* The queue is still going => there is space */
|
162
|
+
if (!ctx->shutdown) {
|
163
|
+
ctx->queue[ctx->queueTail] = job;
|
164
|
+
ctx->queueTail = newTail;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
pthread_mutex_unlock(&ctx->queueMutex);
|
168
|
+
pthread_cond_signal(&ctx->queuePopCond);
|
169
|
+
}
|
170
|
+
|
171
|
+
#else /* ZSTD_MULTITHREAD not defined */
|
172
|
+
/* No multi-threading support */
|
173
|
+
|
174
|
+
/* We don't need any data, but if it is empty malloc() might return NULL. */
|
175
|
+
struct POOL_ctx_s {
|
176
|
+
int data;
|
177
|
+
};
|
178
|
+
|
179
|
+
POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) {
|
180
|
+
(void)numThreads;
|
181
|
+
(void)queueSize;
|
182
|
+
return (POOL_ctx *)malloc(sizeof(POOL_ctx));
|
183
|
+
}
|
184
|
+
|
185
|
+
void POOL_free(POOL_ctx *ctx) {
|
186
|
+
if (ctx) free(ctx);
|
187
|
+
}
|
188
|
+
|
189
|
+
void POOL_add(void *ctx, POOL_function function, void *opaque) {
|
190
|
+
(void)ctx;
|
191
|
+
function(opaque);
|
192
|
+
}
|
193
|
+
|
194
|
+
#endif /* ZSTD_MULTITHREAD */
|