5 #define main testglib_main
9 #define TEST_PRIVATE_THREADS 9
10 #define TEST_PRIVATE_ROUNDS 5
17 GStaticMutex static_mutex
= G_STATIC_MUTEX_INIT
;
18 G_LOCK_DEFINE (test_me
);
20 if (g_thread_supported ())
22 mutex
= g_mutex_new ();
27 g_mutex_unlock (mutex
);
29 g_static_mutex_lock (&static_mutex
);
30 g_static_mutex_unlock (&static_mutex
);
33 g_cond_broadcast (cond
);
38 if (g_thread_supported ())
45 #if defined(G_THREADS_IMPL_NSPR)
46 #warning "note, that you have to link with whatever library"
47 #warning "nspr is building upon, it might otherwise (as on solaris) lead to"
48 #warning "run time failure, as the mutex functions are defined in libc, but"
49 #warning "as noops, that will make some nspr assertions fail."
53 new_thread (GHookFunc func
, gpointer data
)
55 PRThread
*thread
= PR_CreateThread (PR_SYSTEM_THREAD
, func
, data
,
56 PR_PRIORITY_NORMAL
, PR_LOCAL_THREAD
,
57 PR_JOINABLE_THREAD
, 0);
60 #define join_thread(thread) PR_JoinThread (thread)
61 #define self_thread() PR_GetCurrentThread ()
63 #elif defined(G_THREADS_IMPL_SOLARIS)
67 new_thread (GHookFunc func
, gpointer data
)
70 thr_create (NULL
, 0, (void *(*)(void *)) func
, data
, THR_BOUND
, &thread
);
71 return GUINT_TO_POINTER (thread
);
73 #define join_thread(thread) \
74 thr_join ((thread_t)GPOINTER_TO_UINT (thread), NULL, NULL)
75 #define self_thread() GUINT_TO_POINTER (thr_self ())
77 #elif defined(G_THREADS_IMPL_POSIX)
81 new_thread(GHookFunc func
, gpointer data
)
84 pthread_attr_t pthread_attr
;
85 pthread_attr_init (&pthread_attr
);
86 /* This is the default, it seems, so leave that out for now
87 pthread_attr_setdetachstate (&pthread_attr, PTHREAD_CREATE_JOINABLE);
89 pthread_create (&thread
, &pthread_attr
, (void *(*)(void *)) func
, data
);
90 return GUINT_TO_POINTER (thread
);
92 #define join_thread(thread) \
93 pthread_join ((pthread_t)GPOINTER_TO_UINT (thread), NULL)
94 #define self_thread() GUINT_TO_POINTER (pthread_self ())
96 #else /* we are not having a thread implementation, do nothing */
98 #define new_thread(func,data) (NULL)
99 #define join_thread(thread) ((void)0)
100 #define self_thread() NULL
104 #define G_MICROSEC 1000000
107 wait_thread (double seconds
)
111 GTimeVal current_time
;
113 g_get_current_time (¤t_time
);
114 mutex
= g_mutex_new ();
115 cond
= g_cond_new ();
117 current_time
.tv_sec
+= (guint
) seconds
;
118 seconds
-= (guint
) seconds
;
119 current_time
.tv_usec
+= (guint
) (seconds
* G_MICROSEC
);
120 while (current_time
.tv_usec
>= G_MICROSEC
)
122 current_time
.tv_usec
-= G_MICROSEC
;
123 current_time
.tv_sec
++;
126 g_mutex_lock (mutex
);
127 g_cond_timed_wait (cond
, mutex
, ¤t_time
);
128 g_mutex_unlock (mutex
);
130 g_mutex_free (mutex
);
135 private_constructor (void)
137 gpointer
*result
= g_new (gpointer
, 2);
139 result
[1] = self_thread ();
140 g_print ("allocating data for the thread %p.\n", result
[1]);
145 private_destructor (gpointer data
)
147 gpointer
*real
= data
;
148 g_print ("freeing data for the thread %p.\n", real
[1]);
152 GStaticPrivate private_key
;
155 test_private_func (void *data
)
158 static unsigned int seed
= 0;
162 g_get_current_time (&now
);
166 while (i
< TEST_PRIVATE_ROUNDS
)
169 guint random_value
= rand_r (&seed
) % 10000;
171 guint random_value
= rand() % 10000;
173 guint
*data
= g_static_private_get (&private_key
);
176 data
= private_constructor ();
177 g_static_private_set (&private_key
, data
, private_destructor
);
179 *data
= random_value
;
181 g_assert (*(guint
*) g_static_private_get (&private_key
) == random_value
);
190 gpointer threads
[TEST_PRIVATE_THREADS
];
191 for (i
= 0; i
< TEST_PRIVATE_THREADS
; i
++)
193 threads
[i
] = new_thread (test_private_func
, GINT_TO_POINTER(i
));
195 for (i
= 0; i
< TEST_PRIVATE_THREADS
; i
++)
197 join_thread (threads
[i
]);
207 g_thread_init (NULL
);
213 /* later we might want to start n copies of that */
214 testglib_main (0, NULL
);