efl
129 строк · 3.3 Кб
1//Compile with:
2//gcc -o efl_thread_4 efl_thread_4.c -g `pkg-config --cflags --libs elementary`
3#include <Elementary.h>
4#include <pthread.h>
5
6static Evas_Object *win = NULL;
7static Evas_Object *rect = NULL;
8
9struct info
10{
11double x, y;
12};
13
14static void my_thread_mainloop_code(void *data);
15
16static pthread_t thread_id;
17static pthread_mutex_t th_lock;
18static int th_exit = 0;
19
20// BEGIN - code running in my custom pthread instance
21//
22static void *
23my_thread_run(void *arg EINA_UNUSED)
24{
25double t = 0.0;
26
27// inside the pthread function lets loop forever incrementing a time point
28for (;;)
29{
30struct info *inf = malloc(sizeof(struct info));
31int do_exit;
32
33if (inf)
34{
35inf->x = 200 + (200 * sin(t));
36inf->y = 200 + (200 * cos(t));
37// now call a function in the mainloop and pass it our allocated
38// data that it will free when it gets it
39ecore_main_loop_thread_safe_call_async
40(my_thread_mainloop_code, inf);
41}
42// and sleep and loop
43usleep(1000);
44t += 0.02;
45// in case someone has asked us to cancel - then cancel this loop
46// co-operatively (cancelling is co-operative)
47pthread_mutex_lock(&th_lock);
48do_exit = th_exit;
49pthread_mutex_unlock(&th_lock);
50if (do_exit) break;
51}
52return NULL;
53}
54//
55// END - code running in my custom pthread instance
56
57static void
58my_thread_new(void)
59{
60pthread_attr_t attr;
61
62pthread_mutex_init(&th_lock, NULL);
63if (pthread_attr_init(&attr) != 0)
64perror("pthread_attr_init");
65if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
66perror("pthread_create");
67}
68
69static void
70my_thread_mainloop_code(void *data)
71{
72struct info *inf = data;
73evas_object_move(rect, inf->x - 50, inf->y - 50);
74free(inf);
75}
76
77// just test cancelling the thread
78static void
79down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
80{
81pthread_mutex_lock(&th_lock);
82th_exit = 1;
83pthread_mutex_unlock(&th_lock);
84}
85
86// on window delete - cancel thread then delete window and exit mainloop
87static void
88del(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
89{
90pthread_mutex_lock(&th_lock);
91th_exit = 1;
92pthread_mutex_unlock(&th_lock);
93evas_object_del(obj);
94elm_exit();
95}
96
97EAPI_MAIN int
98elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
99{
100Evas_Object *o;
101
102elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
103
104win = elm_win_util_standard_add("efl-thread-4", "EFL Thread 4");
105evas_object_smart_callback_add(win, "delete,request", del, NULL);
106
107o = evas_object_rectangle_add(evas_object_evas_get(win));
108evas_object_color_set(o, 50, 80, 180, 255);
109evas_object_resize(o, 100, 100);
110evas_object_show(o);
111// new in the examples - we have a mouse down on the blue box cancel
112// the thread
113evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
114rect = o;
115
116// create custom thread to do some "work on the side"
117my_thread_new();
118
119evas_object_resize(win, 400, 400);
120evas_object_show(win);
121
122elm_run();
123pthread_mutex_lock(&th_lock);
124th_exit = 1;
125pthread_mutex_unlock(&th_lock);
126
127return 0;
128}
129ELM_MAIN()
130