forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpthread.c
More file actions
73 lines (56 loc) · 1.55 KB
/
Copy pathpthread.c
File metadata and controls
73 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "pthread.h"
#include "thread.h"
#include "runtime.h"
git_tlsdata_key thread_handle;
static void git_threads_global_shutdown(void) {
git_tlsdata_dispose(thread_handle);
}
int git_threads_global_init(void) {
int error = git_tlsdata_init(&thread_handle, NULL);
if (error != 0) {
return error;
}
return git_runtime_shutdown_register(git_threads_global_shutdown);
}
static void *git_unix__threadproc(void *arg)
{
void *result;
int error;
git_thread *thread = arg;
error = git_tlsdata_set(thread_handle, thread);
if (error != 0) {
return NULL;
}
if (thread->tls.set_storage_on_thread) {
thread->tls.set_storage_on_thread(thread->tls.payload);
}
result = thread->proc(thread->param);
if (thread->tls.teardown_storage_on_thread) {
thread->tls.teardown_storage_on_thread();
}
return result;
}
int git_thread_create(
git_thread *thread,
void *(*start_routine)(void*),
void *arg)
{
thread->proc = start_routine;
thread->param = arg;
if (git_custom_tls__init(&thread->tls) < 0)
return -1;
return pthread_create(&thread->thread, NULL, git_unix__threadproc, thread);
}
void git_thread_exit(void *value)
{
git_thread *thread = git_tlsdata_get(thread_handle);
if (thread && thread->tls.teardown_storage_on_thread)
thread->tls.teardown_storage_on_thread();
return pthread_exit(value);
}