Page 1 of 1

Client/Server + Mutex

Posted: Sat Apr 16, 2005 12:21 pm
by eriansa
Server :

Code: Select all

Structure RWMemory
  mutex.l
  Transport.b
  SampleRate.l
  DevSamplePos.l
  PanSamplePos.l
  DevBufferPos.l
  PanBufferPos.l
  SamplesToRender.l
EndStructure

OpenConsole()     
*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4",SizeOf(RWMemory))
If *myRWMemory
  *myRWMemory\mutex = CreateMutex_(0,1,"SQ4_Rewire")
  WaitForSingleObject_(*myRWMemory\mutex,10000) ; Lock it
  CommandLine.s =  "c:\rewiresdk\sq4\client.exe"
  StartupInfo.STARTUPINFO 
  StartupInfo\cb = SizeOf(STARTUPINFO) 
  StartupInfo\dwFlags = #STARTF_USESHOWWINDOW 
  StartupInfo\wShowWindow = #SW_SHOW
  ProcessInfo.PROCESS_INFORMATION 
  CreateProcess_(0, @CommandLine, 0, 0, 0, 0, 0, 0, @StartupInfo, @ProcessInfo)
EndIf
PrintN("Press return to release mutex and exit") 
Input() 
CloseConsole() 
CloseHandle_(*myRWMemory\mutex)
FreeMemoryGlobal(*myRWMemory)

Client :

Code: Select all

Structure RWMemory
  mutex.l
  Transport.b
  SampleRate.l
  DevSamplePos.l
  PanSamplePos.l
  DevBufferPos.l
  PanBufferPos.l
  SamplesToRender.l
EndStructure

OpenConsole()
*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4",SizeOf(RWMemory))
If *myRWMemory\mutex
  PrintN("Waiting...") 
  WaitForSingleObject_(*myRWMemory\mutex,#INFINITE) ; Lock it
Else
  PrintN("No mutex to wait for...")
EndIf
PrintN("Press return to exit") 
Input() 
CloseConsole() 
Problem :
Client is NOT waiting for mutex...
Someone any idea?

Thx.

Posted: Sat Apr 16, 2005 12:42 pm
by El_Choni
This is what I would do. I think you need to open the mutex, having only the handle value won't work.

Code: Select all

*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4", SizeOf(RWMemory))
hMutex = OpenMutex_(#MUTEX_ALL_ACCESS, #TRUE, "SQ4_Rewire")
If hMutex=*myRWMemory\mutex
  PrintN("Waiting...")
  While WaitForSingleObject_(hMutex, #INFINITE)<>#WAIT_OBJECT_0:Delay(10):Wend
Else
  PrintN("No mutex to wait for...")
EndIf

Posted: Sat Apr 16, 2005 2:01 pm
by eriansa
El_Choni wrote:This is what I would do. I think you need to open the mutex, having only the handle value won't work.

Code: Select all

*myRWMemory.RWMemory = AllocateMemoryGlobal("SQ4", SizeOf(RWMemory))
hMutex = OpenMutex_(#MUTEX_ALL_ACCESS, #TRUE, "SQ4_Rewire")
If hMutex=*myRWMemory\mutex
  PrintN("Waiting...")
  While WaitForSingleObject_(hMutex, #INFINITE)<>#WAIT_OBJECT_0:Delay(10):Wend
Else
  PrintN("No mutex to wait for...")
EndIf
I understand, but what's the use of having a [While Wend] loop when WaitForSingleObject_ is waiting #INFINITE ?

Posted: Sat Apr 16, 2005 2:31 pm
by El_Choni
ur right, sry,took the code from a snippet which didn't wait for infinite.

Posted: Mon Jun 04, 2007 10:57 pm
by blueznl
What is 'AllocateMemoryGlobal'? Besides old :-)

Also, what is the value of '#MUTEX_ALL_ACCESS'?

Posted: Tue Jun 05, 2007 9:36 am
by JCV

Code: Select all

Procedure AllocateMemoryGlobal(Name.s,Size.l)
  Shared HandleMap.l
  HandleMap.l = CreateFileMapping_($FFFFFFFF, 0, 402653188, 0, Size, @Name)
  If HandleMap
    ProcedureReturn MapViewOfFile_(HandleMap, 983071, 0, 0 ,0)
  EndIf
EndProcedure

Procedure FreeMemoryGlobal(MemoryAddress.l)
  Shared HandleMap.l
  UnmapViewOfFile_(MemoryAddress)
  CloseHandle_(HandleMap)
EndProcedure
#MUTEX_ALL_ACCESS = $1F0001 from http://www.purebasic.fr/english/viewtopic.php?t=27439