Skip to content

Commit a8639be

Browse files
authored
Improve lock contention in render_list etc (#335)
* Minimise the time we hold the lock when dequeuing a request * Add missing static to global variables * Scale the queue length with the number of threads
1 parent aa6e968 commit a8639be

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

src/render_submit_queue.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@
3535
#include "protocol_helper.h"
3636
#include "render_config.h"
3737

38-
#define QMAX 32
39-
40-
pthread_mutex_t qLock;
41-
pthread_mutex_t qStatsLock;
38+
static pthread_mutex_t qLock;
39+
static pthread_mutex_t qStatsLock;
4240
static pthread_cond_t qCondNotEmpty;
4341
static pthread_cond_t qCondNotFull;
4442

4543
static int maxLoad = 0;
4644

45+
static unsigned int qMaxLen;
4746
static unsigned int qLen;
4847
struct qItem {
4948
char *mapname;
@@ -139,7 +138,6 @@ static int process(struct protocol * cmd, int fd)
139138

140139
static struct protocol * fetch(void)
141140
{
142-
struct protocol * cmd;
143141
pthread_mutex_lock(&qLock);
144142

145143
while (qLen == 0) {
@@ -157,32 +155,30 @@ static struct protocol * fetch(void)
157155
exit(1);
158156
}
159157

160-
cmd = malloc(sizeof(struct protocol));
161-
memset(cmd, 0, sizeof(struct protocol));
158+
struct qItem *e = qHead;
162159

163-
cmd->ver = 2;
164-
cmd->cmd = cmdRenderBulk;
165-
cmd->z = qHead->z;
166-
cmd->x = qHead->x;
167-
cmd->y = qHead->y;
168-
strncpy(cmd->xmlname, qHead->mapname, XMLCONFIG_MAX - 1);
169-
170-
if (qHead == qTail) {
171-
free(qHead->mapname);
172-
free(qHead);
160+
if (--qLen == 0) {
173161
qHead = NULL;
174162
qTail = NULL;
175-
qLen = 0;
176163
} else {
177-
struct qItem *e = qHead;
178164
qHead = qHead->next;
179-
free(e->mapname);
180-
free(e);
181-
qLen--;
182165
}
183166

184167
pthread_cond_signal(&qCondNotFull);
185168
pthread_mutex_unlock(&qLock);
169+
170+
struct protocol * cmd = malloc(sizeof(struct protocol));;
171+
172+
cmd->ver = 2;
173+
cmd->cmd = cmdRenderBulk;
174+
cmd->z = e->z;
175+
cmd->x = e->x;
176+
cmd->y = e->y;
177+
strncpy(cmd->xmlname, e->mapname, XMLCONFIG_MAX - 1);
178+
179+
free(e->mapname);
180+
free(e);
181+
186182
return cmd;
187183
}
188184

@@ -204,7 +200,7 @@ void enqueue(const char *xmlname, int x, int y, int z)
204200

205201
pthread_mutex_lock(&qLock);
206202

207-
while (qLen == QMAX) {
203+
while (qLen == qMaxLen) {
208204
int ret = pthread_cond_wait(&qCondNotFull, &qLock);
209205

210206
if (ret != 0) {
@@ -403,6 +399,8 @@ void spawn_workers(int num, const char *spath, int max_load)
403399
pthread_cond_init(&qCondNotEmpty, NULL);
404400
pthread_cond_init(&qCondNotFull, NULL);
405401

402+
qMaxLen = no_workers;
403+
406404
printf("Starting %d rendering threads\n", no_workers);
407405
workers = calloc(sizeof(pthread_t), no_workers);
408406

0 commit comments

Comments
 (0)