Changeset 1829
- Timestamp:
- Mar 13, 2005, 11:48:31 AM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/emx/src/lib/sys/sharedpm.c
-
Property cvs2svn:cvs-rev
changed from
1.25
to1.26
r1828 r1829 5 5 * 6 6 * 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> 8 8 * 9 9 * … … 129 129 static __LIBC_PSPMSIGNAL spmAllocSignal(void); 130 130 static void spmFreeSignal(__LIBC_PSPMSIGNAL pSig); 131 static int spmSocketAllocProcess(void); 131 132 static void *spmAlloc(size_t cbSize); 132 133 static void *spmAllocSub(size_t cbSize); … … 1135 1136 if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets) 1136 1137 { 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 } 1142 1152 } 1143 1153 else … … 1154 1164 * 1155 1165 * @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. 1156 1168 * @returns Negative error code (errno.h) on failure. 1157 1169 * @param iSocket socket to reference. … … 1163 1175 if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets) 1164 1176 { 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 } 1174 1202 } 1175 1203 } … … 1187 1215 * 1188 1216 * @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. 1190 1219 * @returns Negative error code (errno.h) on failure. 1191 1220 * @param iSocket Socket to dereference. … … 1197 1226 if (iSocket >= 0 && iSocket < gpSPMHdr->pTcpip->cSockets) 1198 1227 { 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 } 1208 1253 } 1209 1254 } … … 1885 1930 LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, cReferences) == 12); 1886 1931 LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pvForkHandle) == 140); 1932 LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pacTcpipRefs) == 180); 1887 1933 LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, cPoolPointers) == 224); 1888 1934 LIBC_ASSERT(offsetof(__LIBC_SPMPROCESS, pInheritLocked) == 232); … … 2906 2952 2907 2953 /** 2954 * Allocates the per process socket reference map. 2955 * 2956 * @returns 0 on success. 2957 * @returns Negative errno on failure. 2958 */ 2959 static 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 /** 2908 2997 * Allocate memory in SPM pool of given size. 2909 2998 * -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.