|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * sem.c |
| 4 | + * Microsoft Windows Win32 Semaphores Emulation |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + *------------------------------------------------------------------------- |
| 9 | + */ |
| 10 | + |
| 11 | +#include"postgres.h" |
| 12 | +#include"storage/shmem.h" |
| 13 | + |
| 14 | +#include<errno.h> |
| 15 | + |
| 16 | +typedefstruct { |
| 17 | +intm_numSems; |
| 18 | +off_tm_semaphoreHandles;// offset from beginning of header |
| 19 | +off_tm_semaphoreCounts;// offset from beginning of header |
| 20 | +}win32_sem_set_hdr; |
| 21 | + |
| 22 | +/* Control of a semaphore pool. The pool is an area in which we stored all |
| 23 | +** the semIds of the pool. The first long is the number of semaphore |
| 24 | +** allocated in the pool followed by semaphore handles |
| 25 | +*/ |
| 26 | + |
| 27 | +int |
| 28 | +semctl(intsemId,intsemNum,intflag,unionsemunsemun) |
| 29 | +{ |
| 30 | +win32_sem_set_hdr*the_set= (win32_sem_set_hdr*)MAKE_PTR(semId); |
| 31 | + |
| 32 | +/* semNum might be 0 */ |
| 33 | +/* semun.array contains the sem initial values */ |
| 34 | +int*sem_counts= (int*)((off_t)the_set+the_set->m_semaphoreCounts); |
| 35 | + |
| 36 | +/* Fix the count of all sem of the pool to semun.array */ |
| 37 | +if (flag==SETALL) |
| 38 | +{ |
| 39 | +inti; |
| 40 | +structsembufsops; |
| 41 | +sops.sem_flg=IPC_NOWAIT; |
| 42 | + |
| 43 | +for (i=0;i<the_set->m_numSems;++i) { |
| 44 | +if (semun.array[i]==sem_counts[i]) |
| 45 | +continue;/* Nothing to do */ |
| 46 | + |
| 47 | +if (semun.array[i]<sem_counts[i]) |
| 48 | +sops.sem_op=-1; |
| 49 | +else |
| 50 | +sops.sem_op=1; |
| 51 | + |
| 52 | +sops.sem_num=i; |
| 53 | + |
| 54 | +/* Quickly lock/unlock the semaphore (if we can) */ |
| 55 | +if (semop(semId,&sops,1)<0) |
| 56 | +return-1; |
| 57 | +} |
| 58 | +return1; |
| 59 | +} |
| 60 | + |
| 61 | +/* Fix the count of one semaphore to semun.val */ |
| 62 | +elseif (flag==SETVAL) |
| 63 | +{ |
| 64 | +if (semun.val!=sem_counts[semNum]) { |
| 65 | +structsembufsops; |
| 66 | +sops.sem_flg=IPC_NOWAIT; |
| 67 | +sops.sem_num=semNum; |
| 68 | + |
| 69 | +if (semun.val<sem_counts[semNum]) |
| 70 | +sops.sem_op=-1; |
| 71 | +else |
| 72 | +sops.sem_op=1; |
| 73 | + |
| 74 | +/* Quickly lock/unlock the semaphore (if we can) */ |
| 75 | +if (semop(semId,&sops,1)<0) |
| 76 | +return-1; |
| 77 | +} |
| 78 | + |
| 79 | +return1; |
| 80 | +} |
| 81 | + |
| 82 | +/* Delete the pool */ |
| 83 | +elseif (flag==IPC_RMID) |
| 84 | +{ |
| 85 | +inti; |
| 86 | +HANDLE*sem_handles= (HANDLE*)((off_t)the_set+the_set->m_semaphoreHandles); |
| 87 | + |
| 88 | +/* Loop over all semaphore to delete them */ |
| 89 | +for (i=0;i<the_set->m_numSems;++i) |
| 90 | +CloseHandle(sem_handles[i]); |
| 91 | + |
| 92 | +return1; |
| 93 | +} |
| 94 | + |
| 95 | +/* Get the current semaphore count */ |
| 96 | +elseif (flag==GETNCNT) |
| 97 | +{ |
| 98 | +returnthe_set->m_numSems; |
| 99 | +} |
| 100 | + |
| 101 | +/* Get the current semaphore count of the first semaphore in the pool */ |
| 102 | +elseif (flag==GETVAL) |
| 103 | +{ |
| 104 | +returnsem_counts[semNum]; |
| 105 | +} |
| 106 | + |
| 107 | +/* Other commands not yet supported */ |
| 108 | +else |
| 109 | +{ |
| 110 | +errno=EINVAL; |
| 111 | +return-1; |
| 112 | +} |
| 113 | +} |
| 114 | + |
| 115 | +/* Find a pool id based on IPC key */ |
| 116 | +int |
| 117 | +semget(intsemKey,intsemNum,intflags) |
| 118 | +{ |
| 119 | +charsemname[32]; |
| 120 | +charcur_num[20]; |
| 121 | +DWORDlast_error; |
| 122 | +char*num_part; |
| 123 | +boolans= true; |
| 124 | +SECURITY_ATTRIBUTESsec_attrs; |
| 125 | +HANDLEcur_handle; |
| 126 | +boolfound= false; |
| 127 | +Sizesem_set_size=sizeof(win32_sem_set_hdr)+semNum* (sizeof(HANDLE)+sizeof(int)); |
| 128 | +HANDLE*sem_handles=NULL; |
| 129 | +int*sem_counts=NULL; |
| 130 | + |
| 131 | +sec_attrs.nLength=sizeof(sec_attrs); |
| 132 | +sec_attrs.lpSecurityDescriptor=NULL; |
| 133 | +sec_attrs.bInheritHandle= TRUE; |
| 134 | + |
| 135 | +sprintf(semname,"PG_SEMSET.%d.",semKey); |
| 136 | +num_part=semname+strlen(semname); |
| 137 | + |
| 138 | +strcpy(num_part,_itoa(_getpid()*-1,cur_num,10));/* For shared memory, include the pid */ |
| 139 | +win32_sem_set_hdr*new_set= (win32_sem_set_hdr*)ShmemInitStruct(semname,sem_set_size,&found); |
| 140 | + |
| 141 | +if (found) { |
| 142 | +/* This should *never* happen */ |
| 143 | +errno=EEXIST; |
| 144 | +return-1; |
| 145 | +} |
| 146 | + |
| 147 | +new_set->m_numSems=semNum; |
| 148 | +new_set->m_semaphoreHandles=sizeof(win32_sem_set_hdr);// array starts after header |
| 149 | +new_set->m_semaphoreCounts=new_set->m_semaphoreHandles+ (sizeof(HANDLE)*semNum); |
| 150 | + |
| 151 | +sem_handles= (HANDLE*)((off_t)new_set+new_set->m_semaphoreHandles); |
| 152 | +sem_counts= (int*)((off_t)new_set+new_set->m_semaphoreCounts); |
| 153 | + |
| 154 | +for (inti=0;i<semNum&&ans;++i) { |
| 155 | +strcpy(num_part,_itoa(i,cur_num,10)); |
| 156 | + |
| 157 | +if (flags&IPC_CREAT) |
| 158 | +cur_handle=CreateSemaphore(&sec_attrs,0,1,semname); |
| 159 | +else |
| 160 | +cur_handle=OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE,semname); |
| 161 | + |
| 162 | +sem_handles[i]=cur_handle; |
| 163 | + |
| 164 | +last_error=GetLastError(); |
| 165 | +if (!cur_handle) |
| 166 | +{ |
| 167 | +errno=EACCES; |
| 168 | +ans= false; |
| 169 | +} |
| 170 | +elseif (last_error==ERROR_ALREADY_EXISTS&& (flags& (IPC_CREAT |IPC_EXCL))) |
| 171 | +{ |
| 172 | +errno=EEXIST; |
| 173 | +ans= false; |
| 174 | +} |
| 175 | +} |
| 176 | + |
| 177 | +if (ans) { |
| 178 | +returnMAKE_OFFSET(new_set); |
| 179 | +}else { |
| 180 | +// Blow away what we've got right now... |
| 181 | +for (inti=0;i<semNum;++i) { |
| 182 | +if (sem_handles[i]) |
| 183 | +CloseHandle(sem_handles[i]); |
| 184 | +else |
| 185 | +break; |
| 186 | +} |
| 187 | + |
| 188 | +return-1; |
| 189 | +} |
| 190 | +} |
| 191 | + |
| 192 | +/* Acquire or release in the semaphore pool */ |
| 193 | +int |
| 194 | +semop(intsemId,structsembuf*sops,intnsops) |
| 195 | +{ |
| 196 | +win32_sem_set_hdr*the_set= (win32_sem_set_hdr*)MAKE_PTR(semId); |
| 197 | +HANDLE*sem_handles= (HANDLE*)((off_t)the_set+the_set->m_semaphoreHandles); |
| 198 | +int*sem_counts= (int*)((off_t)the_set+the_set->m_semaphoreCounts); |
| 199 | +HANDLEcur_handle; |
| 200 | + |
| 201 | +if (nsops!=1) { |
| 202 | +/* Not supported (we return on 1st success, and don't cancel earlier ops) */ |
| 203 | +errno=E2BIG; |
| 204 | +return-1; |
| 205 | +} |
| 206 | + |
| 207 | +cur_handle=sem_handles[sops[0].sem_num]; |
| 208 | + |
| 209 | +if (sops[0].sem_op==-1) |
| 210 | +{ |
| 211 | +DWORDret; |
| 212 | +if (sops[0].sem_flg&IPC_NOWAIT) |
| 213 | +ret=WaitForSingleObject(cur_handle,0); |
| 214 | +else |
| 215 | +ret=WaitForSingleObject(cur_handle,INFINITE); |
| 216 | + |
| 217 | +if (ret==WAIT_OBJECT_0) { |
| 218 | +/* We got it! */ |
| 219 | +sem_counts[sops[0].sem_num]--; |
| 220 | +return0; |
| 221 | +}elseif (ret==WAIT_TIMEOUT) |
| 222 | +/* Couldn't get it */ |
| 223 | +errno=EAGAIN; |
| 224 | +else |
| 225 | +errno=EIDRM; |
| 226 | +} |
| 227 | +elseif (sops[0].sem_op>0) { |
| 228 | +/* Don't want the lock anymore */ |
| 229 | +sem_counts[sops[0].sem_num]++; |
| 230 | +ReleaseSemaphore(cur_handle,sops[0].sem_op,NULL); |
| 231 | +return0; |
| 232 | +} |
| 233 | +else |
| 234 | +/* Not supported */ |
| 235 | +errno=ERANGE; |
| 236 | + |
| 237 | +/* If we get down here, then something is wrong */ |
| 238 | +return-1; |
| 239 | +} |