GNOME Bugzilla – Bug 772084
more safe async managment
Last modified: 2018-05-22 15:39:07 UTC
Created attachment 336385 [details] [review] safe async patch if you have a C async function with vapi file like: [CCode (cheader_filename = "fakeasync.h")] public static async int fakeasync (int val); and this function is not really async but directly call the callback without go to the mainloop. it is unsafe write vala code like: async int test1(int val) { SourceFunc callback = test1.callback; int result = -1; fakeasync.begin(val, (obj, res) => { stderr.printf("begin fakeasync callback\n"); result = fakeasync.end(res); callback(); }); yield; return result; } because the callback is executed before reach the yield stantment. This is not an infrequent case, for example reading from socket when data is already available sometime does not go back to the mainloop but immidiatly call the callback. see this discussion for example: https://mail.gnome.org/archives/vala-list/2016-March/msg00016.html or also if you write buggy code like this: https://bugzilla.gnome.org/show_bug.cgi?id=767877 All this possible scenarios can be manage from vala code generator. I Attach a patch for that. other possible advantage for this patch is that you can write code like async int test3(int val) { SourceFunc callback = test3.callback; int result1 = -1; int result2 = -1; stderr.printf("begin test3\n"); fakeasync.begin(val, (obj, res) => { result1 = fakeasync.end(res); callback(); }); result2 = yield test2(val); yield; return result1 + result2; } the result2 = yield test2(val); really wait the return of test2 and it is not important if fakesync end before of after test2. Someone can review this patches. If it seams ok I can write some test for it. I have also add and attribute 'fast_async' to have the possibility to write fake_async function if vala like: [ CCode ( fast_async = true ) ] public async int fakeasync(int val) { stderr.printf("fake async call\n"); return val +1; } because otherwise vala code generator add a idle callback to the mainloop. regards
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/556.