|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "runtime.h" |
| 9 | + |
| 10 | +#ifdef GIT_THREADS |
| 11 | + |
| 12 | +#ifdef GIT_WIN32 |
| 13 | +# include "win32/thread.h" |
| 14 | +#else |
| 15 | +# include "unix/pthread.h" |
| 16 | +#endif |
| 17 | + |
| 18 | +struct git_custom_tls_callbacks { |
| 19 | + git_retrieve_tls_for_internal_thread_cb retrieve_storage_for_internal_thread; |
| 20 | + |
| 21 | + git_set_tls_on_internal_thread_cb set_storage_on_thread; |
| 22 | + |
| 23 | + git_teardown_tls_on_internal_thread_cb teardown_storage_on_thread; |
| 24 | + |
| 25 | + git_rwlock lock; |
| 26 | +}; |
| 27 | + |
| 28 | +struct git_custom_tls_callbacks git__custom_tls = { 0, 0, 0 }; |
| 29 | + |
| 30 | +static void git_custom_tls_global_shutdown(void) |
| 31 | +{ |
| 32 | + if (git_rwlock_wrlock(&git__custom_tls.lock) < 0) |
| 33 | + return; |
| 34 | + |
| 35 | + git__custom_tls.retrieve_storage_for_internal_thread = 0; |
| 36 | + git__custom_tls.set_storage_on_thread = 0; |
| 37 | + git__custom_tls.teardown_storage_on_thread = 0; |
| 38 | + |
| 39 | + git_rwlock_wrunlock(&git__custom_tls.lock); |
| 40 | + git_rwlock_free(&git__custom_tls.lock); |
| 41 | +} |
| 42 | + |
| 43 | +int git_custom_tls__global_init(void) |
| 44 | +{ |
| 45 | + if (git_rwlock_init(&git__custom_tls.lock) < 0) |
| 46 | + return -1; |
| 47 | + |
| 48 | + return git_runtime_shutdown_register(git_custom_tls_global_shutdown); |
| 49 | +} |
| 50 | + |
| 51 | +int git_custom_tls_set_callbacks( |
| 52 | + git_retrieve_tls_for_internal_thread_cb retrieve_storage_for_internal_thread, |
| 53 | + git_set_tls_on_internal_thread_cb set_storage_on_thread, |
| 54 | + git_teardown_tls_on_internal_thread_cb teardown_storage_on_thread) |
| 55 | +{ |
| 56 | + /* We want to ensure that all callbacks are set or not set in totality. |
| 57 | + * It does not make sense to have a subset of callbacks set. |
| 58 | + */ |
| 59 | + assert((retrieve_storage_for_internal_thread && set_storage_on_thread && |
| 60 | + teardown_storage_on_thread) || !(retrieve_storage_for_internal_thread && |
| 61 | + set_storage_on_thread && teardown_storage_on_thread)); |
| 62 | + |
| 63 | + if (git_rwlock_wrlock(&git__custom_tls.lock) < 0) { |
| 64 | + git_error_set(GIT_ERROR_OS, "failed to lock custom thread local storage"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + git__custom_tls.retrieve_storage_for_internal_thread = |
| 69 | + retrieve_storage_for_internal_thread; |
| 70 | + git__custom_tls.set_storage_on_thread = |
| 71 | + set_storage_on_thread; |
| 72 | + git__custom_tls.teardown_storage_on_thread = |
| 73 | + teardown_storage_on_thread; |
| 74 | + |
| 75 | + git_rwlock_wrunlock(&git__custom_tls.lock); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +int git_custom_tls__init(git_custom_tls *tls) |
| 80 | +{ |
| 81 | + if (git_rwlock_rdlock(&git__custom_tls.lock) < 0) { |
| 82 | + git_error_set(GIT_ERROR_OS, "failed to lock custom thread local storage"); |
| 83 | + return -1; |
| 84 | + } |
| 85 | + |
| 86 | + /* We try to ensure that all 3 callbacks must be set or not set. |
| 87 | + * It would not make sense to have a subset of the callbacks set. |
| 88 | + */ |
| 89 | + if (!git__custom_tls.retrieve_storage_for_internal_thread) { |
| 90 | + tls->set_storage_on_thread = NULL; |
| 91 | + tls->teardown_storage_on_thread = NULL; |
| 92 | + tls->payload = NULL; |
| 93 | + } else { |
| 94 | + /* We set these on a struct so that if for whatever reason the opts are changed |
| 95 | + * at least the opts will remain consistent for any given thread already in |
| 96 | + * motion. |
| 97 | + */ |
| 98 | + tls->set_storage_on_thread = git__custom_tls.set_storage_on_thread; |
| 99 | + tls->teardown_storage_on_thread = git__custom_tls.teardown_storage_on_thread; |
| 100 | + tls->payload = git__custom_tls.retrieve_storage_for_internal_thread(); |
| 101 | + } |
| 102 | + |
| 103 | + git_rwlock_rdunlock(&git__custom_tls.lock); |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +#else |
| 108 | + |
| 109 | +int git_custom_tls__global_init(void) |
| 110 | +{ |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int git_custom_tls_set_callbacks( |
| 115 | + git_retrieve_tls_for_internal_thread_cb retrieve_storage_for_internal_thread, |
| 116 | + git_set_tls_on_internal_thread_cb set_storage_on_thread, |
| 117 | + git_teardown_tls_on_internal_thread_cb teardown_storage_on_thread) |
| 118 | +{ |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +#endif |
0 commit comments