xref: /haiku/src/system/libroot/posix/locale/ThreadLocale.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2022, Trung Nguyen, trungnt282910@gmail.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <ctype.h>
8 
9 #include <tls.h>
10 #include <kernel/OS.h>
11 #include <ThreadLocale.h>
12 
13 
14 // From glibc's localeinfo.h
15 extern BPrivate::Libroot::GlibcLocaleStruct _nl_global_locale;
16 
17 
18 namespace BPrivate {
19 namespace Libroot {
20 
21 
22 static void DestroyThreadLocale(void* ptr)
23 {
24 	ThreadLocale* threadLocale = (ThreadLocale*)ptr;
25 	delete threadLocale;
26 }
27 
28 
29 ThreadLocale*
30 GetCurrentThreadLocale()
31 {
32 	ThreadLocale* threadLocale = (ThreadLocale*)tls_get(TLS_LOCALE_SLOT);
33 	if (threadLocale == NULL) {
34 		threadLocale = new ThreadLocale();
35 		threadLocale->glibcLocaleStruct = _nl_global_locale;
36 		threadLocale->threadLocaleInfo = NULL;
37 		on_exit_thread(DestroyThreadLocale, threadLocale);
38 		tls_set(TLS_LOCALE_SLOT, threadLocale);
39 	}
40 	return threadLocale;
41 }
42 
43 
44 // Exported so that glibc could also use.
45 extern "C" GlibcLocaleStruct*
46 _nl_current_locale()
47 {
48 	return &GetCurrentThreadLocale()->glibcLocaleStruct;
49 }
50 
51 
52 extern "C" const unsigned short**
53 __ctype_b_loc()
54 {
55 	return &GetCurrentThreadLocale()->glibcLocaleStruct.__ctype_b;
56 }
57 
58 
59 extern "C" const int**
60 __ctype_tolower_loc()
61 {
62 	return &GetCurrentThreadLocale()->glibcLocaleStruct.__ctype_tolower;
63 }
64 
65 
66 extern "C" const int**
67 __ctype_toupper_loc()
68 {
69 	return &GetCurrentThreadLocale()->glibcLocaleStruct.__ctype_toupper;
70 }
71 
72 
73 }	// namespace Libroot
74 }	// namespace BPrivate
75