RichAlgeni wrote:
Thank you Idle for your time and efforts with this. It is truly appreciated!
I'm not sure I explained myself clearly enough as to what I need this for. I have an IIS Extension dll process running inside, of course, Internet Information Server, the web server from Microsoft. When the Extension initially loads, the
AttachProcess procedure creates a fixed number of client socket connections to a separate server process. The number of connections is limited to the
numberConnections variable. As the sockets connect, they are set to an initial available state, by setting
availabConnections(useThisNumber) = 0. As browsers connect to the IIS server, and request data, a thread will check for an available socket. If the socket is available, it will grab the socket, and request data. Basically, it looks like this:
Code:
; AttachProcess...
Global useConnectLock.i = CreateMutex()
; thread procedure...
; now lock the mutex, and get the next socket to use
Repeat
attemptCount + 1
LockMutex(useConnectLock)
nextSocketToUse + 1
useThisNumber = nextSocketToUse
UnlockMutex(useConnectLock)
useThisNumber = Mod(useThisNumber, numberConnections)
If availabConnections(useThisNumber) = 0
socketToUse = mappingConnections(useThisNumber)
If socketToUse > 0
availabConnections(useThisNumber) = 1
Break
EndIf
Else
Delay(10 * attemptCount)
EndIf
ForEver
; send request to server, read in returned data
; now release the socket we used
availabConnections(useThisNumber) = 0
From what I have read, using a mutex for this is like using a sledgehammer to kill a mosquito. There must be a lightweight way to lock access to a global integer or address, increment it, assign the value to local variable, then release the lock. My concern is that the local variable must be assigned, before the lock is released, to keep from crashing by two threads attempting to use a single socket.
I don't know what the best option is
PB mutex is a critical section so it's probably not that bad.
but if you have a thread pool maybe semaphores are a better option to use so the thread can sleep until a connection request is made
as for a spinlock it might be ok in this case but It depends on how fast it can be resolved as it'll just eat cycles
but if you just need an atomic inc and set maybe this will work
Code:
Macro _gLockINCXCHG(var,var1)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
lock inc dword [v_#var1]
lock xchg eax, [v_#var1]
mov var , eax
CompilerElse
lock inc qword [v_#var1]
lock xchg rax, [v_#var1]
mov var , rax
CompilerEndIf
EndMacro
Global nextSocketToUse=1
Procedure attach()
Protected useThisNumber
_gLockINCXCHG(useThisNumber,nextSocketToUse)
Debug useThisNumber
EndProcedure
attach()
@davido
it'll be wanting me to specify the sizes as either a qword or dword
maybe wilbert can take a look since I don't have osx