PDA

View Full Version : help! atomic assembly needed on x86 for win32 emulation


angoyal
10-06-2004, 12:25 AM
sorry to ask this folks, but are dependent on InterlockedIncrement() returning a value.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/interlockedincrement.asp

Unfortunately, linux offers some calls in <asm/atomic.h> that are close, but do not quite meet our needs.

we wanted to use this: 93 static __inline__ void atomic_inc(atomic_t *v)
94 {
95 __asm__ __volatile__(
96 LOCK "incl %0"
97 :"=m" (v->counter)
98 :"m" (v->counter));
99 }
but as you can see, atomic_inc does not return a value (only void).

we looked at other functions in this file, but none of them do exactly what InterlockedIncrement does.

Can someone point me in the right direction?

Nope
10-06-2004, 08:03 AM
What return value do you need? Do you use the inc as part of a larger
asm section or on its own? I mean as long as the inc is atomic, couldn't
you just write the few additional lines that create the return value you
need?

Just as something to think about. I am not into asm these days anymore.

RobSeace
10-06-2004, 12:50 PM
Personally, I'd just write my own using a mutex to guarantee
exclusive access to the variable... Eg:


static int counter;
static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;

int adj_counter (int adjval)
{
int ret, err;
err = pthread_mutex_lock (&counter_mutex);
if (err) /* die, or fail, or whatever... */
counter += adjval;
ret = counter;
pthread_mutex_unlock (&counter_mutex);
return (ret);
}


I coded it to take an adjustment value, just to make it more
general-purpose than a simple incrementer... This way, you
can increment by more than 1 at a time, or decrement by passing
negative, or simply read the current value by passing 0... Of
course, you could make it more general-purpose still by having
it take as args a pointer to the counter to be adjusted, and to the
mutex to be locked for it, and then make every caller pass in
those values... But, if you're just dealing with one specific counter
in your code that you need exclusive access to like this, then the
code will be cleaner without every caller having to even know
about the mutex, and just let this one function worry about it...
But, if you need to secure exclusive access to more than one
variable, you'll need to switch to the more general approach (or,
just have multiple such functions, one for each variable)...