From 637de21aca7b25b319ed59b84388b5bc4b03682b Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Wed, 17 Dec 2014 20:54:40 -0500 Subject: [PATCH] add locking to resolve some thread safety issues --- src/org/galexander/sshd/SimpleSSHD.java | 49 +++++++++++++++++-- .../galexander/sshd/SimpleSSHDService.java | 16 ++---- src/org/galexander/sshd/UpdaterThread.java | 6 +-- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/org/galexander/sshd/SimpleSSHD.java b/src/org/galexander/sshd/SimpleSSHD.java index b26193d..121ab6e 100644 --- a/src/org/galexander/sshd/SimpleSSHD.java +++ b/src/org/galexander/sshd/SimpleSSHD.java @@ -19,6 +19,7 @@ import java.io.BufferedReader; public class SimpleSSHD extends Activity { + private static final Object lock = new Object(); private EditText log_view; private Button startstop_view; public static SimpleSSHD curr = null; @@ -34,14 +35,18 @@ public class SimpleSSHD extends Activity public void onResume() { super.onResume(); - curr = this; - update_startstop(); + synchronized (lock) { + curr = this; + } + update_startstop_prime(); updater = new UpdaterThread(); updater.start(); } public void onPause() { - curr = null; + synchronized (lock) { + curr = null; + } updater.interrupt(); super.onPause(); } @@ -66,7 +71,7 @@ public class SimpleSSHD extends Activity } } - public void update_startstop() { + private void update_startstop_prime() { if (SimpleSSHDService.is_started()) { startstop_view.setText("STOP"); startstop_view.setTextColor(0xFF881111); @@ -76,6 +81,23 @@ public class SimpleSSHD extends Activity } } + public static void update_startstop() { + Thread t = new Thread() { + public void run() { + synchronized (lock) { + if (curr != null) { + curr.update_startstop_prime(); + } + } + } + }; + synchronized (lock) { + if (curr != null) { + curr.runOnUiThread(t); + } + } + } + public void startstop_clicked(View v) { Intent i = new Intent(this, SimpleSSHDService.class); if (SimpleSSHDService.is_started()) { @@ -84,7 +106,7 @@ public class SimpleSSHD extends Activity startService(i); } - public void update_log() { + private void update_log_prime() { String[] lines = new String[50]; int curr_line = 0; boolean wrapped = false; @@ -118,4 +140,21 @@ public class SimpleSSHD extends Activity log_view.setText(output); log_view.setSelection(output.length()); } + + public static void update_log() { + Thread t = new Thread() { + public void run() { + synchronized (lock) { + if (curr != null) { + curr.update_log_prime(); + } + } + } + }; + synchronized (lock) { + if (curr != null) { + curr.runOnUiThread(t); + } + } + } } diff --git a/src/org/galexander/sshd/SimpleSSHDService.java b/src/org/galexander/sshd/SimpleSSHDService.java index a4a816b..4e3fae0 100644 --- a/src/org/galexander/sshd/SimpleSSHDService.java +++ b/src/org/galexander/sshd/SimpleSSHDService.java @@ -34,7 +34,7 @@ public class SimpleSSHDService extends Service { return START_STICKY; } else { stop_sshd(); - update_activity(); + SimpleSSHD.update_startstop(); stopSelf(); /* XXX - need stopForeground() too ? */ return START_NOT_STICKY; @@ -75,21 +75,11 @@ public class SimpleSSHDService extends Service { if (sshd_pid == pid) { sshd_pid = 0; } - update_activity(); + SimpleSSHD.update_startstop(); } }).start(); } - update_activity(); - } - - private static void update_activity() { - if (SimpleSSHD.curr != null) { - SimpleSSHD.curr.runOnUiThread(new Runnable() { - public void run() { - SimpleSSHD.curr.update_startstop(); - } - }); - } + SimpleSSHD.update_startstop(); } private static void read_pidfile() { diff --git a/src/org/galexander/sshd/UpdaterThread.java b/src/org/galexander/sshd/UpdaterThread.java index 40446c8..152084e 100644 --- a/src/org/galexander/sshd/UpdaterThread.java +++ b/src/org/galexander/sshd/UpdaterThread.java @@ -18,11 +18,7 @@ public class UpdaterThread extends Thread { long mod = f.lastModified(); long len = f.length(); if ((mod != lastmod) || (len != lastlen)) { - SimpleSSHD.curr.runOnUiThread(new Thread() { - public void run() { - SimpleSSHD.curr.update_log(); - } - }); + SimpleSSHD.update_log(); lastmod = mod; lastlen = len; }