[haiku-development] Writing Device Drivers - CRAM - Ticket Locking

  • From: Earl Pottinger <earl_colby_pottinger@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 19 Aug 2011 19:13:58 -0700 (PDT)

The last few times I posted questions I did not receive any replies because 
everybody was getting busy preparing for the release of Alpha-3.  I hope 
somebody has the time to help me with my questions at this time.
After a lot of work insuring read/write/flush commands are executed in the 
order they were called I seemed to have recreated Ticket-Locking, however 
ticket-locking requires a counter that is incremented atomicly.
The Atomic_Add() function documented in BeOS is only 32 bits in size.  However, 
a large drive (1TB or greater) could easily have more than 2^32 operations done 
to it between resets. To be safe I need a 64 bit counter.
Is there a 64 bit Atomic_Add() function in Haiku?????


At present I emulate an atomic 64 bit counter with the following code:
int64 ticket_counter; // Counter is a global variable.
int64 Increment_Counter() { // Increments the counter and returns value thru 
the stack.      aquire_sem(counter_sem);      ticket_counter++;      
temp_counter = ticket_counter;      release_sem(counter_sem);      return 
temp_counter; }


Should I use spinlocks instead of semaphores for this????
Should I be disabling interrupts only with spin-locks or will disabling 
interrupts work with semaphores as well????
What is the overhead of spinlocks vs semaphores, and enabling/disabling 
interrupts anyway?

Other related posts: