]> Pileus Git - ~andy/gtk/blob - tests/testthreads.c
stylecontext: Do invalidation on first resize container
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <gtk/gtk.h>
28 #include "config.h"
29
30 #ifdef USE_PTHREADS
31 #include <pthread.h>
32
33 static int nthreads = 0;
34 static pthread_mutex_t nthreads_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36 void
37 close_cb (GtkWidget *w, gint *flag)
38 {
39   *flag = 1;
40 }
41
42 gint
43 delete_cb (GtkWidget *w, GdkEvent *event, gint *flag)
44 {
45   *flag = 1;
46   return TRUE;
47 }
48
49 void *
50 counter (void *data)
51 {
52   gchar *name = data;
53   gint flag = 0;
54   gint counter = 0;
55   gchar buffer[32];
56   
57   GtkWidget *window;
58   GtkWidget *vbox;
59   GtkWidget *label;
60   GtkWidget *button;
61
62   gdk_threads_enter();
63
64   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
65   gtk_window_set_title (GTK_WINDOW (window), name);
66   gtk_widget_set_size_request (window, 100, 50);
67
68   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
69
70   g_signal_connect (window, "delete-event",
71                     G_CALLBACK (delete_cb), &flag);
72
73   gtk_container_add (GTK_CONTAINER (window), vbox);
74
75   label = gtk_label_new ("0");
76   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
77
78   button = gtk_button_new_with_label ("Close");
79   g_signal_connect (button, "clicked",
80                     G_CALLBACK (close_cb), &flag);
81   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
82
83   gtk_widget_show_all (window);
84
85   /* Since flag is only checked or set inside the GTK lock,
86    * we don't have to worry about locking it explicitly
87    */
88   while (!flag)
89     {
90       sprintf(buffer, "%d", counter);
91       gtk_label_set_text (GTK_LABEL (label), buffer);
92       gdk_threads_leave();
93       counter++;
94       /* Give someone else a chance to get the lock next time.
95        * Only necessary because we don't do anything else while
96        * releasing the lock.
97        */
98       sleep(0);
99       
100       gdk_threads_enter();
101     }
102
103   gtk_widget_destroy (window);
104
105   pthread_mutex_lock (&nthreads_mutex);
106   nthreads--;
107   if (nthreads == 0)
108     gtk_main_quit();
109   pthread_mutex_unlock (&nthreads_mutex);
110
111   gdk_threads_leave();
112
113   return NULL;
114 }
115
116 #endif
117
118 int 
119 main (int argc, char **argv)
120 {
121 #ifdef USE_PTHREADS
122   int i;
123
124   if (!gdk_threads_init())
125     {
126       fprintf(stderr, "Could not initialize threads\n");
127       exit(1);
128     }
129
130   gtk_init (&argc, &argv);
131
132   pthread_mutex_lock (&nthreads_mutex);
133
134   for (i=0; i<5; i++)
135     {
136       char buffer[5][10];
137       pthread_t thread;
138       
139       sprintf(buffer[i], "Thread %i", i);
140       if (pthread_create (&thread, NULL, counter, buffer[i]))
141         {
142           fprintf(stderr, "Couldn't create thread\n");
143           exit(1);
144         }
145       nthreads++;
146     }
147
148   pthread_mutex_unlock (&nthreads_mutex);
149
150   gdk_threads_enter();
151   gtk_main();
152   gdk_threads_leave();
153   fprintf(stderr, "Done\n");
154 #else /* !USE_PTHREADS */
155   fprintf (stderr, "GTK+ not compiled with threads support\n");
156   exit (1);
157 #endif /* USE_PTHREADS */  
158
159   return 0;
160 }