1 | /* $Id: shthread.c 3505 2021-12-15 22:53:57Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Shell Thread Management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2010 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "shthread.h"
|
---|
28 | #include "shinstance.h"
|
---|
29 |
|
---|
30 | #if K_OS == K_OS_WINDOWS
|
---|
31 | # include <Windows.h>
|
---|
32 | #elif K_OS == K_OS_OS2
|
---|
33 | # include <InnoTekLIBC/FastInfoBlocks.h>
|
---|
34 | # include <InnoTekLIBC/thread.h>
|
---|
35 | #else
|
---|
36 | # include <pthread.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 | #if K_OS == K_OS_WINDOWS
|
---|
44 | static DWORD sh_tls = TLS_OUT_OF_INDEXES;
|
---|
45 | #elif K_OS == K_OS_OS2
|
---|
46 | static int sh_tls = -1;
|
---|
47 | #else
|
---|
48 | static int sh_tls_inited = 0;
|
---|
49 | static pthread_key_t sh_tls;
|
---|
50 | #endif
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Stores the shell instance pointer in a TLS entry.
|
---|
55 | *
|
---|
56 | * This will allocate the TLS entry on the first call. We assume
|
---|
57 | * there will no be races at that time.
|
---|
58 | *
|
---|
59 | * @param psh The shell instance.
|
---|
60 | */
|
---|
61 | void shthread_set_shell(struct shinstance *psh)
|
---|
62 | {
|
---|
63 | #if K_OS == K_OS_WINDOWS
|
---|
64 | if (sh_tls == TLS_OUT_OF_INDEXES)
|
---|
65 | {
|
---|
66 | sh_tls = TlsAlloc();
|
---|
67 | kHlpAssert(sh_tls != TLS_OUT_OF_INDEXES);
|
---|
68 | }
|
---|
69 | if (!TlsSetValue(sh_tls, psh))
|
---|
70 | kHlpAssert(0);
|
---|
71 |
|
---|
72 | #elif K_OS == K_OS_OS2
|
---|
73 | if (sh_tls == -1)
|
---|
74 | {
|
---|
75 | sh_tls = __libc_TLSAlloc();
|
---|
76 | kHlpAssert(sh_tls != -1);
|
---|
77 | }
|
---|
78 | if (__libc_TLSSet(sh_tls, psh) == -1)
|
---|
79 | kHlpAssert(0);
|
---|
80 | #else
|
---|
81 | if (!sh_tls_inited)
|
---|
82 | {
|
---|
83 | if (pthread_key_create(&sh_tls, NULL) != 0)
|
---|
84 | kHlpAssert(0);
|
---|
85 | sh_tls_inited = 1;
|
---|
86 | }
|
---|
87 | if (pthread_setspecific(sh_tls, psh) != 0)
|
---|
88 | kHlpAssert(0);
|
---|
89 | #endif
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Get the shell instance pointer from TLS.
|
---|
94 | *
|
---|
95 | * @returns The shell instance.
|
---|
96 | */
|
---|
97 | struct shinstance *shthread_get_shell(void)
|
---|
98 | {
|
---|
99 | shinstance *psh;
|
---|
100 | #if K_OS == K_OS_WINDOWS
|
---|
101 | psh = (shinstance *)TlsGetValue(sh_tls);
|
---|
102 | #elif K_OS == K_OS_OS2
|
---|
103 | psh = (shinstance *)__libc_TLSGet(sh_tls);
|
---|
104 | #else
|
---|
105 | psh = (shinstance *)pthread_getspecific(sh_tls);
|
---|
106 | #endif
|
---|
107 | return psh;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Sets the name of the current thread if supported by the OS.
|
---|
113 | */
|
---|
114 | void shthread_set_name(const char *name)
|
---|
115 | {
|
---|
116 | #if K_OS == K_OS_WINDOWS
|
---|
117 | typedef BOOL (WINAPI * PFNSETTHREADDESCRIPTION)(HANDLE, WCHAR *);
|
---|
118 | static KBOOL volatile s_initialized = K_FALSE;
|
---|
119 | static PFNSETTHREADDESCRIPTION volatile s_pfnSetThreadDescription = NULL;
|
---|
120 | PFNSETTHREADDESCRIPTION pfnSetThreadDescription = s_pfnSetThreadDescription;
|
---|
121 | WCHAR wszName[32];
|
---|
122 | size_t i;
|
---|
123 |
|
---|
124 | /* Get the function pointer, return if not available. */
|
---|
125 | if (pfnSetThreadDescription)
|
---|
126 | { }
|
---|
127 | else if (s_initialized)
|
---|
128 | return;
|
---|
129 | else
|
---|
130 | {
|
---|
131 | pfnSetThreadDescription = (PFNSETTHREADDESCRIPTION)GetProcAddress(GetModuleHandleW(L"KERNEL32.DLL"),
|
---|
132 | "SetThreadDescription");
|
---|
133 | s_pfnSetThreadDescription = pfnSetThreadDescription;
|
---|
134 | s_initialized = K_TRUE;
|
---|
135 | if (!pfnSetThreadDescription)
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Convert the name to UTF-16 and call the API. */
|
---|
140 | i = strlen(name);
|
---|
141 | kHlpAssertStmt(i < K_ELEMENTS(wszName), i = K_ELEMENTS(wszName));
|
---|
142 | wszName[i] = '\0';
|
---|
143 | while (i-- > 0)
|
---|
144 | wszName[i] = name[i];
|
---|
145 |
|
---|
146 | pfnSetThreadDescription(GetCurrentThread(), wszName);
|
---|
147 | #else
|
---|
148 | K_NOREF(name);
|
---|
149 | #endif
|
---|
150 | }
|
---|
151 |
|
---|