ROOT logo
// $Id: GMutex.cxx 2367 2010-04-18 20:50:40Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#include "GMutex.h"
#include <Gled/GledTypes.h>
#include <Glasses/ZGlass.h>
#include <errno.h>

//______________________________________________________________________________
//
// POSIX mutex wrapper class.
//
// Note that GMutex is used as base-class for GCondition and GSElector
// but DOES NOT have any virtual functions, nor a virtual destructor.
//
// This is to save space, as Gled uses mutexes a bit too excessively.

ClassImp(GMutex);

//extern int pthread_mutexattr_settype(pthread_mutexattr_t*, int);
//#define pthread_mutexattr_settype __pthread_mutexattr_settype
// safr ... ADAPTIVE not in glibc b4 rh7 ... falling back to FAST

GMutex::GMutex(Init_e e)
{
  // This can't fail ... so says the pthread man
  pthread_mutexattr_t	attr;
  pthread_mutexattr_init(&attr);
  switch(e) {
  case fast:
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); break;
  case recursive:
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); break;
  case error_checking:
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); break;
  default:
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); break;
  }
  int ret;
  if( (ret = pthread_mutex_init(&mMut, &attr)) ) {
    perror("GMutex::GMutex");
  }
}

GMutex::~GMutex()
{
  pthread_mutex_destroy(&mMut);
}

GMutex::Lock_e GMutex::Lock()
{
  int ret = pthread_mutex_lock(&mMut);
  switch(ret) {
  case 0:	return ok;
  case EDEADLK:	return deadlock;
  default:	return bad_init;
  }
}

GMutex::Lock_e GMutex::TryLock()
{
  int ret = pthread_mutex_trylock(&mMut);
  switch(ret) {
  case 0:	return ok;
  case EBUSY:	return busy;
  default:	return bad_init;
  }
}

GMutex::Lock_e GMutex::Unlock()
{
  int ret = pthread_mutex_unlock(&mMut);
  switch(ret) {
  case 0:	return ok;
  case EPERM:	return perm_fail;
  default:	return bad_init;
  }
}

/**************************************************************************/

GLensReadHolder::GLensReadHolder(ZGlass* lens) : mLens(lens)
{ mLens->ReadLock(); }

GLensReadHolder::~GLensReadHolder()
{ mLens->ReadUnlock(); }

GLensWriteHolder::GLensWriteHolder(ZGlass* lens) : mLens(lens)
{ mLens->WriteLock(); }

GLensWriteHolder::~GLensWriteHolder()
{ mLens->WriteUnlock(); }
 GMutex.cxx:1
 GMutex.cxx:2
 GMutex.cxx:3
 GMutex.cxx:4
 GMutex.cxx:5
 GMutex.cxx:6
 GMutex.cxx:7
 GMutex.cxx:8
 GMutex.cxx:9
 GMutex.cxx:10
 GMutex.cxx:11
 GMutex.cxx:12
 GMutex.cxx:13
 GMutex.cxx:14
 GMutex.cxx:15
 GMutex.cxx:16
 GMutex.cxx:17
 GMutex.cxx:18
 GMutex.cxx:19
 GMutex.cxx:20
 GMutex.cxx:21
 GMutex.cxx:22
 GMutex.cxx:23
 GMutex.cxx:24
 GMutex.cxx:25
 GMutex.cxx:26
 GMutex.cxx:27
 GMutex.cxx:28
 GMutex.cxx:29
 GMutex.cxx:30
 GMutex.cxx:31
 GMutex.cxx:32
 GMutex.cxx:33
 GMutex.cxx:34
 GMutex.cxx:35
 GMutex.cxx:36
 GMutex.cxx:37
 GMutex.cxx:38
 GMutex.cxx:39
 GMutex.cxx:40
 GMutex.cxx:41
 GMutex.cxx:42
 GMutex.cxx:43
 GMutex.cxx:44
 GMutex.cxx:45
 GMutex.cxx:46
 GMutex.cxx:47
 GMutex.cxx:48
 GMutex.cxx:49
 GMutex.cxx:50
 GMutex.cxx:51
 GMutex.cxx:52
 GMutex.cxx:53
 GMutex.cxx:54
 GMutex.cxx:55
 GMutex.cxx:56
 GMutex.cxx:57
 GMutex.cxx:58
 GMutex.cxx:59
 GMutex.cxx:60
 GMutex.cxx:61
 GMutex.cxx:62
 GMutex.cxx:63
 GMutex.cxx:64
 GMutex.cxx:65
 GMutex.cxx:66
 GMutex.cxx:67
 GMutex.cxx:68
 GMutex.cxx:69
 GMutex.cxx:70
 GMutex.cxx:71
 GMutex.cxx:72
 GMutex.cxx:73
 GMutex.cxx:74
 GMutex.cxx:75
 GMutex.cxx:76
 GMutex.cxx:77
 GMutex.cxx:78
 GMutex.cxx:79
 GMutex.cxx:80
 GMutex.cxx:81
 GMutex.cxx:82
 GMutex.cxx:83
 GMutex.cxx:84
 GMutex.cxx:85
 GMutex.cxx:86
 GMutex.cxx:87
 GMutex.cxx:88
 GMutex.cxx:89
 GMutex.cxx:90
 GMutex.cxx:91
 GMutex.cxx:92
 GMutex.cxx:93
 GMutex.cxx:94
 GMutex.cxx:95