Changeset 1986


Ignore:
Timestamp:
May 9, 2005, 7:02:45 AM (20 years ago)
Author:
bird
Message:

SysV Shared Memory.

Location:
trunk/src/emx
Files:
6 added
8 edited

Legend:

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

    • Property cvs2svn:cvs-rev changed from 1.45 to 1.46
    r1985 r1986  
    77        o Added a signal notification callback to the thread structure.
    88        o Changed tcpip term callback to more generic exitlist callback (SPM).
     9        o Ported the BSD SysV Shared Memory module.
    910
    10112005-05-05: knut st. osmundsen <bird-gccos2-spam@anduin.net>
  • trunk/src/emx/include/InnoTekLIBC/backend.h

    • Property cvs2svn:cvs-rev changed from 1.23 to 1.24
    r1985 r1986  
    894894/** @defgroup grp_Back_sysvipc LIBC Backend - SysV IPC
    895895 * @{ */
     896
     897/**
     898 * sysget syscall.
     899 */
     900int __libc_Back_sysvSemGet(key_t key, int nsems, int semflg);
     901
     902/**
     903 * semop syscall.
     904 */
     905int __libc_Back_sysvSemOp(int semid, struct sembuf *sops_user, size_t nsops);
     906
    896907/**
    897908 * semctl syscall
     
    899910int __libc_Back_sysvSemCtl(int semid, int semnum, int cmd, union semun real_arg);
    900911
    901 /**
    902  * sysget syscall.
    903  */
    904 int __libc_Back_sysvSemGet(key_t key, int nsems, int semflg);
    905 
    906 /**
    907  * semop syscall.
    908  */
    909 int __libc_Back_sysvSemOp(int semid, struct sembuf *sops_user, size_t nsops);
     912
     913/**
     914 * shmget.
     915 */
     916int __libc_Back_sysvShmGet(key_t key, size_t size, int shmflg);
     917
     918/**
     919 * shmat.
     920 */
     921int __libc_Back_sysvShmAt(int shmid, const void *shmaddr, int shmflg, void **ppvActual);
     922
     923/**
     924 * shmdt.
     925 */
     926int __libc_Back_sysvShmDt(const void *shmaddr);
     927
     928/**
     929 * shmctl.
     930 */
     931int __libc_Back_sysvShmCtl(int shmid, int cmd, struct shmid_ds *bufptr);
     932
     933/** @} */
     934
     935
     936/** @defgroup grp_Back_safesem LIBC Backend - Internal Signal-Safe Semaphores.
     937 * @{ */
     938
     939/**
     940 * Creates a safe mutex sem.
     941 *
     942 * @returns 0 on success.
     943 * @returns Negative error code (errno.h) on failure.
     944 * @param   phmtx       Where to store the semaphore handle.
     945 * @param   fShared     Set if the semaphore should be sharable between processes.
     946 */
     947int __libc_Back_safesemMtxCreate(uintptr_t *phmtx, int fShared);
     948
     949/**
     950 * Opens a shared safe mutex sem.
     951 *
     952 * @returns 0 on success.
     953 * @returns Negative error code (errno.h) on failure.
     954 * @param   hmtx        The semaphore handle.
     955 */
     956int __libc_Back_safesemMtxOpen(uintptr_t hmtx);
     957
     958/**
     959 * Closes a shared safe mutex sem.
     960 *
     961 * @returns 0 on success.
     962 * @returns Negative error code (errno.h) on failure.
     963 * @param   hmtx        The semaphore handle.
     964 */
     965int __libc_Back_safesemMtxClose(uintptr_t hmtx);
     966
     967/**
     968 * Locks a mutex semaphore.
     969 *
     970 * @returns 0 on success.
     971 * @returns Negative errno on failure.
     972 * @param   hmtx    Handle to the mutex.
     973 */
     974int __libc_Back_safesemMtxLock(uintptr_t hmtx);
     975
     976/**
     977 * Unlocks a mutex semaphore.
     978 *
     979 * @returns 0 on success.
     980 * @returns Negative errno on failure.
     981 * @param   hmtx    Handle to the mutex.
     982 */
     983int __libc_Back_safesemMtxUnlock(uintptr_t hmtx);
     984
     985/**
     986 * Creates a safe event sem.
     987 *
     988 * @returns 0 on success.
     989 * @returns Negative error code (errno.h) on failure.
     990 * @param   phev        Where to store the semaphore handle.
     991 * @param   fShared     Set if the semaphore should be sharable between processes.
     992 */
     993int __libc_Back_safesemEvCreate(uintptr_t *phev, int fShared);
     994
     995/**
     996 * Opens a shared safe event sem.
     997 *
     998 * @returns 0 on success.
     999 * @returns Negative error code (errno.h) on failure.
     1000 * @param   hev         The semaphore handle.
     1001 */
     1002int __libc_Back_safesemEvOpen(uintptr_t hev);
     1003
     1004/**
     1005 * Closes a shared safe mutex sem.
     1006 *
     1007 * @returns 0 on success.
     1008 * @returns Negative error code (errno.h) on failure.
     1009 * @param   hev         The semaphore handle.
     1010 */
     1011int __libc_Back_safesemEvClose(uintptr_t hev);
     1012
     1013/**
     1014 * Sleep on a semaphore.
     1015 *
     1016 * This is the most difficult thing we're doing in this file.
     1017 *
     1018 * @returns 0 on success.
     1019 * @returns Negative error code (errno.h) on failure.
     1020 *
     1021 * @param   hev         The semaphore to sleep on.
     1022 * @param   hmtx        The semaphore protecting hev and which is to be unlocked while
     1023 *                      sleeping and reaquired upon waking up.
     1024 * @param   pfnComplete Function to execute on signal and on wait completion.
     1025 * @param   pvUser      User argument to pfnComplete.
     1026 */
     1027int __libc_Back_safesemEvSleep(uintptr_t hev, uintptr_t hmtx, void (*pfnComplete)(void *pvUser), void *pvUser);
     1028
     1029/**
     1030 * Wakes up all threads sleeping on a given event semaphore.
     1031 *
     1032 * @returns 0 on success.
     1033 * @returns Negative error code (errno.h) on failure.
     1034 * @param   hev         Event semaphore to post.
     1035 */
     1036int __libc_Back_safesemEvWakeup(uintptr_t hev);
     1037
    9101038
    9111039/** @} */
  • trunk/src/emx/include/InnoTekLIBC/logstrict.h

    • Property cvs2svn:cvs-rev changed from 1.12 to 1.13
    r1985 r1986  
    184184#define __LIBC_LOG_GRP_MMAN         16
    185185
     186/** Backend SysV IPC APIs. */
     187#define __LIBC_LOG_GRP_BACK_IPC     17
    186188/** Backend Thread APIs. */
    187189#define __LIBC_LOG_GRP_BACK_THREAD  18
  • trunk/src/emx/include/InnoTekLIBC/sharedpm.h

    • Property cvs2svn:cvs-rev changed from 1.22 to 1.23
    r1985 r1986  
    557557    /** 116 - Pointer to SysV Sempahore globals. */
    558558    struct __libc_SysV_Sem             *pSysVSem;
    559 
    560     /*  120 - The rest of the block, up to cbProcess, is undefined in this version.
     559    /** 120 - Pointer to SysV Shared Memory globals. */
     560    struct __libc_SysV_Shm             *pSysVShm;
     561    /** 124 - Pointer to SysV Message Queue globals. */
     562    struct __libc_SysV_Msg             *pSysVMsg;
     563
     564    /*  128 - The rest of the block, up to cbProcess, is undefined in this version.
    561565     * Future versions of LIBC may use this area assuming it's initalized with zeros.
    562566     */
  • trunk/src/emx/include/sys/shm.h

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r1985 r1986  
    3939/** @file
    4040 * FreeBSD 5.3
     41 * @changed the SHMLBA to 64KB.
    4142 */
    4243
     
    4849#define SHM_RDONLY  010000  /* Attach read-only (else read-write) */
    4950#define SHM_RND     020000  /* Round attach address to SHMLBA */
    50 #define SHMLBA      PAGE_SIZE /* Segment low boundary address multiple */
     51#define SHMLBA      (0x10000) /* Segment low boundary address multiple */
    5152
    5253/* "official" access mode definitions; somewhat braindead since you have
     
    6364#define SHM_INFO        14
    6465
     66#ifdef __EMX__
     67typedef __uint32_t shmatt_t;
     68#endif
     69
    6570struct shmid_ds {
    6671        struct ipc_perm shm_perm;       /* operation permission structure */
     72#ifdef __EMX__
     73        size_t          shm_segsz;      /* size of segment in bytes */
     74#else
    6775        int             shm_segsz;      /* size of segment in bytes */
     76#endif
    6877        pid_t           shm_lpid;   /* process ID of last shared memory op */
    6978        pid_t           shm_cpid;       /* process ID of creator */
    70         short           shm_nattch;     /* number of current attaches */
     79#ifdef __EMX__
     80        shmatt_t        shm_nattch;     /* number of current attaches */
     81#else
     82        short           shm_nattch;     /* number of current attaches */
     83#endif
    7184        time_t          shm_atime;      /* time of last shmat() */
    7285        time_t          shm_dtime;      /* time of last shmdt() */
     
    88101                shmall;         /* max amount of shared memory (pages) */
    89102};
     103#ifndef __EMX__
    90104extern struct shminfo   shminfo;
    91105extern struct shmid_ds  *shmsegs;
     106#endif
    92107
    93108struct shm_info {
     
    100115};
    101116
     117#ifndef __EMX__
    102118struct thread;
    103119struct proc;
     
    106122void    shmexit(struct vmspace *);
    107123void    shmfork(struct proc *, struct proc *);
     124#endif /* !__EMX__ */
    108125#else /* !_KERNEL */
    109126
     
    116133
    117134__BEGIN_DECLS
     135#ifndef __EMX__
    118136int shmsys(int, ...);
     137#endif
    119138void *shmat(int, const void *, int);
    120139int shmget(key_t, size_t, int);
  • trunk/src/emx/src/lib/libc.def

    • Property cvs2svn:cvs-rev changed from 1.111 to 1.112
    r1985 r1986  
    15291529    "__getenv_long" @1534
    15301530    "__getenv_longlong" @1535
    1531     "___libc_Back_sysvSemCtl" @1536
    1532     "___libc_Back_sysvSemGet" @1537
    1533     "___libc_Back_sysvSemOp" @1538
    1534     "__std_ftok" @1539
    1535     "__std_semctl" @1540
    1536     "__std_semget" @1541
    1537     "__std_semop" @1542
     1531    "___libc_Back_safesemEvClose" @1536
     1532    "___libc_Back_safesemEvCreate" @1537
     1533    "___libc_Back_safesemEvOpen" @1538
     1534    "___libc_Back_safesemEvSleep" @1539
     1535    "___libc_Back_safesemEvWakeup" @1540
     1536    "___libc_Back_safesemMtxClose" @1541
     1537    "___libc_Back_safesemMtxCreate" @1542
     1538    "___libc_Back_safesemMtxLock" @1543
     1539    "___libc_Back_safesemMtxOpen" @1544
     1540    "___libc_Back_safesemMtxUnlock" @1545
     1541    "___libc_Back_sysvSemCtl" @1546
     1542    "___libc_Back_sysvSemGet" @1547
     1543    "___libc_Back_sysvSemOp" @1548
     1544    "___libc_Back_sysvShmAt" @1549
     1545    "___libc_Back_sysvShmCtl" @1550
     1546    "___libc_Back_sysvShmDt" @1551
     1547    "___libc_Back_sysvShmGet" @1552
     1548    "__std_ftok" @1553
     1549    "__std_semctl" @1554
     1550    "__std_semget" @1555
     1551    "__std_semop" @1556
     1552    "__std_shmat" @1557
     1553    "__std_shmctl" @1558
     1554    "__std_shmdt" @1559
     1555    "__std_shmget" @1560
  • trunk/src/emx/src/lib/process/semctl.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r1985 r1986  
    4040
    4141/**
    42  * semop.
     42 * semctl.
    4343 */
    4444int _STD(semctl)(int semid, int semnum, int cmd, ...)
  • trunk/src/emx/src/lib/sys/sysv_sem.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r1985 r1986  
    88
    99#include "libc-alias.h"
     10#include <sys/cdefs.h>
    1011//__FBSDID("$FreeBSD: src/sys/kern/sysv_sem.c,v 1.70.2.3 2005/03/22 17:20:07 sam Exp $");
    11 #include <sys/cdefs.h>
    1212
    1313#include <sys/types.h>
     
    3333#include <InnoTekLIBC/libc.h>
    3434#include <InnoTekLIBC/backend.h>
    35 #define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_BACK_FS
     35#define __LIBC_LOG_GROUP    __LIBC_LOG_GRP_BACK_IPC
    3636#include <InnoTekLIBC/logstrict.h>
    3737
     
    5151    struct semid_ds u;
    5252    /** Event semaphore to wait on. */
    53     HEV             hev;
    54     /** Mutex protecting the semid_ds and HEV members. */
    55     HMTX            hmtx;
     53    uintptr_t       hev;
     54    /** Mutex protecting the semid_ds and hev members. */
     55    uintptr_t       hmtx;
    5656};
    5757
     
    7474    struct sem         *sem;
    7575    /** Mutex protecting the undo list and other global sem stuff. */
    76     HMTX                hmtx;
     76    uintptr_t           hmtx;
    7777    /** list of active undo structures. */
    7878    SLIST_HEAD(, sem_undo) semu_list;
     
    9696
    9797//#define SEMUNDO_MTX           gpGlobals->hmtx
    98 #define SEMUNDO_LOCK()          semMtxLock(gpGlobals->hmtx);
    99 #define SEMUNDO_UNLOCK()        semMtxUnlock(gpGlobals->hmtx);
     98#define SEMUNDO_LOCK()          __libc_Back_safesemMtxLock(gpGlobals->hmtx);
     99#define SEMUNDO_UNLOCK()        __libc_Back_safesemMtxUnlock(gpGlobals->hmtx);
    100100#define SEMUNDO_LOCKASSERT(how) do {} while (0) //mtx_assert(gpGlobals->hmtx, (how));
    101101
     
    238238#define TUNABLE_INT_FETCH(path, var) _getenv_int("LIBC_" path, (var))
    239239
    240 //static int  spmRequestMutexErrno(__LIBC_PSPMXCPTREGREC pRegRec);
    241 //static int  spmRequestMutex(__LIBC_PSPMXCPTREGREC pRegRec);
    242 //static int  spmReleaseMutex(__LIBC_PSPMXCPTREGREC pRegRec);
    243 
    244 //int __libc_spmLock(__LIBC_PSPMXCPTREGREC pRegRec);
    245 
    246240
    247241static int seminit(void)
     
    290284         */
    291285        size_t cb = sizeof(*gpGlobals);
    292         cb += sizeof(struct semid_kernel) * seminfo.semmni;
     286        cb += sizeof(struct semid_kernel) * si.semmni;
    293287        cb += sizeof(struct sem) * si.semmns;
    294         cb += seminfo.semmnu * seminfo.semusz;
     288        cb += si.semmnu * si.semusz;
    295289
    296290        /*
    297          * Take the sem and allocate the memory.
     291         * Retake the sem and allocate the memory.
    298292         */
    299293        rc = __libc_spmLock(&RegRec, &pSPMHdr);
     
    321315            SLIST_INIT(&pGlobals->semu_list);
    322316            pGlobals->semu       = (int *)pu8;
    323             pGlobals->seminfo  = si;
     317            pGlobals->seminfo    = si;
    324318
    325319            /* Set globals. */
     
    333327     */
    334328    int i;
    335     FS_VAR_SAVE_LOAD();
    336329    if (!pGlobals->cUsers)
    337330    {
    338331        /* create */
    339 
    340         rc = DosCreateMutexSemEx(NULL, &pGlobals->hmtx, DC_SEM_SHARED, FALSE);
     332        rc = __libc_Back_safesemMtxCreate(&pGlobals->hmtx, 1);
    341333        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    342             rc = DosCreateMutexSemEx(NULL, &pGlobals->sema[i].hmtx, DC_SEM_SHARED, FALSE);
     334            rc = __libc_Back_safesemMtxCreate(&pGlobals->sema[i].hmtx, 1);
    343335        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    344             rc = DosCreateEventSemEx(NULL, &pGlobals->sema[i].hev, DC_SEM_SHARED, FALSE);
     336            rc = __libc_Back_safesemEvCreate(&pGlobals->sema[i].hev, 1);
    345337    }
    346338    else
    347339    {
    348340        /* open */
    349         rc = DosOpenMutexSemEx(NULL, &pGlobals->hmtx);
     341        rc = __libc_Back_safesemMtxOpen(pGlobals->hmtx);
    350342        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    351             rc = DosOpenMutexSemEx(NULL, &pGlobals->sema[i].hmtx);
     343            rc = __libc_Back_safesemMtxOpen(pGlobals->sema[i].hmtx);
    352344        for (i = 0; i < pGlobals->seminfo.semmni && !rc; i++)
    353             rc = DosOpenEventSemEx(NULL, &pGlobals->sema[i].hev);
     345            rc = __libc_Back_safesemEvOpen(pGlobals->sema[i].hev);
    354346    }
    355347    __atomic_increment_u32(&pGlobals->cUsers);
    356     FS_RESTORE();
    357348
    358349    /*
     
    369360    if (rc)
    370361        rc = -__libc_native2errno(rc);
    371     LIBCLOG_RETURN_INT(rc);
    372 }
    373 
    374 
    375 /**
    376  * This function checks that there is at least 2k of writable
    377  * stack available. If there isn't, a crash is usually the
    378  * result.
    379  * @internal
    380  */
    381 static int semMtxLockStackChecker(void)
    382 {
    383     char volatile *pch = alloca(2048);
    384     if (!pch)
    385         return -1;
    386     /* With any luck the compiler doesn't optimize this away. */
    387     pch[0] = pch[1024] = pch[2044] = 0x7f;
    388     return 0;
    389 }
    390 
    391 
    392 /**
    393  * Locks a mutex semaphore.
    394  *
    395  * @returns 0 on success.
    396  * @returns Negative errno on failure.
    397  * @param   hmtx    Handle to the mutex.
    398  */
    399 static int semMtxLock(HMTX hmtx)
    400 {
    401     LIBCLOG_ENTER("hmtx=%#lx\n", hmtx);
    402     ULONG       ul;
    403     int         rc;
    404     FS_VAR();
    405 
    406     /*
    407      * Check stack.
    408      */
    409     if (semMtxLockStackChecker())
    410     {
    411         LIBC_ASSERTM_FAILED("Too little stack left!\n");
    412         LIBCLOG_RETURN_INT(-EFAULT);
    413     }
    414 
    415     /*
    416      * Request semaphore and enter "must complete section" to avoid signal trouble.
    417      */
    418     FS_SAVE_LOAD();
    419     rc = DosRequestMutexSem(hmtx, 30*1000);
    420     DosEnterMustComplete(&ul);
    421     if (!rc)
    422     {
    423         FS_RESTORE();
    424         LIBCLOG_RETURN_INT(0);
    425     }
    426 
    427     /* failure out */
    428     DosExitMustComplete(&ul);
    429     LIBC_ASSERTM_FAILED("DosRequestMutexSem(%lu) failed with rc=%d!\n", hmtx, rc);
    430     rc = -__libc_native2errno(rc);
    431     FS_RESTORE();
    432     LIBCLOG_RETURN_INT(rc);
    433 }
    434 
    435 
    436 /**
    437  * Unlocks a mutex semaphore.
    438  *
    439  * @returns 0 on success.
    440  * @returns Negative errno on failure.
    441  * @param   hmtx    Handle to the mutex.
    442  */
    443 static int semMtxUnlock(HMTX hmtx)
    444 {
    445     LIBCLOG_ENTER("hmtx=%#lx\n", hmtx);
    446     ULONG   ul = 0;
    447     int     rc;
    448     FS_VAR();
    449 
    450     /*
    451      * Release the semaphore.
    452      */
    453     FS_SAVE_LOAD();
    454     rc = DosReleaseMutexSem(hmtx);
    455     if (rc)
    456     {
    457         FS_RESTORE();
    458         LIBC_ASSERTM_FAILED("DosReleaseMutexSem(%lu) -> %d\n", hmtx, rc);
    459         rc = -__libc_native2errno(rc);
    460         LIBCLOG_RETURN_INT(rc);
    461     }
    462 
    463     DosExitMustComplete(&ul);
    464     FS_RESTORE();
    465     LIBCLOG_RETURN_INT(0);
    466 }
    467 
    468 
    469 /**
    470  * Arguments to semEvSleepSignalCallback().
    471  */
    472 struct semEvSleepSignalCallback_args
    473 {
    474     /** Set if pfnComplete have already been executed. */
    475     volatile int            fDone;
    476     /** Callback to execute. */
    477     void                  (*pfnComplete)(int semid, struct semid_kernel *semaptr, uint16_t volatile *pu16);
    478     /** Semaphore id. */
    479     int                     semid;
    480     /** Semaphore id we was sleeping on. */
    481     struct semid_kernel    *semaptr;
    482     /** Pointer to the value to decrement. */
    483     uint16_t volatile      *pu16;
    484 };
    485 
    486 /**
    487  * Signal notification callback.
    488  */
    489 static void semEvSleepSignalCallback(int iSignal, void *pvUser)
    490 {
    491     struct semEvSleepSignalCallback_args *pArgs = (struct semEvSleepSignalCallback_args *)pvUser;
    492     if (!pArgs->fDone)
    493     {
    494         pArgs->pfnComplete(pArgs->semid, pArgs->semaptr, pArgs->pu16);
    495         pArgs->fDone = 1;
    496     }
    497 }
    498 
    499 /**
    500  * Sleep on a semaphore.
    501  *
    502  * This is the most difficult thing we're doing in this file.
    503  *
    504  * @returns 0 on success.
    505  * @returns Negative error code (errno.h) on failure.
    506  * @param   semaptr     Semaphore to wakeup.
    507  * @param   pfnComplete Function to execute on signal and on wait completion.
    508  */
    509 static int semEvSleep(struct semid_kernel *semaptr, void (*pfnComplete)(int semid, struct semid_kernel *semaptr, uint16_t volatile *pu16), int semid, uint16_t volatile *pu16)
    510 {
    511     LIBCLOG_ENTER("semaptr=%p pfnComplete=%p semid=%#x pu16=%p:{%d}\n", (void *)semaptr, (void *)pfnComplete, semid, (void *)pu16, *pu16);
    512 
    513     /*
    514      * Setup signal notification.
    515      */
    516     __LIBC_PTHREAD pThrd = __libc_threadCurrentNoAuto();
    517     int rc;
    518     if (pThrd)
    519     {
    520         struct semEvSleepSignalCallback_args Args;
    521         Args.fDone               = 0;
    522         Args.pfnComplete         = pfnComplete;
    523         Args.semid               = semid;
    524         Args.semaptr             = semaptr;
    525         Args.pu16                = pu16;
    526         pThrd->pvSigCallbackUser = &Args;
    527         pThrd->pfnSigCallback    = semEvSleepSignalCallback;
    528 
    529         /*
    530          * Reset the event semaphore.
    531          */
    532         ULONG ulIgnore;
    533         FS_VAR_SAVE_LOAD();
    534         rc = DosResetEventSem(semaptr->hev, &ulIgnore);
    535         if (!rc || rc == ERROR_ALREADY_RESET)
    536         {
    537             /*
    538              * Release the sempahore and exit the must complete section.
    539              */
    540             rc = semMtxUnlock(semaptr->hev);
    541             if (!rc)
    542             {
    543                 /*
    544                  * Now, lets wait.
    545                  */
    546                 DosWaitEventSem(semaptr->hev, SEM_INDEFINITE_WAIT);
    547 
    548                 /*
    549                  * Regain access to the semaphore and must complete section
    550                  * before checking if we got an interrupt or not.
    551                  */
    552                 rc = semMtxLock(semaptr->hmtx);
    553                 if (!Args.fDone)
    554                     Args.pfnComplete(Args.semid, Args.semaptr, Args.pu16);
    555                 else if (!rc)
    556                     rc = -EINTR;
    557             }
    558         }
    559         else
    560             rc = -__libc_native2errno(rc);
    561         FS_RESTORE();
    562     }
    563     else
    564         rc = -ENOSYS;
    565 
    566     LIBCLOG_RETURN_INT(rc);
    567 }
    568 
    569 
    570 /**
    571  * Wakes up all threads sleeping on a given event semaphore.
    572  *
    573  * @returns 0 on success.
    574  * @returns Negative error code (errno.h) on failure.
    575  * @param   semaptr     Semaphore to wakeup.
    576  */
    577 static int semEvWakeup(struct semid_kernel *semaptr)
    578 {
    579     LIBCLOG_ENTER("semaptr=%p\n", (void *)semaptr);
    580     FS_VAR_SAVE_LOAD();
    581     int rc = DosPostEventSem(semaptr->hev);
    582     FS_RESTORE();
    583     if (rc)
    584     {
    585         if (rc == ERROR_ALREADY_POSTED || rc == ERROR_TOO_MANY_POSTS)
    586             rc = 0;
    587         else
    588             rc = -__libc_native2errno(rc);
    589     }
    590362    LIBCLOG_RETURN_INT(rc);
    591363}
     
    789561        u_short usval, count;
    790562        uid_t uid;
    791         HMTX hmtx;
     563        uintptr_t hmtx;
    792564
    793565
     
    814586                        LIBCLOG_RETURN_INT(-EINVAL);
    815587                semaptr = &sema[semid];
    816                 semMtxLock(hmtx = semaptr->hmtx);
     588                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    817589                if ((semaptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
    818590                        error = EINVAL;
    819591                        goto done2;
    820592                }
    821                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    822                         goto done2;
    823                 semMtxUnlock(hmtx); hmtx = NULLHANDLE;
     593                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     594                        goto done2;
     595                __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
    824596                memcpy(real_arg.buf, &semaptr->u, sizeof(struct semid_ds));
    825597                rval = IXSEQ_TO_IPCID(semid, semaptr->u.sem_perm);
     
    835607        switch (cmd) {
    836608        case IPC_RMID:
    837                 semMtxLock(hmtx = semaptr->hmtx);
     609                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    838610                if ((error = semvalid(semid, semaptr)) != 0)
    839611                        goto done2;
    840                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_M)))
     612                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_M)))
    841613                        goto done2;
    842614                uid = __libc_spmGetId(__LIBC_SPMID_EUID);
     
    855627                semundo_clear(semid, -1);
    856628                SEMUNDO_UNLOCK();
    857                 semEvWakeup(semaptr);
     629                __libc_Back_safesemEvWakeup(semaptr->hev);
    858630                break;
    859631
     
    861633                if ((error = copyin(real_arg.buf, &sbuf, sizeof(sbuf))) != 0)
    862634                        goto done2;
    863                 semMtxLock(hmtx = semaptr->hmtx);
     635                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    864636                if ((error = semvalid(semid, semaptr)) != 0)
    865637                        goto done2;
    866                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_M)))
     638                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_M)))
    867639                        goto done2;
    868640                semaptr->u.sem_perm.uid = sbuf.sem_perm.uid;
     
    874646
    875647        case IPC_STAT:
    876                 semMtxLock(hmtx = semaptr->hmtx);
     648                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    877649                if ((error = semvalid(semid, semaptr)) != 0)
    878650                        goto done2;
    879                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     651                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    880652                        goto done2;
    881653                sbuf = semaptr->u;
    882                 semMtxUnlock(hmtx); hmtx = NULLHANDLE;
     654                __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
    883655                error = copyout(semaptr, real_arg.buf,
    884656                                sizeof(struct semid_ds));
     
    886658
    887659        case GETNCNT:
    888                 semMtxLock(hmtx = semaptr->hmtx);
     660                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    889661                if ((error = semvalid(semid, semaptr)) != 0)
    890662                        goto done2;
    891                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     663                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    892664                        goto done2;
    893665                if (semnum < 0 || semnum >= semaptr->u.sem_nsems) {
     
    899671
    900672        case GETPID:
    901                 semMtxLock(hmtx = semaptr->hmtx);
     673                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    902674                if ((error = semvalid(semid, semaptr)) != 0)
    903675                        goto done2;
    904                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     676                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    905677                        goto done2;
    906678                if (semnum < 0 || semnum >= semaptr->u.sem_nsems) {
     
    912684
    913685        case GETVAL:
    914                 semMtxLock(hmtx = semaptr->hmtx);
     686                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    915687                if ((error = semvalid(semid, semaptr)) != 0)
    916688                        goto done2;
    917                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     689                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    918690                        goto done2;
    919691                if (semnum < 0 || semnum >= semaptr->u.sem_nsems) {
     
    926698        case GETALL:
    927699                array = _hmalloc(sizeof(*array) * semaptr->u.sem_nsems);
    928                 semMtxLock(hmtx = semaptr->hmtx);
     700                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    929701                if ((error = semvalid(semid, semaptr)) != 0)
    930702                        goto done2;
    931                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     703                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    932704                        goto done2;
    933705                for (i = 0; i < semaptr->u.sem_nsems; i++)
    934706                        array[i] = semaptr->u.sem_base[i].semval;
    935                 semMtxUnlock(hmtx); hmtx = NULLHANDLE;
     707                __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
    936708                error = copyout(array, real_arg.array,
    937709                    i * sizeof(real_arg.array[0]));
     
    939711
    940712        case GETZCNT:
    941                 semMtxLock(hmtx = semaptr->hmtx);
     713                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    942714                if ((error = semvalid(semid, semaptr)) != 0)
    943715                        goto done2;
    944                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
     716                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_R)))
    945717                        goto done2;
    946718                if (semnum < 0 || semnum >= semaptr->u.sem_nsems) {
     
    952724
    953725        case SETVAL:
    954                 semMtxLock(hmtx = semaptr->hmtx);
     726                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    955727                if ((error = semvalid(semid, semaptr)) != 0)
    956728                        goto done2;
    957                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_W)))
     729                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_W)))
    958730                        goto done2;
    959731                if (semnum < 0 || semnum >= semaptr->u.sem_nsems) {
     
    969741                semundo_clear(semid, semnum);
    970742                SEMUNDO_UNLOCK();
    971                 semEvWakeup(semaptr);
     743                __libc_Back_safesemEvWakeup(semaptr->hev);
    972744                break;
    973745
    974746        case SETALL:
    975                 semMtxLock(hmtx = semaptr->hmtx);
     747                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    976748raced:
    977749                if ((error = semvalid(semid, semaptr)) != 0)
    978750                        goto done2;
    979751                count = semaptr->u.sem_nsems;
    980                 semMtxUnlock(hmtx); hmtx = NULLHANDLE;
     752                __libc_Back_safesemMtxUnlock(hmtx); hmtx = NULLHANDLE;
    981753                array = _hmalloc(sizeof(*array) * count);
    982754                error = copyin(real_arg.array, array, count * sizeof(*array));
    983755                if (error)
    984756                        break;
    985                 semMtxLock(hmtx = semaptr->hmtx);
     757                __libc_Back_safesemMtxLock(hmtx = semaptr->hmtx);
    986758                if ((error = semvalid(semid, semaptr)) != 0)
    987759                        goto done2;
     
    992764                        goto raced;
    993765                }
    994                 if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, IPC_W)))
     766                if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, IPC_W)))
    995767                        goto done2;
    996768                for (i = 0; i < semaptr->u.sem_nsems; i++) {
     
    1005777                semundo_clear(semid, -1);
    1006778                SEMUNDO_UNLOCK();
    1007                 semEvWakeup(semaptr);
     779                __libc_Back_safesemEvWakeup(semaptr->hev);
    1008780                break;
    1009781
     
    1015787done2:
    1016788        if (hmtx)
    1017                 semMtxUnlock(hmtx);
     789                __libc_Back_safesemMtxUnlock(hmtx);
    1018790
    1019791        if (array != NULL)
     
    1046818        struct semid_kernel * sema = gpGlobals->sema;
    1047819
    1048         error = semMtxLock(gpGlobals->hmtx);
     820        error = __libc_Back_safesemMtxLock(gpGlobals->hmtx);
    1049821        if (error)
    1050822            LIBCLOG_RETURN_INT(error);
     
    1058830                if (semid < seminfo.semmni) {
    1059831                        DPRINTF(("found public key\n"));
    1060                         if ((error = __libc_spmCanIPC(&sema[semid].u.sem_perm,
     832                        if ((error = -__libc_spmCanIPC(&sema[semid].u.sem_perm,
    1061833                            semflg & 0700))) {
    1062834                                goto done2;
     
    1124896        errno = -IXSEQ_TO_IPCID(semid, sema[semid].u.sem_perm);
    1125897done2:
    1126         semMtxUnlock(gpGlobals->hmtx);
     898        __libc_Back_safesemMtxUnlock(gpGlobals->hmtx);
    1127899        LIBCLOG_RETURN_INT(-error);
    1128900}
    1129901
    1130 
    1131 static void __libc_Back_sysvSemSleepDone(int semid, struct semid_kernel *semaptr, uint16_t volatile *pu16)
     902/**
     903 * Args to __libc_Back_sysvSemSleepDone.
     904 */
     905struct SleepDoneArgs
    1132906{
     907    int semid;
     908    struct semid_kernel *semaptr;
     909    uint16_t volatile *pu16;
     910};
     911
     912/**
     913 * Wakeup function, called before signals are delivered.
     914 */
     915static void SleepDone(void *pvUser)
     916{
     917    struct SleepDoneArgs *pArgs = (struct SleepDoneArgs *)pvUser;
     918
    1133919    /*
    1134920     * Make sure that the semaphore still exists
    1135921     */
    1136     if ((semaptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
    1137         semaptr->u.sem_perm.seq != IPCID_TO_SEQ(semid)) {
     922    if ((pArgs->semaptr->u.sem_perm.mode & SEM_ALLOC) == 0 ||
     923        pArgs->semaptr->u.sem_perm.seq != IPCID_TO_SEQ(pArgs->semid)) {
    1138924            return;
    1139925    }
     
    1143929     * waiting processes.
    1144930     */
    1145     __atomic_decrement_u16(pu16);
     931    __atomic_decrement_u16(pArgs->pu16);
    1146932}
    1147933
     
    1199985
    1200986        semaptr = &sema[semid];
    1201         error = semMtxLock(semaptr->hmtx);
     987        error = __libc_Back_safesemMtxLock(semaptr->hmtx);
    1202988        if (error) {
    1203989                if (sops != small_sops)
     
    12321018        }
    12331019
    1234         if ((error = __libc_spmCanIPC(&semaptr->u.sem_perm, j))) {
     1020        if ((error = -__libc_spmCanIPC(&semaptr->u.sem_perm, j))) {
    12351021                DPRINTF(("error = %d from ipaccess\n", error));
    12361022                goto done2;
     
    13151101                }
    13161102
    1317                 uint16_t volatile *pu16 = sopptr->sem_op == 0 ? &semptr->semzcnt : &semptr->semncnt;
    1318                 __atomic_increment_u16(pu16);
     1103                struct SleepDoneArgs Args;
     1104                Args.semaptr = semaptr;
     1105                Args.semid = semid;
     1106                Args.pu16 = sopptr->sem_op == 0 ? &semptr->semzcnt : &semptr->semncnt;
     1107                __atomic_increment_u16(Args.pu16);
    13191108                DPRINTF(("semop:  good night (%d)!\n", sopptr->sem_op));
    1320                 error = -semEvSleep(semaptr, __libc_Back_sysvSemSleepDone, semid, pu16);
     1109                error = -__libc_Back_safesemEvSleep(semaptr->hev, semaptr->hmtx, SleepDone, &Args);
    13211110                DPRINTF(("semop:  good morning (error=%d)!\n", error));
    13221111                /* return code is checked below, after sem[nz]cnt-- */
     
    14131202        if (do_wakeup) {
    14141203                DPRINTF(("semop:  doing wakeup\n"));
    1415                 semEvWakeup(semaptr);
     1204                __libc_Back_safesemEvWakeup(semaptr->hev);
    14161205                DPRINTF(("semop:  back from wakeup\n"));
    14171206        }
    14181207        DPRINTF(("semop:  done\n"));
    14191208done2:
    1420         semMtxUnlock(semaptr->hmtx);
     1209        __libc_Back_safesemMtxUnlock(semaptr->hmtx);
    14211210        if (sops != small_sops)
    14221211                free(sops);
     
    14731262                        semaptr = &gpGlobals->sema[semid];
    14741263                        SEMUNDO_LOCK();
    1475                         semMtxUnlock(semaptr->hmtx);
     1264                        __libc_Back_safesemMtxUnlock(semaptr->hmtx);
    14761265                        if ((semaptr->u.sem_perm.mode & SEM_ALLOC) == 0) {
    14771266                                panic("semexit - semid not allocated");
     
    14991288                                semaptr->u.sem_base[semnum].semval += adjval;
    15001289
    1501                         semEvWakeup(semaptr);
     1290                        __libc_Back_safesemEvWakeup(semaptr->hev);
    15021291                        DPRINTF(("semexit:  back from wakeup\n"));
    15031292                    skip:
    1504                         semMtxUnlock(semaptr->hmtx);
     1293                        __libc_Back_safesemMtxUnlock(semaptr->hmtx);
    15051294                        SEMUNDO_UNLOCK();
    15061295                }
Note: See TracChangeset for help on using the changeset viewer.