Boost logo

Boost :

Subject: Re: [boost] [fiber] new version in vault
From: Stefan Strasser (strasser_at_[hidden])
Date: 2009-11-30 01:13:24


Am Monday 30 November 2009 01:21:42 schrieb Vicente Botet Escriba:

> For the mutexes is another history. Have you tried to make a mutex class
> that works for process, threads and fibers? If you did, I'm interested in
> seen how you have reached that.

no I have not, but if you look at...

https://svn.boost.org/svn/boost/sandbox/fiber/libs/fiber/src/mutex.cpp

...Oliver almost has.

slightly changed to:

namespace sync{

template<class Suspend>
struct basic_cas_mutex{
  void lock(){
    while(true){
      uint32_t expected = 0;
      if ( detail::atomic_compare_exchange_strong( & state_, & expected, 1) )
        break;
      else
        Suspend()();
    }
  }
  ...
  ...
};

namespace fiber{

struct suspend{
  void operator()() const{
    this_fiber::yield();
  }
};
typedef sync::basic_cas_mutex<suspend> mutex;

}

namespace thread{

struct suspend{
  void operator()() const{
    this_thread::yield();
  }
};
#if CAS supported on platform
typedef sync::basic_cas_mutex<suspend> mutex;
#else
typedef sync::native_mutex mutex;
#endif

}

...and if you have multiple CPUs and shortlived locks only, you might choose
to not yield at all, to avoid a system call:

struct null_suspend{
  void operator()() const{}
};

typedef sync::basic_cas_mutex<null_suspend> spin_mutex;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk