summaryrefslogtreecommitdiffstats
path: root/c-user/self_contained_objects.rst
blob: a581e0e4619a6471a6def7353ec735d84df42572 (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
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
.. SPDX-License-Identifier: CC-BY-SA-4.0

.. Copyright (C) 2014, 2017.
.. COMMENT: embedded brains GmbH.

Self-Contained Objects
**********************

Introduction
============

One of the original design goals of RTEMS was the support for heterogeneous
computing based on message passing.  This was realized by synchronization
objects with an architecture-independent identifier provided by the system
during object creation (a 32-bit unsigned integer used as a bitfield) and a
user-defined four character name.  This approach in the so called Classic API
has some weaknesses:

* Dynamic memory (the workspace) is used to allocate object pools.  This
  requires a complex configuration with heavy use of the C pre-processor.
  The unlimited objects support optionally expands and shrinks the object pool.
  Dynamic memory is strongly discouraged by some coding standards, e.g.  MISRA
  C:2012 :cite:`MISRA:2012:C`.

* Objects are created via function calls which return an object identifier.
  The object operations use this identifier and map it internally to an object
  representation.

* The object identifier is only known at run-time.  This hinders compiler
  optimizations and static analysis.

* The objects reside in a table, e.g. they are suspect to false sharing of
  cache lines :cite:`Drepper:2007:Memory`.

* The object operations use a rich set of options and attributes.  For each
  object operation these parameters must be evaluated and validated at run-time
  to figure out what to do exactly for this operation.

For applications that use fine grained locking the mapping of the identifier to
the object representation and the parameter evaluation are a significant
overhead that may degrade the performance dramatically.  An example is the `new
network stack (libbsd) <https://git.rtems.org/rtems-libbsd>`_ which uses
hundreds of locks in a basic setup.  Another example is the OpenMP support
(libgomp).

To overcome these issues new self-contained synchronization objects are
available since RTEMS 4.11.  Self-contained synchronization objects encapsulate
all their state in exactly one data structure.  The user must provide the
storage space for this structure and nothing more.  The user is responsible for
the object life-cycle.  Initialization and destruction of self-contained
synchronization objects cannot fail provided all function parameters are valid.
In particular, a not enough memory error cannot happen.  It is possible to
statically initialize self-contained synchronization objects.  This allows an
efficient use of static analysis tools.

Several header files define self-contained synchronization objects.  The Newlib
:file:`<sys/lock.h>` header file provides

* mutexes,

* recursive mutexes,

* condition variables,

* counting semaphores,

* binary semaphores, and

* Futex synchronization :cite:`Franke:2002:Futex`.

They are used internally in Newlib (e.g. for FILE objects), for the C++11
threads and the OpenMP support (libgomp).  The Newlib provided self-contained
synchronization objects focus on performance.  There are no error checks to
catch software errors, e.g. invalid parameters.  The application configuration
is significantly simplified, since it is no longer necessary to account for
lock objects used by Newlib and GCC.  The Newlib defined self-contained
synchronization objects can be a statically initialized and reside in the
``.bss`` section.  Destruction is a no-operation.

The header file :file:`<pthread.h>` provides

* POSIX barriers (:c:type:`pthread_barrier_t`),

* POSIX condition variables (:c:type:`pthread_cond_t`),

* POSIX mutexes (:c:type:`pthread_mutex_t`),

* POSIX reader/writer locks (:c:type:`pthread_rwlock_t`), and

* POSIX spinlocks (:c:type:`pthread_spinlock_t`)

as self-contained synchronization objects.  The POSIX synchronization objects are
used for example by the Ada run-time support.  The header file
:file:`<semaphore.h>` provides self-contained

* POSIX unnamed semaphores (:c:type:`sem_t` initialized via :c:func:`sem_init`).

RTEMS Thread API
================

To give RTEMS users access to self-contained synchronization objects an API is
necessary.  One option would be to simply use the POSIX threads API (pthreads),
C11 threads or C++11 threads.  However, these standard APIs lack for example
binary semaphores which are important for task/interrupt synchronization.  The
timed operations use in general time values specified by seconds and
nanoseconds.  Setting up the time values in seconds (time_t has 64 bits) and
nanoseconds is burdened with a high overhead compared to time values in clock
ticks for relative timeouts.  The POSIX API mutexes can be configured for
various protocols and options, this adds a run-time overhead.  There are a
variety of error conditions.  This is a problem in combination with some coding
standards, e.g.  MISRA C:2012.  APIs used by Linux (e.g. `<linux/mutex.h>
<http://lxr.free-electrons.com/source/include/linux/mutex.h>`_) or the FreeBSD
kernel (e.g. `MUTEX(9)
<https://www.freebsd.org/cgi/man.cgi?query=mutex&sektion=9>`_) are better
suited as a template for high-performance synchronization objects.  The goal of
the `RTEMS Thread API` is to offer the highest performance with the lowest
space-overhead on RTEMS.  It should be suitable for device drivers.

Mutual Exclusion
================

The :c:type:`rtems_mutex` and :c:type:`rtems_recursive_mutex` objects provide
mutual-exclusion synchronization using the :ref:`PriorityInheritance` in
uniprocessor configurations or the :ref:`OMIP` in SMP configurations.
Recursive locking should be used with care :cite:`Williams:2012:CA`.  The
storage space for these object must be provided by the user.  There are no
defined comparison or assignment operators for these type.  Only the object
itself may be used for performing synchronization.  The result of referring to
copies of the object in calls to

* :c:func:`rtems_mutex_lock`,

* :c:func:`rtems_recursive_mutex_lock`,

* :c:func:`rtems_mutex_unlock`,

* :c:func:`rtems_recursive_mutex_unlock`,

* :c:func:`rtems_mutex_set_name`,

* :c:func:`rtems_recursive_mutex_set_name`,

* :c:func:`rtems_mutex_get_name`,

* :c:func:`rtems_recursive_mutex_get_name`,

* :c:func:`rtems_mutex_destroy`, and

* :c:func:`rtems_recursive_mutex_destroy`

is undefined.  Objects of the type :c:type:`rtems_mutex` must be initialized
via

* :c:func:`RTEMS_MUTEX_INITIALIZER`, or

* :c:func:`rtems_mutex_init`.

They must be destroyed via

* :c:func:`rtems_mutex_destroy`.

is undefined.  Objects of the type :c:type:`rtems_recursive_mutex` must be
initialized via

* :c:func:`RTEMS_RECURSIVE_MUTEX_INITIALIZER`, or

* :c:func:`rtems_recursive_mutex_init`.

They must be destroyed via

* :c:func:`rtems_recursive_mutex_destroy`.

.. raw:: latex

    \clearpage

Static mutex initialization
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        rtems_mutex mutex = RTEMS_MUTEX_INITIALIZER(
          name
        );

        rtems_recursive_mutex mutex = RTEMS_RECURSIVE_MUTEX_INITIALIZER(
          name
        );

DESCRIPTION:
    An initializer for static initialization.  It is equivalent to a call to
    :c:func:`rtems_mutex_init` or :c:func:`rtems_recursive_mutex_init`
    respectively.

NOTES:
    Global mutexes with a ``name`` of ``NULL`` may reside in the ``.bss``
    section.

.. raw:: latex

    \clearpage

Run-time mutex initialization
-----------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_mutex_init(
          rtems_mutex *mutex,
          const char  *name
        );

        void rtems_recursive_mutex_init(
          rtems_recursive_mutex *mutex,
          const char            *name
        );

DESCRIPTION:
    Initializes the ``mutex`` with the specified ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the mutex.  A
    ``name`` of ``NULL`` is valid.  The mutex is unlocked after initialization.

.. raw:: latex

    \clearpage

Lock the mutex
--------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_mutex_lock(
          rtems_mutex *mutex
        );

        void rtems_recursive_mutex_lock(
          rtems_recursive_mutex *mutex
        );

DESCRIPTION:
    Locks the ``mutex``.

NOTES:
    This function must be called from thread context with interrupts enabled.
    In case the mutex is currently locked by another thread, then the thread is
    blocked until it becomes the mutex owner.  Threads wait in priority order.

    A recursive lock happens in case the mutex owner tries to lock the mutex
    again.  The result of recursively locking a mutex depends on the mutex
    variant.  For a normal (non-recursive) mutex (:c:type:`rtems_mutex`) the
    result is unpredictable.  It could block the owner indefinetly or lead to a
    fatal deadlock error.  A recursive mutex (:c:type:`rtems_recursive_mutex`)
    can be locked recursively by the mutex owner.

    Each mutex lock operation must have a corresponding unlock operation.

.. raw:: latex

    \clearpage

Unlock the mutex
----------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_mutex_unlock(
          rtems_mutex *mutex
        );

        void rtems_recursive_mutex_unlock(
          rtems_recursive_mutex *mutex
        );

DESCRIPTION:
    Unlocks the ``mutex``.

NOTES:
    This function must be called from thread context with interrupts enabled.
    In case the currently executing thread is not the owner of the ``mutex``,
    then the result is unpredictable.

    Exactly the outer-most unlock will make a recursive mutex available to
    other threads.

.. raw:: latex

    \clearpage

.. raw:: latex

    \clearpage

Set mutex name
--------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_mutex_set_name(
          rtems_mutex *mutex,
          const char  *name
        );

        void rtems_recursive_mutex_set_name(
          rtems_recursive_mutex *mutex,
          const char            *name
        );

DESCRIPTION:
    Sets the ``mutex`` name to ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the mutex.  A
    ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Get mutex name
--------------

CALLING SEQUENCE:
    .. code-block:: c

        const char *rtems_mutex_get_name(
          const rtems_mutex *mutex
        );

        const char *rtems_recursive_mutex_get_name(
          const rtems_recursive_mutex *mutex
        );

DESCRIPTION:
    Returns the ``mutex`` name.

NOTES:
    The name may be ``NULL``.

Mutex destruction
-----------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_mutex_destroy(
          rtems_mutex *mutex
        );

        void rtems_recursive_mutex_destroy(
          rtems_recursive_mutex *mutex
        );

DESCRIPTION:
    Destroys the ``mutex``.

NOTES:
    In case the mutex is locked or still in use, then the result is
    unpredictable.

Condition Variables
===================

The :c:type:`rtems_condition_variable` object provides a condition variable
synchronization object.  The storage space for this object must be provided by
the user.  There are no defined comparison or assignment operators for this
type.  Only the object itself may be used for performing synchronization.  The
result of referring to copies of the object in calls to

* :c:func:`rtems_condition_variable_wait`,

* :c:func:`rtems_condition_variable_signal`,

* :c:func:`rtems_condition_variable_broadcast`,

* :c:func:`rtems_condition_variable_set_name`,

* :c:func:`rtems_condition_variable_get_name`, and

* :c:func:`rtems_condition_variable_destroy`

is undefined.  Objects of this type must be initialized via

* :c:func:`RTEMS_CONDITION_VARIABLE_INITIALIZER`, or

* :c:func:`rtems_condition_variable_init`.

They must be destroyed via

* :c:func:`rtems_condition_variable_destroy`.

.. raw:: latex

    \clearpage

Static condition variable initialization
----------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        rtems_condition_variable condition_variable = RTEMS_CONDITION_VARIABLE_INITIALIZER(
          name
        );

DESCRIPTION:
    An initializer for static initialization.  It is equivalent to a call to
    :c:func:`rtems_condition_variable_init`.

NOTES:
    Global condition variables with a ``name`` of ``NULL`` may reside in the
    ``.bss`` section.

.. raw:: latex

    \clearpage

Run-time condition variable initialization
------------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_init(
          rtems_condition_variable *condition_variable,
          const char               *name
        );

DESCRIPTION:
    Initializes the ``condition_variable`` with the specified ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the condition
    variable.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Wait for condition signal
-------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_wait(
          rtems_condition_variable *condition_variable,
          rtems_mutex              *mutex
        );

DESCRIPTION:
    Atomically waits for a condition signal and unlocks the mutex.  Once the
    condition is signalled to the thread it wakes up and locks the mutex again.

NOTES:
    This function must be called from thread context with interrupts enabled.
    Threads wait in priority order.

.. raw:: latex

    \clearpage

Signals a condition change
--------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_signal(
          rtems_condition_variable *condition_variable
        );

DESCRIPTION:
    Signals a condition change to the highest priority waiting thread.  If no
    threads wait currently on this condition variable, then nothing happens.

.. raw:: latex

    \clearpage

Broadcasts a condition change
-----------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_broadcast(
          rtems_condition_variable *condition_variable
        );

DESCRIPTION:
    Signals a condition change to all waiting thread.  If no threads wait
    currently on this condition variable, then nothing happens.

.. raw:: latex

    \clearpage

.. raw:: latex

    \clearpage

Set condition variable name
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_set_name(
          rtems_condition_variable *condition_variable,
          const char               *name
        );

DESCRIPTION:
    Sets the ``condition_variable`` name to ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the condition
    variable.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Get condition variable name
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        const char *rtems_condition_variable_get_name(
          const rtems_condition_variable *condition_variable
        );

DESCRIPTION:
    Returns the ``condition_variable`` name.

NOTES:
    The name may be ``NULL``.

Condition variable destruction
------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_condition_variable_destroy(
          rtems_condition_variable *condition_variable
        );

DESCRIPTION:
    Destroys the ``condition_variable``.

NOTES:
    In case the condition variable still in use, then the result is
    unpredictable.

Counting Semaphores
===================

The :c:type:`rtems_counting_semaphore` object provides a counting semaphore
synchronization object.  The storage space for this object must be provided by
the user.  There are no defined comparison or assignment operators for this
type.  Only the object itself may be used for performing synchronization.  The
result of referring to copies of the object in calls to

* :c:func:`rtems_counting_semaphore_wait`,

* :c:func:`rtems_counting_semaphore_post`,

* :c:func:`rtems_counting_semaphore_set_name`,

* :c:func:`rtems_counting_semaphore_get_name`, and

* :c:func:`rtems_counting_semaphore_destroy`

is undefined.  Objects of this type must be initialized via

* :c:func:`RTEMS_COUNTING_SEMAPHORE_INITIALIZER`, or

* :c:func:`rtems_counting_semaphore_init`.

They must be destroyed via

* :c:func:`rtems_counting_semaphore_destroy`.

.. raw:: latex

    \clearpage

Static counting semaphore initialization
----------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        rtems_counting_semaphore counting_semaphore = RTEMS_COUNTING_SEMAPHORE_INITIALIZER(
          name,
          value
        );

DESCRIPTION:
    An initializer for static initialization.  It is equivalent to a call to
    :c:func:`rtems_counting_semaphore_init`.

NOTES:
    Global counting semaphores with a ``name`` of ``NULL`` may reside in the
    ``.bss`` section.

.. raw:: latex

    \clearpage

Run-time counting semaphore initialization
------------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_counting_semaphore_init(
          rtems_counting_semaphore *counting_semaphore,
          const char               *name,
          unsigned int              value
        );

DESCRIPTION:
    Initializes the ``counting_semaphore`` with the specified ``name`` and
    ``value``.  The initial value is set to ``value``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the counting
    semaphore.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Wait for a counting semaphore
-----------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_counting_semaphore_wait(
          rtems_counting_semaphore *counting_semaphore
        );

DESCRIPTION:
    Waits for the ``counting_semaphore``.  In case the current semaphore value
    is positive, then the value is decremented and the function returns
    immediately, otherwise the thread is blocked waiting for a semaphore post.

NOTES:
    This function must be called from thread context with interrupts enabled.
    Threads wait in priority order.

.. raw:: latex

    \clearpage

Post a counting semaphore
-------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_counting_semaphore_post(
          rtems_counting_semaphore *counting_semaphore
        );

DESCRIPTION:
    Posts the ``counting_semaphore``.  In case at least one thread is waiting
    on the counting semaphore, then the highest priority thread is woken up,
    otherwise the current value is incremented.

NOTES:
    This function may be called from interrupt context.  In case it is called
    from thread context, then interrupts must be enabled.

.. raw:: latex

    \clearpage

.. raw:: latex

    \clearpage

Set counting semaphore name
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_counting_semaphore_set_name(
          rtems_counting_semaphore *counting_semaphore,
          const char               *name
        );

DESCRIPTION:
    Sets the ``counting_semaphore`` name to ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the counting
    semaphore.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Get counting semaphore name
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        const char *rtems_counting_semaphore_get_name(
          const rtems_counting_semaphore *counting_semaphore
        );

DESCRIPTION:
    Returns the ``counting_semaphore`` name.

NOTES:
    The name may be ``NULL``.

Counting semaphore destruction
------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_counting_semaphore_destroy(
          rtems_counting_semaphore *counting_semaphore
        );

DESCRIPTION:
    Destroys the ``counting_semaphore``.

NOTES:
    In case the counting semaphore still in use, then the result is
    unpredictable.

Binary Semaphores
=================

The :c:type:`rtems_binary_semaphore` object provides a binary semaphore
synchronization object.  The storage space for this object must be provided by
the user.  There are no defined comparison or assignment operators for this
type.  Only the object itself may be used for performing synchronization.  The
result of referring to copies of the object in calls to

* :c:func:`rtems_binary_semaphore_wait`,

* :c:func:`rtems_binary_semaphore_wait_timed_ticks`,

* :c:func:`rtems_binary_semaphore_try_wait`,

* :c:func:`rtems_binary_semaphore_post`,

* :c:func:`rtems_binary_semaphore_set_name`,

* :c:func:`rtems_binary_semaphore_get_name`, and

* :c:func:`rtems_binary_semaphore_destroy`

is undefined.  Objects of this type must be initialized via

* :c:func:`RTEMS_BINARY_SEMAPHORE_INITIALIZER`, or

* :c:func:`rtems_binary_semaphore_init`.

They must be destroyed via

* :c:func:`rtems_binary_semaphore_destroy`.

.. raw:: latex

    \clearpage

Static binary semaphore initialization
--------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        rtems_binary_semaphore binary_semaphore = RTEMS_BINARY_SEMAPHORE_INITIALIZER(
          name
        );

DESCRIPTION:
    An initializer for static initialization.  It is equivalent to a call to
    :c:func:`rtems_binary_semaphore_init`.

NOTES:
    Global binary semaphores with a ``name`` of ``NULL`` may reside in the
    ``.bss`` section.

.. raw:: latex

    \clearpage

Run-time binary semaphore initialization
----------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_binary_semaphore_init(
          rtems_binary_semaphore *binary_semaphore,
          const char             *name
        );

DESCRIPTION:
    Initializes the ``binary_semaphore`` with the specified ``name``.  The
    initial value is set to zero.

NOTES:
    The ``name`` must be persistent throughout the life-time of the binary
    semaphore.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Wait for a binary semaphore
---------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_binary_semaphore_wait(
          rtems_binary_semaphore *binary_semaphore
        );

DESCRIPTION:
    Waits for the ``binary_semaphore``.  In case the current semaphore value is
    one, then the value is set to zero and the function returns immediately,
    otherwise the thread is blocked waiting for a semaphore post.

NOTES:
    This function must be called from thread context with interrupts enabled.
    Threads wait in priority order.

.. raw:: latex

    \clearpage

Wait for a binary semaphore with timeout in ticks
-------------------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        int rtems_binary_semaphore_wait_timed_ticks(
          rtems_binary_semaphore *binary_semaphore,
          uint32_t                ticks
        );

DIRECTIVE STATUS CODES:
    .. list-table::
      :class: rtems-table

      * - ``0``
        - The semaphore wait was successful.
      * - ``ETIMEDOUT``
        - The semaphore wait timed out.

DESCRIPTION:
    Waits for the ``binary_semaphore`` with a timeout in ``ticks``.  In case
    the current semaphore value is one, then the value is set to zero and the
    function returns immediately with a return value of ``0``, otherwise the
    thread is blocked waiting for a semaphore post.  The time waiting for a
    semaphore post is limited by ``ticks``.  A ``ticks`` value of zero
    specifies an infinite timeout.

NOTES:
    This function must be called from thread context with interrupts enabled.
    Threads wait in priority order.

.. raw:: latex

    \clearpage

Tries to wait for a binary semaphore
------------------------------------

CALLING SEQUENCE:
    .. code-block:: c

        int rtems_binary_semaphore_try_wait(
          rtems_binary_semaphore *binary_semaphore
        );

DIRECTIVE STATUS CODES:
    .. list-table::
      :class: rtems-table

      * - ``0``
        - The semaphore wait was successful.
      * - ``EAGAIN``
        - The semaphore wait failed.

DESCRIPTION:
    Tries to wait for the ``binary_semaphore``.  In case the current semaphore
    value is one, then the value is set to zero and the function returns
    immediately with a return value of ``0``, otherwise it returns immediately
    with a return value of ``EAGAIN``.

NOTES:
    This function may be called from interrupt context.  In case it is called
    from thread context, then interrupts must be enabled.

.. raw:: latex

    \clearpage

Post a binary semaphore
-----------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_binary_semaphore_post(
          rtems_binary_semaphore *binary_semaphore
        );

DESCRIPTION:
    Posts the ``binary_semaphore``.  In case at least one thread is waiting
    on the binary semaphore, then the highest priority thread is woken up,
    otherwise the current value is set to one.

NOTES:
    This function may be called from interrupt context.  In case it is called
    from thread context, then interrupts must be enabled.

.. raw:: latex

    \clearpage

.. raw:: latex

    \clearpage

Set binary semaphore name
-------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_binary_semaphore_set_name(
          rtems_binary_semaphore *binary_semaphore,
          const char             *name
        );

DESCRIPTION:
    Sets the ``binary_semaphore`` name to ``name``.

NOTES:
    The ``name`` must be persistent throughout the life-time of the binary
    semaphore.  A ``name`` of ``NULL`` is valid.

.. raw:: latex

    \clearpage

Get binary semaphore name
-------------------------

CALLING SEQUENCE:
    .. code-block:: c

        const char *rtems_binary_semaphore_get_name(
          const rtems_binary_semaphore *binary_semaphore
        );

DESCRIPTION:
    Returns the ``binary_semaphore`` name.

NOTES:
    The name may be ``NULL``.

Binary semaphore destruction
----------------------------

CALLING SEQUENCE:
    .. code-block:: c

        void rtems_binary_semaphore_destroy(
          rtems_binary_semaphore *binary_semaphore
        );

DESCRIPTION:
    Destroys the ``binary_semaphore``.

NOTES:
    In case the binary semaphore still in use, then the result is
    unpredictable.

Threads
=======

.. warning::

   The self-contained threads support is work in progress.  In contrast to the
   synchronization objects the self-contained thread support is not just an API
   glue layer to already existing implementations.

The :c:type:`rtems_thread` object provides a thread of execution.

CALLING SEQUENCE:
    .. code-block:: c

        RTEMS_THREAD_INITIALIZER(
          name,
          thread_size,
          priority,
          flags,
          entry,
          arg
        );

        void rtems_thread_start(
          rtems_thread *thread,
          const char   *name,
          size_t        thread_size,
          uint32_t      priority,
          uint32_t      flags,
          void       ( *entry )( void * ),
          void         *arg
        );

        void rtems_thread_restart(
          rtems_thread *thread,
          void         *arg
        ) RTEMS_NO_RETURN;

        void rtems_thread_event_send(
          rtems_thread *thread,
          uint32_t      events
        );

        uint32_t rtems_thread_event_poll(
          rtems_thread *thread,
          uint32_t      events_of_interest
        );

        uint32_t rtems_thread_event_wait_all(
          rtems_thread *thread,
          uint32_t      events_of_interest
        );

        uint32_t rtems_thread_event_wait_any(
          rtems_thread *thread,
          uint32_t      events_of_interest
        );

        void rtems_thread_destroy(
          rtems_thread *thread
        );

        void rtems_thread_destroy_self(
          void
        ) RTEMS_NO_RETURN;