~ubuntu-branches/ubuntu/precise/nordugrid-arc/precise

« back to all changes in this revision

Viewing changes to src/hed/libs/common/Thread.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2012-03-01 19:48:16 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120301194816-m1ezrwnwt2qgnc2e
Tags: 1.1.1-1
* 1.1.1 Bugfix Release
* Fixes FTBFS (Closes: #661774) (LP: #935007)
* Fix typo in package description (Closes: #646979)
* Split binary rule in debian/rules for arch and indep

Show diffs side-by-side

added added

removed removed

Lines of Context:
321
321
 
322
322
  // ----------------------------------------
323
323
 
 
324
  void SharedMutex::add_shared_lock(void) {
 
325
    shared_list::iterator s = shared_.find(Glib::Thread::self());
 
326
    if(s != shared_.end()) {
 
327
      ++(s->second);
 
328
    } else {
 
329
      shared_[Glib::Thread::self()] = 1;
 
330
    };
 
331
  }
 
332
 
 
333
  void SharedMutex::remove_shared_lock(void) {
 
334
    shared_list::iterator s = shared_.find(Glib::Thread::self());
 
335
    if(s != shared_.end()) {
 
336
      --(s->second);
 
337
      if(!(s->second)) {
 
338
        shared_.erase(s);
 
339
      };
 
340
    };
 
341
  }
 
342
 
 
343
  bool SharedMutex::have_shared_lock(void) {
 
344
    if(shared_.size() >= 2) return true;
 
345
    if(shared_.size() == 1) {
 
346
      if(shared_.begin()->first != Glib::Thread::self()) return true;
 
347
    };
 
348
    return false;
 
349
  }
 
350
 
 
351
  void SharedMutex::lockShared(void) {
 
352
    lock_.lock();
 
353
    while(have_exclusive_lock()) {
 
354
      cond_.wait(lock_);
 
355
    };
 
356
    add_shared_lock();
 
357
    lock_.unlock();
 
358
  };
 
359
 
 
360
  void SharedMutex::unlockShared(void) {
 
361
    lock_.lock();
 
362
    remove_shared_lock();
 
363
    cond_.broadcast();
 
364
    lock_.unlock();
 
365
  };
 
366
 
 
367
  void SharedMutex::lockExclusive(void) {
 
368
    lock_.lock();
 
369
    while(have_exclusive_lock() || have_shared_lock()) {
 
370
      cond_.wait(lock_);
 
371
    };
 
372
    ++exclusive_;
 
373
    thread_ = Glib::Thread::self();
 
374
    lock_.unlock();
 
375
  }
 
376
 
 
377
  void SharedMutex::unlockExclusive(void) {
 
378
    lock_.lock();
 
379
    if(thread_ == Glib::Thread::self()) {
 
380
      if(exclusive_) --exclusive_;
 
381
      if(!exclusive_) thread_ = NULL;
 
382
    };
 
383
    cond_.broadcast();
 
384
    lock_.unlock();
 
385
  }
 
386
 
 
387
  // ----------------------------------------
 
388
 
324
389
  ThreadRegistry::ThreadRegistry(void):counter_(0),cancel_(false) {
325
390
  }
326
391