Page 1 of 1

Selfmade Critical Sections

Posted: Thu Apr 28, 2005 2:54 pm
by h00k-m4st3r
Yeah, I know there are lots of APIs, but they are all ugly, slow and require (of course) the windows dlls. Cause someone told me, it would be impossible to find an other way to manage "Critical Sections", I wrote this code to show him the opposite.


What are Critical Sections

They are useful, when you do much memory access.. When you (for example) change a string from multiple processes, your program will crash when you dont use it.


How it works

- In the Enter-procedure every thread receives an unique number, this is guaranteed by the XADD command, which reads and modifies the variable in one call.

- Then the Thread waits, until its his turn

- When the Thread finished, we increase the current Thread-Index, so that the next threads enters the section

Code: Select all

;- 
;- PureFan's Critical Sections *SPECIAL* 
;- 
;- Posted in: English PureBasic Board 
;- Date:      28.04.05 
;- 

Structure Pure_CS 
  In.l 
  Out.l 
EndStructure 

Procedure Pure_InitCS(*PCS.Pure_CS) 
  *PCS\In=0 
  *PCS\Out=0 
EndProcedure 

Procedure Pure_EnterCS(*PCS.Pure_CS) 
  !POP Edi
  !PUSH Edi 
  !XOR Esi,Esi 
  !INC Esi            
  !XADD [Edi],Esi
  
  !.loop: 
  !PUSH dword 1 
  !CALL _Sleep@4
  
  !CMP [Edi+4],Esi
  !JNE .loop 
EndProcedure 
  
Procedure Pure_LeaveCS(*PCS.Pure_CS) 
  !POP Edi 
  !PUSH Edi 
  !INC dword[Edi+4]
EndProcedure 


;/ 
;/ Example for PureFan's Critical Sections 
;/ 

Global SichereStrings.Pure_CS 
Pure_InitCS(SichereStrings) 

Global GlobalerString.s 

Procedure String_Thread(Parameter) 
  Repeat 
    Pure_EnterCS(SichereStrings) 
    
    lokaler_string.s=GlobalerString+"hallo" 
    GlobalerString.s=Right(lokaler_string.s,100) 
    
    Pure_LeaveCS(SichereStrings) 
  ForEver 
EndProcedure 

For I=1 To 10 
  CreateThread(@String_Thread(),I) 
Next I 

Repeat 
  Pure_EnterCS(SichereStrings) 
  
  lokaler_string.s=GlobalerString+"hallo" 
  GlobalerString.s=Right(lokaler_string.s,100) 
  
  Pure_LeaveCS(SichereStrings) 
ForEver 

EDIT: Just published another way to do this in the german forum.. here it is!

Advantage:
* Works also with Multiprocessor-Systems
* You can kill threads with KillThread()/TerminateThread_(), when the commands are inside a CriticalSection! The first code would make problems in some cases!

Code: Select all

;- 
;- PureFan's Critical Sections *SPECIAL* 
;- 
;- Posted in: English PureBasic Board 
;- Date:      30.04.05 
;- 

Structure Pure_CS 
  Cnt.l 
EndStructure 

Procedure Pure_InitCS(*PCS.Pure_CS) 
  *PCS\Cnt=0 
EndProcedure 

Procedure Pure_EnterCS(*PCS.Pure_CS) 
  !POP Edi
  !PUSH Edi 
  
  !.loop: 
    !PUSH dword 1 
    !CALL _Sleep@4 
    
    !MOV Esi,1 
    !XCHG [Edi],Esi
    
  !CMP Esi,0
  !JNE .loop
EndProcedure 
  
Procedure Pure_LeaveCS(*PCS.Pure_CS) 
  !POP Edi
  !PUSH Edi 
  !MOV dword [Edi],0 
EndProcedure 


;/ 
;/ Example for PureFan's Critical Sections 
;/ 

Global SichereStrings.Pure_CS 
Pure_InitCS(SichereStrings) 

Global GlobalerString.s 

Procedure String_Thread(Parameter) 
  Repeat 
    Pure_EnterCS(SichereStrings) 

    lokaler_string.s=GlobalerString+"hallo" 
    GlobalerString.s=Right(lokaler_string.s,100) 

    Pure_LeaveCS(SichereStrings) 
  ForEver 
EndProcedure 

For I=1 To 10 
  CreateThread(@String_Thread(),I) 
Next I 

Repeat 
  Pure_EnterCS(SichereStrings) 

  lokaler_string.s=GlobalerString+"hallo" 
  GlobalerString.s=Right(lokaler_string.s,100) 

  Pure_LeaveCS(SichereStrings) 
ForEver


Here also the link to the thread in the german forum: http://forums.purebasic.com/german/viewtopic.php?t=3061

Re: Selfmade Critical Sections

Posted: Thu Apr 28, 2005 3:06 pm
by traumatic
h00k-m4st3r! wh47 4 c00l n4m3! :lol:

SCNR...


...cool code anyway! :)

Posted: Sat Apr 30, 2005 12:31 pm
by h00k-m4st3r
thx ;) Just published a second method! (see above)

Have Fun!