Changeset 1829


Ignore:
Timestamp:
Mar 13, 2005, 11:48:31 AM (20 years ago)
Author:
bird
Message:

Socket duplication.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/lib/sys/sharedpm.c

    • Property cvs2svn:cvs-rev changed from 1.25 to 1.26
    r1828 r1829  
    55 *
    66 * Copyright (c) 2004 nickk
    7  * Copyright (c) 2004 knut st. osmundsen <bird-srcspam@anduin.net>
     7 * Copyright (c) 2004-2005 knut st. osmundsen <bird-srcspam@anduin.net>
    88 *
    99 *
     
    129129static __LIBC_PSPMSIGNAL spmAllocSignal(void);
    130130static void spmFreeSignal(__LIBC_PSPMSIGNAL pSig);
     131static int spmSocketAllocProcess(void);
    131132static void *spmAlloc(size_t cbSize);
    132133static void *spmAllocSub(size_t cbSize);
     
    11351136    if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets)
    11361137    {
    1137         unsigned    uOldRefs;
    1138         uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
    1139         uOldRefs = __atomic_xchg_word(pu16, 1);
    1140         LIBC_ASSERTM(uOldRefs == 0, "Previous iSocket=%d had %#x refs left.\n", iSocket, uOldRefs);
    1141         rc = 0;
     1138        rc = 0; /* shut up! */
     1139        if (!gpSPMSelf->pacTcpipRefs)
     1140            rc = spmSocketAllocProcess();
     1141        if (gpSPMSelf->pacTcpipRefs)
     1142        {
     1143            unsigned    uOldRefs;
     1144            uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
     1145            uOldRefs = __atomic_xchg_word(pu16, 1);
     1146            LIBC_ASSERTM(uOldRefs == 0, "Previous iSocket=%d had %#x refs left.\n", iSocket, uOldRefs);
     1147            pu16 = &gpSPMSelf->pacTcpipRefs[iSocket];
     1148            uOldRefs = __atomic_xchg_word(pu16, 1);
     1149            LIBC_ASSERTM(uOldRefs == 0, "Previous iSocket=%d had %#x refs left in the current process.\n", iSocket, uOldRefs);
     1150            rc = 0;
     1151        }
    11421152    }
    11431153    else
     
    11541164 *
    11551165 * @returns The new reference count.
     1166 *          The low 16-bits are are the global count.
     1167 *          The high 15-bits are are the process count.
    11561168 * @returns Negative error code (errno.h) on failure.
    11571169 * @param   iSocket     socket to reference.
     
    11631175    if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets)
    11641176    {
    1165         unsigned    uRefs;
    1166         uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
    1167         uRefs = __atomic_increment_word_max(pu16, 0xfff0);
    1168         if (!(uRefs & 0xffff0000))
    1169             rc = uRefs;
    1170         else
    1171         {
    1172             LIBC_ASSERTM_FAILED("iSocket=%d is referenced too many times! uRefs=%#x\n", iSocket, uRefs);
    1173             rc = -EBADF;
     1177        rc = 0; /* shut up! */
     1178        if (!gpSPMSelf->pacTcpipRefs)
     1179            rc = spmSocketAllocProcess();
     1180        if (gpSPMSelf->pacTcpipRefs)
     1181        {
     1182            uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
     1183            unsigned uRefs = __atomic_increment_word_max(pu16, 0x777f);
     1184            if (!(uRefs & 0xffff0000))
     1185            {
     1186                pu16 = &gpSPMSelf->pacTcpipRefs[iSocket];
     1187                unsigned uRefsProc = __atomic_increment_word_max(pu16, 0x777f);
     1188                if (!(uRefs & 0xffff0000))
     1189                    rc = (uRefsProc << 16) | uRefs;
     1190                else
     1191                {
     1192                    LIBC_ASSERTM_FAILED("iSocket=%d is referenced too many times! uRefs=%#x\n", iSocket, uRefs);
     1193                    *pu16 = 1;
     1194                    rc = (1 << 16) | uRefs;
     1195                }
     1196            }
     1197            else
     1198            {
     1199                LIBC_ASSERTM_FAILED("iSocket=%d is referenced too many times! uRefs=%#x\n", iSocket, uRefs);
     1200                rc = -EBADF;
     1201            }
    11741202        }
    11751203    }
     
    11871215 *
    11881216 * @returns The new reference count.
    1189  *          The caller must close the socket if 0 is returned.
     1217 *          The low 16-bits are are the global count.
     1218 *          The high 15-bits are are the process count.
    11901219 * @returns Negative error code (errno.h) on failure.
    11911220 * @param   iSocket     Socket to dereference.
     
    11971226    if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets)
    11981227    {
    1199         unsigned    uRefs;
    1200         uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
    1201         uRefs = __atomic_decrement_word_min(pu16, 0);
    1202         if (!(uRefs & 0xffff0000))
    1203             rc = uRefs;
    1204         else
    1205         {
    1206             LIBC_ASSERTM_FAILED("iSocket=%d already 0! (uRefs=%#x)\n", iSocket, uRefs);
    1207             rc = -EBADF;
     1228        rc = 0; /* shut up! */
     1229        if (!gpSPMSelf->pacTcpipRefs)
     1230            rc = spmSocketAllocProcess();
     1231        if (gpSPMSelf->pacTcpipRefs)
     1232        {
     1233            uint16_t   *pu16 = &gpSPMHdr->pTcpip->acRefs[iSocket];
     1234            unsigned uRefs = __atomic_decrement_word_min(pu16, 0);
     1235            if (!(uRefs & 0xffff0000))
     1236            {
     1237                pu16 = &gpSPMSelf->pacTcpipRefs[iSocket];
     1238                unsigned uRefsProc = __atomic_decrement_word_min(pu16, 0);
     1239                if (!(uRefs & 0xffff0000))
     1240                    rc = (uRefsProc << 16) | uRefs;
     1241                else
     1242                {
     1243                    LIBC_ASSERTM_FAILED("iSocket=%d already 0 for process! (uRefsProc=%#x)\n", iSocket, uRefsProc);
     1244                    *pu16 = 0;
     1245                    rc = (0 << 16) | uRefs;
     1246                }
     1247            }
     1248            else
     1249            {
     1250                LIBC_ASSERTM_FAILED("iSocket=%d already 0! (uRefs=%#x)\n", iSocket, uRefs);
     1251                rc = -EBADF;
     1252            }
    12081253        }
    12091254    }
     
    18851930    LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, cReferences) == 12);
    18861931    LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pvForkHandle) == 140);
     1932    LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pacTcpipRefs) == 180);
    18871933    LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, cPoolPointers) == 224);
    18881934    LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pInheritLocked) == 232);
     
    29062952
    29072953/**
     2954 * Allocates the per process socket reference map.
     2955 *
     2956 * @returns 0 on success.
     2957 * @returns Negative errno on failure.
     2958 */
     2959static int spmSocketAllocProcess(void)
     2960{
     2961    /*
     2962     * Obtain the mutex.
     2963     */
     2964    __LIBC_SPMXCPTREGREC    RegRec;
     2965    int rc = spmRequestMutex(&RegRec);
     2966    if (!rc)
     2967    {
     2968        /*
     2969         * Before allocating the reference map, check if someone got here before us.
     2970         */
     2971        if (!gpSPMSelf->pacTcpipRefs)
     2972        {
     2973            FS_VAR_SAVE_LOAD();
     2974            PVOID pv = NULL;
     2975            rc = DosAllocMem(&pv, 0x10000 * sizeof(uint16_t), PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY);
     2976            if (rc) /* and once again for the pre fp13 kernels. */
     2977                rc = DosAllocMem(&pv, 0x10000 * sizeof(uint16_t), PAG_READ | PAG_WRITE | PAG_COMMIT);
     2978            FS_RESTORE();
     2979            if (!rc)
     2980            {
     2981                /*
     2982                 * Update the self structure.
     2983                 */
     2984                gpSPMSelf->pacTcpipRefs = (uint16_t *)pv;
     2985            }
     2986            else
     2987                rc = -ENOMEM;
     2988        }
     2989        spmReleaseMutex(&RegRec);
     2990    }
     2991
     2992    return rc;
     2993}
     2994
     2995
     2996/**
    29082997 * Allocate memory in SPM pool of given size.
    29092998 *
Note: See TracChangeset for help on using the changeset viewer.