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
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/runnables.h"
#include "gate/results.h"

static gate_size_t align_param_size(gate_size_t native_size)
{
    gate_size_t diff = native_size % sizeof(gate_c_maxalign_t);
    if (diff != 0)
    {
        native_size += (sizeof(gate_c_maxalign_t) - diff);
    }
    return native_size;
}

static void gate_runnable_count_sizes(gate_size_t* ptr_param_count, gate_size_t* ptr_param_sizes, gate_va_list_carrier_t* carrier)
{
    void* ptr_param;
    gate_size_t param_size;

    *ptr_param_count = 0;
    *ptr_param_sizes = 0;

    for (;;)
    {
        ptr_param = va_arg(carrier->data, void*);
        if (!ptr_param)
        {
            break;
        }
        param_size = va_arg(carrier->data, size_t);
        param_size = align_param_size(param_size);
        ++(*ptr_param_count);
        *ptr_param_sizes += param_size;
    }
}

static void gate_runnable_copy_params(void** ptr_target_param, void* ptr_target_content, gate_va_list_carrier_t* carrier)
{
    char* ptr = (char*)ptr_target_content;
    void* src_param_ptr;
    size_t src_param_size;

    for (;;)
    {
        src_param_ptr = va_arg(carrier->data, void*);
        if (!src_param_ptr)
        {
            break;
        }
        src_param_size = va_arg(carrier->data, size_t);

        gate_mem_copy((void*)ptr, src_param_ptr, src_param_size);

        *ptr_target_param = (void*)ptr;
        ++ptr_target_param;

        src_param_size = align_param_size(src_param_size);

        ptr += src_param_size;
    }
}

typedef struct gate_runner_disp_impl
{
    GATE_INTERFACE_VTBL(gate_runnable) const* vtbl;

    gate_atomic_int_t			ref_counter;
    gate_runnable_storage_t		store;
} gate_runner_disp_t;

static char const* disp_get_interface_name(void* obj)
{
    (void)obj;
    return GATE_INTERFACE_NAME_RUNNABLE;
}
static void disp_release(void* obj)
{
    gate_runner_disp_t* disp = (gate_runner_disp_t*)obj;
    if (gate_atomic_int_dec(&disp->ref_counter) == 0)
    {
        gate_mem_dealloc(disp);
    }
}
static int disp_retain(void* obj)
{
    gate_runner_disp_t* disp = (gate_runner_disp_t*)obj;
    return gate_atomic_int_inc(&disp->ref_counter);
}

static gate_result_t disp_run(void* obj)
{
    gate_runner_disp_t* disp = (gate_runner_disp_t*)obj;
    gate_result_t result = disp->store.dispatcher(&disp->store);
    return result;
}

static GATE_INTERFACE_VTBL(gate_runnable) gate_runnable_disp_vtbl;
static void gate_init_runnable_disp_vtbl()
{
    if (!gate_runnable_disp_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_runnable) const local_vtbl =
        {
            &disp_get_interface_name,
            &disp_release,
            &disp_retain,
            &disp_run
        };
        gate_runnable_disp_vtbl = local_vtbl;
    }
}

gate_runnable_t* gate_runnable_dispatcher_create(void* ptr_target_func, gate_size_t func_size,
    gate_runnable_dispatcher_t dispatcher,
    ...)
{
    gate_runner_disp_t* ret = NULL;
    gate_size_t param_count;
    gate_size_t param_size;
    gate_va_list_carrier_t carrier;
    gate_size_t total_size;

    do
    {
        va_start(carrier.data, dispatcher);
        gate_runnable_count_sizes(&param_count, &param_size, &carrier);
        va_end(carrier.data);

        total_size = sizeof(gate_runner_disp_t) + sizeof(void*) * param_count + param_size;
        ret = (gate_runner_disp_t*)gate_mem_alloc(total_size);
        if (ret == NULL)
        {
            break;
        }
        gate_mem_clear(ret, total_size);

        gate_init_runnable_disp_vtbl();
        ret->vtbl = &gate_runnable_disp_vtbl;
        gate_atomic_int_init(&ret->ref_counter, 1);

        gate_mem_copy(&ret->store.function_storage, &ptr_target_func, func_size);
        ret->store.dispatcher = dispatcher;

        va_start(carrier.data, dispatcher);
        gate_runnable_copy_params(&ret->store.parameters.params[0], &ret->store.parameters.params[param_count], &carrier);
        va_end(carrier.data);

    } while (0);

    return (gate_runnable_t*)ret;
}







typedef struct gate_taskimpl_class
{
    GATE_INTERFACE_VTBL(gate_task) const* vtbl;

    gate_atomic_int_t			ref_counter;
    gate_atomic_int_t			status;
    gate_result_t				task_result;
    gate_syncevent_t			signal;
    gate_task_storage_t			store;
} gate_taskimpl_t;

static char const* gate_taskimpl_get_interface_name(void* obj)
{
    GATE_UNUSED_ARG(obj);
    return GATE_INTERFACE_NAME_TASK;
}

static void gate_taskimpl_destroy(gate_taskimpl_t* impl)
{
    /* TODO */
    gate_syncevent_destroy(&impl->signal);
    gate_mem_dealloc(impl);
}

static void gate_taskimpl_release(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    if (obj)
    {
        if (0 == gate_atomic_int_dec(&impl->ref_counter))
        {
            gate_taskimpl_destroy(impl);
        }
    }
}
static int gate_taskimpl_retain(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    if (impl)
    {
        return gate_atomic_int_inc(&impl->ref_counter);
    }
    return 0;
}
static gate_bool_t gate_taskimpl_update_state(gate_taskimpl_t* impl, int from_state, int to_state)
{
    gate_int32_t old_state = gate_atomic_int_xchg_if(&impl->status, from_state, to_state);
    return old_state == from_state;
}

static gate_result_t gate_taskimpl_run(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_int32_t old_state;
    gate_int32_t target_state;

    do
    {
        if (!gate_taskimpl_update_state(impl, GATE_TASK_STATUS_PREPARED, GATE_TASK_STATUS_RUNNING))
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }

        impl->task_result = impl->store.dispatcher(&impl->store);

        target_state = GATE_SUCCEEDED(impl->task_result) ? GATE_TASK_STATUS_COMPLETED : GATE_TASK_STATUS_FAILED;
        old_state = gate_atomic_int_xchg_if(&impl->status, GATE_TASK_STATUS_RUNNING, target_state);
        if (GATE_TASK_STATUS_RUNNING == old_state)
        {
            gate_syncevent_set(&impl->signal);
            ret = GATE_RESULT_OK;
        }
        else if (GATE_TASK_STATUS_CANCELED == old_state)
        {
            ret = GATE_RESULT_CANCELED;
        }
        else
        {
            ret = GATE_RESULT_INVALIDSTATE;
        }

    } while (0);

    return ret;
}

static gate_result_t gate_taskimpl_cancel(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;

    if (gate_taskimpl_update_state(impl, GATE_TASK_STATUS_PREPARED, GATE_TASK_STATUS_CANCELED))
    {
        gate_syncevent_set(&impl->signal);
        return GATE_RESULT_OK;
    }

    if (gate_taskimpl_update_state(impl, GATE_TASK_STATUS_RUNNING, GATE_TASK_STATUS_CANCELED))
    {
        gate_syncevent_set(&impl->signal);
        return GATE_RESULT_OK;
    }

    return GATE_RESULT_INVALIDSTATE;
}
static int gate_taskimpl_get_status(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    return gate_atomic_int_get(&impl->status);
}
static gate_bool_t gate_taskimpl_completed(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    int state = gate_atomic_int_get(&impl->status);
    switch (state)
    {
    case GATE_TASK_STATUS_COMPLETED:
    case GATE_TASK_STATUS_CANCELED:
    case GATE_TASK_STATUS_FAILED:
        return true;
    }
    return false;
}
static gate_result_t gate_taskimpl_get_result(void* obj, gate_result_t* ptr_task_result, void const** ptr_task_data)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    gate_result_t task_result;
    void const* task_data = NULL;
    int state = gate_atomic_int_get(&impl->status);
    switch (state)
    {
    case GATE_TASK_STATUS_PREPARED:
    case GATE_TASK_STATUS_RUNNING:
        task_result = GATE_RESULT_INVALIDSTATE;
        break;
    case GATE_TASK_STATUS_COMPLETED:
    case GATE_TASK_STATUS_FAILED:
        task_result = impl->task_result;
        task_data = impl->store.parameters.params[0];
        break;
    case GATE_TASK_STATUS_CANCELED:
        task_result = GATE_RESULT_CANCELED;
        break;
    default:
        return GATE_RESULT_INVALIDSTATE;
    }
    if (ptr_task_result)
    {
        *ptr_task_result = task_result;
    }
    if (ptr_task_data)
    {
        *ptr_task_data = task_data;
    }
    return GATE_RESULT_OK;
}
static gate_result_t gate_taskimpl_wait(void* obj)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    return gate_syncevent_wait(&impl->signal);
}
static gate_result_t gate_taskimpl_timed_wait(void* obj, gate_uint32_t timeout_ms)
{
    gate_taskimpl_t* impl = (gate_taskimpl_t*)obj;
    return gate_syncevent_timed_wait(&impl->signal, timeout_ms);
}

static GATE_INTERFACE_VTBL(gate_task) gate_taskimpl_vtbl;

void gate_init_taskimpl_vtbl()
{
    if (!gate_taskimpl_vtbl.get_interface_name)
    {
        GATE_INTERFACE_VTBL(gate_task) const local_vtbl =
        {
            &gate_taskimpl_get_interface_name,
            &gate_taskimpl_release,
            &gate_taskimpl_retain,

            &gate_taskimpl_run,

            &gate_taskimpl_cancel,
            &gate_taskimpl_get_status,
            &gate_taskimpl_completed,
            &gate_taskimpl_get_result,
            &gate_taskimpl_wait,
            &gate_taskimpl_timed_wait
        };
        gate_taskimpl_vtbl = local_vtbl;
    }
}

gate_task_t* create_task()
{
    gate_task_t* ret = NULL;
    gate_taskimpl_t* impl = NULL;

    do
    {
        gate_init_taskimpl_vtbl();
        impl = (gate_taskimpl_t*)gate_mem_alloc(sizeof(gate_taskimpl_t));
        if (!impl)
        {
            break;
        }
        impl->vtbl = &gate_taskimpl_vtbl;
        gate_atomic_int_init(&impl->ref_counter, 1);
        gate_atomic_int_init(&impl->status, GATE_TASK_STATUS_PREPARED);

        ret = (gate_task_t*)impl;
        impl = NULL;
    } while (0);

    if (impl)
    {
        gate_taskimpl_destroy(impl);
    }

    return ret;
}


gate_task_t* gate_task_dispatcher_create(void* func_ptr, gate_size_t func_ptr_size,
    gate_task_dispatcher_t dispatcher,
    gate_size_t ret_type_size,
    ...)
{
    gate_task_t* ret = NULL;
    gate_taskimpl_t* impl = NULL;

    gate_size_t param_count;
    gate_size_t param_size;
    gate_va_list_carrier_t carrier;
    gate_size_t total_size;
    gate_result_t result;
    char* result_address;
    char* param_store_address;

    do
    {
        va_start(carrier.data, ret_type_size);
        gate_runnable_count_sizes(&param_count, &param_size, &carrier);
        va_end(carrier.data);

        ret_type_size = align_param_size(ret_type_size);
        total_size = sizeof(gate_taskimpl_t) + sizeof(void*) * (param_count + 1) + ret_type_size + param_size;
        impl = (gate_taskimpl_t*)gate_mem_alloc(total_size);
        if (impl == NULL)
        {
            break;
        }
        gate_mem_clear(impl, total_size);

        gate_init_taskimpl_vtbl();

        impl->vtbl = &gate_taskimpl_vtbl;
        gate_atomic_int_init(&impl->ref_counter, 1);
        gate_atomic_int_init(&impl->status, GATE_TASK_STATUS_PREPARED);

        result = gate_syncevent_create(&impl->signal, false);
        if (GATE_FAILED(result))
        {
            break;
        }

        gate_mem_copy(&impl->store.function_storage, &func_ptr, func_ptr_size);
        impl->store.dispatcher = dispatcher;

        result_address = (char*)&impl->store.parameters.params[param_count + 1];
        param_store_address = result_address + ret_type_size;
        impl->store.parameters.params[0] = result_address;

        va_start(carrier.data, ret_type_size);
        gate_runnable_copy_params(&impl->store.parameters.params[1], param_store_address, &carrier);
        va_end(carrier.data);

        ret = (gate_task_t*)impl;
        impl = NULL;

    } while (0);

    if (impl != NULL)
    {
        gate_taskimpl_destroy(impl);
    }

    return ret;
}