org_apache_hadoop.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * This file includes some common utilities
  20. * for all native code used in hadoop.
  21. */
  22. #if !defined ORG_APACHE_HADOOP_H
  23. #define ORG_APACHE_HADOOP_H
  24. #include <dlfcn.h>
  25. #include <jni.h>
  26. #include "config.h"
  27. /* A helper macro to 'throw' a java exception. */
  28. #define THROW(env, exception_name, message) \
  29. { \
  30. jclass ecls = (*env)->FindClass(env, exception_name); \
  31. if (ecls) { \
  32. (*env)->ThrowNew(env, ecls, message); \
  33. (*env)->DeleteLocalRef(env, ecls); \
  34. } \
  35. }
  36. /* Helper macro to return if an exception is pending */
  37. #define PASS_EXCEPTIONS(env) \
  38. { \
  39. if ((*env)->ExceptionCheck(env)) return; \
  40. }
  41. #define PASS_EXCEPTIONS_GOTO(env, target) \
  42. { \
  43. if ((*env)->ExceptionCheck(env)) goto target; \
  44. }
  45. #define PASS_EXCEPTIONS_RET(env, ret) \
  46. { \
  47. if ((*env)->ExceptionCheck(env)) return (ret); \
  48. }
  49. /**
  50. * A helper function to dlsym a 'symbol' from a given library-handle.
  51. *
  52. * @param env jni handle to report contingencies.
  53. * @param handle handle to the dlopen'ed library.
  54. * @param symbol symbol to load.
  55. * @return returns the address where the symbol is loaded in memory,
  56. * <code>NULL</code> on error.
  57. */
  58. static __attribute__ ((unused))
  59. void *do_dlsym(JNIEnv *env, void *handle, const char *symbol) {
  60. if (!env || !handle || !symbol) {
  61. THROW(env, "java/lang/InternalError", NULL);
  62. return NULL;
  63. }
  64. char *error = NULL;
  65. void *func_ptr = dlsym(handle, symbol);
  66. if ((error = dlerror()) != NULL) {
  67. THROW(env, "java/lang/UnsatisfiedLinkError", symbol);
  68. return NULL;
  69. }
  70. return func_ptr;
  71. }
  72. /* A helper macro to dlsym the requisite dynamic symbol and bail-out on error. */
  73. #define LOAD_DYNAMIC_SYMBOL(func_ptr, env, handle, symbol) \
  74. if ((func_ptr = do_dlsym(env, handle, symbol)) == NULL) { \
  75. return; \
  76. }
  77. #define LOCK_CLASS(env, clazz, classname) \
  78. if ((*env)->MonitorEnter(env, clazz) != 0) { \
  79. char exception_msg[128]; \
  80. snprintf(exception_msg, 128, "Failed to lock %s", classname); \
  81. THROW(env, "java/lang/InternalError", exception_msg); \
  82. }
  83. #define UNLOCK_CLASS(env, clazz, classname) \
  84. if ((*env)->MonitorExit(env, clazz) != 0) { \
  85. char exception_msg[128]; \
  86. snprintf(exception_msg, 128, "Failed to unlock %s", classname); \
  87. THROW(env, "java/lang/InternalError", exception_msg); \
  88. }
  89. #define RETRY_ON_EINTR(ret, expr) do { \
  90. ret = expr; \
  91. } while ((ret == -1) && (errno == EINTR));
  92. #endif
  93. //vim: sw=2: ts=2: et