Changeset 2298


Ignore:
Timestamp:
Aug 22, 2005, 2:14:50 AM (20 years ago)
Author:
bird
Message:

Tried to workaround the race conditions in libc_Back_safesemEvSleep/Wakeup.
These might affect SysV sems. (2nd try)

Location:
trunk/src/emx
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/ChangeLog.LIBC

    • Property cvs2svn:cvs-rev changed from 1.122 to 1.123
    r2297 r2298  
    66    - libc:
    77        o Tried to workaround the race conditions in __libc_Back_safesemEvSleep/Wakeup.
    8           These might affect SysV sems and shm.
     8          These might affect SysV sems.
    99        o Corrected the declarations of _DLL_InitTerm.
    1010
  • trunk/src/emx/include/InnoTekLIBC/backend.h

    • Property cvs2svn:cvs-rev changed from 1.30 to 1.31
    r2297 r2298  
    967967 * @{ */
    968968
     969/**
     970 * Safe Mutex Semaphore structure.
     971 *
     972 * For shared semaphores this structure must be in shared memory so all users
     973 * actually use the very same structure.
     974 */
     975typedef struct __LIBC_SAFESEMMTX
     976{
     977#ifdef __OS2__
     978    /** Mutex handle. */
     979    unsigned long hmtx;
     980#endif
     981} __LIBC_SAFESEMMTX;
     982/** Pointer to a SAFESEM Mutex structure. */
     983typedef __LIBC_SAFESEMMTX *__LIBC_PSAFESEMMTX;
     984
    969985/**
    970986 * Creates a safe mutex sem.
     
    972988 * @returns 0 on success.
    973989 * @returns Negative error code (errno.h) on failure.
    974  * @param   phmtx       Where to store the semaphore handle.
     990 * @param   pmtx        Pointer to the semaphore structure to initialize.
    975991 * @param   fShared     Set if the semaphore should be sharable between processes.
    976992 */
    977 int __libc_Back_safesemMtxCreate(uintptr_t *phmtx, int fShared);
     993int __libc_Back_safesemMtxCreate(__LIBC_PSAFESEMMTX pmtx, int fShared);
    978994
    979995/**
     
    982998 * @returns 0 on success.
    983999 * @returns Negative error code (errno.h) on failure.
    984  * @param   hmtx        The semaphore handle.
    985  */
    986 int __libc_Back_safesemMtxOpen(uintptr_t hmtx);
     1000 * @param   pmtx        Pointer to the semaphore structure.
     1001 */
     1002int __libc_Back_safesemMtxOpen(__LIBC_PSAFESEMMTX pmtx);
    9871003
    9881004/**
     
    9911007 * @returns 0 on success.
    9921008 * @returns Negative error code (errno.h) on failure.
    993  * @param   hmtx        The semaphore handle.
    994  */
    995 int __libc_Back_safesemMtxClose(uintptr_t hmtx);
     1009 * @param   pmtx        Pointer to the semaphore structure.
     1010 */
     1011int __libc_Back_safesemMtxClose(__LIBC_PSAFESEMMTX pmtx);
    9961012
    9971013/**
     
    10001016 * @returns 0 on success.
    10011017 * @returns Negative errno on failure.
    1002  * @param   hmtx    Handle to the mutex.
    1003  */
    1004 int __libc_Back_safesemMtxLock(uintptr_t hmtx);
     1018 * @param   pmtx        Pointer to the semaphore structure.
     1019 */
     1020int __libc_Back_safesemMtxLock(__LIBC_PSAFESEMMTX pmtx);
    10051021
    10061022/**
     
    10091025 * @returns 0 on success.
    10101026 * @returns Negative errno on failure.
    1011  * @param   hmtx    Handle to the mutex.
    1012  */
    1013 int __libc_Back_safesemMtxUnlock(uintptr_t hmtx);
     1027 * @param   pmtx        Pointer to the semaphore structure.
     1028 */
     1029int __libc_Back_safesemMtxUnlock(__LIBC_PSAFESEMMTX pmtx);
     1030
     1031
     1032/**
     1033 * Safe Event Semaphore structure.
     1034 *
     1035 * For shared semaphores this structure must be in shared memory so all users
     1036 * actually use the very same structure.
     1037 *
     1038 * @remark  The event semaphore business is difficult because the lack of
     1039 *          atomic mutex release + event wait apis in OS/2. We have to
     1040 *          jump around the place to get this working nearly safly...
     1041 */
     1042typedef struct __LIBC_SAFESEMEV
     1043{
     1044#ifdef __OS2__
     1045    /** The event semaphore. */
     1046    unsigned long       hev;
     1047#endif
     1048    /** Number of threads which are supposed to be blocking on the above event semaphore. */
     1049    uint32_t volatile   cWaiters;
     1050    /** The mutex semaphore used to protect the event semaphore. */
     1051    __LIBC_PSAFESEMMTX  pmtx;
     1052    /** Set if the semaphore is shared.
     1053     * If shared this structure is allocated from SPM, else it's from the heap. */
     1054    unsigned            fShared;
     1055} __LIBC_SAFESEMEV;
     1056/** Pointer to a SAFESEM Event structure. */
     1057typedef __LIBC_SAFESEMEV *__LIBC_PSAFESEMEV;
    10141058
    10151059/**
     
    10181062 * @returns 0 on success.
    10191063 * @returns Negative error code (errno.h) on failure.
    1020  * @param   phev        Where to store the semaphore handle.
     1064 * @param   pev         Pointer to the semaphore structure to initialize.
     1065 * @param   pmtx        Pointer to the mutex semaphore which protects the event semaphore.
    10211066 * @param   fShared     Set if the semaphore should be sharable between processes.
    10221067 */
    1023 int __libc_Back_safesemEvCreate(uintptr_t *phev, int fShared);
     1068int __libc_Back_safesemEvCreate(__LIBC_PSAFESEMEV pev, __LIBC_PSAFESEMMTX pmtx, int fShared);
    10241069
    10251070/**
    10261071 * Opens a shared safe event sem.
    10271072 *
    1028  * @returns 0 on success.
    1029  * @returns Negative error code (errno.h) on failure.
    1030  * @param   hev         The semaphore handle.
    1031  */
    1032 int __libc_Back_safesemEvOpen(uintptr_t hev);
     1073 * The caller is responsible for opening the associated mutex
     1074 * semaphore before calling this function.
     1075 *
     1076 * @returns 0 on success.
     1077 * @returns Negative error code (errno.h) on failure.
     1078 * @param   pev         Pointer to the semaphore structure to open.
     1079 */
     1080int __libc_Back_safesemEvOpen(__LIBC_PSAFESEMEV pev);
    10331081
    10341082/**
     
    10371085 * @returns 0 on success.
    10381086 * @returns Negative error code (errno.h) on failure.
    1039  * @param   hev         The semaphore handle.
    1040  */
    1041 int __libc_Back_safesemEvClose(uintptr_t hev);
     1087 * @param   pev         Pointer to the semaphore structure to close.
     1088 */
     1089int __libc_Back_safesemEvClose(__LIBC_PSAFESEMEV pev);
    10421090
    10431091/**
    10441092 * Sleep on a semaphore.
    10451093 *
    1046  * This is the most difficult thing we're doing in this file.
    1047  *
    1048  * @returns 0 on success.
    1049  * @returns Negative error code (errno.h) on failure.
    1050  *
    1051  * @param   hev         The semaphore to sleep on.
    1052  * @param   hmtx        The semaphore protecting hev and which is to be unlocked while
    1053  *                      sleeping and reaquired upon waking up.
    1054  * @param   pfnComplete Function to execute on signal and on wait completion.
     1094 * The caller must own the associated mutex semaphore. The mutex semaphore will
     1095 * be released as we go to sleep and reclaimed when we wake up.
     1096 *
     1097 * The pfnComplete callback is used to correct state before signals are handled.
     1098 * It will always be called be for this function returns, and it'll either be under
     1099 * the protection of the signal mutex or the associated mutex (both safe sems).
     1100 *
     1101 * This is the most difficult thing we're doing in this API. On OS/2 we have
     1102 * potential (at least theoretically) race conditions...
     1103 *
     1104 * @returns 0 on success.
     1105 * @returns Negative error code (errno.h) on failure.
     1106 *
     1107 * @param   pev         Pointer to the semaphore structure to sleep on.
     1108 * @param   pfnComplete Function to execute on signal or on wait completion.
    10551109 * @param   pvUser      User argument to pfnComplete.
    10561110 */
    1057 int __libc_Back_safesemEvSleep(uintptr_t hev, uintptr_t hmtx, void (*pfnComplete)(void *pvUser), void *pvUser);
     1111int __libc_Back_safesemEvSleep(__LIBC_PSAFESEMEV pev, void (*pfnComplete)(void *pvUser), void *pvUser);
    10581112
    10591113/**
     
    10621116 * @returns 0 on success.
    10631117 * @returns Negative error code (errno.h) on failure.
    1064  * @param   hev         Event semaphore to post.
    1065  * @param   hmtx        The semaphore protecting hev (see __libc_Back_safesemEvSleep).
    1066  *                      The caller should own this semaphore, it's purpose is debug strictness only!
    1067  */
    1068 int __libc_Back_safesemEvWakeup(uintptr_t hev, uintptr_t hmtx);
     1118 * @param   pev         Pointer to the semaphore structure to post.
     1119 */
     1120int __libc_Back_safesemEvWakeup(__LIBC_PSAFESEMEV pev);
    10691121
    10701122
  • trunk/src/emx/src/lib/sys/safesems.c

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r2297 r2298  
    3838#include <sys/errno.h>
    3939#include "syscalls.h"
    40 #include <emx/umalloc.h>
     40#include <sys/builtin.h>
     41#include <InnoTekLIBC/FastInfoBlocks.h>
    4142#include <InnoTekLIBC/thread.h>
    4243#include <InnoTekLIBC/backend.h>
    43 #include <InnoTekLIBC/sharedpm.h>
    4444#define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_BACK_IPC
    4545#include <InnoTekLIBC/logstrict.h>
     
    4949*   Structures and Typedefs                                                    *
    5050*******************************************************************************/
    51 /**
    52  * Event semaphore tracking structure.
    53  *
    54  * The event semaphore business is difficult because the lack of
    55  * atomic mutex release + event wait apis in OS/2. We have to
    56  * jump around the place to get this working nearly safly...
    57  */     
    58 typedef struct __LIBC_SAFESEMEV
    59 {
    60     /** The event semaphore. */
    61     HEV                 hev;
    62     /** Number of threads which are supposed to be blocking on the above event semaphore. */
    63     uint32_t volatile   cWaiters;
    64     /** Number of processes using the semaphore. */
    65     uint32_t volatile   cUsers;
    66     /** Set if the semaphore is shared.
    67      * If shared this structure is allocated from SPM, else it's from the heap. */
    68     unsigned            fShared;
    69 #ifdef __LIBC_STRICT
    70     /** The mutex semaphore used to protect the event semaphore.
    71      * Strict builds only. */
    72     uintptr_t           hmtx;
    73 #endif
    74 } __LIBC_SAFESEMEV, *__LIBC_PSAFESEMEV;
    75 
    76 
    7751/**
    7852 * Arguments to semEvSleepSignalCallback().
     
    10074 * @returns 0 on success.
    10175 * @returns Negative error code (errno.h) on failure.
    102  * @param   phmtx       Where to store the semaphore handle.
     76 * @param   pmtx        Pointer to the semaphore structure to initialize.
    10377 * @param   fShared     Set if the semaphore should be sharable between processes.
    10478 */
    105 int __libc_Back_safesemMtxCreate(uintptr_t *phmtx, int fShared)
    106 {
    107     FS_VAR_SAVE_LOAD();
    108     HMTX hmtx = NULLHANDLE;
    109     int rc = DosCreateMutexSemEx(NULL, &hmtx, fShared ? DC_SEM_SHARED : 0, FALSE);
    110     FS_RESTORE();
     79int __libc_Back_safesemMtxCreate(__LIBC_PSAFESEMMTX pmtx, int fShared)
     80{
     81    LIBCLOG_ENTER("pmtx=%p fShared=%d\n", (void *)pmtx, fShared);
     82    pmtx->hmtx = NULLHANDLE;
     83    int rc = DosCreateMutexSemEx(NULL, &pmtx->hmtx, fShared ? DC_SEM_SHARED : 0, FALSE);
    11184    if (rc)
    112         return -__libc_native2errno(rc);
    113     *phmtx = hmtx;
    114     return 0;
     85        rc = -__libc_native2errno(rc);
     86    LIBCLOG_RETURN_MSG(rc, "ret %d hmtx=%#lx\n", rc, pmtx->hmtx);
    11587}
    11688
     
    12193 * @returns 0 on success.
    12294 * @returns Negative error code (errno.h) on failure.
    123  * @param   hmtx        The semaphore handle.
    124  */
    125 int __libc_Back_safesemMtxOpen(uintptr_t hmtx)
    126 {
    127     FS_VAR_SAVE_LOAD();
    128     HMTX hmtxOS2 = hmtx;
    129     int rc = DosOpenMutexSemEx(NULL, &hmtxOS2);
    130     FS_RESTORE();
     95 * @param   pmtx        Pointer to the semaphore structure.
     96 */
     97int __libc_Back_safesemMtxOpen(__LIBC_PSAFESEMMTX pmtx)
     98{
     99    LIBCLOG_ENTER("pmtx=%p:{.hmtx=%#lx}\n", (void *)pmtx, pmtx->hmtx);
     100    int rc = DosOpenMutexSemEx(NULL, &pmtx->hmtx);
    131101    if (rc)
    132         return -__libc_native2errno(rc);
    133     return 0;
     102        rc = -__libc_native2errno(rc);
     103    LIBCLOG_RETURN_INT(rc);
    134104}
    135105
     
    140110 * @returns 0 on success.
    141111 * @returns Negative error code (errno.h) on failure.
    142  * @param   hmtx        The semaphore handle.
    143  */
    144 int __libc_Back_safesemMtxClose(uintptr_t hmtx)
    145 {
    146     FS_VAR_SAVE_LOAD();
    147     int rc = DosCloseMutexSemEx(hmtx);
    148     FS_RESTORE();
    149     if (rc)
    150         return -__libc_native2errno(rc);
    151     return 0;
     112 * @param   pmtx        Pointer to the semaphore structure.
     113 */
     114int __libc_Back_safesemMtxClose(__LIBC_PSAFESEMMTX pmtx)
     115{
     116    LIBCLOG_ENTER("pmtx=%p:{.hmtx=%#lx}\n", (void *)pmtx, pmtx->hmtx);
     117    int rc = DosCloseMutexSemEx(pmtx->hmtx);
     118    if (!rc)
     119        rc = -__libc_native2errno(rc);
     120    LIBCLOG_RETURN_INT(rc);
    152121}
    153122
     
    157126 * stack available. If there isn't, a crash is usually the
    158127 * result.
     128 *
    159129 * @internal
    160130 */
     
    175145 * @returns 0 on success.
    176146 * @returns Negative errno on failure.
    177  * @param   hmtx    Handle to the mutex.
    178  */
    179 int __libc_Back_safesemMtxLock(uintptr_t hmtx)
    180 {
    181     LIBCLOG_ENTER("hmtx=%#x\n", hmtx);
    182     ULONG       ul;
     147 * @param   pmtx        Pointer to the semaphore structure.
     148 */
     149int __libc_Back_safesemMtxLock(__LIBC_PSAFESEMMTX pmtx)
     150{
     151    LIBCLOG_ENTER("pmtx=%p:{.hmtx=%#lx}\n", (void *)pmtx, pmtx->hmtx);
     152    ULONG       ul = 0;
     153    HMTX        hmtx = pmtx->hmtx;
    183154    int         rc;
    184155    FS_VAR();
     
    194165
    195166    /*
    196      * Request semaphore and enter "must complete section" to avoid signal trouble.
     167     * Request semaphore and enter "must-complete section" to avoid signal trouble.
    197168     */
    198169    FS_SAVE_LOAD();
     170    /* try for 2ms in a must-complete section. */
     171    DosEnterMustComplete(&ul);
     172    rc = DosRequestMutexSem(hmtx, 2);
     173    if (!rc)
     174    {
     175        FS_RESTORE();
     176        LIBCLOG_RETURN_INT(0);
     177    }
     178
     179    /* retry outside the must-complete section. */
     180    DosExitMustComplete(&ul);
    199181    rc = DosRequestMutexSem(hmtx, 30*1000);
    200182    DosEnterMustComplete(&ul);
     
    207189    /* failure out */
    208190    DosExitMustComplete(&ul);
    209     LIBC_ASSERTM_FAILED("DosRequestMutexSem(%x) failed with rc=%d!\n", hmtx, rc);
     191    LIBC_ASSERTM_FAILED("DosRequestMutexSem(%lx) failed with rc=%d!\n", pmtx->hmtx, rc);
    210192    rc = -__libc_native2errno(rc);
    211193    FS_RESTORE();
     
    219201 * @returns 0 on success.
    220202 * @returns Negative errno on failure.
    221  * @param   hmtx    Handle to the mutex.
    222  */
    223 int __libc_Back_safesemMtxUnlock(uintptr_t hmtx)
    224 {
    225     LIBCLOG_ENTER("hmtx=%#x\n", hmtx);
    226     ULONG   ul = 0;
    227     int     rc;
     203 * @param   pmtx        Pointer to the semaphore structure.
     204 */
     205int __libc_Back_safesemMtxUnlock(__LIBC_PSAFESEMMTX pmtx)
     206{
     207    LIBCLOG_ENTER("pmtx=%p:{.hmtx=%#lx}\n", (void *)pmtx, pmtx->hmtx);
     208    ULONG       ul = 0;
     209    int         rc;
    228210    FS_VAR();
    229211
     
    232214     */
    233215    FS_SAVE_LOAD();
    234     rc = DosReleaseMutexSem(hmtx);
     216    rc = DosReleaseMutexSem(pmtx->hmtx);
    235217    if (rc)
    236218    {
    237219        FS_RESTORE();
    238         LIBC_ASSERTM_FAILED("DosReleaseMutexSem(%x) -> %d\n", hmtx, rc);
     220        LIBC_ASSERTM_FAILED("DosReleaseMutexSem(%lx) -> %d\n", pmtx->hmtx, rc);
    239221        rc = -__libc_native2errno(rc);
    240222        LIBCLOG_RETURN_INT(rc);
     
    248230
    249231
     232
    250233/**
    251234 * Creates a safe event sem.
     
    253236 * @returns 0 on success.
    254237 * @returns Negative error code (errno.h) on failure.
    255  * @param   phev        Where to store the semaphore handle.
     238 * @param   pev         Pointer to the semaphore structure to initialize.
     239 * @param   pmtx        Pointer to the mutex semaphore which protects the event semaphore.
    256240 * @param   fShared     Set if the semaphore should be sharable between processes.
    257241 */
    258 int __libc_Back_safesemEvCreate(uintptr_t *phev, int fShared)
    259 {
    260     LIBCLOG_ENTER("phev=%p fShared=%d\n", (void *)phev, fShared);
    261     int rc;
    262     __LIBC_PSAFESEMEV pEv = NULL;
    263     FS_VAR_SAVE_LOAD();
    264     if (fShared)
    265     {
    266         /*
    267          * Allocate from SPM, be very careful.
    268          */
    269         __LIBC_SPMXCPTREGREC RegRec;
    270         rc = __libc_spmLock(&RegRec, NULL);
    271         if (!rc)
    272         {
    273             pEv = __libc_spmAllocLocked(sizeof(*pEv));
    274             if (pEv)
    275             {
    276                 pEv->hev = NULLHANDLE;
    277                 pEv->cWaiters = 0;
    278                 pEv->cUsers = 1;
    279                 pEv->fShared = fShared;
    280 #ifdef __LIBC_STRICT
    281                 pEv->hmtx = 0;
    282 #endif
    283            
    284                 rc = DosCreateEventSemEx(NULL, &pEv->hev, fShared ? DC_SEM_SHARED : 0, FALSE);
    285                 if (rc)
    286                 {
    287                     __libc_spmFree(pEv);
    288                     rc = -__libc_native2errno(rc);
    289                 }
    290             }
    291             else
    292                 rc = -ENOMEM;
    293             __libc_spmUnlock(&RegRec);
    294         }
    295     }
    296     else
    297     {
    298         /*
    299          * Allocate from high mem heap.
    300          */
    301         pEv = _hmalloc(sizeof(*pEv));
    302         if (pEv)
    303         {
    304             pEv->hev = NULLHANDLE;
    305             pEv->cWaiters = 0;
    306             pEv->cUsers = 1;
    307             pEv->fShared = fShared;
    308 #ifdef __LIBC_STRICT
    309             pEv->hmtx = 0;
    310 #endif
    311 
    312             rc = DosCreateEventSemEx(NULL, &pEv->hev, fShared ? DC_SEM_SHARED : 0, FALSE);
    313             if (rc)
    314             {
    315                 free(pEv);
    316                 rc = -__libc_native2errno(rc);
    317             }
    318         }
    319         else
    320             rc = -ENOMEM;
    321     }
    322     FS_RESTORE();
    323 
    324     if (!rc)
    325         *phev = (uintptr_t)pEv;
    326     LIBCLOG_RETURN_MSG(rc, "ret %d *phev=%#x\n", rc, *phev);
     242int __libc_Back_safesemEvCreate(__LIBC_PSAFESEMEV pev, __LIBC_PSAFESEMMTX pmtx, int fShared)
     243{
     244    LIBCLOG_ENTER("phev=%p pmtx=%p:{.hmtx=%#lx} fShared=%d\n", (void *)pev, (void *)pmtx, pmtx->hmtx, fShared);
     245
     246    pev->hev        = NULLHANDLE;
     247    pev->cWaiters   = 0;
     248    pev->pmtx       = pmtx;
     249    pev->fShared    = fShared;
     250
     251    int rc = DosCreateEventSemEx(NULL, &pev->hev, fShared ? DC_SEM_SHARED : 0, FALSE);
     252    if (rc)
     253        rc = -__libc_native2errno(rc);
     254
     255    LIBCLOG_RETURN_MSG(rc, "ret %d pev->hev=%#lx\n", rc, pev->hev);
    327256}
    328257
     
    331260 * Opens a shared safe event sem.
    332261 *
    333  * @returns 0 on success.
    334  * @returns Negative error code (errno.h) on failure.
    335  * @param   hev         The semaphore handle.
    336  */
    337 int __libc_Back_safesemEvOpen(uintptr_t hev)
    338 {
    339     LIBCLOG_ENTER("hev=%#x\n", hev);
    340     __LIBC_PSAFESEMEV pEv = (__LIBC_PSAFESEMEV)hev;
    341     FS_VAR_SAVE_LOAD();
    342     int rc = DosOpenEventSemEx(NULL, &pEv->hev);
    343     FS_RESTORE();
    344     if (!rc)
    345         __atomic_increment_u32(&pEv->cUsers);
    346     else
    347         rc = -__libc_native2errno(rc);
     262 * The caller is responsible for opening the associated mutex
     263 * semaphore before calling this function.
     264 *
     265 * @returns 0 on success.
     266 * @returns Negative error code (errno.h) on failure.
     267 * @param   pev         Pointer to the semaphore structure to open.
     268 */
     269int __libc_Back_safesemEvOpen(__LIBC_PSAFESEMEV pev)
     270{
     271    LIBCLOG_ENTER("pev=%p:{.hev=%#lx .cWaiters=%d .pmtx=%p .fShared=%d}\n",
     272                  (void *)pev, pev->hev, pev->cWaiters, (void *)pev->pmtx, pev->fShared);
     273
     274    int rc = DosOpenEventSemEx(NULL, &pev->hev);
     275    if (rc)
     276        rc = -__libc_native2errno(rc);
     277
    348278    LIBCLOG_RETURN_INT(rc);
    349279}
     
    355285 * @returns 0 on success.
    356286 * @returns Negative error code (errno.h) on failure.
    357  * @param   hev         The semaphore handle.
    358  */
    359 int __libc_Back_safesemEvClose(uintptr_t hev)
    360 {
    361     LIBCLOG_ENTER("hev=%#x\n", hev);
    362     __LIBC_PSAFESEMEV pEv = (__LIBC_PSAFESEMEV)hev;
    363     FS_VAR_SAVE_LOAD();
    364     int rc = DosCloseEventSemEx(pEv->hev);
    365     FS_RESTORE();
    366     if (!rc)
    367     {
    368         if (    pEv->cUsers
    369             &&  !__atomic_decrement_u32(&pEv->cUsers))
    370         {
    371             pEv->hev = NULLHANDLE;
    372             if (pEv->fShared)
    373                 __libc_spmFree(pEv);
    374             else
    375                 free(pEv);
    376         }
    377     }
    378     else
    379         rc = -__libc_native2errno(rc);
     287 * @param   pev         Pointer to the semaphore structure to close.
     288 */
     289int __libc_Back_safesemEvClose(__LIBC_PSAFESEMEV pev)
     290{
     291    LIBCLOG_ENTER("pev=%p:{.hev=%#lx .cWaiters=%d .pmtx=%p .fShared=%d}\n",
     292                  (void *)pev, pev->hev, pev->cWaiters, (void *)pev->pmtx, pev->fShared);
     293
     294    int rc = DosCloseEventSemEx(pev->hev);
     295    if (rc)
     296        rc = -__libc_native2errno(rc);
     297
    380298    LIBCLOG_RETURN_INT(rc);
    381299}
     
    398316        int rc = DosSetPriority(PRTYS_THREAD, pArgs->ulOldPri >> 8, pArgs->ulOldPri & 0xff, 0);
    399317        if (rc)
    400             __libc_Back_panic(0, NULL, "DosSetPriority(PRTYS_THREAD, %x, %x, 0) -> %d\n",
     318            __libc_Back_panic(0, NULL, "DosSetPriority(PRTYS_THREAD, %x, %x, 0) -> %d (3)\n",
    401319                              (unsigned)pArgs->ulOldPri >> 8, (unsigned)pArgs->ulOldPri & 0xff, rc);
    402320        pArgs->fDone = 1;
     
    409327 * Sleep on a semaphore.
    410328 *
    411  * This is the most difficult thing we're doing in this file.
    412  *
    413  * @returns 0 on success.
    414  * @returns Negative error code (errno.h) on failure.
    415  *
    416  * @param   hev         The semaphore to sleep on.
    417  * @param   hmtx        The semaphore protecting hev and which is to be unlocked while
    418  *                      sleeping and reaquired upon waking up.
    419  * @param   pfnComplete Function to execute on signal and on wait completion.
     329 * The caller must own the associated mutex semaphore. The mutex semaphore will
     330 * be released as we go to sleep and reclaimed when we wake up.
     331 *
     332 * The pfnComplete callback is used to correct state before signals are handled.
     333 * It will always be called be for this function returns, and it'll either be under
     334 * the protection of the signal mutex or the associated mutex (both safe sems).
     335 *
     336 * This is the most difficult thing we're doing in this API. On OS/2 we have
     337 * potential (at least theoretically) race conditions...
     338 *
     339 * @returns 0 on success.
     340 * @returns Negative error code (errno.h) on failure.
     341 *
     342 * @param   pev         Pointer to the semaphore structure to sleep on.
     343 * @param   pfnComplete Function to execute on signal or on wait completion.
    420344 * @param   pvUser      User argument to pfnComplete.
    421345 */
    422 int __libc_Back_safesemEvSleep(uintptr_t hev, uintptr_t hmtx, void (*pfnComplete)(void *pvUser), void *pvUser)
    423 {
    424     LIBCLOG_ENTER("hev=%#x hmtx=%#x pfnComplete=%p pvUser=%p\n", hev, hmtx, (void *)pfnComplete, pvUser);
    425     __LIBC_PSAFESEMEV pEv = (__LIBC_PSAFESEMEV)hev;
    426 
    427 #ifdef __LIBC_STRICT
    428     /*
    429      * Keep track of hmtx.
    430      */
    431     if (pEv->hmtx)
    432         LIBC_ASSERTM(pEv->hmtx == hmtx, "pEv->hmtx=%#x hmtx=%#x\n", pEv->hmtx, hmtx);
    433     else
    434         pEv->hmtx = hmtx;
    435 #endif
     346int __libc_Back_safesemEvSleep(__LIBC_PSAFESEMEV pev, void (*pfnComplete)(void *pvUser), void *pvUser)
     347{
     348    LIBCLOG_ENTER("pev=%p:{.hev=%#lx .cWaiters=%d .pmtx=%p .fShared=%d} pfnComplete=%p pvUser=%p\n",
     349                  (void *)pev, pev->hev, pev->cWaiters, (void *)pev->pmtx, pev->fShared, (void *)pfnComplete, pvUser);
    436350
    437351    /*
     
    446360        PPIB    pPib;
    447361        DosGetInfoBlocks(&pTib, &pPib);
    448    
     362
    449363        struct SignalArgs Args;
    450364        Args.ulOldPri       = pTib->tib_ptib2->tib2_ulpri;
     
    453367        Args.pfnComplete    = pfnComplete;
    454368        Args.pvUser         = pvUser;
    455    
     369
     370        /* install signal callback. */
    456371        pThrd->pvSigCallbackUser = &Args;
    457372        pThrd->pfnSigCallback    = semEvSleepSignalCallback;
    458    
     373
    459374        /*
    460          * Raise priority to time critical to increase our chances of actually getting 
     375         * Raise priority to time critical to increase our chances of actually getting
    461376         * blocked before something bad like rescheduling or signaling strikes us.
    462377         */
     
    468383            LIBC_ASSERTM(!rc, "DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0) -> %d\n", rc);
    469384        }
    470    
     385
    471386        /*
    472387         * Release the sempahore and exit the must complete section.
    473388         */
    474         __atomic_increment_u32(&pEv->cWaiters);
    475         rc = __libc_Back_safesemMtxUnlock(hmtx);
     389        __atomic_increment_u32(&pev->cWaiters);
     390        rc = __libc_Back_safesemMtxUnlock(pev->pmtx);
    476391        if (!rc)
    477392        {
     
    481396            int rc2 = 0;
    482397            if (!Args.fDone)
    483                 rc2 = DosWaitEventSem(pEv->hev, SEM_INDEFINITE_WAIT);
    484    
     398                rc2 = DosWaitEventSem(pev->hev, SEM_INDEFINITE_WAIT);
     399
    485400            /*
    486              * Reclaim the ownership of the sempahore (w/must-complete) and deregister
    487              * the signal notification callback before we check if we was interrupted or not during the wait.
     401             * Reclaim the ownership of the mutex and handle event semaphore errors.
    488402             */
    489             __atomic_decrement_u32(&pEv->cWaiters);
     403            __atomic_decrement_u32(&pev->cWaiters);
    490404            LIBCLOG_MSG("woke up - rc2=%d\n", rc2);
    491             rc = __libc_Back_safesemMtxLock(hmtx);
    492             pThrd->pfnSigCallback = NULL;
    493             pThrd->pvSigCallbackUser = NULL;
    494             if (!Args.fDone)
    495             {
    496                 /*
    497                  * Not interrupted.
    498                  * Check for errors, do completion and restore priority.
    499                  */
    500                 if (rc2)
    501                     rc = -__libc_native2errno(rc2);
    502                 pfnComplete(pvUser);
    503                 rc2 = DosSetPriority(PRTYS_THREAD, Args.ulOldPri >> 8, Args.ulOldPri & 0xff, 0);
    504                 if (rc2)
    505                     __libc_Back_panic(0, NULL, "DosSetPriority(PRTYS_THREAD, %x, %x, 0) -> %d\n",
    506                                       (unsigned)Args.ulOldPri >> 8, (unsigned)Args.ulOldPri & 0xff, rc2);
    507             }
    508             else if (!rc)
    509                 rc = -EINTR;
    510    
     405            rc = __libc_Back_safesemMtxLock(pev->pmtx);
     406            if (!rc && rc2)
     407                rc = -__libc_native2errno(rc2);
     408
     409            /*
     410             * Restore priority before resetting the semaphore.
     411             */
     412            rc2 = DosSetPriority(PRTYS_THREAD, Args.ulOldPri >> 8, Args.ulOldPri & 0xff, 0);
     413            if (rc2)
     414                __libc_Back_panic(0, NULL, "DosSetPriority(PRTYS_THREAD, %x, %x, 0) -> %d (1)\n",
     415                                  (unsigned)Args.ulOldPri >> 8, (unsigned)Args.ulOldPri & 0xff, rc2);
     416            if (pev->cWaiters > 0)
     417                DosSleep(0); /* hurry up guys */
     418
    511419            /*
    512420             * Reset the event semaphore.
    513421             */
    514422            ULONG ulIgnore;
    515             rc2 = DosResetEventSem(pEv->hev, &ulIgnore);
    516             LIBC_ASSERTM(!rc2 && ERROR_ALREADY_RESET, "DosResetEventSem(%#lx,)->%d\n", pEv->hev, rc2);
     423            rc2 = DosResetEventSem(pev->hev, &ulIgnore);
     424            LIBC_ASSERTM(!rc2 && ERROR_ALREADY_RESET, "DosResetEventSem(%#lx,)->%d\n", pev->hev, rc2);
    517425        }
     426        else
     427        {
     428            /*
     429             * Restore priority.
     430             */
     431            int rc2 = DosSetPriority(PRTYS_THREAD, Args.ulOldPri >> 8, Args.ulOldPri & 0xff, 0);
     432            if (rc2)
     433                __libc_Back_panic(0, NULL, "DosSetPriority(PRTYS_THREAD, %x, %x, 0) -> %d (2)\n",
     434                                  (unsigned)Args.ulOldPri >> 8, (unsigned)Args.ulOldPri & 0xff, rc2);
     435        }
     436
     437        /*
     438         * Uninstall the signal callback.
     439         */
     440        pThrd->pfnSigCallback = NULL;
     441        pThrd->pvSigCallbackUser = NULL;
     442
     443        /*
     444         * Check for interruption and do completion.
     445         */
     446        if (!Args.fDone)
     447        {
     448            pfnComplete(pvUser);
     449            Args.fDone = 1;
     450        }
     451        else if (!rc)
     452            rc = -EINTR;
     453
    518454        FS_RESTORE();
    519455    }
    520456    else
     457    {
     458        pfnComplete(pvUser);
    521459        rc = -ENOSYS;
     460    }
    522461
    523462    LIBCLOG_RETURN_INT(rc);
     
    528467 * Wakes up all threads sleeping on a given event semaphore.
    529468 *
    530  * @returns 0 on success.
    531  * @returns Negative error code (errno.h) on failure.
    532  * @param   hev         Event semaphore to post.
    533  * @param   hmtx        The semaphore protecting hev (see __libc_Back_safesemEvSleep).
    534  *                      The caller should own this semaphore, it's purpose is debug strictness only!
    535  */
    536 int __libc_Back_safesemEvWakeup(uintptr_t hev, uintptr_t hmtx)
    537 {
    538     LIBCLOG_ENTER("hev=%#x hmtx=%#x\n", hev, hmtx);
    539     __LIBC_PSAFESEMEV pEv = (__LIBC_PSAFESEMEV)hev;
     469 * The caller must own the associated mutex semaphore when calling this
     470 * function.
     471 *
     472 * @returns 0 on success.
     473 * @returns Negative error code (errno.h) on failure.
     474 * @param   pev         Pointer to the semaphore structure to post.
     475 */
     476int __libc_Back_safesemEvWakeup(__LIBC_PSAFESEMEV pev)
     477{
     478    LIBCLOG_ENTER("pev=%p:{.hev=%#lx .cWaiters=%d .pmtx=%p .fShared=%d}\n",
     479                  (void *)pev, pev->hev, pev->cWaiters, (void *)pev->pmtx, pev->fShared);
    540480    FS_VAR_SAVE_LOAD();
    541481    int rc;
     
    543483#ifdef __LIBC_STRICT
    544484    /*
    545      * Check hmtx.
     485     * Check mutex ownership.
    546486     */
    547     LIBC_ASSERTM(!pEv->hmtx || pEv->hmtx == hmtx, "pEv->hmtx=%#x hmtx=%#x\n", pEv->hmtx, hmtx);
    548487    ULONG   cNesting;
    549488    PID     pid;
    550489    TID     tid;
    551     rc = DosQueryMutexSem(hmtx, &pid, &tid, &cNesting);
    552     LIBC_ASSERTM(!rc, "DosQueryMutexSem(%#x,,,) -> %d\n", hmtx, rc);
     490    rc = DosQueryMutexSem(pev->pmtx->hmtx, &pid, &tid, &cNesting);
     491    LIBC_ASSERTM(!rc, "DosQueryMutexSem(%#lx,,,) -> %d\n", pev->pmtx->hmtx, rc);
    553492    if (!rc)
    554493        LIBC_ASSERTM(pid == fibGetPid() && tid == fibGetTid(),
    555494                     "pid=%d fibGetPid()->%d  tid=%d fibGetTid()->%d\n", (int)pid, fibGetPid(), (int)tid, fibGetTid());
    556 #endif 
     495#endif
    557496
    558497    /*
    559498     * Post it.
    560499     */
    561     rc = DosPostEventSem(hev);
     500    rc = DosPostEventSem(pev->hev);
    562501    if (!rc)
    563502    {
    564         if (!rc && pEv->cWaiters)
     503        if (pev->cWaiters)
    565504        {
    566             /* give the waiting threads a fair chance to get past the DosWaitEventSem() call. */
     505            /* hurry up guys! get past the DosWaitEventSem! */
    567506            unsigned cYields = 3;
    568507            do
    569508            {
    570509                DosSleep(0);
    571                 LIBCLOG_MSG("cWaiters=%d cYields=%d\n", pEv->cWaiters, cYields);
    572             } while (pEv->cWaiters && --cYields > 0);
     510                LIBCLOG_MSG("cWaiters=%d cYields=%d\n", pev->cWaiters, cYields);
     511            } while (pev->cWaiters && --cYields > 0);
    573512        }
    574513    }
     
    580519            rc = -__libc_native2errno(rc);
    581520    }
     521
    582522    FS_RESTORE();
    583523    LIBCLOG_RETURN_INT(rc);
  • trunk/src/emx/src/lib/sys/sysv_sem.c

    • Property cvs2svn:cvs-rev changed from 1.5 to 1.6
    r2297 r2298  
    5151    struct semid_ds u;
    5252    /** Event semaphore to wait on. */
    53     uintptr_t       hev;
    54     /** Mutex protecting the semid_ds and hev members. */
    55     uintptr_t       hmtx;
     53    __LIBC_SAFESEMEV    ev;
     54    /** Mutex protecting the semid_ds and ev members. */
     55    __LIBC_SAFESEMMTX   mtx;
    5656};
    5757
     
    7474    struct sem         *sem;
    7575    /** Mutex protecting the undo list and other global sem stuff. */
    76     uintptr_t           hmtx;
     76    __LIBC_SAFESEMMTX   mtx;
    7777    /** list of active undo structures. */
    7878    SLIST_HEAD(, sem_undo) semu_list;
     
    9595//static int    *semu;          /* undo structure pool */
    9696
    97 //#define SEMUNDO_MTX           gpGlobals->hmtx
    98 #define SEMUNDO_LOCK()          __libc_Back_safesemMtxLock(gpGlobals->hmtx);
    99 #define SEMUNDO_UNLOCK()        __libc_Back_safesemMtxUnlock(gpGlobals->hmtx);
    100 #define SEMUNDO_LOCKASSERT(how) do {} while (0) //mtx_assert(gpGlobals->hmtx, (how));
     97//#define SEMUNDO_MTX           gpGlobals->mtx
     98#define SEMUNDO_LOCK()          __libc_Back_safesemMtxLock(&gpGlobals->mtx);
     99#define SEMUNDO_UNLOCK()        __libc_Back_safesemMtxUnlock(&gpGlobals->mtx);
     100#define SEMUNDO_LOCKASSERT(how) do {} while (0) //mtx_assert(&gpGlobals->mtx, (how));
    101101
    102102struct sem {
     
    306306            bzero(pGlobals, cb);
    307307            //pGlobals->cUsers    = 0;
    308             pGlobals->uVersion  = 1;
     308            pGlobals->uVersion  = 0x00020000;
    309309            //pGlobals->semtot    = 0;
    310310            uint8_t *pu8  = (uint8_t *)(pGlobals + 1);
     
    322322        }
    323323    }
     324    else
     325    {
     326        if ((pGlobals->uVersion >> 16) != (0x00020000 >> 16))
     327        {
     328            __libc_spmUnlock(&RegRec);
     329            __libc_Back_panic(0, NULL, "sysv_sem - pGlobal->uVersion %#x is not compatible with this LIBC. Don't mix incompatible LIBCs!\n",
     330                              pGlobals->uVersion);
     331        }
     332    }
    324333
    325334    /*
     
    330339    {
    331340        /* create */
    332         rc = __libc_Back_safesemMtxCreate(&pGlobals->hmtx, 1);
     341        rc = __libc_Back_safesemMtxCreate(&pGlobals->mtx, 1);
    333342        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    334             rc = __libc_Back_safesemMtxCreate(&pGlobals->sema[i].hmtx, 1);
     343            rc = __libc_Back_safesemMtxCreate(&pGlobals->sema[i].mtx, 1);
    335344        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    336             rc = __libc_Back_safesemEvCreate(&pGlobals->sema[i].hev, 1);
     345            rc = __libc_Back_safesemEvCreate(&pGlobals->sema[i].ev, &pGlobals->sema[i].mtx, 1);
    337346    }
    338347    else
    339348    {
    340349        /* open */
    341         rc = __libc_Back_safesemMtxOpen(pGlobals->hmtx);
     350        rc = __libc_Back_safesemMtxOpen(&pGlobals->mtx);
    342351        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    343             rc = __libc_Back_safesemMtxOpen(pGlobals->sema[i].hmtx);
     352            rc = __libc_Back_safesemMtxOpen(&pGlobals->sema[i].mtx);
    344353        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    345             rc = __libc_Back_safesemEvOpen(pGlobals->sema[i].hev);
     354            rc = __libc_Back_safesemEvOpen(&pGlobals->sema[i].ev);
    346355    }
    347356    __atomic_increment_u32(&pGlobals->cUsers);
     
    562571        u_short usval, count;
    563572        uid_t uid;
    564         uintptr_t hmtx;
     573        __LIBC_PSAFESEMMTX pmtx;
    565574        int semid_in = semid;
    566575
     
    580589        error = 0;
    581590        rval = 0;
    582         hmtx = NULLHANDLE;
     591        pmtx = NULL;
    583592
    584593        switch(cmd) {
     
    587596                        LIBCLOG_ERROR_RETURN_INT(-EINVAL);
    588597                semaptr = &sema[semid];
    589                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     598                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    590599                if ((semaptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
    591600                        error = EINVAL;
     
    594603                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    595604                        goto done2;
    596                 __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
     605                __libc_Back_safesemMtxUnlock(pmtx); pmtx = NULL;
    597606                memcpy(real_arg.buf, &semaptr->u, sizeof(struct semid_ds));
    598607                rval = IXSEQ_TO_IPCID(semid, semaptr->u.sem_perm);
     
    608617        switch (cmd) {
    609618        case IPC_RMID:
    610                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     619                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    611620                if ((error = semvalid(semid_in, semaptr)) != 0)
    612621                        goto done2;
     
    628637                semundo_clear(semid, -1);
    629638                SEMUNDO_UNLOCK();
    630                 __libc_Back_safesemEvWakeup(semaptr->hev, semaptr->hmtx);
     639                __libc_Back_safesemEvWakeup(&semaptr->ev);
    631640                break;
    632641
     
    634643                if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
    635644                        goto done2;
    636                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     645                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    637646                if ((error = semvalid(semid_in, semaptr)) != 0)
    638647                        goto done2;
     
    647656
    648657        case IPC_STAT:
    649                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     658                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    650659                if ((error = semvalid(semid_in, semaptr)) != 0)
    651660                        goto done2;
     
    653662                        goto done2;
    654663                sbuf = semaptr->u;
    655                 __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
     664                __libc_Back_safesemMtxUnlock(pmtx); pmtx = NULL;
    656665                error = copyout(semaptr, real_arg.buf,
    657666                                sizeof(struct semid_ds));
     
    659668
    660669        case GETNCNT:
    661                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     670                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    662671                if ((error = semvalid(semid_in, semaptr)) != 0)
    663672                        goto done2;
     
    672681
    673682        case GETPID:
    674                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     683                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    675684                if ((error = semvalid(semid_in, semaptr)) != 0)
    676685                        goto done2;
     
    685694
    686695        case GETVAL:
    687                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     696                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    688697                if ((error = semvalid(semid_in, semaptr)) != 0)
    689698                        goto done2;
     
    699708        case GETALL:
    700709                array = _hmalloc(sizeof(*array) * semaptr->u.sem_nsems);
    701                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     710                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    702711                if ((error = semvalid(semid_in, semaptr)) != 0)
    703712                        goto done2;
     
    706715                for (i = 0; i < semaptr->u.sem_nsems; i++)
    707716                        array[i] = semaptr->u.sem_base[i].semval;
    708                 __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
     717                __libc_Back_safesemMtxUnlock(pmtx); pmtx = NULL;
    709718                error = copyout(array, real_arg.array,
    710719                    i * sizeof(real_arg.array[0]));
     
    712721
    713722        case GETZCNT:
    714                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     723                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    715724                if ((error = semvalid(semid_in, semaptr)) != 0)
    716725                        goto done2;
     
    725734
    726735        case SETVAL:
    727                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     736                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    728737                if ((error = semvalid(semid_in, semaptr)) != 0)
    729738                        goto done2;
     
    742751                semundo_clear(semid, semnum);
    743752                SEMUNDO_UNLOCK();
    744                 __libc_Back_safesemEvWakeup(semaptr->hev, semaptr->hmtx);
     753                __libc_Back_safesemEvWakeup(&semaptr->ev);
    745754                break;
    746755
    747756        case SETALL:
    748                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     757                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    749758raced:
    750759                if ((error = semvalid(semid_in, semaptr)) != 0)
    751760                        goto done2;
    752761                count = semaptr->u.sem_nsems;
    753                 __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
     762                __libc_Back_safesemMtxUnlock(pmtx); pmtx = NULL;
    754763                array = _hmalloc(sizeof(*array) * count);
    755764                error = copyin(real_arg.array, array, count * sizeof(*array));
    756765                if (error)
    757766                        break;
    758                 __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
     767                __libc_Back_safesemMtxLock(pmtx = &semaptr->mtx);
    759768                if ((error = semvalid(semid_in, semaptr)) != 0)
    760769                        goto done2;
     
    778787                semundo_clear(semid, -1);
    779788                SEMUNDO_UNLOCK();
    780                 __libc_Back_safesemEvWakeup(semaptr->hev, semaptr->hmtx);
     789                __libc_Back_safesemEvWakeup(&semaptr->ev);
    781790                break;
    782791
     
    787796
    788797done2:
    789         if (hmtx)
    790                 __libc_Back_safesemMtxUnlock(hmtx);
     798        if (pmtx)
     799                __libc_Back_safesemMtxUnlock(pmtx);
    791800
    792801        if (array != NULL)
     
    821830        struct semid_kernel * sema = gpGlobals->sema;
    822831
    823         error = __libc_Back_safesemMtxLock(gpGlobals->hmtx);
     832        error = __libc_Back_safesemMtxLock(&gpGlobals->mtx);
    824833        if (error)
    825834            LIBCLOG_ERROR_RETURN_INT(error);
     
    899908        error = -IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
    900909done2:
    901         __libc_Back_safesemMtxUnlock(gpGlobals->hmtx);
     910        __libc_Back_safesemMtxUnlock(&gpGlobals->mtx);
    902911        if (!error)
    903912                LIBCLOG_RETURN_INT(-error);
     
    9911000
    9921001        semaptr = &sema[semid];
    993         error = __libc_Back_safesemMtxLock(semaptr->hmtx);
     1002        error = __libc_Back_safesemMtxLock(&semaptr->mtx);
    9941003        if (error) {
    9951004                if (sops != small_sops)
     
    11131122                __atomic_increment_u16(Args.pu16);
    11141123                DPRINTF(("semop:  good night (%d)!\n", sopptr->sem_op));
    1115                 error = -__libc_Back_safesemEvSleep(semaptr->hev, semaptr->hmtx, SleepDone, &Args);
     1124                error = -__libc_Back_safesemEvSleep(&semaptr->ev, SleepDone, &Args);
    11161125                DPRINTF(("semop:  good morning (error=%d)!\n", error));
    11171126                /* return code is checked below, after sem[nz]cnt-- */
     
    12081217        if (do_wakeup) {
    12091218                DPRINTF(("semop:  doing wakeup\n"));
    1210                 __libc_Back_safesemEvWakeup(semaptr->hev, semaptr->hmtx);
     1219                __libc_Back_safesemEvWakeup(&semaptr->ev);
    12111220                DPRINTF(("semop:  back from wakeup\n"));
    12121221        }
    12131222        DPRINTF(("semop:  done\n"));
    12141223done2:
    1215         __libc_Back_safesemMtxUnlock(semaptr->hmtx);
     1224        __libc_Back_safesemMtxUnlock(&semaptr->mtx);
    12161225        if (sops != small_sops)
    12171226                free(sops);
     
    12721281                        semaptr = &gpGlobals->sema[semid];
    12731282                        SEMUNDO_LOCK();
    1274                         __libc_Back_safesemMtxUnlock(semaptr->hmtx);
     1283                        __libc_Back_safesemMtxUnlock(&semaptr->mtx);
    12751284                        if ((semaptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
    12761285                                panic("semexit - semid not allocated");
     
    12981307                                semaptr->u.sem_base[semnum].semval += adjval;
    12991308
    1300                         __libc_Back_safesemEvWakeup(semaptr->hev, semaptr->hmtx);
     1309                        __libc_Back_safesemEvWakeup(&semaptr->ev);
    13011310                        DPRINTF(("semexit:  back from wakeup\n"));
    13021311                    skip:
    1303                         __libc_Back_safesemMtxUnlock(semaptr->hmtx);
     1312                        __libc_Back_safesemMtxUnlock(&semaptr->mtx);
    13041313                        SEMUNDO_UNLOCK();
    13051314                }
  • trunk/src/emx/src/lib/sys/sysv_shm.c

    • Property cvs2svn:cvs-rev changed from 1.5 to 1.6
    r2297 r2298  
    3030 */
    3131
     32#define _BSD_NAMESPACE_POLLUTION
    3233#include "libc-alias.h"
    3334#include <sys/cdefs.h>
     
    6566/* debug printf */
    6667#define DPRINTF(a)      LIBCLOG_MSG a
     68#include <386/param.h>
     69#ifndef round_page
     70#error round_page is missing
     71#endif
     72
    6773
    6874
     
    8288    volatile uint32_t   cUsers;
    8389    /** Mutex protecting everything. */
    84     uintptr_t           hmtx;
     90    __LIBC_SAFESEMMTX   mtx;
    8591    /** Event semaphore which the daemon should sleep on.
    8692     * This is posted whenever a memory object is created or deleted. */
    87     uintptr_t           hevDaemon;
     93    __LIBC_SAFESEMEV    evDaemon;
    8894
    8995    int                 shm_last_free;
     
    286292        pGlobals->shm_nused--;
    287293        shmseg->shm_perm.mode = SHMSEG_FREE;
    288         __libc_Back_safesemEvWakeup(pGlobals->hevDaemon, pGlobals->hmtx);
     294        __libc_Back_safesemEvWakeup(&pGlobals->evDaemon);
    289295}
    290296
     
    314320                pGlobals->shm_last_free = segnum;
    315321        }
    316         __libc_Back_safesemEvWakeup(pGlobals->hevDaemon, pGlobals->hmtx);
     322        __libc_Back_safesemEvWakeup(&pGlobals->evDaemon);
    317323        return (0);
    318324}
     
    344350        }
    345351       
    346         error = __libc_Back_safesemMtxLock(gpGlobals->hmtx);
     352        error = __libc_Back_safesemMtxLock(&gpGlobals->mtx);
    347353        if (error)
    348354            LIBCLOG_ERROR_RETURN_INT(error);
     
    365371        error = shm_delete_mapping(shmmap_s, 0);
    366372done2:
    367         __libc_Back_safesemMtxUnlock(gpGlobals->hmtx);
     373        __libc_Back_safesemMtxUnlock(&gpGlobals->mtx);
    368374        return (-error);
    369375}
     
    394400        struct __libc_SysV_Shm *pGlobals = gpGlobals;
    395401       
    396         error = __libc_Back_safesemMtxLock(pGlobals->hmtx);
     402        error = __libc_Back_safesemMtxLock(&pGlobals->mtx);
    397403        if (error)
    398404            LIBCLOG_ERROR_RETURN_INT(error);
     
    403409        shmmap_s = g_vm_shm;
    404410        if (shmmap_s == NULL) {
    405                 __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     411                __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    406412                size = shminfo.shmseg * sizeof(struct shmmap_state);
    407413                shmmap_s = _hmalloc(size);
     
    410416                for (i = 0; i < shminfo.shmseg; i++)
    411417                        shmmap_s[i].shmid = -1;
    412                 __libc_Back_safesemMtxLock(pGlobals->hmtx);
     418                __libc_Back_safesemMtxLock(&pGlobals->mtx);
    413419                if (!g_vm_shm)
    414420                    g_vm_shm = shmmap_s;
     
    479485       
    480486done2:
    481         __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     487        __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    482488        *ppvActual = (void *)attach_va;
    483489        if (!error)
     
    516522        struct __libc_SysV_Shm *pGlobals = gpGlobals;
    517523       
    518         error = __libc_Back_safesemMtxLock(pGlobals->hmtx);
     524        error = __libc_Back_safesemMtxLock(&pGlobals->mtx);
    519525        if (error)
    520526            return error;
     
    602608        }
    603609done2:
    604         __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     610        __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    605611        if (error)
    606612            rval = -error;
     
    755761        struct __libc_SysV_Shm *pGlobals = gpGlobals;
    756762       
    757         rval = __libc_Back_safesemMtxLock(pGlobals->hmtx);
     763        rval = __libc_Back_safesemMtxLock(&pGlobals->mtx);
    758764        if (rval)
    759765            LIBCLOG_ERROR_RETURN_INT(rval);
     
    778784        rval = shmget_allocate_segment(key, size, mode);
    779785done2:
    780         __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     786        __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    781787        if (rval >= 0)
    782788            LIBCLOG_RETURN_INT(rval);
     
    812818                struct shmmap_state *shm = g_vm_shm;
    813819                if (shm) {
    814                     __libc_Back_safesemMtxLock(pGlobals->hmtx);
     820                    __libc_Back_safesemMtxLock(&pGlobals->mtx);
    815821                    int c = shminfo.shmseg;
    816822                    while (c-- > 0)
     
    819825                            pGlobals->shmsegs[IPCID_TO_IX(shm->shmid)].shm_nattch++;
    820826                    }
    821                     __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     827                    __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    822828                }
    823829            }
     
    839845        if (pGlobals && !fDone) {
    840846                fDone = 1;
    841                 __libc_Back_safesemMtxLock(pGlobals->hmtx);
     847                __libc_Back_safesemMtxLock(&pGlobals->mtx);
    842848                struct shmmap_state *shm = g_vm_shm;
    843849                if (shm) {
     
    874880                    }
    875881                }
    876                 __libc_Back_safesemMtxUnlock(pGlobals->hmtx);
     882                __libc_Back_safesemMtxUnlock(&pGlobals->mtx);
    877883                __atomic_decrement_u32(&pGlobals->cUsers);
    878884                gpGlobals = NULL;
     
    937943            /* init globals */
    938944            bzero(pGlobals, cb);
    939             pGlobals->uVersion = 1;
     945            pGlobals->uVersion = 0x00020000;
    940946            pGlobals->cUsers = 0;
    941947            //pGlobals->shm_last_free = 0;
     
    956962        }
    957963    }
     964    else
     965    {
     966        if ((pGlobals->uVersion >> 16) != (0x00020000 >> 16))
     967        {
     968            __libc_spmUnlock(&RegRec);
     969            __libc_Back_panic(0, NULL, "sysv_shm - pGlobal->uVersion %#x is not compatible with this LIBC. Don't mix incompatible LIBCs!\n",
     970                              pGlobals->uVersion);
     971        }
     972    }
    958973
    959974    /*
     
    963978    {
    964979        /* create */
    965         rc = __libc_Back_safesemMtxCreate(&pGlobals->hmtx, 1);
     980        rc = __libc_Back_safesemMtxCreate(&pGlobals->mtx, 1);
    966981        if (!rc)
    967             rc = __libc_Back_safesemEvCreate(&pGlobals->hevDaemon, 1);
     982            rc = __libc_Back_safesemEvCreate(&pGlobals->evDaemon, &pGlobals->mtx, 1);
    968983    }
    969984    else
    970985    {
    971986        /* open */
    972         rc = __libc_Back_safesemMtxOpen(pGlobals->hmtx);
     987        rc = __libc_Back_safesemMtxOpen(&pGlobals->mtx);
    973988        if (!rc)
    974             rc = __libc_Back_safesemEvOpen(pGlobals->hevDaemon);
     989            rc = __libc_Back_safesemEvOpen(&pGlobals->evDaemon);
    975990    }
    976991    __atomic_increment_u32(&pGlobals->cUsers);
Note: See TracChangeset for help on using the changeset viewer.