Re: g_io_channel_win32_new_messages() & WaitForSingleObject()



Well if any lone googlers ever find this, and want to know what happened. I did fix it with this, but I still think the right way is to some how create a new structure that is "inherits" the GSourceFuncs and adds a place to store the event handle and the protected data (in this case global_int). If there was an example of how to create a source, or some doc on a source life cycle, that would be helpful. I did look at the source, but was quickly lost. I'm guessing the main loop architecture involves multiple "sources", "fds", and "realization callbacks" all being able to exist without "extras". So for instance 1 fd could trigger 5 sources, which each result in glib calling a variable number of callbacks. If only there was more time to spend on these things....

#include <glib.h>
#include <windows.h>

HANDLE windows_event = NULL;
GPollFD g_poll_fd;
GSourceFuncs g_source_funcs;
int global_int = 0;
GMainLoop *main_loop;

gboolean my_source_prepare(GSource *source, gint *timeout_)
{
g_print("1) my_source_prepare()\n");
*timeout_ = -1;
return FALSE;
}

gboolean my_source_check(GSource *source)
{
g_print("1) my_source_check()\n");
if(global_int)
{
 g_print("1)   bam!\n");
//  g_source_remove_poll(source, &g_poll_fd);
 return TRUE;
}

return FALSE;
}

gboolean my_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
g_print("1) my_source_dispatch()\n");
if(callback == NULL)
 return FALSE;

return callback(user_data);
}

gboolean my_source_callback(gpointer data)
{
g_print("1) my_source_callback()\n");
g_main_loop_quit(main_loop);
return FALSE;
}


gboolean timeout_callback(gpointer opaq)
{
g_print("1) still here...\n");

return TRUE;
}

DWORD WINAPI thread_entry(LPVOID lpParameter)
{
g_print("2) I'm thread #%d\n", GetCurrentThreadId());
Sleep(4000);
g_print("2) stuff is going to happen now..\n");
global_int = 1;
SetEvent(windows_event);
g_print("2) I'm done\n");
return 0;
}


int main(int argc, char **argv)
{
DWORD error_code;
gchar *error_string;
GSource *g_source;

g_print("1) I'm thread #%d\n", GetCurrentThreadId());

if((windows_event = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
{
 error_code = GetLastError();
 error_string = g_win32_error_message(error_code);
 g_print("1) CreateEvent() failed, \"%s\"\n", error_string);
 g_free(error_string);
 return 1;
}

main_loop = g_main_loop_new(NULL, FALSE);

g_timeout_add(1000, timeout_callback, NULL);



g_poll_fd.events = 0;
g_poll_fd.revents = G_IO_IN;
g_poll_fd.fd = windows_event;

memset(&g_source_funcs, 0, sizeof(GSourceFuncs));
g_source_funcs.prepare = my_source_prepare;
g_source_funcs.check = my_source_check;
g_source_funcs.dispatch = my_source_dispatch;

g_source = g_source_new(&g_source_funcs, sizeof(GSource));
g_source_attach(g_source, NULL);
g_source_add_poll(g_source, &g_poll_fd);

g_source_set_callback(g_source, my_source_callback, NULL, NULL);

if(CreateThread(NULL, 0, thread_entry, NULL, 0, NULL) == NULL)
{
 error_code = GetLastError();
 error_string = g_win32_error_message(error_code);
 g_print("1) CreateThread() failed, \"%s\"\n", error_string);
 g_free(error_string);
 return 1;
}

g_main_loop_run(main_loop);
return 0;
}


Thomas Stover wrote:
Man, I'm almost there. First, just ignore everything having to do with win32 services. I was misreading the msdn service example. The WaitFor*() is really done on an "event". So here is a stripped down demo of the progress so far. It does what I want, but then crashes before ending the event loop gracefully, which means something needs to be different. I'm sure it has something to do with the size arg to g_source_new() call, since "GSource" is simply:

typedef struct {
} GSource;

Clearly I have no idea what I even want to pass to it.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]