Added 'create_thread' function to enable thread creation after a exit.
[nit.git] / lib / pnacl.nit
index 8f50fa1..994bbdb 100644 (file)
@@ -712,9 +712,37 @@ class PnaclApp
 
        # Checks if there is a dictionary in the queue, and if so the dictionary is handled automatically.
        fun check_dictionary `{
-               while(1) {
-                       NitHandleDictionary();
-               }
+               NitHandleDictionary();
        `}
+
+       # Infinite loop on check_dictionary
+       fun run
+       do
+               loop
+                       check_dictionary
+               end
+       end
 end
+
+redef interface Object
+       # Creates a new thread for Nit.
+       fun create_thread `{
+               pthread_create(&g_nit_thread, NULL, &WrapperNitMain, NULL);
+       `}
+
+       # Calls 'pthread_exit on current thread.
+        fun exit_thread(exit_value: Int) `{
+               pthread_exit((void*) exit_value);
+       `}
+
+       # Redef of exit in order to avoid the module to crash by terminating only the Nit thread.
+       redef fun exit(exit_value: Int)
+       do
+               var dictionary = new PepperDictionary
+               dictionary["exit"] = exit_value
+               app.post_dictionary dictionary
+               exit_thread exit_value
+       end
+end
+
 fun app: PnaclApp do return once new PnaclApp