Skip to content

Commit a5cb2cc

Browse files
authored
Merge pull request libgit2#5720 from libgit2/ethomson/tlsdata
Thread-local storage: a generic internal library (with no allocations)
2 parents 1adb841 + 20ce17f commit a5cb2cc

32 files changed

+454
-266
lines changed

src/attr_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ int git_attr_session__init(git_attr_session *session, git_repository *repo)
960960
GIT_ASSERT_ARG(repo);
961961

962962
memset(session, 0, sizeof(*session));
963-
session->key = git_atomic_inc(&repo->attr_session_key);
963+
session->key = git_atomic32_inc(&repo->attr_session_key);
964964

965965
return 0;
966966
}

src/attrcache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
108108
* Replace the existing value if another thread has
109109
* created it in the meantime.
110110
*/
111-
old = git__swap(entry->file[file->source], file);
111+
old = git_atomic_swap(entry->file[file->source], file);
112112

113113
if (old) {
114114
GIT_REFCOUNT_OWN(old, NULL);
@@ -132,7 +132,7 @@ static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
132132
return error;
133133

134134
if ((entry = attr_cache_lookup_entry(cache, file->entry->path)) != NULL)
135-
old = git__compare_and_swap(&entry->file[file->source], file, NULL);
135+
old = git_atomic_compare_and_swap(&entry->file[file->source], file, NULL);
136136

137137
attr_cache_unlock(cache);
138138

@@ -321,7 +321,7 @@ static void attr_cache__free(git_attr_cache *cache)
321321

322322
git_strmap_foreach_value(cache->files, entry, {
323323
for (i = 0; i < GIT_ATTR_FILE_NUM_SOURCES; ++i) {
324-
if ((file = git__swap(entry->file[i], NULL)) != NULL) {
324+
if ((file = git_atomic_swap(entry->file[i], NULL)) != NULL) {
325325
GIT_REFCOUNT_OWN(file, NULL);
326326
git_attr_file__free(file);
327327
}
@@ -395,7 +395,7 @@ int git_attr_cache__init(git_repository *repo)
395395
(ret = git_pool_init(&cache->pool, 1)) < 0)
396396
goto cancel;
397397

398-
cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
398+
cache = git_atomic_compare_and_swap(&repo->attrcache, NULL, cache);
399399
if (cache)
400400
goto cancel; /* raced with another thread, free this but no error */
401401

@@ -417,7 +417,7 @@ int git_attr_cache_flush(git_repository *repo)
417417
/* this could be done less expensively, but for now, we'll just free
418418
* the entire attrcache and let the next use reinitialize it...
419419
*/
420-
if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
420+
if (repo && (cache = git_atomic_swap(repo->attrcache, NULL)) != NULL)
421421
attr_cache__free(cache);
422422

423423
return 0;

src/cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include "repository.h"
1111
#include "commit.h"
12-
#include "thread-utils.h"
12+
#include "thread.h"
1313
#include "util.h"
1414
#include "odb.h"
1515
#include "object.h"
@@ -235,7 +235,7 @@ void git_cached_obj_decref(void *_obj)
235235
{
236236
git_cached_obj *obj = _obj;
237237

238-
if (git_atomic_dec(&obj->refcount) == 0) {
238+
if (git_atomic32_dec(&obj->refcount) == 0) {
239239
switch (obj->flags) {
240240
case GIT_CACHE_STORE_RAW:
241241
git_odb_object__free(_obj);

src/cache.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "git2/oid.h"
1414
#include "git2/odb.h"
1515

16-
#include "thread-utils.h"
16+
#include "thread.h"
1717
#include "oidmap.h"
1818

1919
enum {
@@ -23,11 +23,11 @@ enum {
2323
};
2424

2525
typedef struct {
26-
git_oid oid;
27-
int16_t type; /* git_object_t value */
28-
uint16_t flags; /* GIT_CACHE_STORE value */
29-
size_t size;
30-
git_atomic refcount;
26+
git_oid oid;
27+
int16_t type; /* git_object_t value */
28+
uint16_t flags; /* GIT_CACHE_STORE value */
29+
size_t size;
30+
git_atomic32 refcount;
3131
} git_cached_obj;
3232

3333
typedef struct {
@@ -61,7 +61,7 @@ GIT_INLINE(size_t) git_cache_size(git_cache *cache)
6161
GIT_INLINE(void) git_cached_obj_incref(void *_obj)
6262
{
6363
git_cached_obj *obj = _obj;
64-
git_atomic_inc(&obj->refcount);
64+
git_atomic32_inc(&obj->refcount);
6565
}
6666

6767
void git_cached_obj_decref(void *_obj);

src/common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363
# include <pthread.h>
6464
# include <sched.h>
6565
# endif
66-
#define GIT_STDLIB_CALL
66+
67+
#define GIT_LIBGIT2_CALL
68+
#define GIT_SYSTEM_CALL
6769

6870
#ifdef GIT_USE_STAT_ATIMESPEC
6971
# define st_atim st_atimespec
@@ -78,7 +80,7 @@
7880
#include "git2/types.h"
7981
#include "git2/errors.h"
8082
#include "errors.h"
81-
#include "thread-utils.h"
83+
#include "thread.h"
8284
#include "integer.h"
8385
#include "assert_safe.h"
8486

src/config_cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ int git_config__configmap_lookup(int *out, git_config *config, git_configmap_ite
111111

112112
int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item)
113113
{
114-
*out = (int)(intptr_t)git__load(repo->configmap_cache[(int)item]);
114+
*out = (int)(intptr_t)git_atomic_load(repo->configmap_cache[(int)item]);
115115

116116
if (*out == GIT_CONFIGMAP_NOT_CACHED) {
117117
int error;
@@ -122,7 +122,7 @@ int git_repository__configmap_lookup(int *out, git_repository *repo, git_configm
122122
(error = git_config__configmap_lookup(out, config, item)) < 0)
123123
return error;
124124

125-
git__compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
125+
git_atomic_compare_and_swap(&repo->configmap_cache[(int)item], &oldval, out);
126126
}
127127

128128
return 0;

src/diff_driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static git_diff_driver_registry *git_repository_driver_registry(
144144
{
145145
if (!repo->diff_drivers) {
146146
git_diff_driver_registry *reg = git_diff_driver_registry_new();
147-
reg = git__compare_and_swap(&repo->diff_drivers, NULL, reg);
147+
reg = git_atomic_compare_and_swap(&repo->diff_drivers, NULL, reg);
148148

149149
if (reg != NULL) /* if we race, free losing allocation */
150150
git_diff_driver_registry_free(reg);

src/index.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void index_free(git_index *index)
461461
/* index iterators increment the refcount of the index, so if we
462462
* get here then there should be no outstanding iterators.
463463
*/
464-
if (git_atomic_get(&index->readers))
464+
if (git_atomic32_get(&index->readers))
465465
return;
466466

467467
git_index_clear(index);
@@ -488,14 +488,14 @@ void git_index_free(git_index *index)
488488
/* call with locked index */
489489
static void index_free_deleted(git_index *index)
490490
{
491-
int readers = (int)git_atomic_get(&index->readers);
491+
int readers = (int)git_atomic32_get(&index->readers);
492492
size_t i;
493493

494494
if (readers > 0 || !index->deleted.length)
495495
return;
496496

497497
for (i = 0; i < index->deleted.length; ++i) {
498-
git_index_entry *ie = git__swap(index->deleted.contents[i], NULL);
498+
git_index_entry *ie = git_atomic_swap(index->deleted.contents[i], NULL);
499499
index_entry_free(ie);
500500
}
501501

@@ -516,7 +516,7 @@ static int index_remove_entry(git_index *index, size_t pos)
516516
error = git_vector_remove(&index->entries, pos);
517517

518518
if (!error) {
519-
if (git_atomic_get(&index->readers) > 0) {
519+
if (git_atomic32_get(&index->readers) > 0) {
520520
error = git_vector_insert(&index->deleted, entry);
521521
} else {
522522
index_entry_free(entry);
@@ -2295,7 +2295,7 @@ int git_index_reuc_clear(git_index *index)
22952295
GIT_ASSERT_ARG(index);
22962296

22972297
for (i = 0; i < index->reuc.length; ++i)
2298-
index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL));
2298+
index_entry_reuc_free(git_atomic_swap(index->reuc.contents[i], NULL));
22992299

23002300
git_vector_clear(&index->reuc);
23012301

@@ -3197,7 +3197,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
31973197
/* well, this isn't good */;
31983198
} else {
31993199
git_vector_swap(&entries, &index->entries);
3200-
entries_map = git__swap(index->entries_map, entries_map);
3200+
entries_map = git_atomic_swap(index->entries_map, entries_map);
32013201
}
32023202

32033203
index->dirty = 1;
@@ -3331,7 +3331,7 @@ static int git_index_read_iterator(
33313331
goto done;
33323332

33333333
git_vector_swap(&new_entries, &index->entries);
3334-
new_entries_map = git__swap(index->entries_map, new_entries_map);
3334+
new_entries_map = git_atomic_swap(index->entries_map, new_entries_map);
33353335

33363336
git_vector_foreach(&remove_entries, i, entry) {
33373337
if (index->tree)
@@ -3637,7 +3637,7 @@ int git_index_snapshot_new(git_vector *snap, git_index *index)
36373637

36383638
GIT_REFCOUNT_INC(index);
36393639

3640-
git_atomic_inc(&index->readers);
3640+
git_atomic32_inc(&index->readers);
36413641
git_vector_sort(&index->entries);
36423642

36433643
error = git_vector_dup(snap, &index->entries, index->entries._cmp);
@@ -3652,7 +3652,7 @@ void git_index_snapshot_release(git_vector *snap, git_index *index)
36523652
{
36533653
git_vector_free(snap);
36543654

3655-
git_atomic_dec(&index->readers);
3655+
git_atomic32_dec(&index->readers);
36563656

36573657
git_index_free(index);
36583658
}

src/index.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct git_index {
3333
git_idxmap *entries_map;
3434

3535
git_vector deleted; /* deleted entries if readers > 0 */
36-
git_atomic readers; /* number of active iterators */
36+
git_atomic32 readers; /* number of active iterators */
3737

3838
unsigned int on_disk:1;
3939
unsigned int ignore_case:1;

src/libgit2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "refs.h"
2323
#include "runtime.h"
2424
#include "sysdir.h"
25-
#include "thread-utils.h"
25+
#include "thread.h"
2626
#include "threadstate.h"
2727
#include "git2/global.h"
2828
#include "streams/registry.h"

0 commit comments

Comments
 (0)