📄 hxmutexlock.h
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: $Id: hxmutexlock.h,v 1.3 2003/08/03 22:00:44 dcollins Exp $
*
* Portions Copyright (c) 1995-2003 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file,
* are subject to the current version of the RealNetworks Public
* Source License (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the current version of the RealNetworks Community
* Source License (the "RCSL") available at
* http://www.helixcommunity.org/content/rcsl, in which case the RCSL
* will apply. You may also obtain the license terms directly from
* RealNetworks. You may not use this file except in compliance with
* the RPSL or, if you have a valid RCSL with RealNetworks applicable
* to this file, the RCSL. Please see the applicable RPSL or RCSL for
* the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the
* portions it created.
*
* This file, and the files included with this file, is distributed
* and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
* ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
/*
* Non-reentrant mutexes for various server platforms.
*/
/***********************************************************************
* THIS CODE IS HIGHLY CRITICAL TO THE SERVER'S STABILITY!!!
* DO NOT MAKE CHANGES TO THE ATOMIC OPERATORS OR TO THE
* MUTEX CODE WITHOUT A SERVER TEAM CODE-REVIEW! (dev@helix-server)
*/
/***********************************************************************
*
* Platforms supported:
* Windows / x86
* Solaris / sparc
* IRIX / MIPS
* AIX / PowerPC
* Tru64(OSF) / Alpha
* HP-UX / PA-RISC
* HP-UX / Itanium
* Misc. Unix / x86 (Linux, FreeBSD...)
*
* XXXDC: So far, in nearly all cases, native mutexes provided by
* OS vendors have been buggy and/or very slow. Well-written
* assembly-language mutexes are strongly preferred.
* Be especially careful about byte-alignment issues.
*
*/
/***********************************************************************
*
* Externally-available routines:
*
* inline void HXMutexLock(HX_MUTEX pLock, BOOL bWait = FALSE)
* Locks the mutex, blocking until available; A delay
* between attempts to grab the lock is used if bWait is TRUE.
* Note that the default is a tight no-delay spinlock, so
* always set bWait to TRUE unless you know you should do otherwise.
*
* inline BOOL HXMutexTryLock(HX_MUTEX pLock)
* Locks the mutex if available, returning TRUE if the lock succeeded,
* and returning FALSE otherwise.
*
* inline void HXMutexUnlock(HX_MUTEX pLock)
* Unlocks the mutex.
*
* inline HX_MUTEX HXMutexCreate()
* Creates the mutex.
*
* inline void HXMutexInit(HX_MUTEX pLock)
* Initializes the mutex.
*
* inline void HXMutexDestroy(HX_MUTEX pLock)
* Destroys the mutex.
*
*
* Additionally, this file defines IHXMutex, a COM wrapper for the above.
*
*
* NOTES:
* The older HXxxxMutex variations of the above are depreciated
* and should be converted over the the above variations for consistancy.
* HXMutexUnInit() is also depreciated, it never did anything and
* the purpose of it has been lost. It appears useless.
*
*/
/***********************************************************************
*
* On most platforms the above are implemented using the
* following internal routines/#defines (do NOT call these directly!):
*
* int _HXMutexSetBit(HX_MUTEX pLock)
* Lock the lock and return the old value of the lock
* (0 meaning it was unlocked and is now locked, non-zero otherwise)
*
* _HXMutexClearBit(pLock)
* Clears the mutex flag.
*
* BOOL _HXMutexYieldCPUIfNeeded()
* On some platforms we have to explicitly yield the CPU.
* This is not needed on most platforms.
*
* void _HXMutexInitBit(HX_MUTEX pLock)
* Initializes the mutex, typically just calls _HXMutexClearBit().
*
* void _HXMutexDestroyBit(HX_MUTEX pLock)
* Destroys the mutex, typically does nothing.
*/
#ifndef _HXMUTEXLOCK_H_
#define _HXMUTEXLOCK_H_
/***********************************************************************
* Common defines/includes
*/
//system
#include <stdio.h>
//include
#include "hxtypes.h"
#include "hxengin.h"
//hxmisc
#include "microsleep.h"
/***********************************************************************
* OSF(Tru64) / Alpha defines/includes
*/
#if defined _OSF1
#include <alpha/builtins.h>
typedef struct
{
char p;
unsigned long for_alignment;
} _HXMutexLockType;
#define HX_MUTEX volatile _HXMutexLockType* volatile
#define HX_MUTEX_TYPE volatile _HXMutexLockType
#define HX_MUTEX_BASE_TYPE _HXMutexLockType
/***********************************************************************
* HP-UX / Itanium defines/includes
*/
#elif defined _HPUX && defined _IA64
#define HX_MUTEX volatile int* volatile
#define HX_MUTEX_TYPE volatile int
#define HX_MUTEX_BASE_TYPE int
/***********************************************************************
* HP-UX / PA-RISC defines/includes
*/
#elif defined _HPUX
extern "C" {
int load_and_clear(volatile char* volatile p);
};
typedef struct
{
char p;
long long for_alignment;
} _HXMutexLockType;
#define HX_MUTEX volatile _HXMutexLockType* volatile
#define HX_MUTEX_TYPE volatile _HXMutexLockType
#define HX_MUTEX_BASE_TYPE _HXMutexLockType
// for 64-bit or 32-bit
#define alignedaddr(x) (volatile int *)((PTR_INT)(x) + 15 & ~0xf)
#define release_spinlock(LockArea) if (1) { \
(*alignedaddr(LockArea) = 1); }
/***********************************************************************
* AIX / PowerPC defines/includes
*/
#elif defined _AIX
#include <errno.h>
// NATIVE MUTEX
#include <sys/mman.h>
#define HX_MUTEX msemaphore*
#define HX_MUTEX_TYPE msemaphore
#define HX_MUTEX_BASE_TYPE msemaphore
// ASSEMBLY MUTEX
//#define HX_MUTEX volatile int* volatile
//#define HX_MUTEX_TYPE volatile int
//#define HX_MUTEX_BASE_TYPE int
/***********************************************************************
* IRIX / MIPS defines/includes
*/
#elif defined _IRIX
#include <abi_mutex.h>
#define HX_MUTEX abilock_t*
#define HX_MUTEX_TYPE abilock_t
#define HX_MUTEX_BASE_TYPE abilock_t
/***********************************************************************
* Windows x86 defines/includes
*/
#elif defined _WIN32
#include <winbase.h>
#define HX_MUTEX volatile int* volatile
#define HX_MUTEX_TYPE volatile int
#define HX_MUTEX_BASE_TYPE int
/***********************************************************************
* generic defines/includes
*/
#else
#define HX_MUTEX volatile int* volatile
#define HX_MUTEX_TYPE volatile int
#define HX_MUTEX_BASE_TYPE int
#endif
/***********************************************************************
* misc globals
*
* TODO: analyze more closely what the optimal delay is.
*/
#ifndef HX_MUTEX_DELAY
#define HX_MUTEX_DELAY 10000
#endif
// Backoff Configs
// XXXDC - Needs to be fine-tuned on a platform-specific basis!!!
// XXXDC - highly experimental!
#ifdef XXXDC_USE_BACKOFF
#define BACKOFF_THRESHOLD 5 // # failed locks before delay starts
#define BACKOFF_START 100 // usec to initially delay
#define BACKOFF_FACTOR 10 // muliply each delay by this
#define BACKOFF_MAX 15000 // maximum usec to delay
#endif
/***********************************************************************/
/***********************************************************************
* Windows x86
*
* Implementation Notes:
* Uses assembly mutex
*/
#if defined _WIN32
#define _HXMutexYieldCPUIfNeeded() /* not needed */
#define _HXMutexInitBit(pLock) _HXMutexClearBit(pLock)
#define _HXMutexDestroyBit(pLock) /* not needed */
#define _HXMutexClearBit(pLock) (*(pLock) = 0)
inline static int
_HXMutexSetBit(HX_MUTEX pLock)
{
register int nOldBit;
_asm
{
mov eax, 1
mov ebx, pLock
xchg dword ptr [ebx], eax
mov nOldBit, eax
}
return nOldBit;
}
/***********************************************************************
* Solaris / sparc / gcc
*
* Implementation Notes:
* Uses assembly mutex
* GCC inline-assembly syntax
*/
#elif defined _SOLARIS && defined __GNUC__
#define _HXMutexYieldCPUIfNeeded() /* not needed */
#define _HXMutexInitBit(pLock) _HXMutexClearBit(pLock)
#define _HXMutexDestroyBit(pLock) /* not needed */
#define _HXMutexClearBit(pLock) (*(pLock) = 0)
inline static int
_HXMutexSetBit(HX_MUTEX pLock)
{
volatile int nOldBit;
__asm__ __volatile__("swap [%0], %1"
: "=r" (pLock), "=&r" (nOldBit)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -