]> Pileus Git - ~andy/gtk/blob - tests/testthreads.c
Commented out testthreads from the build process, since we won't have any
[~andy/gtk] / tests / testthreads.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <gtk/gtk.h>
22 #include "config.h"
23
24 #ifdef USE_PTHREADS
25 #include <pthread.h>
26
27 static int nthreads = 0;
28 static pthread_mutex_t nthreads_mutex = PTHREAD_MUTEX_INITIALIZER;
29
30 void
31 close_cb (GtkWidget *w, gint *flag)
32 {
33   *flag = 1;
34 }
35
36 gint
37 delete_cb (GtkWidget *w, GdkEvent *event, gint *flag)
38 {
39   *flag = 1;
40   return TRUE;
41 }
42
43 void *
44 counter (void *data)
45 {
46   gchar *name = data;
47   gint flag = 0;
48   gint counter = 0;
49   gchar buffer[32];
50   
51   GtkWidget *window;
52   GtkWidget *vbox;
53   GtkWidget *label;
54   GtkWidget *button;
55
56   gtk_threads_enter();
57
58   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
59   gtk_window_set_title (GTK_WINDOW (window), name);
60   gtk_widget_set_usize (window, 100, 50);
61
62   vbox = gtk_vbox_new (FALSE, 0);
63
64   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
65                       GTK_SIGNAL_FUNC (delete_cb), &flag);
66
67   gtk_container_add (GTK_CONTAINER (window), vbox);
68
69   label = gtk_label_new ("0");
70   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
71
72   button = gtk_button_new_with_label ("Close");
73   gtk_signal_connect (GTK_OBJECT (button), "clicked",
74                       GTK_SIGNAL_FUNC (close_cb), &flag);
75   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
76
77   gtk_widget_show_all (window);
78
79   /* Since flag is only checked or set inside the GTK lock,
80    * we don't have to worry about locking it explicitly
81    */
82   while (!flag)
83     {
84       sprintf(buffer, "%d", counter);
85       gtk_label_set (GTK_LABEL (label), buffer);
86       gtk_threads_leave();
87       counter++;
88       /* Give someone else a chance to get the lock next time.
89        * Only necessary because we don't do anything else while
90        * releasing the lock.
91        */
92       sleep(0);
93       
94       gtk_threads_enter();
95     }
96
97   gtk_widget_destroy (window);
98
99   pthread_mutex_lock (&nthreads_mutex);
100   nthreads--;
101   if (nthreads == 0)
102     gtk_main_quit();
103   pthread_mutex_unlock (&nthreads_mutex);
104
105   gtk_threads_leave();
106
107   return NULL;
108 }
109
110 #endif
111
112 int 
113 main (int argc, char **argv)
114 {
115 #ifdef USE_PTHREADS
116   int i;
117
118   if (!gtk_threads_init())
119     {
120       fprintf(stderr, "Could not initialize threads\n");
121       exit(1);
122     }
123
124   gtk_init (&argc, &argv);
125
126   pthread_mutex_lock (&nthreads_mutex);
127
128   for (i=0; i<5; i++)
129     {
130       char buffer[5][10];
131       pthread_t thread;
132       
133       sprintf(buffer[i], "Thread %i", i);
134       if (pthread_create (&thread, NULL, counter, buffer[i]))
135         {
136           fprintf(stderr, "Couldn't create thread\n");
137           exit(1);
138         }
139       nthreads++;
140     }
141
142   pthread_mutex_unlock (&nthreads_mutex);
143
144   gtk_threads_enter();
145   gtk_main();
146   gtk_threads_leave();
147   fprintf(stderr, "Done\n");
148 #else /* !USE_PTHREADS */
149   fprintf (stderr, "GTK+ not compiled with threads support\n");
150   exit (1);
151 #endif /* USE_PTHREADS */  
152
153   return 0;
154 }