3 #define main testglib_main
7 #define TEST_PRIVATE_THREADS 9
8 #define TEST_PRIVATE_ROUNDS 5
15 GStaticMutex static_mutex = G_STATIC_MUTEX_INIT;
16 G_LOCK_DECLARE (test_me);
18 if (g_thread_supported ())
20 mutex = g_mutex_new ();
25 g_mutex_unlock (mutex);
27 g_static_mutex_lock (static_mutex);
28 g_static_mutex_unlock (static_mutex);
31 g_cond_broadcast (cond);
36 if (g_thread_supported ())
43 #if defined(NSPR) /* we are using nspr threads */
44 /* this option must be specified by hand during compile of
45 testgthread. also note, that you have to link with whatever library
46 nspr is building upon, it might otherwise (as on solaris) lead to
47 run time failure, as the mutex functions are defined in libc, but
48 as noops, that will make some nspr assertions fail. */
52 new_thread (GHookFunc func, gpointer data)
54 PRThread *thread = PR_CreateThread (PR_SYSTEM_THREAD, func, data,
55 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
56 PR_JOINABLE_THREAD, 0);
59 #define join_thread(thread) PR_JoinThread (thread)
60 #define self_thread() PR_GetCurrentThread ()
62 #elif defined(DEFAULTMUTEX) /* we are using solaris threads */
65 new_thread (GHookFunc func, gpointer data)
68 thr_create (NULL, 0, (void *(*)(void *)) func, data, THR_BOUND, &thread);
69 return GUINT_TO_POINTER (thread);
71 #define join_thread(thread) \
72 thr_join ((thread_t)GPOINTER_TO_UINT (thread), NULL, NULL)
73 #define self_thread() GUINT_TO_POINTER (thr_self ())
75 #elif defined(PTHREAD_MUTEX_INITIALIZER) /* we are using posix threads */
77 new_thread(GHookFunc func, gpointer data)
80 pthread_attr_t pthread_attr;
81 pthread_attr_init (&pthread_attr);
82 pthread_attr_setdetachstate (&pthread_attr, PTHREAD_CREATE_JOINABLE);
83 pthread_create (&thread, &pthread_attr, (void *(*)(void *)) func, data);
84 return GUINT_TO_POINTER (thread);
86 #define join_thread(thread) \
87 pthread_join ((pthread_t)GPOINTER_TO_UINT (thread), NULL)
88 #define self_thread() GUINT_TO_POINTER (pthread_self ())
90 #else /* we are not having a thread implementation, do nothing */
92 #define new_thread(func,data) (NULL)
93 #define join_thread(thread) ((void)0)
94 #define self_thread() NULL
98 #define G_MICROSEC 1000000
101 wait_thread (double seconds)
105 GTimeVal current_time;
107 g_get_current_time (¤t_time);
108 mutex = g_mutex_new ();
109 cond = g_cond_new ();
111 current_time.tv_sec += (guint) seconds;
112 seconds -= (guint) seconds;
113 current_time.tv_usec += (guint) (seconds * G_MICROSEC);
114 while (current_time.tv_usec >= G_MICROSEC)
116 current_time.tv_usec -= G_MICROSEC;
117 current_time.tv_sec++;
120 g_mutex_lock (mutex);
121 g_cond_timed_wait (cond, mutex, ¤t_time);
122 g_mutex_unlock (mutex);
124 g_mutex_free (mutex);
129 private_constructor ()
131 gpointer *result = g_new (gpointer, 2);
133 result[1] = self_thread ();
134 g_print ("allocating data for the thread %p.\n", result[1]);
139 private_destructor (gpointer data)
141 gpointer *real = data;
142 g_print ("freeing data for the thread %p.\n", real[1]);
146 GStaticPrivate private_key;
149 test_private_func (void *data)
153 while (i < TEST_PRIVATE_ROUNDS)
155 guint random_value = rand () % 10000;
156 guint *data = g_static_private_get (&private_key);
159 data = private_constructor ();
160 g_static_private_set (&private_key, data, private_destructor);
162 *data = random_value;
164 g_assert (*(guint *) g_static_private_get (&private_key) == random_value);
173 gpointer threads[TEST_PRIVATE_THREADS];
174 for (i = 0; i < TEST_PRIVATE_THREADS; i++)
176 threads[i] = new_thread (test_private_func, GINT_TO_POINTER(i));
178 for (i = 0; i < TEST_PRIVATE_THREADS; i++)
180 join_thread (threads[i]);
190 g_thread_init (NULL);
196 /* later we might want to start n copies of that */
197 testglib_main (0, NULL);