Thoughts about the main loop



I made a server the other day and I i thought maybe glib had some handy main loop to reduce the amount of code for me to write. But I was just confused because it seamed more complex than I expected it to (so took the standard unix approach instead). I also read some thoughts about integrating gnet/linc with glib (related to orbit2 in this list). So some kind of new API might be needed anyway? I would want a main loop function to work something like this:

callback(object,userdata);

main(){
  add_callback_in_case_of(event,object,userdata,callback);
  run_main_loop();
}

"event" is what should happen (for example a socket becomes readable, a signal is received or a child exited).

"object" is what it happened to. It doesn't need to be an object in an OO kind of way, just a signal number or a file handle might do (but preferable something more portable).

(I understand that a pointer to some kind of main loop-structure is also needed for nested main loops to be supported.)

Some flag to indicate if the callback function is reentrant may also be needed and a lot more I guess. But this was most of what I came up with after brainstorming half the night instead of sleeping.

I think this will also be easier to port if a standard set of abstract events are chosen adequately (there should be possible for applications/libraries to add more OS specific features). Lets say there is an event for application termination, which is caused by signals in unix but perhaps in an entirely different way in some other operating systems. All references to poll() could also be hidden (and implemented in all sorts of different ways if needed). I have personally no experience of any other operating systems than linux and solaris (and dos), but I can imagine some very different potential behaviours.



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