source: trunk/src/kash/shthread.h

Last change on this file was 3515, checked in by bird, 4 years ago

kash: shthread.h: Correction to previous change, K_ARCH_X86_32 not K_ARCH_X86.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 2.6 KB
RevLine 
[1233]1/* $Id: shthread.h 3515 2021-12-16 12:54:03Z bird $ */
[1218]2/** @file
3 *
4 * Shell thread methods.
5 *
[2413]6 * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
[1218]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
[3446]32typedef union shmtx
[1218]33{
34 char b[64];
[3439]35 KU64 au64[64/sizeof(KU64)];
36 void *aptrs[64/sizeof(void *)];
[1218]37} shmtx;
38
[3446]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
[2289]48typedef struct shmtxtmp { int i; } shmtxtmp;
49
[1218]50typedef uintptr_t shtid;
51
52void shthread_set_shell(struct shinstance *);
53struct shinstance *shthread_get_shell(void);
[3502]54void shthread_set_name(const char *name);
[1218]55
[2289]56int shmtx_init(shmtx *pmtx);
57void shmtx_delete(shmtx *pmtx);
58void shmtx_enter(shmtx *pmtx, shmtxtmp *ptmp);
59void shmtx_leave(shmtx *pmtx, shmtxtmp *ptmp);
60
[3448]61
62K_INLINE unsigned sh_atomic_inc(KU32 volatile *valuep)
63{
64#ifdef _MSC_VER
65 return _InterlockedIncrement((long *)valuep);
[3515]66#elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
[3502]67 unsigned uRet;
68 __asm__ __volatile__("lock; xaddl %1, %0" : "=m" (*valuep), "=r" (uRet) : "m" (*valuep), "1" (1) : "memory", "cc");
69 return uRet + 1;
[3448]70#else
[3457]71 return __sync_add_and_fetch(valuep, 1);
[1218]72#endif
[3448]73}
[1218]74
[3448]75K_INLINE unsigned sh_atomic_dec(unsigned volatile *valuep)
76{
77#ifdef _MSC_VER
78 return _InterlockedDecrement((long *)valuep);
[3515]79#elif defined(__GNUC__) && (K_ARCH == K_ARCH_AMD64 || K_ARCH == K_ARCH_X86_32)
[3502]80 unsigned uRet;
81 __asm__ __volatile__("lock; xaddl %1, %0" : "=m" (*valuep), "=r" (uRet) : "m" (*valuep), "1" (-1) : "memory", "cc");
82 return uRet - 1;
[3448]83#else
[3457]84 return __sync_sub_and_fetch(valuep, 1);
[3448]85#endif
86}
87
88#endif
89
Note: See TracBrowser for help on using the repository browser.