Ignore:
Timestamp:
Nov 22, 2004, 2:46:26 AM (21 years ago)
Author:
bird
Message:

Extended fmutex interface.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/include/sys/fmutex.h

    • Property cvs2svn:cvs-rev changed from 1.3 to 1.4
    r1660 r1661  
    66
    77#ifndef _SYS_FMUTEX_H
    8 #define _SYS_FMUTEX_H
     8    #define _SYS_FMUTEX_H
    99
    10 #include <stdlib.h> /* need abort */
     10    #include <sys/cdefs.h>
     11    #include <stdlib.h> /* need abort */
     12    #include <InnoTekLIBC/FastInfoBlocks.h>
    1113
    12 #if defined (__cplusplus)
    13 extern "C" {
    14 #endif
     14__BEGIN_DECLS
    1515
    1616/* Constants for _fmutex.fs.  See _fmutex_available() for ordering. */
    1717
    18 #define _FMS_UNINIT       0
    19 #define _FMS_AVAILABLE    1
    20 #define _FMS_OWNED_SIMPLE 2
    21 #define _FMS_OWNED_HARD   3
     18    #define _FMS_UNINIT       0
     19    #define _FMS_AVAILABLE    1
     20    #define _FMS_OWNED_SIMPLE 2
     21    #define _FMS_OWNED_HARD   3
    2222
    2323/* Constants for _fmutex_create() */
    2424
    25 #define _FMC_SHARED     0x01
     25    #define _FMC_SHARED         0x01
     26    #define _FMC_MUST_COMPLETE  0x02
     27    #define _FMC_DUMMY          0x04    /* internal */
    2628
    2729/* Constants for _fmutex_request() */
    2830
    29 #define _FMR_IGNINT     0x01
    30 #define _FMR_NOWAIT     0x02
     31    #define _FMR_IGNINT     0x01
     32    #define _FMR_NOWAIT     0x02
    3133
    3234/* We cannot use __attribute__ ((__packed__)) because G++ does not
    3335   support this. */
    3436
    35 #pragma pack(1)
     37    #pragma pack(1)
    3638typedef struct
    3739{
    3840    /** Handle to event semaphore. */
    39     unsigned long hev;
     41    unsigned long           hev;
    4042    /** Semaphore status. */
    41     __volatile__ signed char fs;
     43    volatile signed char    fs;
    4244    /** Semaphore create flags. (_FMC_SHARED) */
    43     unsigned char flags;
    44     /** padding the struct to 8 bytes. */
    45     unsigned char padding[2];
     45    unsigned char           flags;
     46    /** padding the struct to 16 bytes. */
     47    unsigned char           padding[2];
     48    /** The (pid << 16) | tid of the owner. */
     49    volatile unsigned       Owner;
     50    /** Descriptive name of the mutex. */
     51    const char             *pszDesc;
    4652} _fmutex;
    47 #pragma pack()
     53    #pragma pack()
     54
    4855
    4956
    5057unsigned __fmutex_request_internal (_fmutex *, unsigned, signed char);
    51 unsigned __fmutex_create_internal (_fmutex *, unsigned);
     58unsigned __fmutex_request_internal_must_complete (_fmutex *, unsigned);
    5259unsigned __fmutex_release_internal (_fmutex *);
     60unsigned __fmutex_release_internal_must_complete (_fmutex *);
    5361
    5462
    5563static __inline__ unsigned _fmutex_request (_fmutex *sem, unsigned flags)
    5664{
    57   signed char fs;
    58 
    59   fs = __cxchg (&sem->fs, _FMS_OWNED_SIMPLE);
    60   if (fs == _FMS_AVAILABLE)
    61     return 0;
    62   else
    63     return __fmutex_request_internal (sem, flags, fs);
     65    if (!(sem->flags & _FMC_MUST_COMPLETE))
     66    {
     67        signed char fs = __cxchg (&sem->fs, _FMS_OWNED_SIMPLE);
     68        if (fs == _FMS_AVAILABLE)
     69        {
     70            __atomic_xchg(&sem->Owner, fibGetTidPid());
     71            return 0;
     72        }
     73        else
     74            return __fmutex_request_internal (sem, flags, fs);
     75    }
     76    else
     77        return __fmutex_request_internal_must_complete (sem, flags);
    6478}
    6579
     
    6781static __inline__ unsigned _fmutex_release (_fmutex *sem)
    6882{
    69   signed char fs;
    70 
    71   fs = __cxchg (&sem->fs, _FMS_AVAILABLE);
    72   if (fs == _FMS_OWNED_HARD)
    73     return __fmutex_release_internal (sem);
    74   else
    75     return 0;
     83    if (!(sem->flags & _FMC_MUST_COMPLETE))
     84    {
     85        signed char fs;
     86        __atomic_xchg(&sem->Owner, 0);
     87        fs = __cxchg(&sem->fs, _FMS_AVAILABLE);
     88        if (fs != _FMS_OWNED_HARD)
     89            return 0;
     90        else
     91            return __fmutex_release_internal (sem);
     92    }
     93    else
     94        return __fmutex_release_internal_must_complete (sem);
    7695}
    77 
    7896
    7997static __inline__ int _fmutex_available (_fmutex *sem)
    8098{
    81   return sem->fs <= _FMS_AVAILABLE;
     99    return sem->fs <= _FMS_AVAILABLE;
    82100}
    83101
    84102
    85103unsigned _fmutex_create (_fmutex *, unsigned);
     104unsigned _fmutex_create2 (_fmutex *sem, unsigned, const char *);
    86105unsigned _fmutex_open (_fmutex *);
    87106unsigned _fmutex_close (_fmutex *);
    88107void _fmutex_dummy (_fmutex *);
    89108
    90 void _fmutex_checked_close (_fmutex *);
    91 void _fmutex_checked_create (_fmutex *, unsigned);
    92 void _fmutex_checked_open (_fmutex *);
     109void _fmutex_checked_close(_fmutex *);
     110void _fmutex_checked_create(_fmutex *, unsigned);
     111void _fmutex_checked_create2(_fmutex *, unsigned, const char *);
     112void _fmutex_checked_open(_fmutex *);
     113void _fmutex_abort(_fmutex *, const char *);
    93114
    94115static __inline__ void _fmutex_checked_release (_fmutex * sem)
    95116{
    96   if (_fmutex_release (sem) != 0)
    97     abort ();
     117    if (sem->Owner != fibGetTidPid())
     118        _fmutex_abort(sem, "release not owner");
     119    if (_fmutex_release (sem) != 0)
     120        _fmutex_abort(sem, "release");
    98121}
    99122
    100123static __inline__ void _fmutex_checked_request (_fmutex * sem, unsigned flags)
    101124{
    102   if (_fmutex_request (sem, flags) != 0)
    103     abort ();
     125    if (_fmutex_request (sem, flags) != 0)
     126        _fmutex_abort(sem, "request");
    104127}
    105128
    106 #if defined (__cplusplus)
    107 }
    108 #endif
     129__END_DECLS
    109130
    110131#endif /* not _SYS_FMUTEX_H */
     132
Note: See TracChangeset for help on using the changeset viewer.