1 | /* $Id: shthread.c 2498 2011-07-22 12:05: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 | #include <assert.h>
|
---|
30 |
|
---|
31 | #if K_OS == K_OS_WINDOWS
|
---|
32 | # include <Windows.h>
|
---|
33 | #elif K_OS == K_OS_OS2
|
---|
34 | # include <InnoTekLIBC/FastInfoBlocks.h>
|
---|
35 | # include <InnoTekLIBC/thread.h>
|
---|
36 | #else
|
---|
37 | # include <pthread.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 | #if K_OS == K_OS_WINDOWS
|
---|
45 | static DWORD sh_tls = TLS_OUT_OF_INDEXES;
|
---|
46 | #elif K_OS == K_OS_OS2
|
---|
47 | static int sh_tls = -1;
|
---|
48 | #else
|
---|
49 | static int sh_tls_inited = 0;
|
---|
50 | static pthread_key_t sh_tls;
|
---|
51 | #endif
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Stores the shell instance pointer in a TLS entry.
|
---|
56 | *
|
---|
57 | * This will allocate the TLS entry on the first call. We assume
|
---|
58 | * there will no be races at that time.
|
---|
59 | *
|
---|
60 | * @param psh The shell instance.
|
---|
61 | */
|
---|
62 | void shthread_set_shell(struct shinstance *psh)
|
---|
63 | {
|
---|
64 | #if K_OS == K_OS_WINDOWS
|
---|
65 | if (sh_tls == TLS_OUT_OF_INDEXES)
|
---|
66 | {
|
---|
67 | sh_tls = TlsAlloc();
|
---|
68 | assert(sh_tls != TLS_OUT_OF_INDEXES);
|
---|
69 | }
|
---|
70 | if (!TlsSetValue(sh_tls, psh))
|
---|
71 | assert(0);
|
---|
72 |
|
---|
73 | #elif K_OS == K_OS_OS2
|
---|
74 | if (sh_tls == -1)
|
---|
75 | {
|
---|
76 | sh_tls = __libc_TLSAlloc();
|
---|
77 | assert(sh_tls != -1);
|
---|
78 | }
|
---|
79 | if (__libc_TLSSet(sh_tls, psh) == -1)
|
---|
80 | assert(0);
|
---|
81 | #else
|
---|
82 | if (!sh_tls_inited)
|
---|
83 | {
|
---|
84 | if (pthread_key_create(&sh_tls, NULL) != 0)
|
---|
85 | assert(0);
|
---|
86 | sh_tls_inited = 1;
|
---|
87 | }
|
---|
88 | if (pthread_setspecific(sh_tls, psh) != 0)
|
---|
89 | assert(0);
|
---|
90 | #endif
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Get the shell instance pointer from TLS.
|
---|
95 | *
|
---|
96 | * @returns The shell instance.
|
---|
97 | */
|
---|
98 | struct shinstance *shthread_get_shell(void)
|
---|
99 | {
|
---|
100 | shinstance *psh;
|
---|
101 | #if K_OS == K_OS_WINDOWS
|
---|
102 | psh = (shinstance *)TlsGetValue(sh_tls);
|
---|
103 | #elif K_OS == K_OS_OS2
|
---|
104 | psh = (shinstance *)__libc_TLSGet(sh_tls);
|
---|
105 | #else
|
---|
106 | psh = (shinstance *)pthread_getspecific(sh_tls);
|
---|
107 | #endif
|
---|
108 | return psh;
|
---|
109 | }
|
---|
110 |
|
---|