@@ -47,10 +47,19 @@ @interface FTAViewController : UIViewController
47
47
// Test Loop on iOS doesn't provide the app under test a path to save logs to, so set it here.
48
48
#define GAMELOOP_DEFAULT_LOG_FILE " Results1.json"
49
49
50
- static int g_exit_status = 0 ;
51
- static bool g_shutdown = false ;
52
- static NSCondition *g_shutdown_complete;
53
- static NSCondition *g_shutdown_signal;
50
+ enum class RunningState {
51
+ kRunning ,
52
+ kShuttingDown ,
53
+ kShutDown
54
+ };
55
+
56
+ // Note: g_running_state and g_exit_status must only be accessed while holding the lock from
57
+ // g_running_state_condition; also, any changes to these values should be followed up with a
58
+ // call to [g_running_state_condition broadcast].
59
+ static NSCondition *g_running_state_condition;
60
+ static RunningState g_running_state = RunningState::kRunning ;
61
+ static int g_exit_status = -1 ;
62
+
54
63
static UITextView *g_text_view;
55
64
static UIView *g_parent_view;
56
65
static FTAViewController *g_view_controller;
@@ -70,9 +79,14 @@ - (void)viewDidLoad {
70
79
char *argv[1 ];
71
80
argv[0 ] = new char [strlen (TESTAPP_NAME) + 1 ];
72
81
strcpy (argv[0 ], TESTAPP_NAME); // NOLINT
73
- [g_shutdown_signal lock ];
74
- g_exit_status = common_main (1 , argv);
75
- [g_shutdown_complete signal ];
82
+ int common_main_result = common_main (1 , argv);
83
+
84
+ [g_running_state_condition lock ];
85
+ g_exit_status = common_main_result;
86
+ g_running_state = RunningState::kShutDown ;
87
+ [g_running_state_condition broadcast ];
88
+ [g_running_state_condition unlock ];
89
+
76
90
delete[] argv[0 ];
77
91
argv[0 ] = nullptr ;
78
92
[NSThread sleepForTimeInterval: kGameLoopSecondsToPauseBeforeQuitting ];
@@ -87,9 +101,17 @@ - (void)viewDidLoad {
87
101
namespace app_framework {
88
102
89
103
bool ProcessEvents (int msec) {
90
- [g_shutdown_signal
91
- waitUntilDate: [NSDate dateWithTimeIntervalSinceNow: static_cast <float >(msec) / 1000 .0f ]];
92
- return g_shutdown;
104
+ NSDate * endDate = [NSDate dateWithTimeIntervalSinceNow: static_cast <float >(msec) / 1000 .0f ];
105
+ [g_running_state_condition lock ];
106
+
107
+ if (g_running_state == RunningState::kRunning ) {
108
+ [g_running_state_condition waitUntilDate: endDate];
109
+ }
110
+
111
+ RunningState running_status = g_running_state;
112
+ [g_running_state_condition unlock ];
113
+
114
+ return running_status != RunningState::kRunning ;
93
115
}
94
116
95
117
std::string PathForResource () {
@@ -297,8 +319,13 @@ int main(int argc, char* argv[]) {
297
319
close (filedes[0 ]);
298
320
close (filedes[1 ]);
299
321
322
+ int exit_status = -1 ;
323
+ [g_running_state_condition lock ];
324
+ exit_status = g_exit_status;
325
+ [g_running_state_condition unlock ];
326
+
300
327
NSLog (@" Application Exit" );
301
- return g_exit_status ;
328
+ return exit_status ;
302
329
}
303
330
304
331
@implementation AppDelegate
@@ -317,9 +344,7 @@ - (BOOL)application:(UIApplication *)app
317
344
318
345
- (BOOL )application : (UIApplication*)application
319
346
didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
320
- g_shutdown_complete = [[NSCondition alloc ] init ];
321
- g_shutdown_signal = [[NSCondition alloc ] init ];
322
- [g_shutdown_complete lock ];
347
+ g_running_state_condition = [[NSCondition alloc ] init ];
323
348
324
349
self.window = [[UIWindow alloc ] initWithFrame: [UIScreen mainScreen ].bounds];
325
350
g_view_controller = [[FTAViewController alloc ] init ];
@@ -339,8 +364,17 @@ - (BOOL)application:(UIApplication*)application
339
364
}
340
365
341
366
- (void )applicationWillTerminate : (UIApplication *)application {
342
- g_shutdown = true ;
343
- [g_shutdown_signal signal ];
344
- [g_shutdown_complete wait ];
367
+ [g_running_state_condition lock ];
368
+
369
+ if (g_running_state == RunningState::kRunning ) {
370
+ g_running_state = RunningState::kShuttingDown ;
371
+ [g_running_state_condition broadcast ];
372
+ }
373
+
374
+ while (g_running_state != RunningState::kShutDown ) {
375
+ [g_running_state_condition wait ];
376
+ }
377
+
378
+ [g_running_state_condition unlock ];
345
379
}
346
380
@end
0 commit comments