Skip to content

Commit bceaf78

Browse files
committed
Added Application#getMainThread and related methods
1 parent 6af1f6a commit bceaf78

File tree

10 files changed

+47
-16
lines changed

10 files changed

+47
-16
lines changed

arc-core/src/arc/Application.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,21 @@ default long getNativeHeap(){
7474
return 0;
7575
}
7676

77-
@Nullable
78-
String getClipboardText();
77+
/** @return the main graphics thread, upon which all ApplicationListener methods are called. May return null if not initialized. */
78+
default @Nullable Thread getMainThread(){
79+
return null;
80+
}
81+
82+
/**
83+
* @return whether the currently executing thread is the main thread.
84+
* If the main thread is not initialized, returns true by default. This is used for error checking purposes.
85+
* */
86+
default boolean isOnMainThread(){
87+
Thread thread = getMainThread();
88+
return thread == null || Thread.currentThread() == thread;
89+
}
90+
91+
@Nullable String getClipboardText();
7992

8093
void setClipboardText(String text);
8194

arc-core/src/arc/mock/MockApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import arc.struct.*;
55

66
public class MockApplication implements Application{
7+
78
@Override
89
public Seq<ApplicationListener> getListeners(){
910
return new Seq<>();

backends/backend-android/src/arc/backend/android/AndroidApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class AndroidApplication extends Activity implements Application{
4141
protected ClipboardManager clipboard;
4242
protected boolean useImmersiveMode = false;
4343
protected boolean hideStatusBar = false;
44+
protected @Nullable Thread mainThread;
4445

4546
static{
4647
ArcNativesLoader.load();
@@ -168,6 +169,11 @@ protected void hideStatusBar(boolean hide){
168169
getWindow().getDecorView().setSystemUiVisibility(0x1);
169170
}
170171

172+
@Override
173+
public Thread getMainThread(){
174+
return mainThread;
175+
}
176+
171177
@Override
172178
public void getDnsServers(Seq<InetSocketAddress> out){
173179
if(getVersion() < 21) return; //needs API level 21

backends/backend-android/src/arc/backend/android/AndroidGraphics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public void onSurfaceChanged(GL10 gl, int width, int height){
217217
updatePpi();
218218
gl.glViewport(0, 0, this.width, this.height);
219219
if(!created){
220+
app.mainThread = Thread.currentThread();
220221
for(ApplicationListener list : app.getListeners()){
221222
list.init();
222223
}

backends/backend-headless/src/arc/backend/headless/HeadlessApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ void mainLoop(){
103103
}
104104
}
105105

106+
@Override
107+
public Thread getMainThread(){
108+
return mainLoopThread;
109+
}
110+
106111
@Override
107112
public ApplicationType getType(){
108113
return ApplicationType.headless;

backends/backend-robovm/src/arc/backend/robovm/IOSApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class IOSApplication implements Application{
2121
IOSApplicationConfiguration config;
2222
IOSGraphics graphics;
2323
IOSInput input;
24+
Thread mainThread;
2425
@Nullable IOSDevice device;
2526
float displayScaleFactor;
2627

@@ -151,6 +152,11 @@ final void willTerminate(UIApplication uiApp){
151152
Gl.finish();
152153
}
153154

155+
@Override
156+
public Thread getMainThread(){
157+
return mainThread;
158+
}
159+
154160
@Override
155161
public ApplicationType getType(){
156162
return ApplicationType.iOS;

backends/backend-robovm/src/arc/backend/robovm/IOSGraphics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public void draw(GLKView view, CGRect rect){
201201
gl20.glViewport(IOSGLES20.x, IOSGLES20.y, IOSGLES20.width, IOSGLES20.height);
202202

203203
if(!created){
204+
app.mainThread = Thread.currentThread();
204205
gl20.glViewport(0, 0, width, height);
205206

206207
String versionString = gl20.glGetString(GL20.GL_VERSION);

backends/backend-sdl/src/arc/backend/sdl/SdlApplication.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class SdlApplication implements Application{
2424
final SdlGraphics graphics;
2525
final SdlInput input;
2626
final SdlConfig config;
27+
final Thread mainThread;
2728

2829
boolean running = true;
2930
long window, context;
@@ -32,6 +33,8 @@ public SdlApplication(ApplicationListener listener, SdlConfig config){
3233
this.config = config;
3334
this.listeners.add(listener);
3435

36+
mainThread = Thread.currentThread();
37+
3538
init();
3639

3740
Core.app = this;
@@ -248,6 +251,11 @@ public long getWindow(){
248251
return window;
249252
}
250253

254+
@Override
255+
public Thread getMainThread(){
256+
return mainThread;
257+
}
258+
251259
@Override
252260
public boolean openFolder(String file){
253261
Threads.daemon(() -> {

extensions/profiling/src/arc/profiling/GL20Interceptor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import arc.*;
44
import arc.graphics.*;
5-
import arc.util.*;
65

76
import java.nio.*;
87

@@ -12,16 +11,14 @@
1211
*/
1312
public class GL20Interceptor extends GLInterceptor implements GL20{
1413
protected final GL20 gl20;
15-
protected volatile @Nullable Thread mainThread;
1614

1715
protected GL20Interceptor(GLProfiler glProfiler, GL20 gl20){
1816
super(glProfiler);
1917
this.gl20 = gl20;
20-
Core.app.post(() -> mainThread = Thread.currentThread());
2118
}
2219

2320
private void check(){
24-
if(mainThread != null && Thread.currentThread() != mainThread){
21+
if(!Core.app.isOnMainThread()){
2522
glProfiler.getListener().onError("GL call on wrong thread: " + Thread.currentThread());
2623
}
2724
int error = gl20.glGetError();

extensions/profiling/src/arc/profiling/GL30Interceptor.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
package arc.profiling;
22

33
import arc.*;
4-
import arc.graphics.GL20;
5-
import arc.graphics.GL30;
6-
import arc.util.*;
4+
import arc.graphics.*;
75

8-
import java.nio.Buffer;
9-
import java.nio.FloatBuffer;
10-
import java.nio.IntBuffer;
11-
import java.nio.LongBuffer;
6+
import java.nio.*;
127

138
/**
149
* @author Daniel Holderbaum
1510
* @author Jan Polák
1611
*/
1712
public class GL30Interceptor extends GLInterceptor implements GL30{
1813
protected final GL30 gl30;
19-
protected volatile @Nullable Thread mainThread;
2014

2115
protected GL30Interceptor(GLProfiler glProfiler, GL30 gl30){
2216
super(glProfiler);
2317
this.gl30 = gl30;
24-
Core.app.post(() -> mainThread = Thread.currentThread());
2518
}
2619

2720
private void check(){
28-
if(mainThread != null && Thread.currentThread() != mainThread){
21+
if(!Core.app.isOnMainThread()){
2922
glProfiler.getListener().onError("GL call on wrong thread: " + Thread.currentThread());
3023
}
3124
int error = gl30.glGetError();

0 commit comments

Comments
 (0)