source: trunk/src/kash/shthread.c@ 3477

Last change on this file since 3477 was 3477, checked in by bird, 5 years ago

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1/* $Id: shthread.c 3477 2020-09-17 21:52:16Z bird $ */
2/** @file
3 *
4 * Shell Thread Management.
5 *
6 * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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
44static DWORD sh_tls = TLS_OUT_OF_INDEXES;
45#elif K_OS == K_OS_OS2
46static int sh_tls = -1;
47#else
48static int sh_tls_inited = 0;
49static 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 */
61void 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 */
97struct 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
Note: See TracBrowser for help on using the repository browser.