CORBA-omniORB

 view release on metacpan or  search on metacpan

omnithreads/omnithreads.xs  view on Meta::CPAN

    int jmp_rc = 0;
    I32 oldscope;
    int exit_app = 0;
    int exit_code = 0;
    int cleanup;

    dJMPENV;

    dTHXa(thread->interp);

    /* Blocked until ->create() call finishes */
    thread->mutex.lock();
    thread->mutex.unlock();

    PERL_SET_CONTEXT(thread->interp);
    S_ithread_set(aTHX_ thread);

    PL_perl_destruct_level = 2;

    {
        AV *params = (AV *)SvRV(thread->params);

omnithreads/shared/shared.pm  view on Meta::CPAN

    $var = A->new;         # error
    $var = &share(A->new); # ok as long as the A object is not nested

Note that it is often not wise to share an object unless the class itself
has been written to support sharing; for example, an object's destructor
may get called multiple times, one for each thread's scope exit.

=item lock VARIABLE

C<lock> places a lock on a variable until the lock goes out of scope.
If the variable is locked by another thread, the C<lock> call will
block until it's available. C<lock> is recursive, so multiple calls
to C<lock> are safe -- the variable will remain locked until the
outermost lock on the variable goes out of scope.

If a container object, such as a hash or array, is locked, all the
elements of that container are not locked. For example, if a thread
does a C<lock @a>, any other thread doing a C<lock($a[12])> won't block.

C<lock> will traverse up references exactly I<one> level.
C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.

Note that you cannot explicitly unlock a variable; you can only wait
for the lock to go out of scope. If you need more fine-grained
control, see L<Thread::Semaphore>.

=item cond_wait VARIABLE

=item cond_wait CONDVAR, LOCKVAR

The C<cond_wait> function takes a B<locked> variable as a parameter,
unlocks the variable, and blocks until another thread does a
C<cond_signal> or C<cond_broadcast> for that same locked variable.
The variable that C<cond_wait> blocked on is relocked after the
C<cond_wait> is satisfied.  If there are multiple threads
C<cond_wait>ing on the same variable, all but one will reblock waiting
to reacquire the lock on the variable. (So if you're only using
C<cond_wait> for synchronisation, give up the lock as soon as
possible). The two actions of unlocking the variable and entering the
blocked wait state are atomic, the two actions of exiting from the
blocked wait state and relocking the variable are not.

In its second form, C<cond_wait> takes a shared, B<unlocked> variable
followed by a shared, B<locked> variable.  The second variable is
unlocked and thread execution suspended until another thread signals
the first variable.

It is important to note that the variable can be notified even if
no thread C<cond_signal> or C<cond_broadcast> on the variable.
It is therefore important to check the value of the variable and
go back to waiting if the requirement is not fulfilled.  For example,
to pause until a shared counter drops to zero:

    { lock($counter); cond_wait($count) until $counter == 0; }

=item cond_timedwait VARIABLE, ABS_TIMEOUT

=item cond_timedwait CONDVAR, ABS_TIMEOUT, LOCKVAR

In its two-argument form, C<cond_timedwait> takes a B<locked> variable
and an absolute timeout as parameters, unlocks the variable, and blocks
until the timeout is reached or another thread signals the variable.  A
false value is returned if the timeout is reached, and a true value
otherwise.  In either case, the variable is re-locked upon return.

Like C<cond_wait>, this function may take a shared, B<locked> variable
as an additional parameter; in this case the first parameter is an
B<unlocked> condition variable protected by a distinct lock variable.

Again like C<cond_wait>, waking up and reacquiring the lock are not
atomic, and you should always check your desired condition after this
function returns.  Since the timeout is an absolute value, however, it
does not have to be recalculated with each pass:

    lock($var);
    my $abs = time() + 15;
    until ($ok = desired_condition($var)) {
      last if !cond_timedwait($var, $abs);
    }
    # we got it if $ok, otherwise we timed out!

=item cond_signal VARIABLE

The C<cond_signal> function takes a B<locked> variable as a parameter
and unblocks one thread that's C<cond_wait>ing on that variable. If
more than one thread is blocked in a C<cond_wait> on that variable,
only one (and which one is indeterminate) will be unblocked.

If there are no threads blocked in a C<cond_wait> on the variable,
the signal is discarded. By always locking before signaling, you can
(with care), avoid signaling before another thread has entered cond_wait().

C<cond_signal> will normally generate a warning if you attempt to use it
on an unlocked variable. On the rare occasions where doing this may be
sensible, you can skip the warning with

    { no warnings 'threads'; cond_signal($foo) }

=item cond_broadcast VARIABLE

The C<cond_broadcast> function works similarly to C<cond_signal>.
C<cond_broadcast>, though, will unblock B<all> the threads that are
blocked in a C<cond_wait> on the locked variable, rather than only one.

=back

=head1 NOTES

threads::shared is designed to disable itself silently if threads are
not available. If you want access to threads, you must C<use threads>
before you C<use threads::shared>.  threads will emit a warning if you
use it after threads::shared.

omnithreads/shared/shared.xs  view on Meta::CPAN

            Perl_croak(aTHX_ "Argument to cond_signal needs to be passed as ref");
	ref = SvRV(ref);
	if(SvROK(ref))
	    ref = SvRV(ref);
	ssv = Perl_sharedsv_find(aTHX_ ref);
	if(!ssv)
	    croak("cond_signal can only be used on shared values");
	ul = S_get_userlock(aTHX_ ssv, 1);
	if (ckWARN(WARN_THREADS) && ul->lock.owner != aTHX)
	    Perl_warner(aTHX_ packWARN(WARN_THREADS),
			    "cond_signal() called on unlocked variable");
	COND_SIGNAL(&ul->user_cond);

void
cond_broadcast_enabled(SV *ref)
	PROTOTYPE: \[$@%]
	CODE:
	SV *ssv;
	user_lock *ul;

	if(!SvROK(ref))
            Perl_croak(aTHX_ "Argument to cond_broadcast needs to be passed as ref");
	ref = SvRV(ref);
	if(SvROK(ref))
	    ref = SvRV(ref);
	ssv = Perl_sharedsv_find(aTHX_ ref);
	if(!ssv)
	    croak("cond_broadcast can only be used on shared values");
	ul = S_get_userlock(aTHX_ ssv, 1);
	if (ckWARN(WARN_THREADS) && ul->lock.owner != aTHX)
	    Perl_warner(aTHX_ packWARN(WARN_THREADS),
			    "cond_broadcast() called on unlocked variable");
	COND_BROADCAST(&ul->user_cond);


SV*
bless(SV* ref, ...);
	PROTOTYPE: $;$
	CODE:
        {
	  HV* stash;
	  SV *ssv;

omnithreads/shared/t/cond.t  view on Meta::CPAN

    $not = "not " unless $bool;
    print "${not}ok " . ($Base + $offset) . " - $text\n";
}

# test locking

{
    my $lock : shared;
    my $tr;

    # test that a subthread can't lock until parent thread has unlocked

    {
	lock($lock);
	ok(1,1,"set first lock");
	$tr = async {
	    lock($lock);
	    ok(3,1,"set lock in subthread");
	};
	omnithreads->yield;
	ok(2,1,"still got lock");

omnithreads/shared/t/cond.t  view on Meta::CPAN

    push @locks1, lock_factory1() for 1..2;
    push @locks1, lock_factory2() for 1..2;
    push @locks2, lock_factory1() for 1..2;
    push @locks2, lock_factory2() for 1..2;

    ok(1,1,"lock factory: locking all locks");
    lock $locks1[0];
    lock $locks1[1];
    lock $locks1[2];
    lock $locks1[3];
    ok(2,1,"lock factory: locked all locks");
    $tr = async {
	ok(3,1,"lock factory: child: locking all locks");
	lock $locks2[0];
	lock $locks2[1];
	lock $locks2[2];
	lock $locks2[3];
	ok(4,1,"lock factory: child: locked all locks");
    };
    $tr->join;
	
    $Base += 4;
}

# test cond_signal()

{
    my $lock : shared;

omnithreads/shared/t/cond.t  view on Meta::CPAN

	ok(1,1,"cond_signal: created first lock");
	my $tr2 = omnithreads->create(\&bar);
	cond_wait($lock);
	$tr2->join();
	ok(5,1,"cond_signal: joined");
    }

    sub bar {
	ok(2,1,"cond_signal: child before lock");
	lock($lock);
	ok(3,1,"cond_signal: child locked");
	cond_signal($lock);
	ok(4,1,"cond_signal: signalled");
    }

    my $tr  = omnithreads->create(\&foo);
    $tr->join();

    $Base += 5;

    # ditto, but with lockrefs

omnithreads/shared/t/cond.t  view on Meta::CPAN

	ok(1,1,"cond_signal: ref: created first lock");
	my $tr2 = omnithreads->create(\&bar2);
	cond_wait($lockref);
	$tr2->join();
	ok(5,1,"cond_signal: ref: joined");
    }

    sub bar2 {
	ok(2,1,"cond_signal: ref: child before lock");
	lock($lockref);
	ok(3,1,"cond_signal: ref: child locked");
	cond_signal($lockref);
	ok(4,1,"cond_signal: ref: signalled");
    }

    $tr  = omnithreads->create(\&foo2);
    $tr->join();

    $Base += 5;

}


# test cond_broadcast()

{
    my $counter : shared = 0;

    # broad(N) forks off broad(N-1) and goes into a wait, in such a way
    # that it's guaranteed to reach the wait before its child enters the
    # locked region. When N reaches 0, the child instead does a
    # cond_broadcast to wake all its ancestors.

    sub broad {
	my $n = shift;
	my $th;
	{
	    lock($counter);
	    if ($n > 0) {
		$counter++;
		$th = omnithreads->new(\&broad, $n-1);

omnithreads/t/thread.t  view on Meta::CPAN

{
    # test that sleep lets other thread run
    my $t = omnithreads->create(\&dorecurse, "ok 11\n");
    omnithreads->yield; # help out non-preemptive thread implementations
    sleep 1;
    print "ok 12\n";
    $t->join();
}
{
    my $lock : shared;
    sub islocked {
        lock($lock);
        my $val = shift;
        my $ret;
        print $val;
        if (@_) {
            $ret = omnithreads->create(\&islocked, shift);
        }
        return $ret;
    }
my $t = omnithreads->create(\&islocked, "ok 13\n", "ok 14\n");
$t->join->join;
}



sub testsprintf {
    my $testno = shift;
    my $same = sprintf( "%0.f", $testno);
    return $testno eq $same;
}

pomni.h  view on Meta::CPAN


//-------------------------------------------------------------------

/** Mutex to serialize servant calls from omniORB.  Allows the mutex
 * to be temporarily released to allow callbacks with a deeper
 * recursion level to execute.
 */

class POmniRatchetLock {
    omni_mutex mutex_;
    volatile bool locked_;
    omni_condition entry_cond_;
    volatile unsigned awaiting_entry_;

    struct Entry {
	bool waiting;
	omni_condition *cond;
	Entry(omni_mutex *mutex)
	    : waiting(false), cond(new omni_condition(mutex)) {
	}
    };

pomni.h  view on Meta::CPAN

    _T::iterator top_;		// Stack top
    
    inline void grow() {
	_T::size_type top = top_ - stack_.begin();
	stack_.push_back(Entry(&mutex_)); // Invalidates iterators
	top_ = stack_.begin() + top;
    }
    
public:
    POmniRatchetLock(void)
	: locked_(true),
	  entry_cond_(&mutex_),
	  awaiting_entry_(0),
	  top_(stack_.begin()) {
	grow();			// initial entry
    }
    ~POmniRatchetLock(void) {
	for(_T::iterator i = stack_.begin(); i != stack_.end(); ++i) {
	    delete i->cond;
	}
    }

    //! Enter a new recursion level.
    void enter(void) {
	mutex_.lock();
	++awaiting_entry_;
	while(locked_)
	    entry_cond_.wait();
	--awaiting_entry_;
	++top_;
	if(top_ == stack_.end())
	    grow();
	locked_ = true;
	mutex_.unlock();
    }

    //! Exit the current recursion level.
    void leave(void) {
	mutex_.lock();
	locked_ = false;
	--top_;
	if(awaiting_entry_ > 0)
	    entry_cond_.signal();
	else if(top_->waiting)
	    top_->cond->signal();
	mutex_.unlock();
    }

    //! Token used to indicate the current recursion level for later
    // resumption.
    typedef _T::size_type token;

    //! Release the lock for use by deeper recursion levels.
    token release(void) {
	mutex_.lock();
	locked_ = false;
	if(awaiting_entry_ > 0)
	    entry_cond_.signal();
	token t = top_ - stack_.begin();
	mutex_.unlock();
	return t;
    }

    //! Resume the specified recursion level.
    void resume(token t) {
	mutex_.lock();
	_T::iterator ti = stack_.begin() + t;
	ti->waiting = true;
	while(locked_ || ti != top_) {
	    ti->cond->wait();
	    ti = stack_.begin() + t;
	}
	ti->waiting = false;
	locked_ = true;
	mutex_.unlock();
    }
};

// Return the entry lock for the current Perl interpreter
POmniRatchetLock *pomni_entry_lock(pTHX);

/** Object to temporarily unlock the Perl interpreter during a blocking
 * operation, allowing other threads to make use of the interpreter.
 * Usage should follow the pattern:



( run in 0.548 second using v1.01-cache-2.11-cpan-49f99fa48dc )