add locking to resolve some thread safety issues

sigsegv_dump
Greg Alexander 10 years ago
parent 5c7d92415c
commit 637de21aca

@ -19,6 +19,7 @@ import java.io.BufferedReader;
public class SimpleSSHD extends Activity public class SimpleSSHD extends Activity
{ {
private static final Object lock = new Object();
private EditText log_view; private EditText log_view;
private Button startstop_view; private Button startstop_view;
public static SimpleSSHD curr = null; public static SimpleSSHD curr = null;
@ -34,14 +35,18 @@ public class SimpleSSHD extends Activity
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
curr = this; synchronized (lock) {
update_startstop(); curr = this;
}
update_startstop_prime();
updater = new UpdaterThread(); updater = new UpdaterThread();
updater.start(); updater.start();
} }
public void onPause() { public void onPause() {
curr = null; synchronized (lock) {
curr = null;
}
updater.interrupt(); updater.interrupt();
super.onPause(); super.onPause();
} }
@ -66,7 +71,7 @@ public class SimpleSSHD extends Activity
} }
} }
public void update_startstop() { private void update_startstop_prime() {
if (SimpleSSHDService.is_started()) { if (SimpleSSHDService.is_started()) {
startstop_view.setText("STOP"); startstop_view.setText("STOP");
startstop_view.setTextColor(0xFF881111); 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) { public void startstop_clicked(View v) {
Intent i = new Intent(this, SimpleSSHDService.class); Intent i = new Intent(this, SimpleSSHDService.class);
if (SimpleSSHDService.is_started()) { if (SimpleSSHDService.is_started()) {
@ -84,7 +106,7 @@ public class SimpleSSHD extends Activity
startService(i); startService(i);
} }
public void update_log() { private void update_log_prime() {
String[] lines = new String[50]; String[] lines = new String[50];
int curr_line = 0; int curr_line = 0;
boolean wrapped = false; boolean wrapped = false;
@ -118,4 +140,21 @@ public class SimpleSSHD extends Activity
log_view.setText(output); log_view.setText(output);
log_view.setSelection(output.length()); 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);
}
}
}
} }

@ -34,7 +34,7 @@ public class SimpleSSHDService extends Service {
return START_STICKY; return START_STICKY;
} else { } else {
stop_sshd(); stop_sshd();
update_activity(); SimpleSSHD.update_startstop();
stopSelf(); stopSelf();
/* XXX - need stopForeground() too ? */ /* XXX - need stopForeground() too ? */
return START_NOT_STICKY; return START_NOT_STICKY;
@ -75,21 +75,11 @@ public class SimpleSSHDService extends Service {
if (sshd_pid == pid) { if (sshd_pid == pid) {
sshd_pid = 0; sshd_pid = 0;
} }
update_activity(); SimpleSSHD.update_startstop();
} }
}).start(); }).start();
} }
update_activity(); SimpleSSHD.update_startstop();
}
private static void update_activity() {
if (SimpleSSHD.curr != null) {
SimpleSSHD.curr.runOnUiThread(new Runnable() {
public void run() {
SimpleSSHD.curr.update_startstop();
}
});
}
} }
private static void read_pidfile() { private static void read_pidfile() {

@ -18,11 +18,7 @@ public class UpdaterThread extends Thread {
long mod = f.lastModified(); long mod = f.lastModified();
long len = f.length(); long len = f.length();
if ((mod != lastmod) || (len != lastlen)) { if ((mod != lastmod) || (len != lastlen)) {
SimpleSSHD.curr.runOnUiThread(new Thread() { SimpleSSHD.update_log();
public void run() {
SimpleSSHD.curr.update_log();
}
});
lastmod = mod; lastmod = mod;
lastlen = len; lastlen = len;
} }

Loading…
Cancel
Save