1 | /* $Id: shthread.h 3515 2021-12-16 12:54:03Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Shell thread methods.
|
---|
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 | #ifndef ___shthread_h___
|
---|
28 | #define ___shthread_h___
|
---|
29 |
|
---|
30 | #include "shtypes.h"
|
---|
31 |
|
---|
32 | typedef union shmtx
|
---|
33 | {
|
---|
34 | char b[64];
|
---|
35 | KU64 au64[64/sizeof(KU64)];
|
---|
36 | void *aptrs[64/sizeof(void *)];
|
---|
37 | } shmtx;
|
---|
38 |
|
---|
39 | /** Magic mutex value (final u64).
|
---|
40 | * This is used to detect whether the mutex has been initialized or not,
|
---|
41 | * allowing shmtx_delete to be called more than once without doing harm.
|
---|
42 | * @internal */
|
---|
43 | #define SHMTX_MAGIC KU64_C(0x8888000019641018) /**< Charles Stross */
|
---|
44 | /** Index into shmtx::au64 of the SHMTX_MAGIC value.
|
---|
45 | * @internal */
|
---|
46 | #define SHMTX_MAGIC_IDX (sizeof(shmtx) / sizeof(KU64) - 1)
|
---|
47 |
|
---|
48 | typedef struct shmtxtmp { int i; } shmtxtmp;
|
---|
49 |
|
---|
50 | typedef uintptr_t shtid;
|
---|
51 |
|
---|
52 | void shthread_set_shell(struct shinstance *);
|
---|
53 | struct shinstance *shthread_get_shell(void);
|
---|
54 | void shthread_set_name(const char *name);
|
---|
55 |
|
---|
56 | int shmtx_init(shmtx *pmtx);
|
---|
57 | void shmtx_delete(shmtx *pmtx);
|
---|
58 | void shmtx_enter(shmtx *pmtx, shmtxtmp *ptmp);
|
---|
59 | void shmtx_leave(shmtx *pmtx, shmtxtmp *ptmp);
|
---|
60 |
|
---|
61 |
|
---|
62 | K_INLINE unsigned sh_atomic_inc(KU32 volatile *valuep)
|
---|
63 | {
|
---|
64 | #ifdef _MSC_VER
|
---|
65 | return _InterlockedIncrement((long *)valuep);
|
---|
66 | #elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
|
---|
67 | unsigned uRet;
|
---|
68 | __asm__ __volatile__("lock; xaddl %1, %0" : "=m" (*valuep), "=r" (uRet) : "m" (*valuep), "1" (1) : "memory", "cc");
|
---|
69 | return uRet + 1;
|
---|
70 | #else
|
---|
71 | return __sync_add_and_fetch(valuep, 1);
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 |
|
---|
75 | K_INLINE unsigned sh_atomic_dec(unsigned volatile *valuep)
|
---|
76 | {
|
---|
77 | #ifdef _MSC_VER
|
---|
78 | return _InterlockedDecrement((long *)valuep);
|
---|
79 | #elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
|
---|
80 | unsigned uRet;
|
---|
81 | __asm__ __volatile__("lock; xaddl %1, %0" : "=m" (*valuep), "=r" (uRet) : "m" (*valuep), "1" (-1) : "memory", "cc");
|
---|
82 | return uRet - 1;
|
---|
83 | #else
|
---|
84 | return __sync_sub_and_fetch(valuep, 1);
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | #endif
|
---|
89 |
|
---|