Design The Mutex of Java

xiaoxiao2021-03-13  232

Java Has Provided

synchronized key word to be used at the aspect of critical section. However it is not convenient for advanced users who want to be agile to utilize mutex. such as trying to lock it and no waiting operation. So the mutex should be designed to be satified .

I have implemented these functions basing one the following the interface of mutex. It is only my ideas to make a description of java mutex. I hope that java funs can discuss it deeply, and improve and make better this interface. Thanks!

/ **

* The interface of multiplying Excludding Variable is buy in the environment of

* Multiplying Threads. It provides recrurisive locking operation and the mechanism

* of priority reverse is forbidden. of course, IT can be used by Single CPU

* Or Multiplying CPUS'S API.

*

* Author: wang yanqing

* Email: hello.wyq@gmail.com

* Version: 0.001

* Creating Date: 03/14/2006

* Modifying Date: 03/14/2006

* Copyright: Free Usage and No Modification

* History:

* [03/14/2006]

* Define immutex interface

* /

Package Osa.mutex;

Import Osa.common.Waiter;

Public Interface Imutex

{

/ **

* In Normal Case, RecuriSive Mutex Will Be Created, And Same Owner of Mutex

* Can Be Entered Critical Setion REATLY. IF this property is set, Mutex Should

* Disable RecuriSive Property, and only permit onCE.

* /

Public final static int ignore_recurision = 0x01;

/ **

* When the Thread of Lower Priority Occupies Mutex, The High Ones Will Be Blocked

* And Enter Sleeping Queue Of Object. After Exitation Critical Setion The Thread of Lower

* Priority Release Mutex, HoWever, Threads of Ile Priority Will Maybe Acquire Mutex

* Even if threads of high prior precuesting mutex at the same time. It can not

* Be permitted in real-time or others special environments. if The property is not set,

* Mutex Will Consider Priority of Thread, And Compete Mutex According To Priority of

* of threads.

* /

Public final static int ignore_priority = 0x02;

/ **

* It is buy by locking function to indicate WHether Threads Will Wait Some MilliseConds

* to Obtain Mutex. 'NO_WAIT' Means That Threads Will Immediately Return Error Codes

* When Mutex Has Been Occupied by Others

* /

Public final static long no_wait = waiter.no_wait;

/ **

* It is buy by locking function to indicate WHether Threads Will Wait Some MilliseConds

* to Obtain Mutex. 'Wait_Forever' Means That Threads Will Wait for Mutex Until

* Mutex Has Been Free by Others.

* /

Public final static long wait_forever = waiter.wait_forever;

/ **

* Acquire Mutex within The Range of Several MilliseConds.

* If 'ignore_recurision' Has Been Set, Error Information Should Be Printed To

* Remind That User Has Bugs To Utilize Mutex and It Makes dead-lock.

*

* @Param Timeout --- MilliseConds of Timeout to Wait To Obtain Mutex, The Value Must> = 0

* No_wait, wait_forever or idiographic value can be used.

* @Return Result.einv --- Error Parameter (when Timeout <0)

* Result.etimeout --- Timeout (hen timeout Equals 'Wait_Forever', IT

* SHOULD NEVER BE RETURNED)

* Result.esys --- System Has Errors, Such as Failing to Acquire Current Thread

* Result.ok --- Success

* @since 0.001

* /

Public int Lock (Long Timeout);

/ **

* Release Mutex. * If 'Ignore_Priority' Has Not Been Set, Mutex Must Disable To Occur Priority-Reversion

* Phenomenon WHEN Threads of Lower Priority Realse Mutex.

* WARNNING INFORMATION SHOULD BE Shown WHEN LOCKING AND UNLOCKING THREADS Are

* Different Threads. I Believe That Should Be Same in Normal Case, So Warnning Information

* Maybe Is Helpful for debugger :-).

*

* @Param null

* @Return Result.ESTAT --- Error State (When Locking and Unlocking Operations Are Unmatched)

* Result.esys --- System Has ErrorS

* Result.ok --- Success

* @since 0.001

* /

Public int unlock ();

} OSA is my main package to include all relative classes of OS, and it is an abbrevication of "Operation System Abstraction". There are mutex, queue, condition variable and so on. Different part occupies different directories. I think that it is available To manage the update of osa later.

转载请注明原文地址:https://www.9cbs.com/read-128983.html

New Post(0)