2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "gst/glib-compat-private.h"
27 static GList *fds = NULL;
28 static GMutex *fdlock;
31 #define MAX_THREADS 100
40 g_mutex_lock (fdlock);
42 for (walk = fds; walk;) {
43 GstPollFD *fd = (GstPollFD *) walk->data;
45 walk = g_list_next (walk);
47 random = (gint) (10.0 * rand () / (RAND_MAX + 1.0));
52 GstPollFD *newfd = g_new0 (GstPollFD, 1);
54 gst_poll_add_fd (set, newfd);
55 fds = g_list_prepend (fds, newfd);
60 if ((gint) (10.0 * rand () / (RAND_MAX + 1.0)) < 2) {
61 gst_poll_remove_fd (set, fd);
62 fds = g_list_remove (fds, fd);
69 gst_poll_fd_ctl_write (set, fd, TRUE);
72 gst_poll_fd_ctl_write (set, fd, FALSE);
75 gst_poll_fd_ctl_read (set, fd, TRUE);
78 gst_poll_fd_ctl_read (set, fd, FALSE);
82 gst_poll_fd_has_closed (set, fd);
85 gst_poll_fd_has_error (set, fd);
88 gst_poll_fd_can_read (set, fd);
91 gst_poll_fd_can_write (set, fd);
94 g_assert_not_reached ();
98 if (g_list_length (fds) < 900) {
99 random = removed + (gint) (2.0 * rand () / (RAND_MAX + 1.0));
101 GstPollFD *newfd = g_new0 (GstPollFD, 1);
103 gst_poll_add_fd (set, newfd);
104 fds = g_list_prepend (fds, newfd);
109 g_mutex_unlock (fdlock);
113 run_test (void *threadid)
115 gint id = GPOINTER_TO_INT (threadid);
119 gint res = gst_poll_wait (set, 10);
122 g_print ("error %d %s\n", errno, g_strerror (errno));
126 if (g_timer_elapsed (timer, NULL) > 0.5) {
127 g_mutex_lock (fdlock);
128 g_print ("active fds :%d\n", g_list_length (fds));
129 g_timer_start (timer);
130 g_mutex_unlock (fdlock);
140 main (gint argc, gchar * argv[])
142 GThread *threads[MAX_THREADS];
146 gst_init (&argc, &argv);
148 fdlock = g_mutex_new ();
149 timer = g_timer_new ();
152 g_print ("usage: %s <num_threads>\n", argv[0]);
156 num_threads = atoi (argv[1]);
158 set = gst_poll_new (TRUE);
160 for (t = 0; t < num_threads; t++) {
161 GError *error = NULL;
163 #if !GLIB_CHECK_VERSION (2, 31, 0)
164 threads[t] = g_thread_create (run_test, GINT_TO_POINTER (t), TRUE, &error);
166 threads[t] = g_thread_try_new ("pollstresstest", run_test,
167 GINT_TO_POINTER (t), &error);
170 printf ("ERROR: g_thread_create() %s\n", error->message);
174 printf ("main(): Created %d threads.\n", t);
176 for (t = 0; t < num_threads; t++) {
177 g_thread_join (threads[t]);