summaryrefslogtreecommitdiff
path: root/src/mwindow.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <carlos@cmartin.tk>2012-08-18 22:11:49 +0200
committerCarlos Martín Nieto <cmn@elego.de>2012-08-20 12:02:52 +0200
commit8cef828d8d115c1f98678c13721fee59ca4540b7 (patch)
tree811ff0ad2b4b324cf4b11fd0f2b0ede1fdc04071 /src/mwindow.c
parent1a10fded40875f986164b80c6efd414cd1507cb8 (diff)
downloadlibgit2-8cef828d8d115c1f98678c13721fee59ca4540b7.tar.gz
Make the memory-window conrol structures global
Up to now, the idea was that the user would do all the operations for one repository in the same thread. Thus we could have the memory-mapped window information thread-local and avoid any locking. This is not practical in a few environments, such as Apple's GCD which allocates threads arbitrarily or the .NET CLR, where the OS-level thread can change at any moment. Make the control structure global and protect it with a mutex so we don't depend on the thread currently executing the code.
Diffstat (limited to 'src/mwindow.c')
-rw-r--r--src/mwindow.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/src/mwindow.c b/src/mwindow.c
index 1a5446b9c..4da5badb6 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -32,14 +32,20 @@ static struct {
DEFAULT_MAPPED_LIMIT,
};
+/* Whenever you want to read or modify this, grab git__mwindow_mutex */
+static git_mwindow_ctl mem_ctl;
+
/*
* Free all the windows in a sequence, typically because we're done
* with the file
*/
void git_mwindow_free_all(git_mwindow_file *mwf)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
unsigned int i;
+
+ git_mutex_lock(&git__mwindow_mutex);
+
/*
* Remove these windows from the global list
*/
@@ -67,6 +73,8 @@ void git_mwindow_free_all(git_mwindow_file *mwf)
mwf->windows = w->next;
git__free(w);
}
+
+ git_mutex_unlock(&git__mwindow_mutex);
}
/*
@@ -82,7 +90,7 @@ int git_mwindow_contains(git_mwindow *win, git_off_t offset)
/*
* Find the least-recently-used window in a file
*/
-void git_mwindow_scan_lru(
+static void git_mwindow_scan_lru(
git_mwindow_file *mwf,
git_mwindow **lru_w,
git_mwindow **lru_l)
@@ -107,11 +115,12 @@ void git_mwindow_scan_lru(
/*
* Close the least recently used window. You should check to see if
- * the file descriptors need closing from time to time.
+ * the file descriptors need closing from time to time. Called under
+ * lock from new_window.
*/
static int git_mwindow_close_lru(git_mwindow_file *mwf)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
unsigned int i;
git_mwindow *lru_w = NULL, *lru_l = NULL, **list = &mwf->windows;
@@ -146,13 +155,14 @@ static int git_mwindow_close_lru(git_mwindow_file *mwf)
return 0;
}
+/* This gets called under lock from git_mwindow_open */
static git_mwindow *new_window(
git_mwindow_file *mwf,
git_file fd,
git_off_t size,
git_off_t offset)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
size_t walign = _mw_options.window_size / 2;
git_off_t len;
git_mwindow *w;
@@ -208,9 +218,10 @@ unsigned char *git_mwindow_open(
size_t extra,
unsigned int *left)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
git_mwindow *w = *cursor;
+ git_mutex_lock(&git__mwindow_mutex);
if (!w || !(git_mwindow_contains(w, offset) && git_mwindow_contains(w, offset + extra))) {
if (w) {
w->inuse_cnt--;
@@ -228,8 +239,10 @@ unsigned char *git_mwindow_open(
*/
if (!w) {
w = new_window(mwf, mwf->fd, mwf->size, offset);
- if (w == NULL)
+ if (w == NULL) {
+ git_mutex_unlock(&git__mwindow_mutex);
return NULL;
+ }
w->next = mwf->windows;
mwf->windows = w;
}
@@ -247,32 +260,43 @@ unsigned char *git_mwindow_open(
if (left)
*left = (unsigned int)(w->window_map.len - offset);
+ git_mutex_unlock(&git__mwindow_mutex);
return (unsigned char *) w->window_map.data + offset;
}
int git_mwindow_file_register(git_mwindow_file *mwf)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
+ int ret;
+ git_mutex_lock(&git__mwindow_mutex);
if (ctl->windowfiles.length == 0 &&
- git_vector_init(&ctl->windowfiles, 8, NULL) < 0)
+ git_vector_init(&ctl->windowfiles, 8, NULL) < 0) {
+ git_mutex_unlock(&git__mwindow_mutex);
return -1;
+ }
+
+ ret = git_vector_insert(&ctl->windowfiles, mwf);
+ git_mutex_unlock(&git__mwindow_mutex);
- return git_vector_insert(&ctl->windowfiles, mwf);
+ return ret;
}
int git_mwindow_file_deregister(git_mwindow_file *mwf)
{
- git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl;
+ git_mwindow_ctl *ctl = &mem_ctl;
git_mwindow_file *cur;
unsigned int i;
+ git_mutex_lock(&git__mwindow_mutex);
git_vector_foreach(&ctl->windowfiles, i, cur) {
if (cur == mwf) {
git_vector_remove(&ctl->windowfiles, i);
+ git_mutex_unlock(&git__mwindow_mutex);
return 0;
}
}
+ git_mutex_unlock(&git__mwindow_mutex);
giterr_set(GITERR_ODB, "Failed to find the memory window file to deregister");
return -1;
@@ -282,7 +306,9 @@ void git_mwindow_close(git_mwindow **window)
{
git_mwindow *w = *window;
if (w) {
+ git_mutex_lock(&git__mwindow_mutex);
w->inuse_cnt--;
+ git_mutex_unlock(&git__mwindow_mutex);
*window = NULL;
}
}