Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reading from Pool more thread-safe #472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/libsolv-pool.txt
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,10 @@ Utility functions

Allocate space on the pool's temporary space area. This space has a limited
lifetime, it will be automatically freed after a fixed amount (currently
16) of other pool_alloctmpspace() calls are done.
500) of other pool_alloctmpspace() calls are done.
The function is not completely thread-safe, but it mitigates the problem by
locking and using sufficiently large temporary space. That allows reading
from pool from several tens of concurrent threads.

void pool_freetmpspace(Pool *pool, const char *space);

Expand Down
9 changes: 7 additions & 2 deletions src/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -1723,15 +1723,20 @@ pool_id2langid(Pool *pool, Id id, const char *lang, int create)
char *
pool_alloctmpspace(Pool *pool, int len)
{
int n = pool->tmpspace.n;
if (!len)
return 0;

pthread_mutex_lock(&pool->tmpspace.lock);
int n = pool->tmpspace.n;
pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
pthread_mutex_unlock(&pool->tmpspace.lock);

if (len > pool->tmpspace.len[n])
{
pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
pool->tmpspace.len[n] = len + 32;
}
pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;

return pool->tmpspace.buf[n];
}

Expand Down
4 changes: 3 additions & 1 deletion src/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LIBSOLV_POOL_H
#define LIBSOLV_POOL_H

#include <pthread.h>
#include <stdio.h>

#include "solvversion.h"
Expand Down Expand Up @@ -53,12 +54,13 @@ typedef struct s_Datapos {
#ifdef LIBSOLV_INTERNAL

/* how many strings to maintain (round robin) */
#define POOL_TMPSPACEBUF 16
#define POOL_TMPSPACEBUF 500

struct s_Pool_tmpspace {
char *buf[POOL_TMPSPACEBUF];
int len[POOL_TMPSPACEBUF];
int n;
pthread_mutex_t lock;
};

#endif
Expand Down