forked from unikraft/lib-pthread-embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pte_osal.c
675 lines (528 loc) · 14.5 KB
/
pte_osal.c
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/* SPDX-License-Identifier: LGPL-2.0-or-later */
/*
* Unikraft port of POSIX Threads Library for embedded systems
* Copyright(C) 2019 Costin Lupu, University Politehnica of Bucharest
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/*
* This port is derived from hermit/pte_osal.c.
*/
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <uk/essentials.h>
#include <uk/init.h>
#include <uk/arch/time.h>
#include <uk/arch/atomic.h>
#include <uk/print.h>
#include <uk/thread.h>
#include "pte_osal.h"
#include "pthread.h"
#include "tls-helper.h"
typedef struct pte_thread_data {
/* thread routine */
pte_osThreadEntryPoint entry_point;
/* thread routine arguments */
void *argv;
/* Unikraft thread */
struct uk_thread *uk_thread;
/* TLS */
void *tls;
/* Semaphore for triggering thread start */
struct uk_semaphore start_sem;
/* Semaphore for cancellation */
struct uk_semaphore cancel_sem;
/* Is non-zero if thread exited */
int done;
} pte_thread_data_t;
/****************************************************************************
*
* Initialization
*
***************************************************************************/
static bool initialized /* false */;
static int pthread_initcall(void)
{
int result;
uk_pr_debug("Initialize pthread-embedded\n");
result = pthread_init();
if (result == PTE_TRUE)
initialized = true;
return result;
}
uk_early_initcall_prio(pthread_initcall, UK_PRIO_EARLIEST);
pte_osResult pte_osInit(void)
{
pte_osResult result = PTE_OS_OK;
pte_thread_data_t *ptd;
struct uk_thread *crnt;
/* Allocate and initialize TLS support */
result = pteTlsGlobalInit(CONFIG_LIBPTHREAD_EMBEDDED_MAX_TLS);
if (result != PTE_OS_OK) {
uk_pr_err("Could not init global TLS");
goto out;
}
/* Create a ptd for initializing thread. */
ptd = calloc(1, sizeof(pte_thread_data_t));
if (ptd == NULL) {
result = PTE_OS_NO_RESOURCES;
goto out;
}
ptd->tls = pteTlsThreadInit();
if (ptd->tls == NULL) {
uk_pr_err("Could not init TLS");
free(ptd);
result = PTE_OS_NO_RESOURCES;
goto out;
}
crnt = uk_thread_current();
crnt->prv = ptd;
ptd->uk_thread = crnt;
out:
return result;
}
/***************************************************************************
*
* Signal handling
*
**************************************************************************/
#if CONFIG_LIBUKSIGNAL
int pte_kill(pte_osThreadHandle threadId, int sig)
{
return uk_sig_thread_kill(threadId->uk_thread, sig);
}
#endif
/****************************************************************************
*
* Threads
*
***************************************************************************/
static pte_thread_data_t *handle_to_ptd(pte_osThreadHandle h)
{
return h;
}
static pte_thread_data_t *current_ptd(void)
{
return uk_thread_current()->prv;
}
static void uk_stub_thread_entry(void *argv)
{
pte_thread_data_t *ptd = (pte_thread_data_t *) argv;
/* wait for the resume command */
uk_semaphore_down(&ptd->start_sem);
ptd->entry_point(ptd->argv);
}
/* NOTE: We need to be able to distinguish if we created a thread through
* pthread API or through uksched API. In case of pthread_create()
* we have to setup some different properties to the thread like creating
* it in paused state.
* In order to distinguish, we will use a magic number as entry function.
* With the thread argument we forward the actual entry point and argument
* vector. During creation our init callback will be executed by uksched
* and we are able to check if we find our magic number again and handle
* the initialization accordingly.
*/
/* Use a pointer that points to itself as magic number. This way we can be
* sure that the magic number (= pointer address) is unique and reserved
* for our purpose.
*/
static const void *PTE_CAPSULE_MAGIC = &PTE_CAPSULE_MAGIC;
struct pte_entry_capsule {
pte_osThreadEntryPoint entry_point;
void *argv;
};
pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entry_point,
int stack_size, int initial_prio, void *argv,
pte_osThreadHandle *ph)
{
struct pte_entry_capsule capsule;
struct uk_thread *th;
capsule.entry_point = entry_point;
capsule.argv = argv;
/* Create the Unikraft thread. This will cause that
* pte_osInitThread() is called.
*/
th = uk_thread_create_attr(NULL, NULL,
PTE_CAPSULE_MAGIC, &capsule);
if (!th)
return PTE_OS_NO_RESOURCES;
/* pte_osInitThread() should have setup a newly created
* pte_thread_data_t which should be stored on th->prv
*/
UK_ASSERT(th->prv != NULL);
/* Return the thread handle */
*ph = th->prv;
return PTE_OS_OK;
}
static int pte_osInitThread(struct uk_thread *th)
{
pte_thread_data_t *ptd;
struct pte_entry_capsule *capsule;
/* NOTE: We reserve th->prv for our exclusive use,
* so it should be NULL when entering here
*/
UK_ASSERT(th->prv == NULL);
/* Initialize pte with first thread creation */
if (unlikely(!initialized)) {
/* Temporarily off to not freak out users */
#if 0
uk_pr_warn("Thread %p created without " STRINGIFY(__LIBNAME__)
" initialized. Utilizing the pthread API from this context may lead to memory leaks.\n",
th);
#endif
return 0;
}
ptd = calloc(1, sizeof(pte_thread_data_t));
if (!ptd)
goto err_out;
/* Allocate TLS structure for this thread. */
ptd->tls = pteTlsThreadInit();
if (ptd->tls == NULL) {
uk_pr_err("Could not allocate TLS\n");
goto err_free_ptd;
}
/* How did we enter this function? */
if (th->entry == PTE_CAPSULE_MAGIC) {
/* This thread got created by pte_osThreadCreate()!
* Lets have a look into the capsule.
*/
UK_ASSERT(th->arg);
capsule = (struct pte_entry_capsule *) th->arg;
ptd->entry_point = capsule->entry_point;
ptd->argv = capsule->argv;
/* this thread has to wait for further setup */
uk_semaphore_init(&ptd->start_sem, 0);
} else {
/* We will encapsulate our thread entry point,
* we have to move our actual entry to ptd
*/
ptd->entry_point = (pte_osThreadEntryPoint) th->entry;
ptd->argv = th->arg;
/* uksched threads need to start automatically */
uk_semaphore_init(&ptd->start_sem, 1);
}
/* Setup encapsulated entry point */
th->entry = uk_stub_thread_entry;
th->arg = ptd;
uk_semaphore_init(&ptd->cancel_sem, 0);
ptd->done = 0;
/* Store cross references (uk_thread <-> pte_thread_data_t) */
th->prv = ptd;
ptd->uk_thread = th;
#if CONFIG_LIBUKSIGNAL
/* inherit signal mask */
ptd->uk_thread->signals_container.mask =
uk_thread_current()->signals_container.mask;
#endif
return 0;
err_free_ptd:
free(ptd);
err_out:
return -1;
}
static void pte_osFiniThread(struct uk_thread *th)
{
/* We clean up resources in pte_osThreadDelete() */
}
UK_THREAD_INIT_PRIO(pte_osInitThread, pte_osFiniThread, UK_PRIO_EARLIEST);
pte_osResult pte_osThreadStart(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
/* wake up thread */
uk_semaphore_up(&ptd->start_sem);
return 0;
}
pte_osResult pte_osThreadDelete(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
/* free resources */
pteTlsThreadDestroy(ptd->tls);
free(ptd);
return PTE_OS_OK;
}
pte_osResult pte_osThreadExitAndDelete(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
UK_ASSERT(ptd->uk_thread);
if (ptd->uk_thread->sched)
uk_thread_kill(ptd->uk_thread);
pte_osThreadDelete(h);
return PTE_OS_OK;
}
void pte_osThreadExit(void)
{
pte_thread_data_t *ptd = current_ptd();
ptd->done = 1;
uk_sched_thread_exit();
}
pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
pte_thread_data_t *self_ptd = current_ptd();
while (1) {
if (ptd->done) {
if (ptd->uk_thread) {
uk_thread_wait(ptd->uk_thread);
/* The thread is destroyed after the wait */
ptd->uk_thread = NULL;
}
return PTE_OS_OK;
}
if (self_ptd && self_ptd->cancel_sem.count > 0)
return PTE_OS_INTERRUPTED;
else
uk_sched_yield();
}
}
pte_osResult pte_osThreadCancel(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
uk_semaphore_up(&ptd->cancel_sem);
return PTE_OS_OK;
}
pte_osResult pte_osThreadCheckCancel(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
if (ptd && ptd->cancel_sem.count > 0)
return PTE_OS_INTERRUPTED;
return PTE_OS_OK;
}
pte_osThreadHandle pte_osThreadGetHandle(void)
{
return current_ptd();
}
int pte_osThreadGetPriority(pte_osThreadHandle h)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
prio_t prio;
int ret = uk_thread_get_prio(ptd->uk_thread, &prio);
return ret ? PTE_OS_GENERAL_FAILURE : PTE_OS_OK;
}
pte_osResult pte_osThreadSetPriority(pte_osThreadHandle h, int new_prio)
{
pte_thread_data_t *ptd = handle_to_ptd(h);
int ret = uk_thread_set_prio(ptd->uk_thread, new_prio);
return ret ? PTE_OS_GENERAL_FAILURE : PTE_OS_OK;
}
void pte_osThreadSleep(unsigned int msecs)
{
__nsec nsec = ukarch_time_msec_to_nsec(msecs);
uk_sched_thread_sleep(nsec);
}
int pte_osThreadGetMinPriority(void)
{
return UK_THREAD_ATTR_PRIO_MIN;
}
int pte_osThreadGetMaxPriority(void)
{
return UK_THREAD_ATTR_PRIO_MAX;
}
int pte_osThreadGetDefaultPriority(void)
{
return UK_THREAD_ATTR_PRIO_DEFAULT;
}
/****************************************************************************
*
* Mutexes
*
****************************************************************************/
pte_osResult pte_osMutexCreate(pte_osMutexHandle *ph)
{
struct uk_mutex *m;
if (!ph)
return PTE_OS_INVALID_PARAM;
m = malloc(sizeof(struct uk_mutex));
if (!m)
return PTE_OS_NO_RESOURCES;
uk_mutex_init(m);
*ph = m;
return PTE_OS_OK;
}
pte_osResult pte_osMutexDelete(pte_osMutexHandle h)
{
if (!h)
return PTE_OS_INVALID_PARAM;
free(h);
return PTE_OS_OK;
}
pte_osResult pte_osMutexLock(pte_osMutexHandle h)
{
if (!h)
return PTE_OS_INVALID_PARAM;
uk_mutex_lock(h);
return PTE_OS_OK;
}
pte_osResult pte_osMutexTimedLock(pte_osMutexHandle h,
unsigned int timeoutMsecs)
{
return PTE_OS_GENERAL_FAILURE;
}
pte_osResult pte_osMutexUnlock(pte_osMutexHandle h)
{
if (!h)
return PTE_OS_INVALID_PARAM;
uk_mutex_unlock(h);
return PTE_OS_OK;
}
/****************************************************************************
*
* Semaphores
*
***************************************************************************/
pte_osResult pte_osSemaphoreCreate(int init_value, pte_osSemaphoreHandle *ph)
{
struct uk_semaphore *s;
if (!ph)
return PTE_OS_INVALID_PARAM;
s = malloc(sizeof(struct uk_semaphore));
if (!s)
return PTE_OS_NO_RESOURCES;
uk_semaphore_init(s, init_value);
*ph = s;
return PTE_OS_OK;
}
pte_osResult pte_osSemaphoreDelete(pte_osSemaphoreHandle h)
{
if (!h)
return PTE_OS_INVALID_PARAM;
free(h);
return PTE_OS_OK;
}
pte_osResult pte_osSemaphorePost(pte_osSemaphoreHandle h, int count)
{
int i;
if (!h)
return PTE_OS_INVALID_PARAM;
for (i = 0; i < count; i++)
uk_semaphore_up(h);
return PTE_OS_OK;
}
pte_osResult pte_osSemaphorePend(pte_osSemaphoreHandle h,
unsigned int *ptimeout_msecs)
{
__nsec timeout;
if (!h)
return PTE_OS_INVALID_PARAM;
if (ptimeout_msecs) {
timeout = ukarch_time_msec_to_nsec(*ptimeout_msecs);
if (uk_semaphore_down_to(h, timeout) == __NSEC_MAX)
return PTE_OS_TIMEOUT;
} else
uk_semaphore_down(h);
return PTE_OS_OK;
}
pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle h,
unsigned int *ptimeout_msecs)
{
pte_thread_data_t *ptd = current_ptd();
pte_osResult result = PTE_OS_OK;
__nsec timeout = 0, start_time = ukplat_monotonic_clock();
if (ptimeout_msecs)
timeout = ukarch_time_msec_to_nsec(*ptimeout_msecs);
while (1) {
if (uk_semaphore_down_try(h))
/* semaphore is up */
break;
else if (timeout &&
(ukplat_monotonic_clock() - start_time > timeout)) {
/* The timeout expired */
result = PTE_OS_TIMEOUT;
break;
} else if (ptd && ptd->cancel_sem.count > 0) {
/* The thread was cancelled */
result = PTE_OS_INTERRUPTED;
break;
} else
/* Maybe next time... */
uk_sched_yield();
}
return result;
}
#if 0
/* We use macros instead */
/****************************************************************************
*
* Atomic Operations
*
***************************************************************************/
static int atomic_add(int *ptarg, int val)
{
return __atomic_add_fetch(ptarg, val, __ATOMIC_SEQ_CST);
}
int pte_osAtomicExchange(int *ptarg, int val)
{
return ukarch_exchange_n(ptarg, val);
}
int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp)
{
int orig = *pdest;
ukarch_compare_exchange_sync(pdest, comp, exchange);
return orig;
}
int pte_osAtomicExchangeAdd(int volatile *paddend, int value)
{
return ukarch_fetch_add(paddend, value);
}
int pte_osAtomicDecrement(int *pdest)
{
return atomic_add(pdest, -1);
}
int pte_osAtomicIncrement(int *pdest)
{
return atomic_add(pdest, 1);
}
#endif
/****************************************************************************
*
* Thread Local Storage
*
***************************************************************************/
static void *current_tls(void)
{
pte_thread_data_t *ptd = current_ptd();
return ptd ? ptd->tls : NULL;
}
pte_osResult pte_osTlsSetValue(unsigned int key, void *value)
{
return pteTlsSetValue(current_tls(), key, value);
}
void *pte_osTlsGetValue(unsigned int index)
{
return (void *) pteTlsGetValue(current_tls(), index);
}
pte_osResult pte_osTlsAlloc(unsigned int *pkey)
{
return pteTlsAlloc(pkey);
}
pte_osResult pte_osTlsFree(unsigned int index)
{
return pteTlsFree(index);
}
/***************************************************************************
*
* Miscellaneous
*
***************************************************************************/
int ftime(struct timeb *tb)
{
__nsec now = ukplat_monotonic_clock();
if (tb) {
tb->time = ukarch_time_nsec_to_sec(now);
tb->millitm = ukarch_time_nsec_to_msec(ukarch_time_subsec(now));
}
return 0;
}