2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
22 #include <asm/unistd.h>
24 #include <sys/syscall.h>
28 #include <glib-object.h>
31 #include <tapi_common.h>
32 #include <ITapiModem.h>
37 static pid_t get_tid ()
39 return syscall (__NR_gettid);
45 GMainContext *context;
48 static void on_cb (TapiHandle *h, int result, void *data, void *user_data)
50 struct meta_data *m = user_data;
54 g_assert (m->tid == get_tid());
56 dbg ("response: %d", *status);
57 g_main_loop_quit (m->loop);
60 static gboolean on_timeout (gpointer user_data)
62 g_assert_not_reached ();
66 static void *do_blocking_request_response (gpointer user_data)
69 TapiHandle *h = user_data;
74 m.context = g_main_context_new();
75 m.loop = g_main_loop_new (m.context, FALSE);
77 g_main_context_push_thread_default (m.context);
84 ret = tel_get_flight_mode (h, on_cb, &m);
85 g_assert (ret == TAPI_API_SUCCESS);
87 timer = g_timeout_source_new_seconds (60);
88 g_source_set_callback (timer, on_timeout, &m, NULL);
89 g_source_attach (timer, m.context);
90 g_source_unref (timer);
92 g_main_loop_run (m.loop);
94 g_main_context_pop_thread_default (m.context);
95 g_main_context_unref (m.context);
96 g_main_loop_unref (m.loop);
104 static void test_each_request (void)
109 /* Request from A thread */
110 th1 = g_thread_new ("test1-A", do_blocking_request_response, NULL);
111 g_assert (th1 != NULL);
113 /* Request from B thread */
114 th2 = g_thread_new ("test1-B", do_blocking_request_response, NULL);
115 g_assert (th2 != NULL);
121 static void test_cross_request (void)
129 g_assert (h != NULL);
131 /* Request from A thread with shared handle */
132 th1 = g_thread_new ("test2-A", do_blocking_request_response, h);
133 g_assert (th1 != NULL);
135 /* Sync Request from Default context */
136 g_assert (tel_get_misc_me_imei_sync (h) != NULL);
138 /* Request from B thread with custom handle */
139 th2 = g_thread_new ("test2-B", do_blocking_request_response, NULL);
140 g_assert (th2 != NULL);
142 /* Request from C thread with shared handle */
143 th3 = g_thread_new ("test3-C", do_blocking_request_response, h);
144 g_assert (th3 != NULL);
153 int main (int argc, char **argv)
155 g_test_init (&argc, &argv, NULL);
158 g_test_add_func ("/thread/each_request", test_each_request);
159 g_test_add_func ("/thread/cross_request", test_cross_request);