summaryrefslogtreecommitdiff
path: root/src/bin/evas/evas_cserve2_requests.c
blob: 9a8eb2f4c0e2620b94defa80aff3d45d69c6f78c (plain) (blame)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "evas_cserve2.h"

#define MAX_SLAVES 1

typedef enum
{
   SLAVE_IMAGE,
   SLAVE_FONT,
   SLAVE_NONE
} Slave_Type;

struct _Slave_Worker
{
   Slave_Type type;
   void *data;
   Slave *slave;
   Eina_Binbuf *ret;
   int ret_size;
   Eina_Bool done;
   Eina_Bool delete_me;
};

typedef struct _Slave_Worker Slave_Worker;

/* This struct is used to match font request types to the respective slave
 * type, and the message type that will be used for that request. The order
 * of the request types on it is the order in which these requests will
 * be processed.
 */
static const struct _Request_Match
{
   Slave_Request_Type rtype;
   Slave_Type stype;
   Slave_Command ctype;
   int require_spares; /* for speculative operations, will require to leave at
                          least this number of slaves always available */
} _request_match[] =
{
   { CSERVE2_REQ_IMAGE_OPEN, SLAVE_IMAGE, IMAGE_OPEN, 0 },
   { CSERVE2_REQ_IMAGE_LOAD, SLAVE_IMAGE, IMAGE_LOAD, 0 },
   { CSERVE2_REQ_IMAGE_SPEC_LOAD, SLAVE_IMAGE, IMAGE_LOAD, 1 },
   { CSERVE2_REQ_FONT_LOAD, SLAVE_FONT, FONT_LOAD, 0 },
   { CSERVE2_REQ_FONT_GLYPHS_LOAD, SLAVE_FONT, FONT_GLYPHS_LOAD, 0 },
   { CSERVE2_REQ_LAST, SLAVE_NONE, ERROR, 0 }
};

static Slave *_create_image_slave(void *data);
static Slave *_create_font_slave(void *data);

static struct _Worker
{
   Slave_Type type;
   unsigned int max;
   Eina_List *idle;
   Eina_List *working;
   Slave *(*create_slave)(void *data);
} _workers[] =
{
   { SLAVE_IMAGE, 3, NULL, NULL, _create_image_slave },
   { SLAVE_FONT, 1, NULL, NULL, _create_font_slave },
};

struct _Slave_Request
{
   EINA_INLIST;
   Slave_Request_Type type;
   void *data;
   void *msg;
   Eina_List *waiters;
   Eina_Bool processing;
   Slave_Request_Funcs *funcs;
   Slave_Request *dependency;
   Eina_List *dependents; /* list of requests that depend on this one finishing */
   Eina_Bool locked : 1; /* locked waiting for a dependency request to finish */
   Eina_Bool cancelled : 1;
};

struct _Waiter
{
   unsigned int rid;
   Client *client;
};

typedef struct _Waiter Waiter;

struct _Request_Queue
{
   Eina_Inlist *waiting;
   Eina_Inlist *processing; // TODO: Check if is there any use for this list.
};

typedef struct _Request_Queue Request_Queue;

static Request_Queue *requests = NULL;
// static Eina_List *processing = NULL;

static void _cserve2_requests_process(void);

static void
_request_waiter_add(Slave_Request *req, Client *client, unsigned int rid)
{
   Waiter *w = malloc(sizeof(*w));

   DBG("Add waiter to request. Client: %d, rid: %d", client->id, rid);

   w->client = client;
   w->rid = rid;

   req->waiters = eina_list_append(req->waiters, w);
}

Slave_Request *
cserve2_request_add(Slave_Request_Type type, unsigned int rid, Client *client, Slave_Request *dep, Slave_Request_Funcs *funcs, void *data)
{
   Slave_Request *req, *r;

   req = NULL;

   /* Check if this request was already being processed. */
   EINA_INLIST_FOREACH(requests[type].processing, r)
     {
        if (r->data != data)
          continue;

        req = r;
        break;
     }

   /* Check if this request was already waiting to be processed. */
   if (!req)
     {
        EINA_INLIST_FOREACH(requests[type].waiting, r)
          {
             if (r->data != data)
               continue;

             req = r;
             break;
          }
     }

   /* create new request */
   if (!req)
     {
        DBG("Add request for rid: %d", rid);
        req = calloc(1, sizeof(*req));
        req->type = type;
        req->data = data;
        req->waiters = NULL;
        req->processing = EINA_FALSE;
        req->funcs = funcs;
        requests[type].waiting = eina_inlist_append(requests[type].waiting,
                                                    EINA_INLIST_GET(req));
     }

   if (dep && !req->dependency)
     {
        req->locked = EINA_TRUE;
        dep->dependents = eina_list_append(dep->dependents, req);
        req->dependency = dep;
     }

   if (client && rid)
     _request_waiter_add(req, client, rid);

   _cserve2_requests_process();

   return req;
}

void
cserve2_request_waiter_add(Slave_Request *req, unsigned int rid, Client *client)
{
   _request_waiter_add(req, client, rid);
}

void
cserve2_request_type_set(Slave_Request *req, Slave_Request_Type type)
{
   Eina_Inlist **from, **to;

   if (req->processing || (type == req->type))
     return;

   from = &requests[req->type].waiting;
   to = &requests[type].waiting;

   req->type = type;
   *from = eina_inlist_remove(*from, EINA_INLIST_GET(req));
   *to = eina_inlist_append(*to, EINA_INLIST_GET(req));
}

static void
_request_dependents_cancel(Slave_Request *req, Error_Type err)
{
   Slave_Request *dep;

   EINA_LIST_FREE(req->dependents, dep)
     {
        dep->locked = EINA_FALSE;
        dep->dependency = NULL;
        /* Maybe we need a better way to inform the creator of the request
         * that it was cancelled because its dependency failed? */
        cserve2_request_cancel_all(dep, err);
     }
}

void
cserve2_request_cancel(Slave_Request *req, Client *client, Error_Type err)
{
   Eina_List *l, *l_next;
   Waiter *w;

   if (req->funcs && req->funcs->error)
     req->funcs->error(req->data, err);

   EINA_LIST_FOREACH_SAFE(req->waiters, l, l_next, w)
     {
        if (w->client->id == client->id)
          {
             DBG("Removing answer from waiter client: %d, rid: %d",
                 client->id, w->rid);
             cserve2_client_error_send(w->client, w->rid, err);
             req->waiters = eina_list_remove_list(req->waiters, l);
             free(w);
          }
     }

   // TODO: When we have speculative preload, there may be no waiters,
   // so we need a flag or something else to make things still load.
   if ((!req->waiters) && (!req->processing))
     {
        Eina_Inlist **reqlist = &requests[req->type].waiting;
        *reqlist = eina_inlist_remove(*reqlist, EINA_INLIST_GET(req));
        // TODO: If the request is being processed, it can't be deleted. Must
        // be marked as delete_me instead.
        req->funcs->msg_free(req->msg, req->data);

        if (req->dependency)
          req->dependency->dependents = eina_list_remove(
             req->dependency->dependents, req);

        _request_dependents_cancel(req, err);

        free(req);
     }
   else if (!req->waiters)
     req->cancelled = EINA_TRUE;
}

void
cserve2_request_cancel_all(Slave_Request *req, Error_Type err)
{
   Waiter *w;

   DBG("Removing all answers.");

   if (req->funcs && req->funcs->error)
     req->funcs->error(req->data, err);

   EINA_LIST_FREE(req->waiters, w)
     {
        DBG("Removing answer from waiter client: %d, rid: %d",
            w->client->id, w->rid);
        cserve2_client_error_send(w->client, w->rid, err);
        free(w);
     }

   _request_dependents_cancel(req, err);

   if (req->processing)
     {
        req->cancelled = EINA_TRUE;
        return;
     }

   if (req->dependency)
     req->dependency->dependents = eina_list_remove(
        req->dependency->dependents, req);

   requests[req->type].waiting = eina_inlist_remove(
      requests[req->type].waiting, EINA_INLIST_GET(req));
   req->funcs->msg_free(req->msg, req->data);
   free(req);
}

void
cserve2_request_dependents_drop(Slave_Request *req, Slave_Request_Type type)
{
   Slave_Request *dep;
   Eina_List *l, *l_next;

   if (type != CSERVE2_REQ_IMAGE_SPEC_LOAD)
     {
        CRIT("Only CSERVE2_REQ_IMAGE_SPEC_LOAD is supported.");
        return;
     }

   EINA_LIST_FOREACH_SAFE(req->dependents, l, l_next, dep)
     {
        if (dep->type == type)
          {
             req->dependents = eina_list_remove_list(req->dependents, l);

             if (dep->processing)
               dep->cancelled = EINA_TRUE;
             else
               {
                  cserve2_entry_request_drop(dep->data, type);
                  requests[type].waiting = eina_inlist_remove(
                           requests[type].waiting, EINA_INLIST_GET(dep));
                  dep->funcs->msg_free(dep->msg, dep->data);
                  free(dep);
               }
          }
     }
}

void
cserve2_requests_init(void)
{
   DBG("Initializing requests.");
   requests = calloc(CSERVE2_REQ_LAST, sizeof(*requests));
}

void
cserve2_requests_shutdown(void)
{
   DBG("Shutting down requests.");
   free(requests);
}

static void
_cserve2_request_failed(Slave_Request *req, Error_Type type)
{
   Waiter *w;

   req->funcs->error(req->data, type);

   EINA_LIST_FREE(req->waiters, w)
     {
        cserve2_client_error_send(w->client, w->rid, type);
        free(w);
     }

   req->funcs->msg_free(req->msg, req->data);
   requests[req->type].processing = eina_inlist_remove(
      requests[req->type].processing, EINA_INLIST_GET(req));

   _request_dependents_cancel(req, type);

   free(req);
}

static void
_slave_read_cb(Slave *s EINA_UNUSED, Slave_Command cmd, void *msg, void *data)
{
   Slave_Worker *sw = data;
   Slave_Request *dep, *req = sw->data;
   Eina_List **working, **idle;
   Waiter *w;
   Msg_Base *resp = NULL;
   int resp_size;

   if (req->cancelled)
     goto free_it;

   if (cmd == ERROR)
     {
        Error_Type *err = msg;
        WRN("Received error %d from slave, for request type %d.",
            *err, req->type);
        req->funcs->error(req->data, *err);
     }
   else
     {
        DBG("Received response from slave for message type %d.", req->type);
        resp = req->funcs->response(req->data, msg, &resp_size);
     }

   EINA_LIST_FREE(req->waiters, w)
     {
        if (cmd == ERROR)
          {
             Error_Type *err = msg;
             cserve2_client_error_send(w->client, w->rid, *err);
          }
        else
          {
             resp->rid = w->rid;
             cserve2_client_send(w->client, &resp_size, sizeof(resp_size));
             cserve2_client_send(w->client, resp, resp_size);
          }
        free(w);
     }

   free(resp);

free_it:
   req->funcs->msg_free(req->msg, req->data);

   // FIXME: We shouldn't free this message directly, it must be freed by a
   // callback.
   free(msg);
   requests[req->type].processing = eina_inlist_remove(
      requests[req->type].processing, EINA_INLIST_GET(req));

   EINA_LIST_FREE(req->dependents, dep)
     {
        dep->locked = EINA_FALSE;
        dep->dependency = NULL;
     }

   free(req);
   sw->data = NULL;

   working = &_workers[sw->type].working;
   idle = &_workers[sw->type].idle;
   *working = eina_list_remove(*working, sw);
   *idle = eina_list_append(*idle, sw);

   _cserve2_requests_process();
}

static void
_slave_dead_cb(Slave *s EINA_UNUSED, void *data)
{
   Slave_Worker *sw = data;
   Slave_Request *req = sw->data;
   Eina_List **working = &_workers[sw->type].working;

   if (req)
     _cserve2_request_failed(req, CSERVE2_LOADER_DIED);

   *working = eina_list_remove(*working, sw);
   free(sw);
}

static Slave *
_create_image_slave(void *data)
{
   char *exe;
   Slave *slave;

   exe = getenv("EVAS_CSERVE2_SLAVE");
   if (!exe) exe = "evas_cserve2_slave";

   slave = cserve2_slave_run(exe, _slave_read_cb,
                             _slave_dead_cb, data);

   return slave;
}

static Slave *
_create_font_slave(void *data)
{
   Slave *slave;

   slave = cserve2_slave_thread_run(cserve2_font_slave_cb, NULL,
                                    _slave_read_cb, _slave_dead_cb,
                                    data);

   return slave;
}

static Slave_Worker *
_slave_for_request_create(Slave_Type type)
{
   Slave_Worker *sw;
   Slave *slave;

   sw = calloc(1, sizeof(Slave_Worker));
   if (!sw) return NULL;

   slave = _workers[type].create_slave(sw);
   if (!slave)
     {
        ERR("Could not launch slave process");
        free(sw);
        return NULL;
     }

   sw->slave = slave;
   sw->type = type;
   _workers[type].idle = eina_list_append(_workers[type].idle, sw);

   return sw;
}

static Eina_Bool
_cserve2_request_dispatch(Slave_Worker *sw, Slave_Command ctype, Slave_Request *req)
{
   int size;
   char *slave_msg = req->funcs->msg_create(req->data, &size);


   DBG("dispatching message of type %d to slave.", req->type);
   if (!slave_msg)
     {
        ERR("Could not create slave message for request type %d.", req->type);
        return EINA_FALSE;
     }

   req->msg = slave_msg;
   sw->data = req;
   cserve2_slave_send(sw->slave, ctype, slave_msg, size);
   req->processing = EINA_TRUE;

   return EINA_TRUE;
}

static void
_cserve2_requests_process(void)
{
    unsigned int rtype, j;

    for (rtype = 0; rtype < CSERVE2_REQ_LAST; rtype++)
      {
         Slave_Type type = SLAVE_NONE;
         Slave_Command ctype;
         unsigned int max_workers;
         Eina_List **idle, **working;
         Eina_Inlist *itr;
         Slave_Request *req;

         for (j = 0; _request_match[j].rtype != CSERVE2_REQ_LAST; j++)
           {
              if (_request_match[j].rtype == rtype)
                {
                   type = _request_match[j].stype;
                   ctype = _request_match[j].ctype;
                   break;
                }
           }

         if (type == SLAVE_NONE)
           continue;

         if (!requests[rtype].waiting)
           continue;

         /* Now we have the worker type to use (image or font), and the list
          * of requests to process. Just process as many requests as we can.
          */
         max_workers = _workers[type].max;
         idle = &_workers[type].idle;
         working = &_workers[type].working;

         EINA_INLIST_FOREACH_SAFE(requests[rtype].waiting, itr, req)
           {
              Slave_Worker *sw;

              if (eina_list_count(*working) >=
                  (max_workers - _request_match[j].require_spares))
                break;

              if (req->locked)
                continue;

              requests[rtype].waiting = eina_inlist_remove(
                 requests[rtype].waiting, EINA_INLIST_GET(req));
              requests[rtype].processing = eina_inlist_append(
                 requests[rtype].processing, EINA_INLIST_GET(req));

              if (!(*idle))
                sw = _slave_for_request_create(type);

              if (!(*idle))
                {
                   ERR("No idle slave available to process request type %d.",
                       rtype);
                   _cserve2_request_failed(req, CSERVE2_GENERIC);
                   continue;
                }

              sw = eina_list_data_get(*idle);
              if (!_cserve2_request_dispatch(sw, ctype, req))
                {
                   ERR("Could not dispatch request.");
                   _cserve2_request_failed(req, CSERVE2_GENERIC);
                   continue;
                }

              *idle = eina_list_remove_list(*idle, *idle);
              *working = eina_list_append(*working, sw);
           }
      }
}