flat assembler
Message board for the users of flat assembler.
![]() Goto page 1, 2 Next |
Author |
|
shoorick
OS? events, mutexes, criticalsections...
|
|||
![]() |
|
sinsi
Isn't there an opcode (prefix?) hint for a spinlock? Depends on what OS you are using I guess...
|
|||
![]() |
|
revolution
tthsqe: Do you want thread synchronization or thread communication or both. Because they are entirely different things. To synchronise means to ensure order. To communicate means to pass information. You can have one or the other or both, it depends upon your application.
It would help if you gave more information on what you are trying to do otherwise any advice given here will likely be not what you are looking for. And remember to say which OS you are intending to target, that makes a big difference. |
|||
![]() |
|
tthsqe
Oops, I don't think I meant a CREW machine... anyways, the following situation shoudl suffice:
I have a global variable n=0. When each thread executes n++, I would want it to behave as expected, that is n reflects the total number of times the statement was excuted - so no side effects caused by two threads reading the same value of n at the same time then both writing back n+1 to n when it really should be n+2. Also, windows. |
|||
![]() |
|
MHajduk
Perhaps you may just use semaphores here? It could be realized with such atomic assembler instructions as XADD and CMPXCHG (check Intel manuals and other resources for proper examples
![]() |
|||
![]() |
|
baldr
MHajduk,
Why not lock inc? Both xadd and cmpxchg look like an overkill (in this particular case). __________ tthsqe, Win32's way to do it is InterlockedIncrement() (XP SP2 implements it via xadd ![]() |
|||
![]() |
|
revolution
Note that both xadd and cmpxchg need a lock prefix to make them atomic.
Further note that not all processors support xadd or cmpxchg. Also note that lock inc is fine if you don't care about the previous value. Lastly note that any instruction that involves a lock prefix will seriously degrade performance. For variables that need to be updated frequently this may be a consideration. |
|||
![]() |
|
LocoDelAssembly
Quote: Further note that not all processors support xadd or cmpxchg. |
|||
![]() |
|
baldr
revolution,
To be atomic, it have to employ #LOCK protocol. |
|||
![]() |
|
revolution
baldr wrote: revolution, ![]() revolution wrote: Note that both xadd and cmpxchg need a lock prefix to make them atomic. |
|||
![]() |
|
revolution
LocoDelAssembly wrote:
|
|||
![]() |
|
baldr
revolution,
Not an echo, just repercussion. ![]() My comment was about performance degradation: #LOCK is a must for them to be atomic. |
|||
![]() |
|
tthsqe
How about if the updating function is not a single instruction like inc that can be used with lock? I was thinking of something like:
Code: start: t = [CT] x = [N] update x if t = [CT] ; if [N] has not been updated in between [N] = x [CT]++ else goto start endif here, N, and CT = 0 are global; x and t are local. But I'm not sure exactly sure what instructions need to be made atomic or how to do this. |
|||
![]() |
|
baldr
tthsqe,
That's what synchronization objects are for. Look up critical section in MSDN. |
|||
![]() |
|
revolution
tthsqe: You need a proper spinlock to do atomically.
In the above pseudo-code: between 'if t=[CT]' and '[N]=x' you have a period of vulnerability. It may help you think about what would happen if two CPUs are in perfect lock-step and see what would happen to the memory values. |
|||
![]() |
|
tthsqe
Ok, I'll look into that more. How about a simple tast synchronization - making sure all thread finish a task before any thread starts the next. I was think of something like this:
Code: ThreadStart: ; start with ct1 = 0 task1 mov [ct2],0 lock inc [ct1] @@: cmp [ct1],[ThreadCount] jl @b task2 mov [ct3],0 lock inc [ct2] @@: cmp [ct2],[ThreadCount] jl @b task3 mov [ct1],0 lock inc [ct3] @@: cmp [ct3],[ThreadCount] jl @b ... It seems that we need to cycle through three counters to avoid any possible side effects. |
|||
![]() |
|
revolution
tthsqe: I think you are trying to reinvent the wheel.
![]() Perhaps you would like to read up about WaitForMultipleObjects and CreateSemaphore. Don't try to fight the OS, use it for what it was designed for. |
|||
![]() |
|
revolution
tthsqe wrote: How about if the updating function is not a single instruction like inc that can be used with lock? I was thinking of something like: ¹ Using xchg has an implicit lock, so that also won't allow you to avoid the bus lock. |
|||
![]() |
|
hopcode
perhaps useful
http://www.azillionmonkeys.com/qed/asmexample.html browse to para 15 .63 bit atomic counter. Cheerz, ![]() hopcode |
|||
![]() |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.