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