blob: c348ae5330e7febafdd03965ac76d5c85b756bdc [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001From c80892f17e44331206c8318d53b63bb6a99554d0 Mon Sep 17 00:00:00 2001
2From: Alexander Kanavin <alex.kanavin@gmail.com>
3Date: Tue, 30 May 2017 13:58:30 +0300
4Subject: [PATCH 3/4] rpmstrpool.c: make operations over string pools
5 thread-safe
6
7Otherwise multithreaded rpm building explodes in various ways due
8to data races.
9
10Upstream-Status: Submitted [https://github.com/rpm-software-management/rpm/pull/226]
11Signed-off-by: Alexander Kanavin <alex.kanavin@gmail.com>
12
13---
14 rpmio/rpmstrpool.c | 56 +++++++++++++++++++++++++++++++++++++++++++++---------
15 1 file changed, 47 insertions(+), 9 deletions(-)
16
17diff --git a/rpmio/rpmstrpool.c b/rpmio/rpmstrpool.c
18index 30a57eb10..58ba95a02 100644
19--- a/rpmio/rpmstrpool.c
20+++ b/rpmio/rpmstrpool.c
21@@ -113,6 +113,8 @@ static poolHash poolHashCreate(int numBuckets)
22 return ht;
23 }
24
25+static const char * rpmstrPoolStrNoLock(rpmstrPool pool, rpmsid sid);
26+
27 static void poolHashResize(rpmstrPool pool, int numBuckets)
28 {
29 poolHash ht = pool->hash;
30@@ -120,7 +122,7 @@ static void poolHashResize(rpmstrPool pool, int numBuckets)
31
32 for (int i=0; i<ht->numBuckets; i++) {
33 if (!ht->buckets[i].keyid) continue;
34- unsigned int keyHash = rstrhash(rpmstrPoolStr(pool, ht->buckets[i].keyid));
35+ unsigned int keyHash = rstrhash(rpmstrPoolStrNoLock(pool, ht->buckets[i].keyid));
36 for (unsigned int j=0;;j++) {
37 unsigned int hash = hashbucket(keyHash, j) % numBuckets;
38 if (!buckets[hash].keyid) {
39@@ -149,7 +151,7 @@ static void poolHashAddHEntry(rpmstrPool pool, const char * key, unsigned int ke
40 ht->buckets[hash].keyid = keyid;
41 ht->keyCount++;
42 break;
43- } else if (!strcmp(rpmstrPoolStr(pool, ht->buckets[hash].keyid), key)) {
44+ } else if (!strcmp(rpmstrPoolStrNoLock(pool, ht->buckets[hash].keyid), key)) {
45 return;
46 }
47 }
48@@ -191,7 +193,7 @@ static void poolHashPrintStats(rpmstrPool pool)
49 int maxcollisions = 0;
50
51 for (i=0; i<ht->numBuckets; i++) {
52- unsigned int keyHash = rstrhash(rpmstrPoolStr(pool, ht->buckets[i].keyid));
53+ unsigned int keyHash = rstrhash(rpmstrPoolStrNoLock(pool, ht->buckets[i].keyid));
54 for (unsigned int j=0;;j++) {
55 unsigned int hash = hashbucket(keyHash, i) % ht->numBuckets;
56 if (hash==i) {
57@@ -221,7 +223,7 @@ static void rpmstrPoolRehash(rpmstrPool pool)
58
59 pool->hash = poolHashCreate(sizehint);
60 for (int i = 1; i <= pool->offs_size; i++)
61- poolHashAddEntry(pool, rpmstrPoolStr(pool, i), i);
62+ poolHashAddEntry(pool, rpmstrPoolStrNoLock(pool, i), i);
63 }
64
65 rpmstrPool rpmstrPoolCreate(void)
66@@ -245,6 +247,8 @@ rpmstrPool rpmstrPoolCreate(void)
67
68 rpmstrPool rpmstrPoolFree(rpmstrPool pool)
69 {
70+ #pragma omp critical(rpmstrpool)
71+ {
72 if (pool) {
73 if (pool->nrefs > 1) {
74 pool->nrefs--;
75@@ -260,18 +264,24 @@ rpmstrPool rpmstrPoolFree(rpmstrPool pool)
76 free(pool);
77 }
78 }
79+ }
80 return NULL;
81 }
82
83 rpmstrPool rpmstrPoolLink(rpmstrPool pool)
84 {
85+ #pragma omp critical(rpmstrpool)
86+ {
87 if (pool)
88 pool->nrefs++;
89+ }
90 return pool;
91 }
92
93 void rpmstrPoolFreeze(rpmstrPool pool, int keephash)
94 {
95+ #pragma omp critical(rpmstrpool)
96+ {
97 if (pool && !pool->frozen) {
98 if (!keephash) {
99 pool->hash = poolHashFree(pool->hash);
100@@ -281,16 +291,20 @@ void rpmstrPoolFreeze(rpmstrPool pool, int keephash)
101 pool->offs_alloced * sizeof(*pool->offs));
102 pool->frozen = 1;
103 }
104+ }
105 }
106
107 void rpmstrPoolUnfreeze(rpmstrPool pool)
108 {
109+ #pragma omp critical(rpmstrpool)
110+ {
111 if (pool) {
112 if (pool->hash == NULL) {
113 rpmstrPoolRehash(pool);
114 }
115 pool->frozen = 0;
116 }
117+ }
118 }
119
120 static rpmsid rpmstrPoolPut(rpmstrPool pool, const char *s, size_t slen, unsigned int hash)
121@@ -350,7 +364,7 @@ static rpmsid rpmstrPoolGet(rpmstrPool pool, const char * key, size_t keylen,
122 return 0;
123 }
124
125- s = rpmstrPoolStr(pool, ht->buckets[hash].keyid);
126+ s = rpmstrPoolStrNoLock(pool, ht->buckets[hash].keyid);
127 /* pool string could be longer than keylen, require exact matche */
128 if (strncmp(s, key, keylen) == 0 && s[keylen] == '\0')
129 return ht->buckets[hash].keyid;
130@@ -373,27 +387,31 @@ static inline rpmsid strn2id(rpmstrPool pool, const char *s, size_t slen,
131 rpmsid rpmstrPoolIdn(rpmstrPool pool, const char *s, size_t slen, int create)
132 {
133 rpmsid sid = 0;
134-
135+ #pragma omp critical(rpmstrpool)
136+ {
137 if (s != NULL) {
138 unsigned int hash = rstrnhash(s, slen);
139 sid = strn2id(pool, s, slen, hash, create);
140 }
141+ }
142 return sid;
143 }
144
145 rpmsid rpmstrPoolId(rpmstrPool pool, const char *s, int create)
146 {
147 rpmsid sid = 0;
148-
149+ #pragma omp critical(rpmstrpool)
150+ {
151 if (s != NULL) {
152 size_t slen;
153 unsigned int hash = rstrlenhash(s, &slen);
154 sid = strn2id(pool, s, slen, hash, create);
155 }
156+ }
157 return sid;
158 }
159
160-const char * rpmstrPoolStr(rpmstrPool pool, rpmsid sid)
161+static const char * rpmstrPoolStrNoLock(rpmstrPool pool, rpmsid sid)
162 {
163 const char *s = NULL;
164 if (pool && sid > 0 && sid <= pool->offs_size)
165@@ -401,12 +419,25 @@ const char * rpmstrPoolStr(rpmstrPool pool, rpmsid sid)
166 return s;
167 }
168
169+const char * rpmstrPoolStr(rpmstrPool pool, rpmsid sid)
170+{
171+ const char *s = NULL;
172+ #pragma omp critical(rpmstrpool)
173+ {
174+ s = rpmstrPoolStrNoLock(pool, sid);
175+ }
176+ return s;
177+}
178+
179 size_t rpmstrPoolStrlen(rpmstrPool pool, rpmsid sid)
180 {
181 size_t slen = 0;
182+ #pragma omp critical(rpmstrpool)
183+ {
184 if (pool && sid > 0 && sid <= pool->offs_size) {
185 slen = strlen(pool->offs[sid]);
186 }
187+ }
188 return slen;
189 }
190
191@@ -421,5 +452,12 @@ int rpmstrPoolStreq(rpmstrPool poolA, rpmsid sidA,
192
193 rpmsid rpmstrPoolNumStr(rpmstrPool pool)
194 {
195- return (pool != NULL) ? pool->offs_size : 0;
196+ rpmsid id = 0;
197+ #pragma omp critical(rpmstrpool)
198+ {
199+ if (pool) {
200+ id = pool->offs_size;
201+ }
202+ }
203+ return id;
204 }
205--
2062.11.0
207