Design the mutex of java

    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 with these special cases.
    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 excluding variable is used in the environment of
 * multiplying threads. It provides recurisive locking operation and the mechanism
 * of priority reversed is forbidden. Of course, it can be implemented by single CPU
 * or multiplying CPUs's API.
 *
 * Author           : Wang yanqing
 * Email            : [email protected]
 * Version          : 0.001
 * Creating date    : 03/14/2006
 * Modifying date    : 03/14/2006
 * Copyright        : free usage and no modification
 * History          :
 * [03/14/2006]
 *    Define IMutex 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 repeatly. 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 exiting critical setion the thread of lower
     * priority release mutex, however, threads of ile priority  will maybe acquire mutex
     * even if threads of high priority are requesting 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 used 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 used 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(When 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 it 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.

你可能感兴趣的:(java,thread,function,System,interface,locking)