aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/examples/ecore_thread_example.c
blob: 7028b259106b3a5b3adfec038bbf16cdca79732c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * gcc -o ecore_thread_example ecore_thread_example.c `pkg-config --cflags --libs ecore`
 */
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <Ecore.h>
#include <Ecore_Getopt.h>

typedef struct
{
   Ecore_Thread *thread_3;
   int msgs_received;
   int max_msgs;
   Eina_Lock mutex;
   Eina_Condition condition;
} App_Data;

typedef struct
{
   Eina_List *list;
} Thread_Data;

typedef struct
{
   char *name;
   char *base;
   Eina_Lock mutex;
} Feedback_Thread_Data;

typedef struct
{
   int all_done;
   Eina_List *list;
} App_Msg;

static void
_local_data_free(void *data)
{
   Thread_Data *td = data;
   char *str;

   EINA_LIST_FREE(td->list, str)
     {
        printf("Freeing string: %s\n", str);
        free(str);
     }
   free(td);
}

static void
_short_job(void *data, Ecore_Thread *th)
{
   Thread_Data *td;
   int i;

   td = ecore_thread_local_data_find(th, "data");
   if (!td)
     {
        td = calloc(1, sizeof(Thread_Data));
        if (!td)
          {
             ecore_thread_cancel(th);
             return;
          }
        ecore_thread_local_data_add(th, "data", td, _local_data_free,
                                    EINA_FALSE);
     }

   for (i = 0; i < 10; i++)
     {
        char buf[200];

        if (ecore_thread_check(th))
          {
             ecore_thread_local_data_del(th, "data");
             break;
          }

        snprintf(buf, sizeof(buf), "Thread %p: String number %d", th, i);
        td->list = eina_list_append(td->list, strdup(buf));
        sleep(1);
     }
}

static void
_feedback_job(void *data, Ecore_Thread *th)
{
   time_t t;
   int i, count;
   Feedback_Thread_Data *ftd = NULL;
   DIR *dir;
   App_Msg *msg;

   count = (int)ecore_thread_global_data_find("count");
   for (i = 0; i < count; i++)
     {
        char buf[32];
        snprintf(buf, sizeof(buf), "data%d", i);
        ftd = ecore_thread_global_data_find(buf);
        if (!ftd)
          continue;
        if (eina_lock_take_try(&ftd->mutex))
          break;
        else
          ftd = NULL;
     }
   if (!ftd)
     return;

   dir = opendir(ftd->base);
   if (!dir)
     goto the_end;

   msg = calloc(1, sizeof(App_Msg));

   t = time(NULL);
   while (time(NULL) < t + 2)
     {
        struct dirent entry, *result;

        if (readdir_r(dir, &entry, &result))
          break;
        if (!result)
          break;

        if (strlen(result->d_name) >= 10)
          msg->list = eina_list_append(msg->list,
                                       strdup(result->d_name));
     }

   closedir(dir);
   ecore_thread_feedback(th, msg);

the_end:
   ecore_thread_global_data_del(ftd->name);
   free(ftd->name);
   free(ftd->base);
   eina_lock_release(&ftd->mutex);
   eina_lock_free(&ftd->mutex);
   free(ftd);
   ecore_thread_reschedule(th);
}

static void
_out_of_pool_job(void *data, Ecore_Thread *th)
{
   App_Data *ad = data;
   App_Msg *msg;

   while (1)
     {
        int msgs;
        eina_condition_wait(&ad->condition);
        msgs = ad->msgs_received;
        eina_lock_release(&ad->mutex);
        if (msgs == ad->max_msgs)
          {
             msg = calloc(1, sizeof(App_Msg));
             msg->all_done = 1;
             ecore_thread_feedback(th, msg);
             return;
          }
     }
}

static void
_print_status(void)
{
   int active, pending_total, pending_feedback, pending_short, available;

   active = ecore_thread_active_get();
   pending_total = ecore_thread_pending_total_get();
   pending_feedback = ecore_thread_pending_feedback_get();
   pending_short = ecore_thread_pending_get();
   available = ecore_thread_available_get();

   printf("Status:\n\t* Active threads: %d\n"
          "\t* Available threads: %d\n"
          "\t* Pending short jobs: %d\n"
          "\t* Pending feedback jobs: %d\n"
          "\t* Pending total: %d\n", active, available, pending_short,
          pending_feedback, pending_total);
}

static void
_feedback_job_msg_cb(void *data, Ecore_Thread *th, void *msg_data)
{
   App_Data *ad = data;
   App_Msg *msg = msg_data;
   char *str;

   if (msg->all_done)
     {
        ecore_main_loop_quit();
        free(msg);
        return;
     }

   _print_status();

   if (!msg->list)
     printf("Received an empty list from thread %p\n", th);
   else
     {
        int i = 0;
        printf("Received %d elements from threads %p (printing first 5):\n",
               eina_list_count(msg->list), th);
        EINA_LIST_FREE(msg->list, str)
          {
             if (i <= 5)
               printf("\t%s\n", str);
             free(str);
             i++;
          }
     }

   eina_lock_take(&ad->mutex);
   ad->msgs_received++;
   eina_condition_signal(&ad->condition);
   eina_lock_release(&ad->mutex);

   free(msg);
}

static void
_thread_end_cb(void *data, Ecore_Thread *th)
{
   App_Data *ad = data;

   printf("Normal termination for thread %p.\n", th);
   if (th == ad->thread_3)
     ad->thread_3 = NULL;
}

static void
_thread_cancel_cb(void *data, Ecore_Thread *th)
{
   App_Data *ad = data;

   printf("Thread %p got cancelled.\n", th);
   if (th == ad->thread_3)
     ad->thread_3 = NULL;
}

static Eina_Bool
_cancel_timer_cb(void *data)
{
   App_Data *ad = data;

   if (ad->thread_3 && !ecore_thread_check(ad->thread_3))
     ecore_thread_cancel(ad->thread_3);

   return EINA_FALSE;
}

static Eina_Bool
_status_timer_cb(void *data)
{
   _print_status();

   return EINA_TRUE;
}

static const Ecore_Getopt optdesc = {
   "ecore_thread_example",
   NULL,
   "0.0",
   "(C) 2011 Enlightenment",
   "Public domain?",
   "Example program for Ecore_Thread",
   0,
   {
      ECORE_GETOPT_STORE_INT('t', "threads", "Max number of threads to run"),
      ECORE_GETOPT_STORE_INT('m', "msgs", "Max number of messages to receive"),
      ECORE_GETOPT_APPEND_METAVAR('p', "path", "Add path for feedback job",
                                  "STRING", ECORE_GETOPT_TYPE_STR),
      ECORE_GETOPT_HELP('h', "help"),
      ECORE_GETOPT_SENTINEL
   }
};

int
main(int argc, char *argv[])
{
   int i, max_threads = 0, max_msgs = 0;
   Eina_Bool opt_quit = EINA_FALSE;
   Eina_List *path_list = NULL;
   App_Data appdata;
   Ecore_Getopt_Value values[] = {
        ECORE_GETOPT_VALUE_INT(max_threads),
        ECORE_GETOPT_VALUE_INT(max_msgs),
        ECORE_GETOPT_VALUE_LIST(path_list),
        ECORE_GETOPT_VALUE_BOOL(opt_quit),
        ECORE_GETOPT_VALUE_NONE
   };

   ecore_init();

   i = ecore_thread_max_get();
   printf("Initial max threads: %d\n", i);

   memset(&appdata, 0, sizeof(App_Data));
   appdata.max_msgs = 1;

   if (ecore_getopt_parse(&optdesc, values, argc, argv) < 0)
     {
        printf("Argument parsing failed\n");
        return 1;
     }

   if (opt_quit)
     return 0;

   if (max_threads)
     {
        ecore_thread_max_set(max_threads);
        printf("Max threads: %d\n", ecore_thread_max_get());
     }
   if (max_msgs)
     appdata.max_msgs = max_msgs;

   if (!path_list)
     {
        Feedback_Thread_Data *ftd;
        ecore_thread_global_data_add("count", (void *)3, NULL, EINA_FALSE);
        ftd = calloc(1, sizeof(Feedback_Thread_Data));
        ftd->name = strdup("data0");
        ftd->base = strdup("/usr/bin");
        eina_lock_new(&ftd->mutex);
        ecore_thread_global_data_add(ftd->name, ftd, NULL, EINA_TRUE);
        ftd = calloc(1, sizeof(Feedback_Thread_Data));
        ftd->name = strdup("data1");
        ftd->base = strdup("/usr/lib");
        eina_lock_new(&ftd->mutex);
        ecore_thread_global_data_add(ftd->name, ftd, NULL, EINA_TRUE);
        ftd = calloc(1, sizeof(Feedback_Thread_Data));
        ftd->name = strdup("data2");
        ftd->base = strdup("/usr/share");
        eina_lock_new(&ftd->mutex);
        ecore_thread_global_data_add(ftd->name, ftd, NULL, EINA_TRUE);
     }
   else
     {
        Feedback_Thread_Data *ftd;
        char *str;
        ecore_thread_global_data_add("count",
                                     (void *)eina_list_count(path_list), NULL,
                                     EINA_FALSE);
        i = 0;
        EINA_LIST_FREE(path_list, str)
          {
             char buf[32];
             snprintf(buf, sizeof(buf), "data%d", i);
             ftd = calloc(1, sizeof(Feedback_Thread_Data));
             ftd->name = strdup(buf);
             ftd->base = strdup(str);
             eina_lock_new(&ftd->mutex);
             ecore_thread_global_data_add(ftd->name, ftd, NULL, EINA_TRUE);
             free(str);
             i++;
          }
     }

   eina_lock_new(&appdata.mutex);
   eina_condition_new(&appdata.condition, &appdata.mutex);

   ecore_thread_feedback_run(_out_of_pool_job, _feedback_job_msg_cb, NULL,
                             NULL, &appdata, EINA_TRUE);

   ecore_thread_run(_short_job, _thread_end_cb, _thread_cancel_cb, &appdata);
   ecore_thread_feedback_run(_feedback_job, _feedback_job_msg_cb,
                             _thread_end_cb, _thread_cancel_cb, &appdata,
                             EINA_FALSE);
   appdata.thread_3 = ecore_thread_run(_short_job, _thread_end_cb,
                                       _thread_cancel_cb, &appdata);
   ecore_thread_feedback_run(_feedback_job, _feedback_job_msg_cb,
                             _thread_end_cb, _thread_cancel_cb, &appdata,
                             EINA_FALSE);

   ecore_timer_add(1.0, _cancel_timer_cb, &appdata);
   ecore_timer_add(2.0, _status_timer_cb, NULL);

   _print_status();

   ecore_main_loop_begin();

   eina_condition_free(&appdata.condition);
   eina_lock_free(&appdata.mutex);

   ecore_shutdown();

   return 0;
}