2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #if !defined(GIT_THREADS)
12 #define TLSDATA_MAX 16
16 void (GIT_SYSTEM_CALL
*destroy_fn
)(void *);
19 static tlsdata_value tlsdata_values
[TLSDATA_MAX
];
20 static int tlsdata_cnt
= 0;
22 int git_tlsdata_init(git_tlsdata_key
*key
, void (GIT_SYSTEM_CALL
*destroy_fn
)(void *))
24 if (tlsdata_cnt
>= TLSDATA_MAX
)
27 tlsdata_values
[tlsdata_cnt
].value
= NULL
;
28 tlsdata_values
[tlsdata_cnt
].destroy_fn
= destroy_fn
;
36 int git_tlsdata_set(git_tlsdata_key key
, void *value
)
38 if (key
< 0 || key
> tlsdata_cnt
)
41 tlsdata_values
[key
].value
= value
;
45 void *git_tlsdata_get(git_tlsdata_key key
)
47 if (key
< 0 || key
> tlsdata_cnt
)
50 return tlsdata_values
[key
].value
;
53 int git_tlsdata_dispose(git_tlsdata_key key
)
56 void (*destroy_fn
)(void *) = NULL
;
58 if (key
< 0 || key
> tlsdata_cnt
)
61 value
= tlsdata_values
[key
].value
;
62 destroy_fn
= tlsdata_values
[key
].destroy_fn
;
64 tlsdata_values
[key
].value
= NULL
;
65 tlsdata_values
[key
].destroy_fn
= NULL
;
67 if (value
&& destroy_fn
)
73 #elif defined(GIT_WIN32)
75 int git_tlsdata_init(git_tlsdata_key
*key
, void (GIT_SYSTEM_CALL
*destroy_fn
)(void *))
77 DWORD fls_index
= FlsAlloc(destroy_fn
);
79 if (fls_index
== FLS_OUT_OF_INDEXES
)
86 int git_tlsdata_set(git_tlsdata_key key
, void *value
)
88 if (!FlsSetValue(key
, value
))
94 void *git_tlsdata_get(git_tlsdata_key key
)
96 return FlsGetValue(key
);
99 int git_tlsdata_dispose(git_tlsdata_key key
)
107 #elif defined(_POSIX_THREADS)
109 int git_tlsdata_init(git_tlsdata_key
*key
, void (GIT_SYSTEM_CALL
*destroy_fn
)(void *))
111 if (pthread_key_create(key
, destroy_fn
) != 0)
117 int git_tlsdata_set(git_tlsdata_key key
, void *value
)
119 if (pthread_setspecific(key
, value
) != 0)
125 void *git_tlsdata_get(git_tlsdata_key key
)
127 return pthread_getspecific(key
);
130 int git_tlsdata_dispose(git_tlsdata_key key
)
132 if (pthread_key_delete(key
) != 0)
139 # error unknown threading model