atomic

How do you implement Software Transactional Memory?

In terms of actual low level atomic instructions and memory fences (I assume they're used), how do you implement STM? The part that's mysterious to me is that given some arbitrary chunk of code, you need a way to go back afterward and determine if the values used in each step were valid. How do you do that, and how do you do it efficient...

Alternatives to GCC's new atomic integer operations...

GCC's recent support for atomic operations (as described here) is great, and is 90% of what we need. Unfortunately, some of our products still need to run on Windows and so we need atomic integer operations for Windows as well. In the past, we had custom assembly language implementations for all our platforms, but I'd like move all the...

Are "benaphores" worth implementing on modern OS's?

Hi all, Back in my days as a BeOS programmer, I read this article by Benoit Schillings, describing how to create a "benaphore": a method of using atomic variable to enforce a critical section that avoids the need acquire/release a mutex in the common (no-contention) case. I thought that was rather clever, and it seems like you could ...

Atomic file save on Linux without losing metadata

I'm working on a Perl-based file synchronization tool. It downloads files into a temporary directory (which is guaranteed to be on the same filesystem as the real file) and then moves the temporary files into place over the old ones, preserving metadata like permissions, ownership, and ACLs. I'm wondering how to achieve that last step ...

Implement atomic increment using atomic swap?

Suppose I'm writing (assembly) code for a CPU whose only atomic operation is an unconditional swap -- no LL/SC, no compare-and-swap, just plain swap. (An ARM9 would be an example of such a beast.) Is there a way to perform atomic increment/decrement operations using the swap operation? There is a relatively easy answer, which is to use ...

Need atomic addition operation code on Solaris(sparc architecture)

I need atomic operation code equivalent to following: __asm__ __volatile__ ( " lock;\n" " addl %1, %0; \n" " movl %0, %%eax" : "=m"(a), "=a" (c) : "ir"(b) ); (Adding two variables a and b and output stored in both a ...

How do I build a lockless queue?

Hi, I've spent today looking into lockless queues. I have a multiple producer, multiple consumer situation. I implemented, for testing, a system using the Interlocked SList thing under Win32 and it doubled the performance of my heavily threaded task based code. Unfortunately, though, I wish to support multiple platforms. Interlockin...

Atomic bitfield operations on 80x86?

Does 80x86 have instructions for atomically testing and setting individual bits of a word? ...

Swapping two DB rows without violating constraints

I have a table regionkey: areaid -- primary key, int region -- char(4) locale -- char(4) The entire rest of the database is foreign-keyed to areaid. In this table there is an index on (region, locale) with a unique constraint. The problem is that I have two records: 101 MICH DETR 102 ILLI CHIC And I need to swap the (r...

Volatile or synchronized for primitive type?

In java, assignment is atomic if the size of the variable is less that or equal to 32 bits but is not if more than 32 bits. What(volatile/synchronized) would be more efficient to use in case of double or long assignment. like, volatile double x = y; synchronized is not applicable with primitive argument. How do i use synchronized i...

In C is "i+=1;" atomic?

In C, is i+=1; atomic? ...

How atomic is the Berkeley DB usage?

I am writing a simple app with 24 items in a hash to be persistent across program executions, so Berkeley DB (DBM) should be well suited for this task. And it is just for fun. But I wonder if using it (with Ruby), then when the user presses CTRL-C, then the execution is stopped. In this case, can't the data be all messed up? For exa...

C# how to protect the field of an atomic class?

I'm trying to make an AtomicReference class in C# and I want to keep the field reference protected, but I also need to return the value in the get method: class AtomicReference { private Object _value; public AtomicReference() { _value = new Object(); } public AtomicReference(Object value) { Opt...

java thread safe code + an atomic method question

I have a class Manager that is going to be accessed by multiple threads at the same time, I want to know if I did it the right way ? also I think I need RemoveFoo to be atomic, but I'm not sure public class Manager { private ConcurrentHashMap<String, Foo> foos; //init in constructor public RemoveFoo(String name) { ...

How can I do an atomic write/append in C#, or how do I get files opened with the FILE_APPEND_DATA flag?

Under most Unixes and Posix conforming operating systems performing an open() operating system call with the O_APPEND indicates to the OS that writes are to be atomic append and write operations. With this behavior,for local filesystems when you do a write, you know it get appended to the end of the file. The Windows operating systems s...

File r/w locking and unlink

Hello, I have following problem. I want to create a file system based session storage where each session data is stored in simple file named with session ids. I want following API: write(sid,data,timeout), read(sid,data,timeout), remove(sid) where sid==file name, Also I want to have some kind of GC that may remove all timed-out session...

How to Verify Atomic Writes?

Hello all, I have searched diligently (both within the S[O|F|U] network and elsewhere) and believe this to be an uncommon question. I am working with an Atmel AT91SAM9263-EK development board (ARM926EJ-S core, ARMv5 instruction set) running Debian Linux 2.6.28-4. I am writing using (I believe) the tty driver to talk to an RS-485 seria...

Oracle SQL: How to read-and-increment a field

I'm refactoring the data import procedure for an enterprise application and came across a snippet I'd like to find a better solution. When importing data we have to create a unique entity for each data set and there is a counter in a field to be used to assign this id sequentially. You read the field to get the next free id and increment...

How can you guarantee the atomicity of read/write operations?

Hello folks, The C# spec states in section 5.5 that reads and writes on certain types (namely bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types) are guaranteed to be atomic. This has piqued my interest. How can you do that? I mean, my lowly personal experience only showed me to lock variables or to use barri...

How to ensure a member is 4-byte aligned ?

In order to use OSAtomicDecrement (mac-specific atomic operation), I need to provide a 4-byte aligned SInt32. Does this kind of cooking work ? Is there another way to deal with alignment issues ? struct SomeClass { SomeClass() { member_ = &storage_ + ((4 - (&storage_ % 4)) % 4); *member_ = 0; } SInt32 *member_; struc...