-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Refactor "global" state #5546
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
Refactor "global" state #5546
Conversation
e283d93 to
e90b4dc
Compare
pks-t
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a huge fan of these refactorings, thanks a lot for working on them!
8c8e473 to
dcb3b86
Compare
8b1e1ba to
f266347
Compare
| extern size_t git_mwindow__window_size; | ||
| extern size_t git_mwindow__mapped_limit; | ||
| extern size_t git_indexer__max_objects; | ||
| extern bool git_disable_pack_keep_file_checks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sometimes wonder if it would improve readability if those were labelled better, like e.g. calling them git_settings_mwindow_window_size. That'd make it much more obvious it's a global setting.
Anyway, definitely nothing we have to do as part of your PR.
src/libgit2.c
Outdated
| return git_runtime_shutdown(); | ||
| } | ||
|
|
||
| /* Library settings */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving stuff like version or feature information definitely makes a ton of sense to me. But I'm not sure I'm a huge fan of moving settings over, too. I kind of get why you're doing it, but if you ask me we should instead work on clearly separating settings and make them more standalone. Like clearly identifying options with git_setting_user_agent, git_setting_ssl_ciphers etc.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with you here, and I agree that this is continuing to grow in scope. Let's tackle this in a followup PR?
|
@ethomson It's becoming quite hard to review due to the PRs growing scope. Would it be a huge burden to ask you to split up this PR into multiple ones to get those parts in more quickly? Fine with a "no" if you think the effort for this is too high. |
6f53beb to
6d7ab77
Compare
Sure. I split out the renaming of crtdbg into #5580. |
6e41dd4 to
2381982
Compare
|
On Sun, Jul 12, 2020 at 01:22:45PM -0700, Edward Thomson wrote:
@ethomson commented on this pull request.
> @@ -48,3 +76,280 @@ int git_libgit2_shutdown(void)
{
return git_runtime_shutdown();
}
+
+/* Library settings */
Agree with you here, and I agree that this is continuing to grow in scope. Let's tackle this in a followup PR?
Fair enough. Is the PR currently in a state where it makes sense to do
another review?
|
Yes I think so |
Instead of treating win32 thread initialization specially in the win32 git_libgit2_init function, add a git_global_threads_init function.
Move the MSVC C runtime debugging bits into the allocator's global init function.
Move the settings global data teardown into its own separate function, instead of intermingled with the global state.
Move the mwindow mutex into the mwindow code itself, initializing it in the mwindow global initialization function instead of in the global initializer.
Ensure that we can allocate the error message buffer. In keeping with our typical policiess, we allow (small) memory leaks in the case where we're out of memory.
2381982 to
d5deace
Compare
Our "global initialization" has accumulated some debris over the years. It was previously responsible for both running the various global initializers (that set up various subsystems) _and_ setting up the "global state", which is actually the thread-local state for things like error reporting. Separate the thread local state out into "threadstate". Use the normal subsystem initialization functions that we already have to set it up. This makes both the global initialization system and the threadstate system simpler to reason about.
We were never properly testing git_thread_exit. Do so.
We want to store a pointer to emulate `pthread_exit` on Windows. Do this within the threading infrastructure so that it could potentially be re-used outside of the context of libgit2 itself.
Provide a mechanism for system components to register for initialization and shutdown of the libgit2 runtime.
Now that we've identified that our global settings really aren't global at all, and refactored the library to match that, change global.c to libgit2.c, which is especially nice since the prefix of the functions matches the filename.
d5deace to
634c285
Compare
|
I renamed the thread-local data that we carry around as part of our "global" state to "threadstate". Upon seeing #5602, it's clear that we should have a generic implementation of an internal TLS API so that (eg) checkout can use it and that "tlsdata" is extremely ambiguous in this regard. |
|
Since this is largely an internal refactoring, and I think that I addressed all of the feedback, I'm going to merge this to unblock #5602. |
The notion of "global" state has become somewhat confused over time;
global.chandles the libraries initialization and shutdown functions, state that's global to the library and thread-local data. This is an attempt to clean this up, breaking each of these notions out into its own separate section instead of keeping them comingled and simplifying the initialization and shutdown of the library by using our existing patterns in places that we had not done that.git_libgit2_initsets up global state for a number of subsystems (it sets up the allocator functions, does hash initialization, etc). Most of these subsystems have their init functions set up in aninit_callbacksarray, and their init functions register shutdown callbacks. But not all; some have initialization scattered around thegit_libgit2_initmachinery. Move them all into the same init/shutdown pattern:The "global state" (
GIT_GLOBAL) is really thread-local state and contains things like the current thread's error data. Rename it accordingly, so that it's easier to reason about.Moves the current thread's return data on Windows (for
pthread_exitemulation) out of the shared TLS data structure, and managing it with the rest of the threading initialization/teardown routines.Adds a "runtime" layer to handle the init/shutdown functions, so that we
git_runtime_shutdown_registerto register a shutdown handler with the runtime (instead ofgit__shutdown_register- thegit__name feels like an antipattern)