| OLD | NEW |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <pthread.h> | 6 #include <pthread.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include "include/fletch_api.h" | 9 #include "include/dartino_api.h" |
| 10 #include "include/service_api.h" | 10 #include "include/service_api.h" |
| 11 #include "generated/cc/simple_todo.h" | 11 #include "generated/cc/simple_todo.h" |
| 12 | 12 |
| 13 static const int kDone = 1; | 13 static const int kDone = 1; |
| 14 static pthread_mutex_t mutex; | 14 static pthread_mutex_t mutex; |
| 15 static pthread_cond_t cond; | 15 static pthread_cond_t cond; |
| 16 static int status = 0; | 16 static int status = 0; |
| 17 | 17 |
| 18 typedef enum { | 18 typedef enum { |
| 19 EC_OK = 0, | 19 EC_OK = 0, |
| 20 EC_THREAD, | 20 EC_THREAD, |
| 21 EC_INPUT_ERR, | 21 EC_INPUT_ERR, |
| 22 } ErrorCode; | 22 } ErrorCode; |
| 23 | 23 |
| 24 static void ChangeStatusAndNotify(int new_status) { | 24 static void ChangeStatusAndNotify(int new_status) { |
| 25 pthread_mutex_lock(&mutex); | 25 pthread_mutex_lock(&mutex); |
| 26 status = new_status; | 26 status = new_status; |
| 27 pthread_cond_signal(&cond); | 27 pthread_cond_signal(&cond); |
| 28 pthread_mutex_unlock(&mutex); | 28 pthread_mutex_unlock(&mutex); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static void WaitForVmThread(int expected) { | 31 static void WaitForVmThread(int expected) { |
| 32 pthread_mutex_lock(&mutex); | 32 pthread_mutex_lock(&mutex); |
| 33 while (expected != status) pthread_cond_wait(&cond, &mutex); | 33 while (expected != status) pthread_cond_wait(&cond, &mutex); |
| 34 pthread_mutex_unlock(&mutex); | 34 pthread_mutex_unlock(&mutex); |
| 35 } | 35 } |
| 36 | 36 |
| 37 static void* StartFletch(void* arg) { | 37 static void* StartDartino(void* arg) { |
| 38 char* snapshot_filepath_with_name = reinterpret_cast<char*>(arg); | 38 char* snapshot_filepath_with_name = reinterpret_cast<char*>(arg); |
| 39 FletchSetup(); | 39 DartinoSetup(); |
| 40 FletchRunSnapshotFromFile(snapshot_filepath_with_name); | 40 DartinoRunSnapshotFromFile(snapshot_filepath_with_name); |
| 41 FletchTearDown(); | 41 DartinoTearDown(); |
| 42 ChangeStatusAndNotify(kDone); | 42 ChangeStatusAndNotify(kDone); |
| 43 return NULL; | 43 return NULL; |
| 44 } | 44 } |
| 45 | 45 |
| 46 static void StartVmThread(char* snapshot_filename) { | 46 static void StartVmThread(char* snapshot_filename) { |
| 47 pthread_mutex_init(&mutex, NULL); | 47 pthread_mutex_init(&mutex, NULL); |
| 48 pthread_cond_init(&cond, NULL); | 48 pthread_cond_init(&cond, NULL); |
| 49 pthread_t tid = 0; | 49 pthread_t tid = 0; |
| 50 int result = pthread_create(&tid, 0, StartFletch, | 50 int result = pthread_create(&tid, 0, StartDartino, |
| 51 reinterpret_cast<void*>(snapshot_filename)); | 51 reinterpret_cast<void*>(snapshot_filename)); |
| 52 if (result != 0) { | 52 if (result != 0) { |
| 53 printf("Error creating thread\n"); | 53 printf("Error creating thread\n"); |
| 54 exit(EC_THREAD); | 54 exit(EC_THREAD); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 class TodoListView { | 58 class TodoListView { |
| 59 public: | 59 public: |
| 60 void showMenu() { | 60 void showMenu() { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 printf("Usage: %s <snapshot file>\n", argv[0]); | 193 printf("Usage: %s <snapshot file>\n", argv[0]); |
| 194 return EC_OK; | 194 return EC_OK; |
| 195 } | 195 } |
| 196 | 196 |
| 197 ServiceApiSetup(); | 197 ServiceApiSetup(); |
| 198 StartVmThread(argv[1]); | 198 StartVmThread(argv[1]); |
| 199 int ec = InteractWithService(); | 199 int ec = InteractWithService(); |
| 200 WaitForVmThread(kDone); | 200 WaitForVmThread(kDone); |
| 201 return ec; | 201 return ec; |
| 202 } | 202 } |
| OLD | NEW |