~ubuntu-branches/ubuntu/quantal/libbonobo/quantal-201207170711

« back to all changes in this revision

Viewing changes to tests/test-thread.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-02-18 14:40:51 UTC
  • mto: (3.1.1 etch) (1.1.25 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050218144051-fo4h9qh2gim8x3wt
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <config.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <string.h>
 
5
#include <libbonobo.h>
 
6
 
 
7
#define NUM_THREADS 8
 
8
#define NUM_GETS    8
 
9
 
 
10
#define PROP_IN_MAIN   1
 
11
#define PROP_NON_MAIN  2
 
12
 
 
13
static GThread *main_thread = NULL;
 
14
 
 
15
static void
 
16
get_fn (BonoboPropertyBag *bag,
 
17
        BonoboArg         *arg,
 
18
        guint              arg_id,
 
19
        CORBA_Environment *ev,
 
20
        gpointer           user_data)
 
21
{
 
22
        fprintf (stderr, "Check property %d\n", arg_id);
 
23
        if (arg_id == PROP_IN_MAIN)
 
24
                g_assert (g_thread_self () == main_thread);
 
25
        else
 
26
                g_assert (g_thread_self () != main_thread);
 
27
        BONOBO_ARG_SET_BOOLEAN (arg, TRUE);
 
28
}
 
29
 
 
30
typedef struct {
 
31
        const char *prop;
 
32
        gboolean    value;
 
33
        Bonobo_PropertyBag pb;
 
34
} TestClosure;
 
35
 
 
36
static void
 
37
test_prop (TestClosure *tc, CORBA_Environment *ev)
 
38
{
 
39
        g_assert (bonobo_pbclient_get_boolean (tc->pb, tc->prop, ev) == tc->value);
 
40
        g_assert (!BONOBO_EX (ev));
 
41
}
 
42
 
 
43
long running_threads;
 
44
G_LOCK_DEFINE_STATIC (running_threads);
 
45
 
 
46
static gpointer
 
47
test_thread (gpointer data)
 
48
{
 
49
        int i;
 
50
        CORBA_Environment ev[1];
 
51
        TestClosure *tc = data;
 
52
 
 
53
        CORBA_exception_init (ev);
 
54
 
 
55
        for (i = 0; i < NUM_GETS; i++)
 
56
                test_prop (tc, ev);
 
57
 
 
58
        G_LOCK (running_threads);
 
59
        running_threads--;
 
60
        G_UNLOCK (running_threads);
 
61
 
 
62
        CORBA_exception_free (ev);
 
63
 
 
64
        return data;
 
65
}
 
66
 
 
67
static gboolean
 
68
wakeup_fn (gpointer data)
 
69
{
 
70
        return TRUE;
 
71
}
 
72
 
 
73
static void
 
74
test_threads (TestClosure *tc)
 
75
{
 
76
        int i;
 
77
        guint wakeup;
 
78
        GThread *threads [NUM_THREADS];
 
79
 
 
80
        running_threads = NUM_THREADS;
 
81
 
 
82
        for (i = 0; i < NUM_THREADS; i++)
 
83
                threads [i] = g_thread_create (test_thread, tc, TRUE, NULL);
 
84
        
 
85
        wakeup = g_timeout_add (100, wakeup_fn, NULL);
 
86
 
 
87
        while (1) {
 
88
                G_LOCK (running_threads);
 
89
                if (running_threads == 0) {
 
90
                        G_UNLOCK (running_threads);
 
91
                        break;
 
92
                }
 
93
                G_UNLOCK (running_threads);
 
94
                g_main_context_iteration (NULL, TRUE);
 
95
        }
 
96
 
 
97
        g_source_remove (wakeup);
 
98
        
 
99
        for (i = 0; i < NUM_THREADS; i++) {
 
100
                if (!(g_thread_join (threads [i]) == tc))
 
101
                        g_error ("Wierd thread join problem '%d'", i);
 
102
        }
 
103
}
 
104
 
 
105
int
 
106
main (int argc, char *argv [])
 
107
{
 
108
        CORBA_Environment  ev[1];
 
109
        BonoboPropertyBag *pb;
 
110
        PortableServer_POA poa;
 
111
        TestClosure        tc;
 
112
 
 
113
        free (malloc (8));
 
114
 
 
115
        CORBA_exception_init (ev);
 
116
 
 
117
        if (bonobo_init (&argc, argv) == FALSE)
 
118
                g_error ("Can not bonobo_init");
 
119
        bonobo_activate ();
 
120
 
 
121
        main_thread = g_thread_self ();
 
122
 
 
123
        {
 
124
                poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_REQUEST);
 
125
                pb = g_object_new (BONOBO_TYPE_PROPERTY_BAG,
 
126
                                   "poa", poa, NULL);
 
127
                bonobo_property_bag_construct
 
128
                        (pb, g_cclosure_new (G_CALLBACK (get_fn), NULL, NULL), NULL,
 
129
                         bonobo_event_source_new ());
 
130
                bonobo_property_bag_add (pb, "non_main", PROP_NON_MAIN, TC_CORBA_boolean,
 
131
                                         NULL, "non_main", BONOBO_PROPERTY_READABLE);
 
132
                tc.prop  = "non_main";
 
133
                tc.value = TRUE;
 
134
                tc.pb    = BONOBO_OBJREF (pb);
 
135
 
 
136
                test_threads (&tc);
 
137
                bonobo_object_unref (pb);
 
138
        }       
 
139
 
 
140
        {
 
141
                pb = bonobo_property_bag_new (get_fn, NULL, NULL);
 
142
                bonobo_property_bag_add (pb, "in_main", PROP_IN_MAIN, TC_CORBA_boolean,
 
143
                                         NULL, "in_main", BONOBO_PROPERTY_READABLE);
 
144
                
 
145
                tc.prop  = "in_main";
 
146
                tc.value = TRUE;
 
147
                tc.pb    = BONOBO_OBJREF (pb);
 
148
                
 
149
                test_prop (&tc, ev);
 
150
                test_threads (&tc);
 
151
 
 
152
                bonobo_object_unref (pb);
 
153
        }
 
154
 
 
155
        CORBA_exception_free (ev);
 
156
 
 
157
        return bonobo_debug_shutdown ();
 
158
}