efl

Форк
0
/
efl_thread_4.c 
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

6
static Evas_Object *win = NULL;
7
static Evas_Object *rect = NULL;
8

9
struct info
10
{
11
   double x, y;
12
};
13

14
static void my_thread_mainloop_code(void *data);
15

16
static pthread_t thread_id;
17
static pthread_mutex_t th_lock;
18
static int th_exit = 0;
19

20
// BEGIN - code running in my custom pthread instance
21
//
22
static void *
23
my_thread_run(void *arg EINA_UNUSED)
24
{
25
   double t = 0.0;
26

27
   // inside the pthread function lets loop forever incrementing a time point
28
   for (;;)
29
     {
30
        struct info *inf = malloc(sizeof(struct info));
31
        int do_exit;
32

33
        if (inf)
34
          {
35
             inf->x = 200 + (200 * sin(t));
36
             inf->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
39
             ecore_main_loop_thread_safe_call_async
40
                (my_thread_mainloop_code, inf);
41
          }
42
        // and sleep and loop
43
        usleep(1000);
44
        t += 0.02;
45
        // in case someone has asked us to cancel - then cancel this loop
46
        // co-operatively (cancelling is co-operative)
47
        pthread_mutex_lock(&th_lock);
48
        do_exit = th_exit;
49
        pthread_mutex_unlock(&th_lock);
50
        if (do_exit) break;
51
     }
52
   return NULL;
53
}
54
//
55
// END - code running in my custom pthread instance
56

57
static void
58
my_thread_new(void)
59
{
60
   pthread_attr_t attr;
61

62
   pthread_mutex_init(&th_lock, NULL);
63
   if (pthread_attr_init(&attr) != 0)
64
     perror("pthread_attr_init");
65
   if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
66
     perror("pthread_create");
67
}
68

69
static void
70
my_thread_mainloop_code(void *data)
71
{
72
   struct info *inf = data;
73
   evas_object_move(rect, inf->x - 50, inf->y - 50);
74
   free(inf);
75
}
76

77
// just test cancelling the thread
78
static void
79
down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
80
{
81
   pthread_mutex_lock(&th_lock);
82
   th_exit = 1;
83
   pthread_mutex_unlock(&th_lock);
84
}
85

86
// on window delete - cancel thread then delete window and exit mainloop
87
static void
88
del(void *data EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
89
{
90
   pthread_mutex_lock(&th_lock);
91
   th_exit = 1;
92
   pthread_mutex_unlock(&th_lock);
93
   evas_object_del(obj);
94
   elm_exit();
95
}
96

97
EAPI_MAIN int
98
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
99
{
100
   Evas_Object *o;
101

102
   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
103

104
   win = elm_win_util_standard_add("efl-thread-4", "EFL Thread 4");
105
   evas_object_smart_callback_add(win, "delete,request", del, NULL);
106

107
   o = evas_object_rectangle_add(evas_object_evas_get(win));
108
   evas_object_color_set(o, 50, 80, 180, 255);
109
   evas_object_resize(o, 100, 100);
110
   evas_object_show(o);
111
   // new in the examples - we have a mouse down on the blue box cancel
112
   // the thread
113
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
114
   rect = o;
115

116
   // create custom thread to do some "work on the side"
117
   my_thread_new();
118

119
   evas_object_resize(win, 400, 400);
120
   evas_object_show(win);
121

122
   elm_run();
123
   pthread_mutex_lock(&th_lock);
124
   th_exit = 1;
125
   pthread_mutex_unlock(&th_lock);
126

127
   return 0;
128
}
129
ELM_MAIN()
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.