1 /*
   2  * Copyright 2012, 2015 SAP AG. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 
  26 #ifndef OS_AIX_VM_MISC_AIX_HPP
  27 #define OS_AIX_VM_MISC_AIX_HPP
  28 
  29 /* misc_aix.hpp, misc_aix.cpp: convenience functions needed for the OpenJDK AIX
  30  * port */
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 #include <pthread.h>
  34 
  35 #define assert0(b) assert( (b), "" )
  36 #define guarantee0(b) assert( (b), "" )
  37 template <class T1, class T2> bool is_aligned_to(T1 what, T2 alignment) {
  38   return  ( ((uintx)(what)) & (((uintx)(alignment)) - 1) ) == 0 ? true : false;
  39 }
  40 
  41 // CritSect: simple critical section implementation using pthread mutexes
  42 namespace MiscUtils {
  43   typedef pthread_mutex_t critsect_t;
  44 
  45   void init_critsect(MiscUtils::critsect_t* cs);
  46   void free_critsect(MiscUtils::critsect_t* cs);
  47   void enter_critsect(MiscUtils::critsect_t* cs);
  48   void leave_critsect(MiscUtils::critsect_t* cs);
  49 
  50   // Need to wrap this in an object because we need to dynamically initialize
  51   // critical section (because of windows, where there is no way to initialize
  52   // a CRITICAL_SECTION statically. On Unix, we could use
  53   // PTHREAD_MUTEX_INITIALIZER)
  54 
  55   // Note: The critical section does NOT get cleaned up in the destructor. That is
  56   // by design: the CritSect class is only ever used as global objects whose
  57   // lifetime spans the whole VM life; in that context we don't want the lock to
  58   // be cleaned up when global C++ objects are destroyed, but to continue to work
  59   // correctly right to the very end of the process life.
  60   class CritSect {
  61     critsect_t _cs;
  62   public:
  63     CritSect()        { init_critsect(&_cs); }
  64     //~CritSect()       { free_critsect(&_cs); }
  65     void enter()      { enter_critsect(&_cs); }
  66     void leave()      { leave_critsect(&_cs); }
  67   };
  68 
  69   class AutoCritSect {
  70     CritSect* const _pcsobj;
  71   public:
  72     AutoCritSect(CritSect* pcsobj)
  73       : _pcsobj(pcsobj)
  74     {
  75       _pcsobj->enter();
  76     }
  77     ~AutoCritSect() {
  78       _pcsobj->leave();
  79     }
  80   };
  81 
  82   // Returns true if pointer can be dereferenced without triggering a segment
  83   // violation. Returns false if pointer is invalid.
  84   // Note: Depends on stub routines; prior to stub routine generation, will
  85   // always return true. Use CanUseSafeFetch32 to handle this case.
  86   bool is_readable_pointer(const void* p);
  87 
  88 }
  89 
  90 #endif // OS_AIX_VM_MISC_AIX_HPP
  91