1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. 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 #include <stdio.h>
  25 #include <stdlib.h>
  26 
  27 #include <pthread.h>
  28 #include <string.h>
  29 
  30 #include "jni.h"
  31 #include "jni_util.h"
  32 
  33 
  34 JavaVM* jvm;
  35 jobject nativeThread;
  36 
  37 static void * thread_start(void* unused) {
  38   JNIEnv *env;
  39   jclass class_id;
  40   jmethodID method_id;
  41   int res;
  42 
  43   printf("Native thread is running and attaching as daemon ...\n");
  44 
  45   res = (*jvm)->AttachCurrentThreadAsDaemon(jvm, (void **)&env, NULL);
  46   if (res != JNI_OK) {
  47     fprintf(stderr, "Test ERROR. Can't attach current thread: %d\n", res);
  48     exit(1);
  49   }
  50 
  51   class_id = (*env)->FindClass (env, "java/lang/Thread");
  52   if (class_id == NULL) {
  53     fprintf(stderr, "Test ERROR. Can't load class Thread\n");
  54     exit(1);
  55   }
  56 
  57   method_id = (*env)->GetStaticMethodID(env, class_id, "currentThread",
  58                                         "()Ljava/lang/Thread;");
  59   if (method_id == NULL) {
  60     fprintf(stderr, "Test ERROR. Can't find method currentThread\n");
  61     exit(1);
  62   }
  63 
  64   nativeThread = (*env)->CallStaticObjectMethod(env, class_id, method_id, NULL);
  65 
  66   if ((*env)->ExceptionOccurred(env) != NULL) {
  67     (*env)->ExceptionDescribe(env);
  68     exit(1);
  69   }
  70   printf("Native thread terminating\n");
  71 
  72   return NULL;
  73 }
  74 
  75 JNIEXPORT jobject JNICALL
  76 Java_TestTerminatedThread_createTerminatedThread
  77 (JNIEnv *env, jclass cls) {
  78   pthread_t thread;
  79   int res = (*env)->GetJavaVM(env, &jvm);
  80   if (res != JNI_OK) {
  81     fprintf(stderr, "Test ERROR. Can't extract JavaVM: %d\n", res);
  82     exit(1);
  83   }
  84 
  85   if ((res = pthread_create(&thread, NULL, thread_start, NULL)) != 0) {
  86     fprintf(stderr, "TEST ERROR: pthread_created failed: %s (%d)\n", strerror(res), res);
  87     exit(1);
  88   }
  89 
  90   if ((res = pthread_join(thread, NULL)) != 0) {
  91     fprintf(stderr, "TEST ERROR: pthread_created failed: %s (%d)\n", strerror(res), res);
  92     exit(1);
  93   }
  94 
  95   return nativeThread;
  96 }