summaryrefslogtreecommitdiffstats
path: root/doc/itron3.0/semaphore.t
blob: 9135ae533566fb80272bc91368b5dabfb60f13e9 (plain)
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
@c
@c  This is the chapter from the RTEMS ITRON User's Guide that
@c  documents the services provided by the semaphore
@c  manager.
@c
@c  $Id$
@c

@chapter Semaphore Manager

@section Introduction

The semaphore manager provides functions to allocate, delete, and
control counting semaphores.  This manager is based on the 
ITRON 3.0 standard.

The services provided by the semaphore manager are:

@itemize @bullet
@item @code{cre_sem} - Create Semaphore
@item @code{del_sem} - Delete Semaphore
@item @code{sig_sem} - Signal Semaphore
@item @code{wai_sem} - Wait on Semaphore
@item @code{preq_sem} - Poll and Request Semaphore
@item @code{twai_sem} - Wait on Semaphore with Timeout
@item @code{ref_sem} - Reference Semaphore Status
@end itemize

@section Background

@subsection Theory

Semaphores are used for synchronization and mutual exclusion by indicating
the availability and number of resources.  The task (the task which is
returning resources) notifying other tasks of an event increases the number
of resources held by the semaphore by one.  The task (the task which
will obtain resources) waiting for the event decreases the number of
resources held by the semaphore by one.  If the number of resources held by a
semaphore is insufficient (namely 0), the task requiring resources will
wait until the next time resources are returned to the semaphore.  If there is
more than one task waiting for a semaphore, the tasks will be placed in the
queue.


@subsection T_CSEM Structure

The T_CSEM structure is used to define the characteristics of a semaphore
and passed an as argument to the @code{cre_sem} service.  The structure
is defined as follows:

@example
@group
/*
 *  Create Semaphore (cre_sem) Structure
 */

typedef struct t_csem @{
  VP    exinf;    /* extended information */
  ATR   sematr;   /* semaphore attributes */
  /* Following is the extended function for [level X]. */
  INT   isemcnt;   /* initial semaphore count */
  INT   maxsem;    /* maximum semaphore count */
  /* additional implementation dependent information may be included */
@} T_CSEM;

/*
 *  sematr - Semaphore Attribute Values
 */

#define TA_TFIFO   0x00   /* waiting tasks are handled by FIFO */
#define TA_TPRI    0x01   /* waiting tasks are handled by priority */

@end group
@end example

where the meaning of each field is:

@table @b
@item exinf
is for any extended information that the implementation may define.
This implementation does not use this field.

@item sematr
is the attributes for this semaphore.  The only attributed
which can be specified is whether tasks wait in FIFO (@code{TA_TFIFO})
or priority (@code{TA_TPRI}) order.

@item isemcnt
is the initial count of the semaphore.

@item maxsem
is the maximum count the semaphore may have.  It places an upper
limit on the value taken by the semaphore.

@end table

@subsection Building a Semaphore Attribute Set

In general, an attribute set is built by a bitwise OR
of the desired attribute components.  The following table lists
the set of valid semaphore attributes:

@itemize @bullet
@item @code{TA_TFIFO} - tasks wait by FIFO

@item @code{TA_TPRI} - tasks wait by priority

@end itemize

Attribute values are specifically designed to be
mutually exclusive, therefore bitwise OR and addition operations
are equivalent as long as each attribute appears exactly once in
the component list.

@subsection T_RSEM Structure

The T_RSEM structure is filled in by the @code{ref_sem} service with
status and state information on a semaphore.  The structure
is defined as follows:

@example
@group
/*
 *  Reference Semaphore (ref_sem) Structure
 */

typedef struct t_rsem @{
  VP      exinf;    /* extended information */
  BOOL_ID wtsk;     /* indicates whether there is a waiting task */
  INT     semcnt;   /* current semaphore count */
  /* additional implementation dependent information may be included */
@} T_RSEM;
@end group
@end example

@table @b

@item exinf
is for any extended information that the implementation may define.
This implementation does not use this field.

@item wtsk
is TRUE when there is one or more task waiting on the semaphore.
It is FALSE if no tasks are currently waiting.  The meaning of this
field is allowed to vary between ITRON implementations.  It may have
the ID of a waiting task, the number of tasks waiting, or a boolean
indication that one or more tasks are waiting.

@item semcnt
is the current semaphore count.

@end table

The information in this table is very volatile and should be used
with caution in an application.

@section Operations

@subsection Using as a Binary Semaphore

Creating a semaphore with a limit on the count of 1 effectively
restricts the semaphore to being a binary semaphore.  When the
binary semaphore is available, the count is 1.  When the binary
semaphore is unavailable, the count is 0.

Since this does not result in a true binary semaphore, advanced
binary features like the Priority Inheritance and Priority
Ceiling Protocols are not available.

@section System Calls

This section details the semaphore manager's services.
A subsection is dedicated to each of this manager's services
and describes the calling sequence, related constants, usage,
and status codes.


@c
@c  cre_sem
@c

@page
@subsection cre_sem - Create Semaphore

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER cre_sem(
  ID semid,
  T_CSEM *pk_csem
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOMEM} - Insufficient memory (Memory for control block cannot be
allocated)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.)

@code{E_RSATR} - Reserved attribute (sematr was invalid or could not be
used)

@code{E_OBJ} - Invalid object state (a semaphore of the same ID already
exists)

@code{E_PAR} - Parameter error (pk_csem is invalid and/or isemcnt or
maxsem is negative or invalid)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_CTXID} - Specified an object on another node when the system call
was issued from a task in dispatch disabled state or from a task-
independent portion

@code{EN_PAR} - A value outside the range supported by the target node
and/or transmission packet format was specified as a parameter (a value
outside supported range was specified for exinf, sematr, isemcnt and/or
maxsem)

@subheading DESCRIPTION:

This routine creates a semaphore that resides on the local node.  The 
semaphore is initialized based on the attributes specified in the 
@code{pk_csem} structure.  The initial and maximum counts of the
semaphore are set based on the @code{isemcnt} and @code{maxsem} fields
in this structure.

Specifying @code{TA_TPRI} in the @code{sematr} field of the
semaphore attributes structure causes tasks
waiting for a semaphore to be serviced according to task
priority.  When @code{TA_TFIFO} is selected, tasks are serviced in First
In-First Out order.

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

All memory is preallocated for RTEMS ITRON objects.  Thus, no dynamic
memory allocation is performed by @code{cre_sem} and the @code{E_NOMEM}
error can not be returned.

This directive will not cause the running task to be preempted.

The following semaphore attribute constants are
defined by RTEMS:

@itemize @bullet
@item @code{TA_TFIFO} - tasks wait by FIFO

@item @code{TA_TPRI} - tasks wait by priority

@end itemize

@c
@c  del_sem
@c

@page
@subsection del_sem - Delete Semaphore

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER del_sem(
  ID semid
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_CTXID} - Specified an object on another node when the system call
was issued from a task in dispatch disabled state or from a
task-independent portion

@subheading DESCRIPTION:

This routine deletes the semaphore specified by @code{semid}.
All tasks blocked waiting to acquire the semaphore will be
readied and returned a status code which indicates that the
semaphore was deleted.  The control block for this semaphore
is reclaimed by RTEMS.


@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

The calling task will be preempted if it is enabled
by the task's execution mode and a higher priority local task is
waiting on the deleted semaphore.  The calling task will NOT be
preempted if all of the tasks that are waiting on the semaphore
are remote tasks.

The calling task does not have to be the task that
created the semaphore.  Any local task that knows the semaphore
id can delete the semaphore.


@c
@c  sig_sem
@c

@page
@subsection sig_sem - Signal Semaphore

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER sig_sem(
  ID semid
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{E_QOVR} - Queuing or nesting overflow (the queuing count given by
semcnt went over the maximum allowed)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_CTXID} - Specified an object on another node when the system call
was issued from a task in dispatch disabled state or from a
task-independent portion

@subheading DESCRIPTION:

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

@c
@c  wai_sem
@c

@page
@subsection wai_sem - Wait on Semaphore

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER wai_sem(
  ID semid
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{E_DLT} - The object being waited for was deleted (the specified
semaphore was deleted while waiting)

@code{E_RLWAI} - Wait state was forcibly released (rel_wai was received
while waiting)

@code{E_CTX} - Context error (issued from task-independent portions or a
task in dispatch disabled state)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_PAR} - A value outside the range supported by the target node
and/or transmission packet format was specified as a parameter (a value
outside supported range was specified for tmout)

@subheading DESCRIPTION:

This routine attempts to acquire the semaphore specified by @code{semid}.
If the semaphore is available (i.e. positive semaphore count), then the
semaphore count is decremented and the calling task returns immediately.  
Otherwise the calling tasking is blocked until the semaphore is released
by a subsequent invocation of @code{sig_sem}.

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

If the semaphore is not available, then the calling task will be blocked.

@c
@c  preq_sem
@c

@page
@subsection preq_sem - Poll and Request Semaphore

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER preq_sem(
  ID semid
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{E_TMOUT} - Polling failure or timeout exceeded

@code{E_CTX} - Context error (issued from task-independent portions or a
task in dispatch disabled state)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_PAR} - A value outside the range supported by the target node
and/or transmission packet format was specified as a parameter (a value
outside supported range was specified for tmout)

@subheading DESCRIPTION:

This routine attempts to acquire the semaphore specified by @code{semid}.
If the semaphore is available (i.e. positive semaphore count), then the
semaphore count is decremented and the calling task returns immediately.
Otherwise, the @code{E_TMOUT} error is returned to the calling task to
indicate the semaphore is unavailable.

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

This routine will not cause the running task to be preempted.

@c
@c  twai_sem
@c

@page
@subsection twai_sem - Wait on Semaphore with Timeout

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER twai_sem(
  ID semid,
  TMO tmout
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{E_PAR} - Parameter error (tmout is -2 or less)

@code{E_DLT} - The object being waited for was deleted (the specified
semaphore was deleted while waiting)

@code{E_RLWAI} - Wait state was forcibly released (rel_wai was received
while waiting)

@code{E_TMOUT} - Polling failure or timeout exceeded

@code{E_CTX} - Context error (issued from task-independent portions or a
task in dispatch disabled state)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_PAR} - A value outside the range supported by the target node
and/or transmission packet format was specified as a parameter (a value
outside supported range was specified for tmout)

@subheading DESCRIPTION:

This routine attempts to acquire the semaphore specified by @code{semid}.
If the semaphore is available (i.e. positive semaphore count), then the
semaphore count is decremented and the calling task returns immediately.
Otherwise the calling tasking is blocked until the semaphore is released
by a subsequent invocation of @code{sig_sem} or the timeout period specified
by @code{tmout} milliseconds is exceeded.  If the timeout period is exceeded,
then the @code{E_TMOUT} error is returned.

By specifiying @code{tmout} as @code{TMO_FEVR}, this routine has the same
behavior as @code{wai_sem}.  Similarly, by specifiying @code{tmout} as
@code{TMO_POL}, this routine has the same behavior as @code{preq_sem}.

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

This routine may cause the calling task to block.

A clock tick is required to support the timeout functionality of
this routine.

@c
@c  ref_sem
@c

@page
@subsection ref_sem - Reference Semaphore Status

@subheading CALLING SEQUENCE:

@ifset is-C
@example
ER ref_sem(
  T_RSEM *pk_rsem,
  ID semid
);
@end example
@end ifset

@ifset is-Ada
@end ifset

@subheading STATUS CODES:

@code{E_OK} - Normal Completion

@code{E_ID} - Invalid ID number (semid was invalid or could not be used)

@code{E_NOEXS} - Object does not exist (the semaphore specified by semid
does not exist)

@code{E_OACV} - Object access violation (A semid less than -4 was
specified from a user task.  This is implementation dependent.)

@code{E_PAR} - Parameter error (the packet address for the return
parameters could not be used)

@code{EN_OBJNO} - An object number which could not be accessed on the
target node is specified.

@code{EN_CTXID} - Specified an object on another node when the system call
was issued from a task in dispatch disabled state or from a
task-independent portion

@code{EN_RPAR} - A value outside the range supported by the requesting
node and/or transmission packet format was returned as a parameter (a
value outside supported range was specified for exinf, wtsk or semcnt)

@subheading DESCRIPTION:

This routine returns status information on the semaphore specified
by @code{semid}.  The @code{pk_rsem} structure is filled in by 
this service call.

@subheading NOTES:

Multiprocessing is not supported.  Thus none of the "EN_" status codes
will be returned.

This routine will not cause the running task to be preempted.