After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 667735 - Add function to wait for condition on a set of GSocket
Add function to wait for condition on a set of GSocket
Status: RESOLVED WONTFIX
Product: glib
Classification: Platform
Component: gio
2.25.x
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2012-01-12 08:02 UTC by Sebastian Dröge (slomo)
Modified: 2012-01-12 13:57 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Bug 667735 – Add function to wait for a condition on a set of GSocket (6.43 KB, patch)
2012-01-12 08:03 UTC, Sebastian Dröge (slomo)
none Details | Review
Bug 667735 – Add function to wait for a condition on a set of GSocket (6.78 KB, patch)
2012-01-12 08:07 UTC, Sebastian Dröge (slomo)
none Details | Review
Bug 667735 – Add function to wait for a condition on a set of GSocket (9.49 KB, patch)
2012-01-12 12:04 UTC, Sebastian Dröge (slomo)
none Details | Review
Bug 667735 – Add function to wait for a condition on a set of GSocket (9.49 KB, patch)
2012-01-12 12:22 UTC, Sebastian Dröge (slomo)
rejected Details | Review

Description Sebastian Dröge (slomo) 2012-01-12 08:02:13 UTC
Hi,

it'd be great if there's a function to wait for a condition on a set of GSocket instead of having this only for a single GSocket. This helps implementing servers that serve clients from a single thread for example.

Attached is a patch that adds this, Windows part is untested though.
Comment 1 Sebastian Dröge (slomo) 2012-01-12 08:03:52 UTC
Created attachment 205062 [details] [review]
Bug 667735 – Add function to wait for a condition on a set of GSocket

Adds g_sockets_condition_wait() for waiting for a condition on
a set of sockets. This is useful for implementing servers that
serve multiple clients from a single thread.

Closes: bgo#667735
Comment 2 Sebastian Dröge (slomo) 2012-01-12 08:07:34 UTC
Created attachment 205064 [details] [review]
Bug 667735 – Add function to wait for a condition on a set of GSocket

Adds g_sockets_condition_wait() for waiting for a condition on
a set of sockets. This is useful for implementing servers that
serve multiple clients from a single thread.

Closes: bgo#667735
Comment 3 Allison Karlitskaya (desrt) 2012-01-12 09:41:06 UTC
GMainContext?
Comment 4 Sebastian Dröge (slomo) 2012-01-12 11:05:55 UTC
What do you mean? There's no integration between a main context and GSocket
Comment 5 Allison Karlitskaya (desrt) 2012-01-12 11:40:50 UTC
g_socket_create_source
Comment 6 Sebastian Dröge (slomo) 2012-01-12 12:03:43 UTC
Oops, sorry I missed that. But still, using a GMainContext in my code for this will be overly complex and ugly. Is there any reason not to add something simpler to poll a set of sockets? New patches comes soon that keeps the amount of code added much lower (g_socket_condition_wait() can be implemented with g_sockets_condition_wait()).
Comment 7 Sebastian Dröge (slomo) 2012-01-12 12:04:07 UTC
Created attachment 205084 [details] [review]
Bug 667735 – Add function to wait for a condition on a set of GSocket

Adds g_sockets_condition_wait() for waiting for a condition on
a set of sockets. This is useful for implementing servers that
serve multiple clients from a single thread.

Closes: bgo#667735
Comment 8 Sebastian Dröge (slomo) 2012-01-12 12:06:30 UTC
Note that we need this in GStreamer to port some plugins from plain sockets to GIO.
Comment 9 Sebastian Dröge (slomo) 2012-01-12 12:22:31 UTC
Created attachment 205089 [details] [review]
Bug 667735 – Add function to wait for a condition on a set of GSocket

Adds g_sockets_condition_wait() for waiting for a condition on
a set of sockets. This is useful for implementing servers that
serve multiple clients from a single thread.

Closes: bgo#667735
Comment 10 Dan Winship 2012-01-12 13:42:28 UTC
(In reply to comment #6)
> Oops, sorry I missed that. But still, using a GMainContext in my code for this
> will be overly complex and ugly.

It's not *that* complex and ugly:

  context = g_main_context_new ();
  for (i = 0; i < nsockets; i++)
    {
      source = g_socket_create_source (sockets[i], condition, NULL);
      g_source_set_dummy_callback (source);
      g_source_attach (source, context);
      g_source_unref (source);
    }
  source = g_cancellable_source_new (cancellable);
  g_source_set_dummy_callback (source);
  g_source_attach (source, context);
  g_source_unref (source);

  g_main_context_iteration (context, TRUE);
  g_main_context_unref (context);

Maybe that could be wrapped up in a "g_something_wait_for_any_source (source_list, cancellable, &error)" method somewhere...

> Is there any reason not to add something
> simpler to poll a set of sockets?

Well, this functions solves your exact use case, but what if you need to wait for some of the sockets to be readable and others to be writable? Or if you need to wait on 3 sockets and 1 pipe?
Comment 11 Sebastian Dröge (slomo) 2012-01-12 13:57:21 UTC
And additionally another GSource for a timeout and detection if the iteration was finished because of a timeout or something else. But I guess you're right, my new function only covers very few use cases and I'll rewrite my code to use a GMainContext instead.