[omniORB] Recursive mutex class for omnithreads

Peter S. Housel housel at acm.org
Tue Jun 28 08:26:22 BST 2005


It would be nice if omnithreads provided a recursive mutex class.  The
following is a portable implementation of recursive mutexes in terms of
the other omnithreads primitives.

class omni_recursive_mutex {
    omni_mutex nesting_mutex_;
    omni_condition cond_;
    volatile unsigned level_;
    volatile omni_thread *thread_;
public:
    omni_recursive_mutex()
	: cond_(&nesting_mutex_), level_(0), thread_(0) {
    }
    void lock() {
	nesting_mutex_.lock();
	if(level_ == 0) {
	    thread_ = omni_thread::self();
	} else if(thread_ != omni_thread::self()) {
	    while(level_ > 0)
		cond_.wait();
	    thread_ = omni_thread::self();
	}
	++level_;
	nesting_mutex_.unlock();
    }
    void unlock() {
	nesting_mutex_.lock();
	if(--level_ == 0) {
	    thread_ = 0;
	    cond_.signal();
	}
	nesting_mutex_.unlock();
    }
};

-- 
Peter S. Housel <housel at acm.org>



More information about the omniORB-list mailing list