Panda3D

atomicAdjustPosixImpl.I

00001 // Filename: atomicAdjustPosixImpl.I
00002 // Created by:  drose (10Feb06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: AtomicAdjustPosixImpl::inc
00018 //       Access: Public, Static
00019 //  Description: Atomically increments the indicated variable.
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE void AtomicAdjustPosixImpl::
00022 inc(TVOLATILE AtomicAdjustPosixImpl::Integer &var) {
00023   pthread_mutex_lock(&_mutex);
00024   ++var;
00025   pthread_mutex_unlock(&_mutex);
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: AtomicAdjustPosixImpl::dec
00030 //       Access: Public, Static
00031 //  Description: Atomically decrements the indicated variable and
00032 //               returns true if the new value is nonzero, false if it
00033 //               is zero.
00034 ////////////////////////////////////////////////////////////////////
00035 INLINE bool AtomicAdjustPosixImpl::
00036 dec(TVOLATILE AtomicAdjustPosixImpl::Integer &var) {
00037   pthread_mutex_lock(&_mutex);
00038   Integer result = --var;
00039   pthread_mutex_unlock(&_mutex);
00040   return (result != 0);
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //     Function: AtomicAdjustPosixImpl::add
00045 //       Access: Public, Static
00046 //  Description: Atomically computes var += delta.  It is legal for
00047 //               delta to be negative.
00048 ////////////////////////////////////////////////////////////////////
00049 INLINE void AtomicAdjustPosixImpl::
00050 add(TVOLATILE AtomicAdjustPosixImpl::Integer &var, AtomicAdjustPosixImpl::Integer delta) {
00051   pthread_mutex_lock(&_mutex);
00052   var += delta;
00053   pthread_mutex_unlock(&_mutex);
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: AtomicAdjustPosixImpl::set
00058 //       Access: Public, Static
00059 //  Description: Atomically changes the indicated variable and
00060 //               returns the original value.
00061 ////////////////////////////////////////////////////////////////////
00062 INLINE AtomicAdjustPosixImpl::Integer AtomicAdjustPosixImpl::
00063 set(TVOLATILE AtomicAdjustPosixImpl::Integer &var, AtomicAdjustPosixImpl::Integer new_value) {
00064   pthread_mutex_lock(&_mutex);
00065   Integer orig_value = var;
00066   var = new_value;
00067   pthread_mutex_unlock(&_mutex);
00068   return orig_value;
00069 }
00070 
00071 ////////////////////////////////////////////////////////////////////
00072 //     Function: AtomicAdjustPosixImpl::get
00073 //       Access: Public, Static
00074 //  Description: Atomically retrieves the snapshot value of the
00075 //               indicated variable.  This is the only guaranteed safe
00076 //               way to retrieve the value that other threads might be
00077 //               asynchronously setting, incrementing, or decrementing
00078 //               (via other AtomicAjust methods).
00079 ////////////////////////////////////////////////////////////////////
00080 INLINE AtomicAdjustPosixImpl::Integer AtomicAdjustPosixImpl::
00081 get(const TVOLATILE AtomicAdjustPosixImpl::Integer &var) {
00082   pthread_mutex_lock(&_mutex);
00083   Integer orig_value = var;
00084   pthread_mutex_unlock(&_mutex);
00085   return orig_value;
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: AtomicAdjustPosixImpl::set_ptr
00090 //       Access: Public, Static
00091 //  Description: Atomically changes the indicated variable and
00092 //               returns the original value.
00093 ////////////////////////////////////////////////////////////////////
00094 INLINE void *AtomicAdjustPosixImpl::
00095 set_ptr(void * TVOLATILE &var, void *new_value) {
00096   pthread_mutex_lock(&_mutex);
00097   void *orig_value = var;
00098   var = new_value;
00099   pthread_mutex_unlock(&_mutex);
00100   return orig_value;
00101 }
00102 
00103 ////////////////////////////////////////////////////////////////////
00104 //     Function: AtomicAdjustPosixImpl::get_ptr
00105 //       Access: Public, Static
00106 //  Description: Atomically retrieves the snapshot value of the
00107 //               indicated variable.  This is the only guaranteed safe
00108 //               way to retrieve the value that other threads might be
00109 //               asynchronously setting, incrementing, or decrementing
00110 //               (via other AtomicAjust methods).
00111 ////////////////////////////////////////////////////////////////////
00112 INLINE void *AtomicAdjustPosixImpl::
00113 get_ptr(void * const TVOLATILE &var) {
00114   pthread_mutex_lock(&_mutex);
00115   void *orig_value = var;
00116   pthread_mutex_unlock(&_mutex);
00117   return orig_value;
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: AtomicAdjustPosixImpl::compare_and_exchange
00122 //       Access: Public, Static
00123 //  Description: Atomic compare and exchange.  
00124 //
00125 //               If mem is equal to old_value, store new_value in mem.
00126 //               In either case, return the original value of mem.
00127 //               The caller can test for success by comparing
00128 //               return_value == old_value.
00129 //
00130 //               The atomic function expressed in pseudo-code:
00131 //
00132 //                 orig_value = mem;
00133 //                 if (mem == old_value) {
00134 //                   mem = new_value;
00135 //                 }
00136 //                 return orig_value;
00137 //
00138 ////////////////////////////////////////////////////////////////////
00139 INLINE AtomicAdjustPosixImpl::Integer AtomicAdjustPosixImpl::
00140 compare_and_exchange(TVOLATILE AtomicAdjustPosixImpl::Integer &mem, AtomicAdjustPosixImpl::Integer old_value,
00141                      AtomicAdjustPosixImpl::Integer new_value) {
00142   pthread_mutex_lock(&_mutex);
00143   Integer orig_value = mem;
00144   if (mem == old_value) {
00145     mem = new_value;
00146   }
00147   pthread_mutex_unlock(&_mutex);
00148   return orig_value;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: AtomicAdjustPosixImpl::compare_and_exchange_ptr
00153 //       Access: Public, Static
00154 //  Description: Atomic compare and exchange.  
00155 //
00156 //               As above, but works on pointers instead of integers.
00157 ////////////////////////////////////////////////////////////////////
00158 INLINE void *AtomicAdjustPosixImpl::
00159 compare_and_exchange_ptr(void * TVOLATILE &mem, void *old_value,
00160                          void *new_value) {
00161   pthread_mutex_lock(&_mutex);
00162   void *orig_value = mem;
00163   if (mem == old_value) {
00164     mem = new_value;
00165   }
00166   pthread_mutex_unlock(&_mutex);
00167   return orig_value;
00168 }
 All Classes Functions Variables Enumerations