Select one of the symbols to view example projects that use it.
 
Outline
#include "sdkconfig.h"
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <sys/lock.h>
#include "esp_err.h"
#include "esp_attr.h"
#include "esp_cpu.h"
#include "sys/queue.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_private/startup_internal.h"
#include "esp_private/freertos_idf_additions_priv.h"
#include "esp_heap_caps.h"
#include "soc/soc_memory_layout.h"
#include "pthread_internal.h"
#include "esp_pthread.h"
#include "esp_compiler.h"
#include "esp_check.h"
#include "esp_log.h"
TAG
esp_pthread_task_state
esp_pthread_entry
esp_pthread_task_arg_t
esp_pthread_mutex_t
s_threads_lock
pthread_lazy_init_lock
s_threads_list
s_pthread_cfg_key
esp_pthread_cfg_key_destructor(void *)
lazy_init_pthread_cfg_key()
esp_pthread_init()
pthread_list_find_item(void *(*)(esp_pthread_t *, void *), void *)
pthread_get_handle_by_desc(esp_pthread_t *, void *)
pthread_get_desc_by_handle(esp_pthread_t *, void *)
pthread_find_handle(pthread_t)
pthread_find(TaskHandle_t)
pthread_delete(esp_pthread_t *)
esp_pthread_set_cfg(const esp_pthread_cfg_t *)
esp_pthread_get_cfg(esp_pthread_cfg_t *)
get_default_pthread_core()
esp_pthread_get_default_config()
pthread_task_func(void *)
pthread_create_freertos_task_with_caps(TaskFunction_t, const char *const, const uint32_t, void *const, UBaseType_t, BaseType_t, UBaseType_t, TaskHandle_t *const)
pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *)
pthread_join(pthread_t, void **)
pthread_detach(pthread_t)
pthread_exit(void *)
pthread_cancel(pthread_t)
sched_yield()
pthread_self()
pthread_equal(pthread_t, pthread_t)
ONCE
pthread_once(pthread_once_t *, void (*)(void))
MUTEX
mutexattr_check(const pthread_mutexattr_t *)
pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *)
pthread_mutex_destroy(pthread_mutex_t *)
pthread_mutex_lock_internal(esp_pthread_mutex_t *, TickType_t)
pthread_mutex_init_if_static(pthread_mutex_t *)
pthread_mutex_lock(pthread_mutex_t *)
pthread_mutex_timedlock(pthread_mutex_t *, const struct timespec *)
pthread_mutex_trylock(pthread_mutex_t *)
pthread_mutex_unlock(pthread_mutex_t *)
pthread_mutexattr_init(pthread_mutexattr_t *)
pthread_mutexattr_destroy(pthread_mutexattr_t *)
pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *)
pthread_mutexattr_settype(pthread_mutexattr_t *, int)
pthread_attr_init(pthread_attr_t *)
pthread_attr_destroy(pthread_attr_t *)
pthread_attr_getstacksize(const pthread_attr_t *, size_t *)
pthread_attr_setstacksize(pthread_attr_t *, size_t)
pthread_attr_getdetachstate(const pthread_attr_t *, int *)
pthread_attr_setdetachstate(pthread_attr_t *, int)
pthread_include_pthread_impl()
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/pthread/pthread.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include "sdkconfig.h" #include <time.h> #include <errno.h> #include <pthread.h> #include <string.h> #include <sys/lock.h> #include "esp_err.h" #include "esp_attr.h" #include "esp_cpu.h" #include "sys/queue.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "esp_private/startup_internal.h"14 includes #if CONFIG_SPIRAM #include "esp_private/freertos_idf_additions_priv.h" #endif #include "esp_heap_caps.h" #include "soc/soc_memory_layout.h" #include "pthread_internal.h" #include "esp_pthread.h" #include "esp_compiler.h" #include "esp_check.h" #include "esp_log.h"7 includes const static char *TAG = "pthread"; /** task state */ enum esp_pthread_task_state { PTHREAD_TASK_STATE_RUN, PTHREAD_TASK_STATE_EXIT }{ ... }; /** pthread thread FreeRTOS wrapper */ typedef struct esp_pthread_entry { SLIST_ENTRY(esp_pthread_entry) list_node; ///< Tasks list node struct. TaskHandle_t handle; ///< FreeRTOS task handle TaskHandle_t join_task; ///< Handle of the task waiting to join enum esp_pthread_task_state state; ///< pthread task state bool detached; ///< True if pthread is detached void *retval; ///< Value supplied to calling thread during join void *task_arg; ///< Task arguments }{ ... } esp_pthread_t; /** pthread wrapper task arg */ typedef struct { void *(*func)(void *); ///< user task entry void *arg; ///< user task argument esp_pthread_cfg_t cfg; ///< pthread configuration }{ ... } esp_pthread_task_arg_t; /** pthread mutex FreeRTOS wrapper */ typedef struct { SemaphoreHandle_t sem; ///< Handle of the task waiting to join int type; ///< Mutex type. Currently supported PTHREAD_MUTEX_NORMAL and PTHREAD_MUTEX_RECURSIVE }{ ... } esp_pthread_mutex_t; static _lock_t s_threads_lock; portMUX_TYPE pthread_lazy_init_lock = portMUX_INITIALIZER_UNLOCKED; // Used for mutexes and cond vars and rwlocks static SLIST_HEAD(esp_thread_list_head, esp_pthread_entry) s_threads_list = SLIST_HEAD_INITIALIZER(s_threads_list); static pthread_key_t s_pthread_cfg_key; static int pthread_mutex_lock_internal(esp_pthread_mutex_t *mux, TickType_t tmo); static void esp_pthread_cfg_key_destructor(void *value) { free(value); }{ ... } static esp_err_t lazy_init_pthread_cfg_key(void) { if (s_pthread_cfg_key != 0) { return ESP_OK; }{...} if (pthread_key_create(&s_pthread_cfg_key, esp_pthread_cfg_key_destructor) != 0) { return ESP_ERR_NO_MEM; }{...} return ESP_OK; }{ ... } esp_err_t esp_pthread_init(void) { lazy_init_pthread_cfg_key(); return ESP_OK; }{ ... } static void *pthread_list_find_item(void *(*item_check)(esp_pthread_t *, void *arg), void *check_arg) { esp_pthread_t *it; SLIST_FOREACH(it, &s_threads_list, list_node) { void *val = item_check(it, check_arg); if (val) { return val; }{...} }{...} return NULL; }{ ... } static void *pthread_get_handle_by_desc(esp_pthread_t *item, void *desc) { if (item == desc) { return item->handle; }{...} return NULL; }{ ... } static void *pthread_get_desc_by_handle(esp_pthread_t *item, void *hnd) { if (hnd == item->handle) { return item; }{...} return NULL; }{ ... } static inline TaskHandle_t pthread_find_handle(pthread_t thread) { return pthread_list_find_item(pthread_get_handle_by_desc, (void *)thread); }{ ... } static esp_pthread_t *pthread_find(TaskHandle_t task_handle) { return pthread_list_find_item(pthread_get_desc_by_handle, task_handle); }{ ... } static void pthread_delete(esp_pthread_t *pthread) { SLIST_REMOVE(&s_threads_list, pthread, esp_pthread_entry, list_node); free(pthread); }{ ... } /* Call this function to configure pthread stacks in Pthreads */ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg) { if (cfg == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (cfg->stack_size < PTHREAD_STACK_MIN) { return ESP_ERR_INVALID_ARG; }{...} // 0 is treated as default value, hence change caps to MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL in that case int heap_caps; if (cfg->stack_alloc_caps == 0) { heap_caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL; }{...} else { // Check that memory is 8-bit capable if (!(cfg->stack_alloc_caps & MALLOC_CAP_8BIT)) { return ESP_ERR_INVALID_ARG; }{...} heap_caps = cfg->stack_alloc_caps; }{...} ESP_RETURN_ON_ERROR(lazy_init_pthread_cfg_key(), TAG, "Failed to initialize pthread key"); /* If a value is already set, update that value */ esp_pthread_cfg_t *p = pthread_getspecific(s_pthread_cfg_key); ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE("-Wanalyzer-malloc-leak") // ignore leak of 'p' if (!p) { p = malloc(sizeof(esp_pthread_cfg_t)); if (!p) { return ESP_ERR_NO_MEM; }{...} }{...} *p = *cfg; p->stack_alloc_caps = heap_caps; pthread_setspecific(s_pthread_cfg_key, p); return ESP_OK; ESP_COMPILER_DIAGNOSTIC_POP("-Wanalyzer-malloc-leak") }{ ... } esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p) { if (p == NULL) { return ESP_ERR_INVALID_ARG; }{...} ESP_RETURN_ON_ERROR(lazy_init_pthread_cfg_key(), TAG, "Failed to initialize pthread key"); esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key); if (cfg) { *p = *cfg; return ESP_OK; }{...} memset(p, 0, sizeof(*p)); return ESP_ERR_NOT_FOUND; }{ ... } static int get_default_pthread_core(void) { return CONFIG_PTHREAD_TASK_CORE_DEFAULT == -1 ? tskNO_AFFINITY : CONFIG_PTHREAD_TASK_CORE_DEFAULT; }{ ... } esp_pthread_cfg_t esp_pthread_get_default_config(void) { esp_pthread_cfg_t cfg = { .stack_size = CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT, .prio = CONFIG_PTHREAD_TASK_PRIO_DEFAULT, .inherit_cfg = false, .thread_name = NULL, .pin_to_core = get_default_pthread_core(), .stack_alloc_caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT, }{...}; return cfg; }{ ... } static void pthread_task_func(void *arg) { void *rval = NULL; esp_pthread_task_arg_t *task_arg = (esp_pthread_task_arg_t *)arg; ESP_LOGV(TAG, "%s ENTER %p", __FUNCTION__, task_arg->func); // wait for start xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); if (task_arg->cfg.inherit_cfg) { /* If inherit option is set, then do a set_cfg() ourselves for future forks, but first set thread_name to NULL to enable inheritance of the name too. (This also to prevents dangling pointers to name of tasks that might possibly have been deleted when we use the configuration).*//* ... */ esp_pthread_cfg_t *cfg = &task_arg->cfg; cfg->thread_name = NULL; esp_pthread_set_cfg(cfg); }{...} ESP_LOGV(TAG, "%s START %p", __FUNCTION__, task_arg->func); rval = task_arg->func(task_arg->arg); ESP_LOGV(TAG, "%s END %p", __FUNCTION__, task_arg->func); pthread_exit(rval); ESP_LOGV(TAG, "%s EXIT", __FUNCTION__); }{ ... } #if CONFIG_SPIRAM && CONFIG_FREERTOS_SMP static UBaseType_t coreID_to_AffinityMask(BaseType_t core_id) { UBaseType_t affinity_mask = tskNO_AFFINITY; if (core_id != tskNO_AFFINITY) { affinity_mask = 1 << core_id; }{...} return affinity_mask; }{...} /* ... */#endif static BaseType_t pthread_create_freertos_task_with_caps(TaskFunction_t pxTaskCode, const char * const pcName, const configSTACK_DEPTH_TYPE usStackDepth, void * const pvParameters, UBaseType_t uxPriority, BaseType_t core_id, UBaseType_t uxStackMemoryCaps, TaskHandle_t * const pxCreatedTask) { #if CONFIG_SPIRAM #if CONFIG_FREERTOS_SMP return prvTaskCreateDynamicAffinitySetWithCaps(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, coreID_to_AffinityMask(core_id), uxStackMemoryCaps, pxCreatedTask);/* ... */ #else return prvTaskCreateDynamicPinnedToCoreWithCaps(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, core_id, uxStackMemoryCaps, pxCreatedTask);/* ... */ #endif/* ... */ #else return xTaskCreatePinnedToCore(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, core_id);/* ... */ #endif }{ ... } int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { TaskHandle_t xHandle = NULL; ESP_LOGV(TAG, "%s", __FUNCTION__); if (lazy_init_pthread_cfg_key() != ESP_OK) { ESP_LOGE(TAG, "Failed to allocate pthread cfg key!"); return ENOMEM; }{...} esp_pthread_task_arg_t *task_arg = calloc(1, sizeof(esp_pthread_task_arg_t)); if (task_arg == NULL) { ESP_LOGE(TAG, "Failed to allocate task args!"); return ENOMEM; }{...} esp_pthread_t *pthread = calloc(1, sizeof(esp_pthread_t)); if (pthread == NULL) { ESP_LOGE(TAG, "Failed to allocate pthread data!"); free(task_arg); return ENOMEM; }{...} uint32_t stack_size = CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT; BaseType_t prio = CONFIG_PTHREAD_TASK_PRIO_DEFAULT; BaseType_t core_id = get_default_pthread_core(); const char *task_name = CONFIG_PTHREAD_TASK_NAME_DEFAULT; uint32_t stack_alloc_caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT; esp_pthread_cfg_t *pthread_cfg = pthread_getspecific(s_pthread_cfg_key); if (pthread_cfg) { if (pthread_cfg->stack_size) { stack_size = pthread_cfg->stack_size; }{...} if (pthread_cfg->prio && pthread_cfg->prio < configMAX_PRIORITIES) { prio = pthread_cfg->prio; }{...} if (pthread_cfg->inherit_cfg) { if (pthread_cfg->thread_name == NULL) { // Inherit task name from current task. task_name = pcTaskGetName(NULL); }{...} else { // Inheriting, but new task name. task_name = pthread_cfg->thread_name; }{...} }{...} else if (pthread_cfg->thread_name == NULL) { task_name = CONFIG_PTHREAD_TASK_NAME_DEFAULT; }{...} else { task_name = pthread_cfg->thread_name; }{...} if (pthread_cfg->pin_to_core >= 0 && pthread_cfg->pin_to_core < CONFIG_FREERTOS_NUMBER_OF_CORES) { core_id = pthread_cfg->pin_to_core; }{...} // Note: validity has been checked during esp_pthread_set_cfg() stack_alloc_caps = pthread_cfg->stack_alloc_caps; task_arg->cfg = *pthread_cfg; }{...} if (attr) { /* Overwrite attributes */ stack_size = attr->stacksize; switch (attr->detachstate) { case PTHREAD_CREATE_DETACHED: pthread->detached = true; break;... case PTHREAD_CREATE_JOINABLE: default: pthread->detached = false;... }{...} }{...} // stack_size is in bytes. This transformation ensures that the units are // transformed to the units used in FreeRTOS. // Note: float division of ceil(m / n) == // integer division of (m + n - 1) / n stack_size = (stack_size + sizeof(StackType_t) - 1) / sizeof(StackType_t); task_arg->func = start_routine; task_arg->arg = arg; pthread->task_arg = task_arg; BaseType_t res = pthread_create_freertos_task_with_caps(&pthread_task_func, task_name, stack_size, task_arg, prio, core_id, stack_alloc_caps, &xHandle); if (res != pdPASS) { ESP_LOGE(TAG, "Failed to create task!"); free(pthread); free(task_arg); if (res == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) { return ENOMEM; }{...} else { return EAGAIN; }{...} }{...} pthread->handle = xHandle; _lock_acquire(&s_threads_lock); SLIST_INSERT_HEAD(&s_threads_list, pthread, list_node); _lock_release(&s_threads_lock); // start task xTaskNotify(xHandle, 0, eNoAction); *thread = (pthread_t)pthread; // pointer value fit into pthread_t (uint32_t) ESP_LOGV(TAG, "Created task %"PRIx32, (uint32_t)xHandle); return 0; }{ ... } int pthread_join(pthread_t thread, void **retval) { esp_pthread_t *pthread = (esp_pthread_t *)thread; int ret = 0; bool wait = false; void *child_task_retval = 0; ESP_LOGV(TAG, "%s %p", __FUNCTION__, pthread); // find task _lock_acquire(&s_threads_lock); TaskHandle_t handle = pthread_find_handle(thread); if (!handle) { // not found ret = ESRCH; }{...} else if (pthread->detached) { // Thread is detached ret = EDEADLK; }{...} else if (pthread->join_task) { // already have waiting task to join ret = EINVAL; }{...} else if (handle == xTaskGetCurrentTaskHandle()) { // join to self not allowed ret = EDEADLK; }{...} else { esp_pthread_t *cur_pthread = pthread_find(xTaskGetCurrentTaskHandle()); if (cur_pthread && cur_pthread->join_task == handle) { // join to each other not allowed ret = EDEADLK; }{...} else { if (pthread->state == PTHREAD_TASK_STATE_RUN) { pthread->join_task = xTaskGetCurrentTaskHandle(); wait = true; }{...} else { // thread has exited and task is already suspended, or about to be suspended child_task_retval = pthread->retval; pthread_delete(pthread); }{...} }{...} }{...} _lock_release(&s_threads_lock); if (ret == 0) { if (wait) { xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); _lock_acquire(&s_threads_lock); child_task_retval = pthread->retval; pthread_delete(pthread); _lock_release(&s_threads_lock); }{...} /* clean up thread local storage before task deletion */ pthread_internal_local_storage_destructor_callback(handle); vTaskDelete(handle); }{...} if (retval) { *retval = child_task_retval; }{...} ESP_LOGV(TAG, "%s %p EXIT %d", __FUNCTION__, pthread, ret); return ret; }{ ... } int pthread_detach(pthread_t thread) { esp_pthread_t *pthread = (esp_pthread_t *)thread; int ret = 0; _lock_acquire(&s_threads_lock); TaskHandle_t handle = pthread_find_handle(thread); if (!handle) { ret = ESRCH; }{...} else if (pthread->detached) { // already detached ret = EINVAL; }{...} else if (pthread->join_task) { // already have waiting task to join ret = EINVAL; }{...} else if (pthread->state == PTHREAD_TASK_STATE_RUN) { // pthread still running pthread->detached = true; }{...} else { // pthread already stopped pthread_delete(pthread); /* clean up thread local storage before task deletion */ pthread_internal_local_storage_destructor_callback(handle); vTaskDelete(handle); }{...} _lock_release(&s_threads_lock); ESP_LOGV(TAG, "%s %p EXIT %d", __FUNCTION__, pthread, ret); return ret; }{ ... } void pthread_exit(void *value_ptr) { bool detached = false; /* clean up thread local storage before task deletion */ pthread_internal_local_storage_destructor_callback(NULL); _lock_acquire(&s_threads_lock); esp_pthread_t *pthread = pthread_find(xTaskGetCurrentTaskHandle()); if (!pthread) { assert(false && "Failed to find pthread for current task!"); }{...} if (pthread->task_arg) { free(pthread->task_arg); }{...} if (pthread->detached) { // auto-free for detached threads pthread_delete(pthread); detached = true; }{...} else { // Set return value pthread->retval = value_ptr; // Remove from list, it indicates that task has exited if (pthread->join_task) { // notify join xTaskNotify(pthread->join_task, 0, eNoAction); }{...} else { pthread->state = PTHREAD_TASK_STATE_EXIT; }{...} }{...} ESP_LOGD(TAG, "Task stk_wm = %d", (int)uxTaskGetStackHighWaterMark(NULL)); _lock_release(&s_threads_lock); // note: if this thread is joinable then after giving back s_threads_mux // this task could be deleted at any time, so don't take another lock or // do anything that might lock (such as printing to stdout) if (detached) { vTaskDelete(NULL); }{...} else { vTaskSuspend(NULL); }{...} // Should never be reached abort(); }{ ... } int pthread_cancel(pthread_t thread) { ESP_LOGE(TAG, "%s: not supported!", __FUNCTION__); return ENOSYS; }{ ... } int sched_yield(void) { vTaskDelay(0); return 0; }{ ... } pthread_t pthread_self(void) { _lock_acquire(&s_threads_lock); esp_pthread_t *pthread = pthread_find(xTaskGetCurrentTaskHandle()); if (!pthread) { assert(false && "Failed to find current thread ID!"); }{...} _lock_release(&s_threads_lock); return (pthread_t)pthread; }{ ... } int pthread_equal(pthread_t t1, pthread_t t2) { return t1 == t2 ? 1 : 0; }{ ... } /***************** ONCE ******************/ int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { if (once_control == NULL || init_routine == NULL || !once_control->is_initialized) { ESP_LOGE(TAG, "%s: Invalid args!", __FUNCTION__); return EINVAL; }{...} // Check if compare and set was successful if (esp_cpu_compare_and_set((volatile uint32_t *)&once_control->init_executed, 0, 1)) { ESP_LOGV(TAG, "%s: call init_routine %p", __FUNCTION__, once_control); init_routine(); }{...} return 0; }{ ... } ONCE /***************** MUTEX ******************/ static int mutexattr_check(const pthread_mutexattr_t *attr) { if (attr->type != PTHREAD_MUTEX_NORMAL && attr->type != PTHREAD_MUTEX_RECURSIVE && attr->type != PTHREAD_MUTEX_ERRORCHECK) { return EINVAL; }{...} return 0; }{ ... } int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { int type = PTHREAD_MUTEX_NORMAL; if (!mutex) { return EINVAL; }{...} if (attr) { if (!attr->is_initialized) { return EINVAL; }{...} int res = mutexattr_check(attr); if (res) { return res; }{...} type = attr->type; }{...} esp_pthread_mutex_t *mux = (esp_pthread_mutex_t *)malloc(sizeof(esp_pthread_mutex_t)); if (!mux) { return ENOMEM; }{...} mux->type = type; if (mux->type == PTHREAD_MUTEX_RECURSIVE) { mux->sem = xSemaphoreCreateRecursiveMutex(); }{...} else { mux->sem = xSemaphoreCreateMutex(); }{...} if (!mux->sem) { free(mux); return EAGAIN; }{...} *mutex = (pthread_mutex_t)mux; // pointer value fit into pthread_mutex_t (uint32_t) return 0; }{ ... } int pthread_mutex_destroy(pthread_mutex_t *mutex) { esp_pthread_mutex_t *mux; ESP_LOGV(TAG, "%s %p", __FUNCTION__, mutex); if (!mutex) { return EINVAL; }{...} if ((intptr_t) *mutex == PTHREAD_MUTEX_INITIALIZER) { return 0; // Static mutex was never initialized }{...} mux = (esp_pthread_mutex_t *)*mutex; if (!mux) { return EINVAL; }{...} // check if mux is busy int res = pthread_mutex_lock_internal(mux, 0); if (res == EBUSY) { return EBUSY; }{...} if (mux->type == PTHREAD_MUTEX_RECURSIVE) { res = xSemaphoreGiveRecursive(mux->sem); }{...} else { res = xSemaphoreGive(mux->sem); }{...} if (res != pdTRUE) { assert(false && "Failed to release mutex!"); }{...} vSemaphoreDelete(mux->sem); free(mux); return 0; }{ ... } static int pthread_mutex_lock_internal(esp_pthread_mutex_t *mux, TickType_t tmo) { if (!mux) { return EINVAL; }{...} if ((mux->type == PTHREAD_MUTEX_ERRORCHECK) && (xSemaphoreGetMutexHolder(mux->sem) == xTaskGetCurrentTaskHandle())) { return EDEADLK; }{...} if (mux->type == PTHREAD_MUTEX_RECURSIVE) { if (xSemaphoreTakeRecursive(mux->sem, tmo) != pdTRUE) { return EBUSY; }{...} }{...} else { if (xSemaphoreTake(mux->sem, tmo) != pdTRUE) { return EBUSY; }{...} }{...} return 0; }{ ... } static int pthread_mutex_init_if_static(pthread_mutex_t *mutex) { int res = 0; if ((intptr_t) *mutex == PTHREAD_MUTEX_INITIALIZER) { portENTER_CRITICAL(&pthread_lazy_init_lock); if ((intptr_t) *mutex == PTHREAD_MUTEX_INITIALIZER) { res = pthread_mutex_init(mutex, NULL); }{...} portEXIT_CRITICAL(&pthread_lazy_init_lock); }{...} return res; }{ ... } int pthread_mutex_lock(pthread_mutex_t *mutex) { if (!mutex) { return EINVAL; }{...} int res = pthread_mutex_init_if_static(mutex); if (res != 0) { return res; }{...} return pthread_mutex_lock_internal((esp_pthread_mutex_t *)*mutex, portMAX_DELAY); }{ ... } int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout) { if (!mutex) { return EINVAL; }{...} int res = pthread_mutex_init_if_static(mutex); if (res != 0) { return res; }{...} struct timespec currtime; clock_gettime(CLOCK_REALTIME, &currtime); TickType_t tmo = ((timeout->tv_sec - currtime.tv_sec) * 1000 + (timeout->tv_nsec - currtime.tv_nsec) / 1000000) / portTICK_PERIOD_MS; res = pthread_mutex_lock_internal((esp_pthread_mutex_t *)*mutex, tmo); if (res == EBUSY) { return ETIMEDOUT; }{...} return res; }{ ... } int pthread_mutex_trylock(pthread_mutex_t *mutex) { if (!mutex) { return EINVAL; }{...} int res = pthread_mutex_init_if_static(mutex); if (res != 0) { return res; }{...} return pthread_mutex_lock_internal((esp_pthread_mutex_t *)*mutex, 0); }{ ... } int pthread_mutex_unlock(pthread_mutex_t *mutex) { esp_pthread_mutex_t *mux; if (!mutex) { return EINVAL; }{...} mux = (esp_pthread_mutex_t *)*mutex; if (!mux) { return EINVAL; }{...} if (((mux->type == PTHREAD_MUTEX_RECURSIVE) || (mux->type == PTHREAD_MUTEX_ERRORCHECK)) && (xSemaphoreGetMutexHolder(mux->sem) != xTaskGetCurrentTaskHandle())) { return EPERM; }{...} int ret; if (mux->type == PTHREAD_MUTEX_RECURSIVE) { ret = xSemaphoreGiveRecursive(mux->sem); }{...} else { ret = xSemaphoreGive(mux->sem); }{...} if (ret != pdTRUE) { assert(false && "Failed to unlock mutex!"); }{...} return 0; }{ ... } int pthread_mutexattr_init(pthread_mutexattr_t *attr) { if (!attr) { return EINVAL; }{...} memset(attr, 0, sizeof(*attr)); attr->type = PTHREAD_MUTEX_NORMAL; attr->is_initialized = 1; return 0; }{ ... } int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { if (!attr) { return EINVAL; }{...} attr->is_initialized = 0; return 0; }{ ... } int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) { if (!attr) { return EINVAL; }{...} *type = attr->type; return 0; }{ ... } int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { if (!attr) { return EINVAL; }{...} pthread_mutexattr_t tmp_attr = {.type = type}; int res = mutexattr_check(&tmp_attr); if (!res) { attr->type = type; }{...} return res; }{ ... } MUTEX /***************** ATTRIBUTES ******************/ int pthread_attr_init(pthread_attr_t *attr) { if (attr) { /* Nothing to allocate. Set everything to default */ memset(attr, 0, sizeof(*attr)); attr->stacksize = CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT; attr->detachstate = PTHREAD_CREATE_JOINABLE; return 0; }{...} return EINVAL; }{ ... } int pthread_attr_destroy(pthread_attr_t *attr) { /* Nothing to deallocate. Reset everything to default */ return pthread_attr_init(attr); }{ ... } int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) { if (attr) { *stacksize = attr->stacksize; return 0; }{...} return EINVAL; }{ ... } int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) { if (attr && !(stacksize < PTHREAD_STACK_MIN)) { attr->stacksize = stacksize; return 0; }{...} return EINVAL; }{ ... } int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) { if (attr) { *detachstate = attr->detachstate; return 0; }{...} return EINVAL; }{ ... } int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) { if (attr) { switch (detachstate) { case PTHREAD_CREATE_DETACHED: attr->detachstate = PTHREAD_CREATE_DETACHED; break;... case PTHREAD_CREATE_JOINABLE: attr->detachstate = PTHREAD_CREATE_JOINABLE; break;... default: return EINVAL;... }{...} return 0; }{...} return EINVAL; }{ ... } /* Hook function to force linking this file */ void pthread_include_pthread_impl(void) { }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.