Moved uiQueueMain() to common/init.c to avoid having multiple initialized variables; the per-OS versions are now uiprivSysQueueMain(). Also more TODOs.

This commit is contained in:
Pietro Gagliardi 2019-05-30 01:39:43 -04:00
parent b0e890ca1d
commit 9daef443b2
5 changed files with 16 additions and 21 deletions

View File

@ -5,6 +5,8 @@
#include "ui.h"
#include "uipriv.h"
// TODO rename this file to main.c
static bool initialized = false;
bool uiInit(void *options, uiInitError *err)
@ -47,6 +49,15 @@ bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...)
return false;
}
void uiQueueMain(void (*f)(void *data), void *data)
{
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc);
return;
}
uiprivSysQueueMain(f, data);
}
bool uiprivCheckInitializedAndThreadImpl(const char *func)
{
// While it does seem risky to not lock this, if this changes during the execution of this function it means only that it was changed from a different thread, and since it can only change from false to true, an error will be reported anyway.

View File

@ -22,6 +22,7 @@ extern "C" {
// init.c
extern bool uiprivSysInit(void *options, uiInitError *err);
extern bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...);
extern void uiprivSysQueueMain(void (*f)(void *data), void *data);
extern bool uiprivCheckInitializedAndThreadImpl(const char *func);
#define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc)
extern bool uiprivSysCheckThread(void);

View File

@ -37,12 +37,12 @@ static uiprivApplicationDelegate *uiprivAppDelegate;
@end
static pthread_t mainThread;
static BOOL initialized = NO; // TODO deduplicate this from common/init.c
bool uiprivSysInit(void *options, uiInitError *err)
{
uiprivApp = [uiprivApplication sharedApplication];
if (![NSApp isKindOfClass:[uiprivApplication class]])
// TODO verify if Info.plist (both in a proper .app bundle and embedded into a standalone binary) can override this and, if so, add that tidbit to this message
return uiprivInitReturnErrorf(err, "NSApp is not of type uiprivApplication; was likely already initialized beforehand");
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
@ -53,7 +53,6 @@ bool uiprivSysInit(void *options, uiInitError *err)
[uiprivApp setDelegate:uiprivAppDelegate];
mainThread = pthread_self();
initialized = YES;
return true;
}
@ -88,12 +87,8 @@ void uiQuit(void)
}
// thanks to mikeash in irc.freenode.net/#macdev for suggesting the use of Grand Central Dispatch for this
void uiQueueMain(void (*f)(void *data), void *data)
void uiprivSysQueueMain(void (*f)(void *data), void *data)
{
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc);
return;
}
// dispatch_get_main_queue() is a serial queue so it will not execute multiple uiQueueMain() functions concurrently
// the signature of f matches dispatch_function_t
dispatch_async_f(dispatch_get_main_queue(), data, f);

View File

@ -2,7 +2,6 @@
#include "uipriv_unix.h"
static pthread_t mainThread;
static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c
bool uiprivSysInit(void *options, uiInitError *err)
{
@ -14,7 +13,6 @@ bool uiprivSysInit(void *options, uiInitError *err)
return false;
}
mainThread = pthread_self();
initialized = TRUE;
return true;
}
@ -46,14 +44,10 @@ static gboolean doqueued(gpointer data)
return FALSE;
}
void uiQueueMain(void (*f)(void *data), void *data)
void uiprivSysQueueMain(void (*f)(void *data), void *data)
{
struct queued *q;
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc);
return;
}
q = g_new0(struct queued, 1);
q->f = f;
q->data = data;

View File

@ -45,7 +45,6 @@ static inline void setHInstance(void)
#define uiprivInitReturnHRESULT(err, msg, hr) uiprivInitReturnErrorf(err, "%s: 0x%08I32X", msg, hr)
static DWORD mainThread;
static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c
bool uiprivSysInit(void *options, uiInitError *err)
{
@ -92,7 +91,6 @@ bool uiprivSysInit(void *options, uiInitError *err)
// LONGTERM turn off COM exception handling
*/
mainThread = GetCurrentThreadId();
initialized = TRUE;
return true;
}
@ -124,14 +122,10 @@ void uiQuit(void)
PostQuitMessage(0);
}
void uiQueueMain(void (*f)(void *data), void *data)
void uiprivSysQueueMain(void (*f)(void *data), void *data)
{
HRESULT hr;
if (!initialized) {
uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc);
return;
}
hr = uiprivHrPostMessageW(uiprivUtilWindow, uiprivUtilWindowMsgQueueMain, (WPARAM) f, (LPARAM) data);
if (hr != S_OK) {
// TODO handle error