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 #include "runtime/globals.hpp"
  33 #include "utilities/debug.hpp"
  34 
  35 #include <pthread.h>
  36 
  37 // Trace if verbose to tty.
  38 #define trcVerbose(fmt, ...) { \
  39   if (Verbose) { \
  40     fprintf(stderr, fmt, ##__VA_ARGS__); \
  41     fputc('\n', stderr); fflush(stderr); \
  42   } \
  43 }
  44 #define ERRBYE(s) { trcVerbose(s); return -1; }
  45 #define trc(fmt, ...)
  46 
  47 #define assert0(b) assert((b), "")
  48 #define guarantee0(b) guarantee((b), "")
  49 template <class T1, class T2> bool is_aligned_to(T1 what, T2 alignment) {
  50   return (((uintx)(what)) & (((uintx)(alignment)) - 1)) == 0 ? true : false;
  51 }
  52 
  53 // CritSect: simple critical section implementation using pthread mutexes.
  54 namespace MiscUtils {
  55   typedef pthread_mutex_t critsect_t;
  56 
  57   void init_critsect(MiscUtils::critsect_t* cs);
  58   void free_critsect(MiscUtils::critsect_t* cs);
  59   void enter_critsect(MiscUtils::critsect_t* cs);
  60   void leave_critsect(MiscUtils::critsect_t* cs);
  61 
  62   // Need to wrap this in an object because we need to dynamically initialize
  63   // critical section (because of windows, where there is no way to initialize
  64   // a CRITICAL_SECTION statically. On Unix, we could use
  65   // PTHREAD_MUTEX_INITIALIZER).
  66 
  67   // Note: The critical section does NOT get cleaned up in the destructor. That is
  68   // by design: the CritSect class is only ever used as global objects whose
  69   // lifetime spans the whole VM life; in that context we don't want the lock to
  70   // be cleaned up when global C++ objects are destroyed, but to continue to work
  71   // correctly right to the very end of the process life.
  72   class CritSect {
  73     critsect_t _cs;
  74    public:
  75     CritSect()        { init_critsect(&_cs); }
  76     //~CritSect()       { free_critsect(&_cs); }
  77     void enter()      { enter_critsect(&_cs); }
  78     void leave()      { leave_critsect(&_cs); }
  79   };
  80 
  81   class AutoCritSect {
  82     CritSect* const _pcsobj;
  83    public:
  84     AutoCritSect(CritSect* pcsobj)
  85       : _pcsobj(pcsobj)
  86     {
  87       _pcsobj->enter();
  88     }
  89     ~AutoCritSect() {
  90       _pcsobj->leave();
  91     }
  92   };
  93 
  94   // Returns true if pointer can be dereferenced without triggering a segment
  95   // violation. Returns false if pointer is invalid.
  96   // Note: Depends on stub routines; prior to stub routine generation, will
  97   // always return true. Use CanUseSafeFetch32 to handle this case.
  98   bool is_readable_pointer(const void* p);
  99 
 100   const char* describe_errno(int err_no);
 101 
 102   void sleep_ms(int milliseconds);
 103 
 104 
 105 }
 106 
 107 #endif // OS_AIX_VM_MISC_AIX_HPP
 108