xref: /haiku/src/system/libroot/add-ons/icu/ICUThreadLocalStorageValue.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright 2011, Oliver Tappe, zooey@hirschkaefer.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ICUThreadLocalStorageValue.h"
8 
9 #include <new>
10 
11 #include <unicode/ucnv.h>
12 
13 
14 namespace BPrivate {
15 namespace Libroot {
16 
17 
18 ICUThreadLocalStorageValue::ICUThreadLocalStorageValue()
19 	: converter(NULL)
20 {
21 	charset[0] = '\0';
22 }
23 
24 
25 ICUThreadLocalStorageValue::~ICUThreadLocalStorageValue()
26 {
27 	if (converter != NULL)
28 		ucnv_close(converter);
29 }
30 
31 
32 status_t
33 ICUThreadLocalStorageValue::GetInstanceForKey(pthread_key_t tlsKey,
34 	ICUThreadLocalStorageValue*& instanceOut)
35 {
36 	ICUThreadLocalStorageValue* tlsValue = NULL;
37 	void* value = pthread_getspecific(tlsKey);
38 	if (value == NULL) {
39 		tlsValue = new (std::nothrow) ICUThreadLocalStorageValue();
40 		if (tlsValue == NULL)
41 			return B_NO_MEMORY;
42 		pthread_setspecific(tlsKey, tlsValue);
43 	} else
44 		tlsValue = static_cast<ICUThreadLocalStorageValue*>(value);
45 
46 	instanceOut = tlsValue;
47 
48 	return B_OK;
49 }
50 
51 
52 }	// namespace Libroot
53 }	// namespace BPrivate
54