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

Eliminate/convert manual pthread synchronization with STL counterparts #3302

Merged
merged 10 commits into from
Sep 24, 2024
10 changes: 3 additions & 7 deletions lib/package.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include "system.h"

#include <mutex>
#include <set>

#include <netinet/in.h>
#include <pthread.h>

#include <rpm/rpmlib.h> /* XXX RPMSIGTAG, other sig stuff */
#include <rpm/rpmts.h>
Expand Down Expand Up @@ -117,21 +117,17 @@ rpmTagVal headerMergeLegacySigs(Header h, Header sigh, char **msg)
*/
static int stashKeyid(unsigned int keyid)
{
static pthread_mutex_t keyid_lock = PTHREAD_MUTEX_INITIALIZER;
static std::mutex keyid_mutex;
static std::set<unsigned int> keyids;
int seen = 0;

if (keyid == 0)
return 0;

/* Just pretend we didn't see the keyid if we fail to lock */
if (pthread_mutex_lock(&keyid_lock))
return 0;

std::lock_guard<std::mutex> lock(keyid_mutex);
auto ret = keyids.insert(keyid);
seen = (ret.second == false);

pthread_mutex_unlock(&keyid_lock);
return seen;
}

Expand Down
63 changes: 26 additions & 37 deletions lib/rpmrc.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "system.h"

#include <mutex>
#include <shared_mutex>

#include <fcntl.h>
#include <stdarg.h>
#include <pthread.h>

#if defined(__linux__)
#include <elf.h>
Expand Down Expand Up @@ -40,6 +42,9 @@

#include "debug.h"

using wrlock = std::unique_lock<std::shared_mutex>;
using rdlock = std::shared_lock<std::shared_mutex>;

static const char * defrcfiles = NULL;
const char * macrofiles = NULL;

Expand Down Expand Up @@ -145,7 +150,7 @@ struct rpmrcCtx_s {
struct tableType_s tables[RPM_MACHTABLE_COUNT];
int machDefaults;
int pathDefaults;
pthread_rwlock_t lock;
std::shared_mutex mutex;
};

/* prototypes */
Expand All @@ -158,8 +163,8 @@ static void rebuildCompatTables(rpmrcCtx ctx, int type, const char * name);

static void rpmRebuildTargetVars(rpmrcCtx ctx, const char **target, const char ** canontarget);

/* Force context (lock) acquisition through a function */
static rpmrcCtx rpmrcCtxAcquire(int write)
/* Force context acquisition through a function */
static rpmrcCtx rpmrcCtxAcquire()
{
static struct rpmrcCtx_s _globalCtx = {
.currTables = { RPM_MACHTABLE_INSTOS, RPM_MACHTABLE_INSTARCH },
Expand All @@ -169,26 +174,12 @@ static rpmrcCtx rpmrcCtxAcquire(int write)
{ "buildarch", 0, 1 },
{ "buildos", 0, 1 }
},
.lock = PTHREAD_RWLOCK_INITIALIZER,
};
rpmrcCtx ctx = &_globalCtx;

/* XXX: errors should be handled */
if (write)
pthread_rwlock_wrlock(&ctx->lock);
else
pthread_rwlock_rdlock(&ctx->lock);

return ctx;
}

/* Release context (lock) */
static rpmrcCtx rpmrcCtxRelease(rpmrcCtx ctx)
{
pthread_rwlock_unlock(&ctx->lock);
return NULL;
}

static int optionCompare(const void * a, const void * b)
{
return rstrcasecmp(((const struct rpmOption *) a)->name,
Expand Down Expand Up @@ -1791,7 +1782,8 @@ static rpmRC rpmReadRC(rpmrcCtx ctx, const char * rcfiles)
int rpmReadConfigFiles(const char * file, const char * target)
{
int rc = -1; /* assume failure */
rpmrcCtx ctx = rpmrcCtxAcquire(1);
rpmrcCtx ctx = rpmrcCtxAcquire();
wrlock lock(ctx->mutex);

if (rpmInitCrypto())
goto exit;
Expand Down Expand Up @@ -1826,13 +1818,13 @@ int rpmReadConfigFiles(const char * file, const char * target)
rc = 0;

exit:
rpmrcCtxRelease(ctx);
return rc;
}

void rpmFreeRpmrc(void)
{
rpmrcCtx ctx = rpmrcCtxAcquire(1);
rpmrcCtx ctx = rpmrcCtxAcquire();
wrlock lock(ctx->mutex);
int i, j, k;

ctx->platpat = argvFree(ctx->platpat);
Expand Down Expand Up @@ -1902,14 +1894,14 @@ void rpmFreeRpmrc(void)
rpmluaFree(lua);
rpmugFree();

rpmrcCtxRelease(ctx);
return;
}

int rpmShowRC(FILE * fp)
{
/* Write-context necessary as this calls rpmSetTables(), ugh */
rpmrcCtx ctx = rpmrcCtxAcquire(1);
rpmrcCtx ctx = rpmrcCtxAcquire();
/* Write-lock necessary as this calls rpmSetTables(), ugh */
wrlock lock(ctx->mutex);
const struct rpmOption *opt;
rpmds ds = NULL;
int i;
Expand Down Expand Up @@ -1977,46 +1969,44 @@ int rpmShowRC(FILE * fp)

rpmDumpMacroTable(NULL, fp);

/* XXX: Move this earlier eventually... */
rpmrcCtxRelease(ctx);

return 0;
}

int rpmMachineScore(int type, const char * name)
{
int score = 0;
if (name) {
rpmrcCtx ctx = rpmrcCtxAcquire(0);
rpmrcCtx ctx = rpmrcCtxAcquire();
rdlock lock(ctx->mutex);
machEquivInfo info = machEquivSearch(&ctx->tables[type].equiv, name);
if (info)
score = info->score;
rpmrcCtxRelease(ctx);
}
return score;
}

int rpmIsKnownArch(const char *name)
{
rpmrcCtx ctx = rpmrcCtxAcquire(0);
rpmrcCtx ctx = rpmrcCtxAcquire();
rdlock lock(ctx->mutex);
canonEntry canon = lookupInCanonTable(name,
ctx->tables[RPM_MACHTABLE_INSTARCH].canons,
ctx->tables[RPM_MACHTABLE_INSTARCH].canonsLength);
int known = (canon != NULL || rstreq(name, "noarch"));
rpmrcCtxRelease(ctx);
return known;
}

void rpmGetArchInfo(const char ** name, int * num)
{
rpmrcCtx ctx = rpmrcCtxAcquire(0);
rpmrcCtx ctx = rpmrcCtxAcquire();
rdlock lock(ctx->mutex);
getMachineInfo(ctx, ARCH, name, num);
rpmrcCtxRelease(ctx);
}

int rpmGetArchColor(const char *arch)
{
rpmrcCtx ctx = rpmrcCtxAcquire(0);
rpmrcCtx ctx = rpmrcCtxAcquire();
rdlock lock(ctx->mutex);
const char *color;
char *e;
int color_i = -1; /* assume failure */
Expand All @@ -2031,15 +2021,14 @@ int rpmGetArchColor(const char *arch)
color_i = -1;
}
}
rpmrcCtxRelease(ctx);

return color_i;
}

void rpmGetOsInfo(const char ** name, int * num)
{
rpmrcCtx ctx = rpmrcCtxAcquire(0);
rpmrcCtx ctx = rpmrcCtxAcquire();
rdlock lock(ctx->mutex);
getMachineInfo(ctx, OS, name, num);
rpmrcCtxRelease(ctx);
}

1 change: 0 additions & 1 deletion lib/rpmvs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "system.h"

#include <pthread.h>
#include <rpm/rpmkeyring.h>
#include <rpm/rpmmacro.h>
#include <rpm/rpmlog.h>
Expand Down
Loading
Loading