@@ -103,24 +103,6 @@ extern void throw_ioe(JNIEnv* env, int errnum);
103
103
static ssize_t get_pw_buflen ();
104
104
#endif
105
105
106
- /**
107
- * Returns non-zero if the user has specified that the system
108
- * has non-threadsafe implementations of getpwuid_r or getgrgid_r.
109
- **/
110
- static int workaround_non_threadsafe_calls (JNIEnv * env , jclass clazz ) {
111
- jboolean result ;
112
- jfieldID needs_workaround_field = (* env )-> GetStaticFieldID (
113
- env , clazz ,
114
- "workaroundNonThreadSafePasswdCalls" ,
115
- "Z" );
116
- PASS_EXCEPTIONS_RET (env , 0 );
117
- assert (needs_workaround_field );
118
-
119
- result = (* env )-> GetStaticBooleanField (
120
- env , clazz , needs_workaround_field );
121
- return result ;
122
- }
123
-
124
106
/**
125
107
* Sets a static boolean field to the specified value.
126
108
*/
@@ -201,10 +183,9 @@ static void consts_init(JNIEnv *env) {
201
183
}
202
184
#endif
203
185
204
- static void stat_init (JNIEnv * env , jclass nativeio_class ) {
186
+ static void stat_init (JNIEnv * env ) {
205
187
jclass clazz = NULL ;
206
- jclass obj_class = NULL ;
207
- jmethodID obj_ctor = NULL ;
188
+ if (stat_ctor2 != NULL ) return ; //Already inited
208
189
// Init Stat
209
190
clazz = (* env )-> FindClass (env , NATIVE_IO_STAT_CLASS );
210
191
if (!clazz ) {
@@ -224,6 +205,20 @@ static void stat_init(JNIEnv *env, jclass nativeio_class) {
224
205
if (!stat_ctor2 ) {
225
206
return ; // exception has been raised
226
207
}
208
+ }
209
+
210
+ static void stat_deinit (JNIEnv * env ) {
211
+ if (stat_clazz != NULL ) {
212
+ (* env )-> DeleteGlobalRef (env , stat_clazz );
213
+ stat_clazz = NULL ;
214
+ }
215
+ }
216
+
217
+ static void workaround_non_threadsafe_calls_init (JNIEnv * env ){
218
+ jclass obj_class = NULL ;
219
+ jmethodID obj_ctor = NULL ;
220
+ if (pw_lock_object != NULL ) return ; // Already inited
221
+
227
222
obj_class = (* env )-> FindClass (env , "java/lang/Object" );
228
223
if (!obj_class ) {
229
224
return ; // exception has been raised
@@ -233,28 +228,21 @@ static void stat_init(JNIEnv *env, jclass nativeio_class) {
233
228
if (!obj_ctor ) {
234
229
return ; // exception has been raised
235
230
}
236
-
237
- if (workaround_non_threadsafe_calls (env , nativeio_class )) {
238
- pw_lock_object = (* env )-> NewObject (env , obj_class , obj_ctor );
239
- PASS_EXCEPTIONS (env );
240
- pw_lock_object = (* env )-> NewGlobalRef (env , pw_lock_object );
241
-
242
- PASS_EXCEPTIONS (env );
243
- }
231
+ pw_lock_object = (* env )-> NewObject (env , obj_class , obj_ctor );
232
+ PASS_EXCEPTIONS (env );
233
+ pw_lock_object = (* env )-> NewGlobalRef (env , pw_lock_object );
234
+ PASS_EXCEPTIONS (env );
244
235
}
245
236
246
- static void stat_deinit (JNIEnv * env ) {
247
- if (stat_clazz != NULL ) {
248
- (* env )-> DeleteGlobalRef (env , stat_clazz );
249
- stat_clazz = NULL ;
250
- }
237
+ static void workaround_non_threadsafe_calls_deinit (JNIEnv * env ){
251
238
if (pw_lock_object != NULL ) {
252
239
(* env )-> DeleteGlobalRef (env , pw_lock_object );
253
240
pw_lock_object = NULL ;
254
241
}
255
242
}
256
243
257
244
static void nioe_init (JNIEnv * env ) {
245
+ if (nioe_ctor != NULL ) return ; // Already inited
258
246
// Init NativeIOException
259
247
nioe_clazz = (* env )-> FindClass (
260
248
env , "org/apache/hadoop/io/nativeio/NativeIOException" );
@@ -349,17 +337,53 @@ static void pmem_region_deinit(JNIEnv *env) {
349
337
*/
350
338
JNIEXPORT void JNICALL
351
339
Java_org_apache_hadoop_io_nativeio_NativeIO_initNative (
352
- JNIEnv * env , jclass clazz ) {
340
+ JNIEnv * env , jclass clazz , jboolean doThreadsafeWorkaround ) {
341
+ nioe_init (env );
342
+ PASS_EXCEPTIONS_GOTO (env , error );
343
+ fd_init (env );
344
+ PASS_EXCEPTIONS_GOTO (env , error );
345
+ #ifdef UNIX
346
+ errno_enum_init (env );
347
+ PASS_EXCEPTIONS_GOTO (env , error );
348
+ #endif
349
+ if (doThreadsafeWorkaround ) {
350
+ workaround_non_threadsafe_calls_init (env );
351
+ PASS_EXCEPTIONS_GOTO (env , error );
352
+ }
353
+ return ;
354
+ error :
355
+ // these are all idempotent and safe to call even if the
356
+ // class wasn't inited yet
357
+ nioe_deinit (env );
358
+ fd_deinit (env );
359
+ #ifdef UNIX
360
+ errno_enum_deinit (env );
361
+ #endif
362
+ if (doThreadsafeWorkaround ) {
363
+ workaround_non_threadsafe_calls_deinit (env );
364
+ }
365
+ }
366
+
367
+ /*
368
+ * private static native void initNativePosix();
369
+ */
370
+ JNIEXPORT void JNICALL
371
+ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_initNativePosix (
372
+ JNIEnv * env , jclass clazz , jboolean doThreadsafeWorkaround ) {
353
373
#ifdef UNIX
354
374
consts_init (env );
355
375
PASS_EXCEPTIONS_GOTO (env , error );
356
376
#endif
357
- stat_init (env , clazz );
377
+ stat_init (env );
358
378
PASS_EXCEPTIONS_GOTO (env , error );
359
379
nioe_init (env );
360
380
PASS_EXCEPTIONS_GOTO (env , error );
361
381
fd_init (env );
362
382
PASS_EXCEPTIONS_GOTO (env , error );
383
+ if (doThreadsafeWorkaround ) {
384
+ workaround_non_threadsafe_calls_init (env );
385
+ PASS_EXCEPTIONS_GOTO (env , error );
386
+ }
363
387
#ifdef UNIX
364
388
errno_enum_init (env );
365
389
PASS_EXCEPTIONS_GOTO (env , error );
@@ -373,17 +397,43 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_initNative(
373
397
error :
374
398
// these are all idempodent and safe to call even if the
375
399
// class wasn't initted yet
376
- #ifdef UNIX
377
400
stat_deinit (env );
378
401
#ifdef HADOOP_PMDK_LIBRARY
379
402
pmem_region_deinit (env );
380
- #endif
381
403
#endif
382
404
nioe_deinit (env );
383
405
fd_deinit (env );
384
406
#ifdef UNIX
385
407
errno_enum_deinit (env );
386
408
#endif
409
+ if (doThreadsafeWorkaround ) {
410
+ workaround_non_threadsafe_calls_deinit (env );
411
+ }
412
+ }
413
+
414
+ /*
415
+ * private static native void initNativeWindows();
416
+ */
417
+ JNIEXPORT void JNICALL
418
+ Java_org_apache_hadoop_io_nativeio_NativeIO_00024Windows_initNativeWindows (
419
+ JNIEnv * env , jclass clazz , jboolean doThreadsafeWorkaround ) {
420
+ nioe_init (env );
421
+ PASS_EXCEPTIONS_GOTO (env , error );
422
+ fd_init (env );
423
+ PASS_EXCEPTIONS_GOTO (env , error );
424
+ if (doThreadsafeWorkaround ) {
425
+ workaround_non_threadsafe_calls_init (env );
426
+ PASS_EXCEPTIONS_GOTO (env , error );
427
+ }
428
+ return ;
429
+ error :
430
+ // these are all idempodent and safe to call even if the
431
+ // class wasn't initted yet
432
+ nioe_deinit (env );
433
+ fd_deinit (env );
434
+ if (doThreadsafeWorkaround ) {
435
+ workaround_non_threadsafe_calls_deinit (env );
436
+ }
387
437
}
388
438
389
439
/*
0 commit comments