sync.h

来自「This is a resource based on j2me embedde」· C头文件 代码 · 共 549 行 · 第 1/2 页

H
549
字号
/* * @(#)sync.h	1.62 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */#ifndef _INCLUDED_SYNC_H#define _INCLUDED_SYNC_H#include "javavm/include/defs.h"#include "javavm/include/porting/defs.h"#include "javavm/include/porting/sync.h"#if CVM_MICROLOCK_TYPE == CVM_MICROLOCK_SWAP_SPINLOCK#define CVM_MICROLOCK_UNLOCKED  0x00#define CVM_MICROLOCK_LOCKED    0xff#endif#ifndef _ASM/* * Global micro locks */typedef enum {    CVM_CODE_MICROLOCK,#ifdef CVM_CLASSLOADING    CVM_CONSTANTPOOL_MICROLOCK,#endif#ifdef CVM_JIT    CVM_COMPILEFLAGS_MICROLOCK,#ifdef CVM_CCM_COLLECT_STATS    CVM_CCM_STATS_MICROLOCK,#endif /* CVM_CCM_COLLECT_STATS */#endif /* CVM_JIT */#if defined(CVM_JVMPI) || defined(CVM_JVMTI)    CVM_TOOLS_MICROLOCK,#endif#if defined(CVM_INSPECTOR) || defined(CVM_JVMPI)    CVM_GC_LOCKER_MICROLOCK,#endif#if defined(CVM_TRACE_ENABLED)    CVM_METHOD_TRACE_MICROLOCK,#endif    CVM_ACCESS_VOLATILE_MICROLOCK,    CVM_NUM_SYS_MICROLOCKS /* This enum should always be the last one. */} CVMSysMicroLock;voidCVMsysMicroLock(CVMExecEnv *ee, CVMSysMicroLock lock);voidCVMsysMicroUnlock(CVMExecEnv *ee, CVMSysMicroLock lock);voidCVMsysMicroLockAll(CVMExecEnv *ee);voidCVMsysMicroUnlockAll(CVMExecEnv *ee);#ifndef CVM_DEBUG#define CVMsysMicroLock(ee, l)   {(void)(ee); CVMsysMicroLockImpl((l));}#define CVMsysMicroUnlock(ee, l) {(void)(ee); CVMsysMicroUnlockImpl((l));}#endif#define CVMsysMicroLockImpl(l) \    CVMmicrolockLock(&CVMglobals.sysMicroLock[(l)])#define CVMsysMicroUnlockImpl(l) \    CVMmicrolockUnlock(&CVMglobals.sysMicroLock[(l)])/********************************************* * Various CVM_MICROLOCK_TYPE implementations. *********************************************/#if CVM_MICROLOCK_TYPE == CVM_MICROLOCK_SCHEDLOCK/* * CVM_MICROLOCK_SCHEDLOCK */struct CVMMicroLock { CVMUint8 unused; };#define CVMmicrolockInit(m)	   (CVM_TRUE)#define CVMmicrolockDestroy(m)#define CVMmicrolockLock(m)        CVMschedLock()#define CVMmicrolockUnlock(m)      CVMschedUnlock()#elif CVM_MICROLOCK_TYPE == CVM_MICROLOCK_SWAP_SPINLOCK/* * CVM_MICROLOCK_SWAP_SPINLOCK */struct CVMMicroLock{    volatile CVMAddr lockWord;};extern void CVMmicrolockLockImpl(CVMMicroLock *m);#define CVMmicrolockLock(m) {                                          \    CVMAddr                                                            \    oldWord = CVMatomicSwap(&(m)->lockWord, CVM_MICROLOCK_LOCKED);     \    if (oldWord != CVM_MICROLOCK_UNLOCKED) {                           \        CVMmicrolockLockImpl(m);                                       \    }                                                                  \}#define CVMmicrolockUnlock(m) \    CVMvolatileStore(&(m)->lockWord, CVM_MICROLOCK_UNLOCKED)#elif CVM_MICROLOCK_TYPE == CVM_MICROLOCK_DEFAULT/* * CVM_MICROLOCK_DEFAULT *//* Microlocks will be implemented using CVMMutex. */#define CVMMicroLock            CVMMutex#define CVMmicrolockInit        CVMmutexInit#define CVMmicrolockDestroy     CVMmutexDestroy#define CVMmicrolockLock        CVMmutexLock#define CVMmicrolockUnlock      CVMmutexUnlock#elif CVM_MICROLOCK_TYPE == CVM_MICROLOCK_PLATFORM_SPECIFIC/* * CVM_MICROLOCK_PLATFORM_SPECIFIC *//* Platform will supply implementation */#else#error "Invalid CVM_MICROLOCK_TYPE"#endif /* CVM_MICROLOCK_TYPE */#define CVM_ACCESS_VOLATILE_LOCK(ee) \    CVMsysMicroLock(ee, CVM_ACCESS_VOLATILE_MICROLOCK)#define CVM_ACCESS_VOLATILE_UNLOCK(ee) \    CVMsysMicroUnlock(ee, CVM_ACCESS_VOLATILE_MICROLOCK)/* * Reentrant mutexes */struct CVMReentrantMutex {    CVMExecEnv *owner;    CVMUint32 count;    CVMMutex mutex;};extern CVMBool CVMreentrantMutexInit   (CVMReentrantMutex *rm,					CVMExecEnv *owner, CVMUint32 count);extern void    CVMreentrantMutexDestroy(CVMReentrantMutex *rm);extern CVMBool CVMreentrantMutexTryLock(CVMExecEnv *ee, CVMReentrantMutex *rm);extern void    CVMreentrantMutexLock   (CVMExecEnv *ee, CVMReentrantMutex *rm);extern void    CVMreentrantMutexUnlock (CVMExecEnv *ee, CVMReentrantMutex *rm);/* * CVM_FALSE is returned to indicate that the wait was interrupted, * otherwise CVM_TRUE is returned. */extern CVMBool CVMreentrantMutexWait   (CVMExecEnv *ee, CVMReentrantMutex *rm,					CVMCondVar *c, CVMInt64 millis);/* Returns TRUE if the execution environment EE currently owns the   Reentrant Mutex RM */extern CVMBool CVMreentrantMutexIAmOwner(CVMExecEnv *ee, CVMReentrantMutex *rm);/* This macro returns an (CVMMutex *) */#define CVMreentrantMutexGetMutex(/* CVMReentrantMutex * */ m) (&((m)->mutex))#define CVMreentrantMutexOwner(/* CVMReentrantMutex * */ m) ((m)->owner)#define CVMreentrantMutexCount(/* CVMReentrantMutex * */ m) ((m)->count)#ifdef CVM_THREAD_BOOST#define CVMreentrantMutexAcquire(ee, m) {			\	CVMBool success = CVMmutexTryLock(&(m)->mutex);	\	CVMassert(success), (void)success;			\	CVMassert((m)->owner == (ee));				\	CVMassert((m)->count > 0);				\}#else#define CVMreentrantMutexSetOwner(ee, m, targetEE)	\	CVMmutexSetOwner(CVMexecEnv2threadID((ee)),	\	    &(m)->mutex, CVMexecEnv2threadID((targetEE)))#endif#define CVMreentrantMutexLockUnsafe(ee, rm)	\{						\    CVMassert(CVMD_isgcUnsafe(ee));		\    if ((rm)->owner == ee) {			\        ++(rm)->count;				\    } else {					\	CVMD_gcSafeExec((ee), {			\	    CVMmutexLock(&(rm)->mutex);		\	});					\        (rm)->owner = ee;			\        (rm)->count = 1;			\    }						\}/* * Deadlock-detecting VM mutexes */struct CVMSysMutex {    const char *name;    CVMUint8 rank;    CVMReentrantMutex rmutex;#ifdef CVM_DEBUG    CVMSysMutex *nextOwned;#endif};#define CVM_SYSMUTEX_MAX_RANK  255extern CVMBool    CVMsysMutexInit   (CVMSysMutex* m,				     const char *name,	/* client managed */				     CVMUint8 rank);	/* deadlock detection */extern void       CVMsysMutexDestroy(CVMSysMutex* m);extern CVMBool    CVMsysMutexTryLock(CVMExecEnv *ee, CVMSysMutex* m);extern void       CVMsysMutexLock   (CVMExecEnv *ee, CVMSysMutex* m);extern void       CVMsysMutexUnlock (CVMExecEnv *ee, CVMSysMutex* m);/* * CVM_FALSE is returned to indicate that the wait was interrupted, * otherwise CVM_TRUE is returned. */extern CVMBool	  CVMsysMutexWait   (CVMExecEnv *ee, CVMSysMutex* m,				     CVMCondVar *c, CVMInt64 millis);/* This macro returns a (CVMReentrantMutex *) */#define CVMsysMutexGetReentrantMutex(/* CVMSysMutex * */ m) \	(&((m)->rmutex))#define CVMsysMutexOwner(m) \	CVMreentrantMutexOwner(&(m)->rmutex)#define CVMsysMutexCount(m) \	CVMreentrantMutexCount(&(m)->rmutex)#define CVMsysMutexIAmOwner(ee, m) \	(CVMsysMutexOwner((m)) == (ee))/* * "raw" object-less monitor */typedef enum {    CVM_WAIT_OK,    CVM_WAIT_INTERRUPTED,    CVM_WAIT_NOT_OWNER} CVMWaitStatus;struct CVMSysMonitor {    CVMReentrantMutex rmutex;    CVMCondVar condvar;};extern CVMBoolCVMsysMonitorInit(CVMSysMonitor* m, CVMExecEnv *owner, CVMUint32 count);extern voidCVMsysMonitorDestroy(CVMSysMonitor* m);extern void

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?