aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2023-06-09 14:03:50 +0800
committerPo Lu2023-06-09 14:03:50 +0800
commitc321eea5af535d102c5e1d2d7e95029ce6fee427 (patch)
treeefd19f5c11d2bf426c196ab97403aa1d58e223b9
parent7f073df53374bc1b96ac07e949af11b8af06b94b (diff)
downloademacs-c321eea5af535d102c5e1d2d7e95029ce6fee427.tar.gz
emacs-c321eea5af535d102c5e1d2d7e95029ce6fee427.zip
Block profiling signals in the Android UI thread
* java/org/gnu/emacs/EmacsNative.java (EmacsNative): New function `setupSystemThread'. * java/org/gnu/emacs/EmacsService.java (onCreate): Block all signals except for SIGBUS and SIGSEGV in the UI thread. * src/android.c (setupSystemThread): New function.
-rw-r--r--java/org/gnu/emacs/EmacsNative.java4
-rw-r--r--java/org/gnu/emacs/EmacsService.java25
-rw-r--r--src/android.c24
3 files changed, 53 insertions, 0 deletions
diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java
index cb89cf6808a..2fcbf8b94ef 100644
--- a/java/org/gnu/emacs/EmacsNative.java
+++ b/java/org/gnu/emacs/EmacsNative.java
@@ -188,6 +188,10 @@ public final class EmacsNative
188 KEYCODE_VOLUME_MUTE should be forwarded to Emacs. */ 188 KEYCODE_VOLUME_MUTE should be forwarded to Emacs. */
189 public static native boolean shouldForwardMultimediaButtons (); 189 public static native boolean shouldForwardMultimediaButtons ();
190 190
191 /* Initialize the current thread, by blocking signals that do not
192 interest it. */
193 public static native void setupSystemThread ();
194
191 195
192 196
193 /* Input connection functions. These mostly correspond to their 197 /* Input connection functions. These mostly correspond to their
diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java
index 48e39f8b355..96216e51cf4 100644
--- a/java/org/gnu/emacs/EmacsService.java
+++ b/java/org/gnu/emacs/EmacsService.java
@@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingException;
25 25
26import java.util.List; 26import java.util.List;
27 27
28import java.util.concurrent.Semaphore;
28import java.util.concurrent.atomic.AtomicInteger; 29import java.util.concurrent.atomic.AtomicInteger;
29 30
30import android.graphics.Matrix; 31import android.graphics.Matrix;
@@ -211,6 +212,7 @@ public final class EmacsService extends Service
211 final String filesDir, libDir, cacheDir, classPath; 212 final String filesDir, libDir, cacheDir, classPath;
212 final double pixelDensityX; 213 final double pixelDensityX;
213 final double pixelDensityY; 214 final double pixelDensityY;
215 final Semaphore signalSemaphore;
214 216
215 SERVICE = this; 217 SERVICE = this;
216 handler = new Handler (Looper.getMainLooper ()); 218 handler = new Handler (Looper.getMainLooper ());
@@ -220,6 +222,7 @@ public final class EmacsService extends Service
220 pixelDensityX = metrics.xdpi; 222 pixelDensityX = metrics.xdpi;
221 pixelDensityY = metrics.ydpi; 223 pixelDensityY = metrics.ydpi;
222 resolver = getContentResolver (); 224 resolver = getContentResolver ();
225 signalSemaphore = new Semaphore (0);
223 226
224 try 227 try
225 { 228 {
@@ -248,11 +251,33 @@ public final class EmacsService extends Service
248 cacheDir, (float) pixelDensityX, 251 cacheDir, (float) pixelDensityX,
249 (float) pixelDensityY, 252 (float) pixelDensityY,
250 classPath, EmacsService.this); 253 classPath, EmacsService.this);
254
255 /* Wait for the signal mask to be set up in the UI
256 thread. */
257
258 while (true)
259 {
260 try
261 {
262 signalSemaphore.acquire ();
263 break;
264 }
265 catch (InterruptedException e)
266 {
267 ;;
268 }
269 }
251 } 270 }
252 }, extraStartupArgument, 271 }, extraStartupArgument,
253 /* If any file needs to be opened, open it now. */ 272 /* If any file needs to be opened, open it now. */
254 EmacsOpenActivity.fileToOpen); 273 EmacsOpenActivity.fileToOpen);
255 thread.start (); 274 thread.start ();
275
276 /* Now that the thread has been started, block signals which
277 don't interest the current thread. */
278
279 EmacsNative.setupSystemThread ();
280 signalSemaphore.release ();
256 } 281 }
257 catch (IOException exception) 282 catch (IOException exception)
258 { 283 {
diff --git a/src/android.c b/src/android.c
index 92aab548180..681723124ee 100644
--- a/src/android.c
+++ b/src/android.c
@@ -3077,6 +3077,30 @@ NATIVE_NAME (answerQuerySpin) (JNIEnv *env, jobject object)
3077 android_answer_query_spin (); 3077 android_answer_query_spin ();
3078} 3078}
3079 3079
3080
3081
3082/* System thread setup. Android doesn't always block signals Emacs is
3083 interested in from being received by the UI or render threads,
3084 which can lead to problems when those signals then interrupt one of
3085 those threads. */
3086
3087JNIEXPORT void JNICALL
3088NATIVE_NAME (setupSystemThread) (void)
3089{
3090 sigset_t sigset;
3091
3092 /* Block everything except for SIGSEGV and SIGBUS; those two are
3093 used by the runtime. */
3094
3095 sigfillset (&sigset);
3096 sigaddset (&sigset, SIGSEGV);
3097 sigaddset (&sigset, SIGBUS);
3098
3099 if (pthread_sigmask (SIG_BLOCK, &sigset, NULL))
3100 __android_log_print (ANDROID_LOG_WARN, __func__,
3101 "pthread_sigmask: %s", strerror (errno));
3102}
3103
3080#ifdef __clang__ 3104#ifdef __clang__
3081#pragma clang diagnostic pop 3105#pragma clang diagnostic pop
3082#else 3106#else