This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

mutexes: "food for thought" [upcoming XBOX]


Given

http://www.theinquirer.net/?article=14407
http://www.gamepro.com/microsoft/xbox/hardware/news/35216.shtml

here's "food for thought" illustration. 

// doesn't provide "POSIX-safety" with respect to destruction
class mutex_for_XBOX_NEXT { // noncopyable

  atomic<int>      m_lock_status; // 0: free, 1/-1: locked/contention
  auto_reset_event m_retry_event; // prohibitively slow bin.sema/gate

  template<typename T>
  int attempt_update(int old, int new, T msync) {
    while (!m_lock_status.store_conditional(new, msync)) {
      int fresh = m_lock_status.load_reserved(msync::none);
      if (fresh != old)
        return fresh;
    }
    return old;
  }

public:

  // ctor/dtor [w/o lazy event init] 

  bool trylock() throw() {
    return !(m_lock_status.load_reserved(msync::none) || 
             attempt_update(0, 1, msync::acq)); 
  }

  // bool timedlock() omitted for brevity

  void lock() throw() {
    int old = m_lock_status.load_reserved(msync::none);
    if (old || old = attempt_update(0, 1, msync::acq)) {
      do {
        while (old < 0 || 
               old = attempt_update(1, -1, msync::none)) { 
          m_retry_event.wait();
          old = m_lock_status.load_reserved(msync::none);
          if (!old) break;
        }
      } while (old = attempt_update(0, -1, msync::acq));
    }
  }

  void unlock() throw() {
    if (m_lock_status.load_reserved(msync::none) < 0 || 
        attempt_update(1, 0, msync::rel) < 0) { // or just !SC
      m_lock_status.store(0, msync::rel);
      m_retry_event.set();
    }
  }

};

Oder?

regards,
alexander.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]