source: rtems/testsuites/sptests/sp29/init.c @ 7d91d72

4.104.114.84.95
Last change on this file since 7d91d72 was 7d91d72, checked in by Joel Sherrill <joel.sherrill@…>, on 12/13/99 at 15:29:20

First attempt at adding simple binary semaphore in addition to the current
"mutex" and counting semaphore. This is at the request of Eric Norum
and his EPICS porting effort.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*
2 *  Test for rtems_semaphore_flush
3 *
4 *  $Id$
5 */
6
7#include <bsp.h>
8
9rtems_task Init (rtems_task_argument argument);
10
11#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
12
13#define CONFIGURE_TEST_NEEDS_CONSOLE_DRIVER
14#define CONFIGURE_TEST_NEEDS_CLOCK_DRIVER
15
16#define CONFIGURE_INIT
17
18#include <confdefs.h>
19
20#include <rtems/error.h>
21#include <stdio.h>
22
23rtems_interval ticksPerSecond;
24
25rtems_task
26subtask (rtems_task_argument arg)
27{
28        int i;
29        rtems_status_code sc;
30        rtems_id sem = (rtems_id)arg;
31
32        for (;;) {
33                rtems_task_wake_after (ticksPerSecond * 2);
34
35                sc = rtems_semaphore_release (sem);
36                if (sc != RTEMS_SUCCESSFUL)
37                        printf ("%d: Can't release semaphore: %s\n", __LINE__, rtems_status_text (sc));
38        }
39}
40
41void
42startTask (rtems_id arg)
43{
44        rtems_id tid;
45        rtems_status_code sc;
46
47        sc = rtems_task_create (rtems_build_name ('S', 'R', 'V', 'A'),
48                100,
49                10000,
50                RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
51                RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
52                &tid);
53        if (sc != RTEMS_SUCCESSFUL) {
54                printf ("Can't create task: %s\n", rtems_status_text (sc));
55                rtems_task_suspend (RTEMS_SELF);
56        }
57        sc = rtems_task_start (tid, subtask, arg);
58        if (sc != RTEMS_SUCCESSFUL) {
59                printf ("Can't start task: %s\n", rtems_status_text (sc));
60                rtems_task_suspend (RTEMS_SELF);
61        }
62}
63
64rtems_task Init (rtems_task_argument ignored)
65{
66        int i;
67        rtems_id semrec, semnorec;
68        rtems_status_code sc;
69        rtems_interval then, now;
70
71        sc = rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
72        if (sc != RTEMS_SUCCESSFUL) {
73                printf ("Can't get ticks per second: %s\n", rtems_status_text (sc));
74                exit (1);
75        }
76        sc = rtems_semaphore_create (rtems_build_name ('S', 'M', 'r', 'c'),
77                1,
78                RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_INHERIT_PRIORITY |RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL,
79                0,
80                &semrec);
81        if (sc != RTEMS_SUCCESSFUL) {
82                printf ("%d: Can't create recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
83                exit (1);
84        }
85        sc = rtems_semaphore_create (rtems_build_name ('S', 'M', 'n', 'c'),
86                1,
87                RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_NO_NESTING_ALLOWED|RTEMS_INHERIT_PRIORITY |RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL,
88                0,
89                &semnorec);
90        if (sc != RTEMS_SUCCESSFUL) {
91                printf ("%d: Can't create non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
92                exit (1);
93        }
94
95        sc = rtems_semaphore_obtain (semrec, RTEMS_NO_WAIT, 0);
96        if (sc != RTEMS_SUCCESSFUL) {
97                printf ("%d: Can't obtain recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
98        }
99        sc = rtems_semaphore_obtain (semrec, RTEMS_NO_WAIT, 0);
100        if (sc != RTEMS_SUCCESSFUL) {
101                printf ("%d: Can't reobtain recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
102        }
103
104        sc = rtems_semaphore_obtain (semnorec, RTEMS_NO_WAIT, 0);
105        if (sc != RTEMS_SUCCESSFUL) {
106                printf ("%d: Can't obtain non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
107        }
108        sc = rtems_semaphore_obtain (semnorec, RTEMS_NO_WAIT, 0);
109        if (sc == RTEMS_SUCCESSFUL) {
110                printf ("%d: Reobtain non-recursive-lock semaphore -- and should not have.\n", __LINE__);
111        }
112
113        sc = rtems_semaphore_release (semnorec);
114        if (sc != RTEMS_SUCCESSFUL) {
115                printf ("%d: Can't release non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
116        }
117        sc = rtems_semaphore_release (semnorec);
118        if (sc != RTEMS_SUCCESSFUL) {
119                printf ("%d: Can't rerelease non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
120        }
121        sc = rtems_semaphore_obtain (semnorec, RTEMS_NO_WAIT, 0);
122        if (sc != RTEMS_SUCCESSFUL) {
123                printf ("%d: Can't obtain non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
124        }
125        sc = rtems_semaphore_obtain (semnorec, RTEMS_NO_WAIT, 0);
126        if (sc == RTEMS_SUCCESSFUL) {
127                printf ("%d: Reobtain non-recursive-lock semaphore -- and should not have.\n", __LINE__);
128        }
129        else if (sc != RTEMS_UNSATISFIED) {
130                printf ("%d: Reobtain non-recursive-lock semaphore failed, but error is %d (%s), not RTEMS_UNSATISFIED.\n", __LINE__, sc, rtems_status_text (sc));
131        }
132
133        sc = rtems_semaphore_release (semnorec);
134        if (sc != RTEMS_SUCCESSFUL) {
135                printf ("%d: Can't release non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
136        }
137        sc = rtems_semaphore_release (semnorec);
138        if (sc != RTEMS_SUCCESSFUL) {
139                printf ("%d: Can't rerelease non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
140        }
141        sc = rtems_semaphore_obtain (semnorec, RTEMS_NO_WAIT, 0);
142        if (sc != RTEMS_SUCCESSFUL) {
143                printf ("%d: Can't obtain non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
144        }
145        /*
146         *  Since this task is holding this, there is no reason to block.
147         *  It is obviously an error to reobtain it.
148         */
149        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
150        sc = rtems_semaphore_obtain (semnorec, RTEMS_WAIT, 5);
151        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
152        if (sc == RTEMS_SUCCESSFUL) {
153                printf ("%d: Reobtain non-recursive-lock semaphore -- and should not have.\n", __LINE__);
154        }
155        else if (sc != RTEMS_UNSATISFIED) {
156                printf ("%d: Reobtain non-recursive-lock semaphore failed, but error is %d (%s), not RTEMS_UNSATISFIED.\n", __LINE__, sc, rtems_status_text (sc));
157        }
158        if ((then - now) < 4)
159                printf ("%d: Reobtain non-recursive-lock semaphore failed without timeout.\n", __LINE__);
160
161        startTask (semnorec);
162        rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
163        for (i = 0 ; i < 5 ; i++) {
164                int diff;
165
166                sc = rtems_semaphore_obtain (semnorec, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
167                rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
168                diff = now - then;
169                then = now;
170                if (sc != RTEMS_SUCCESSFUL)
171                        printf ("%d: Failed to obtain non-recursive-lock semaphore: %s\n", __LINE__, rtems_status_text (sc));
172                else if (diff < (2 * ticksPerSecond + 1))
173                        printf ("%d: Obtained obtain non-recursive-lock semaphore too quickly -- %d ticks\n", __LINE__, diff);
174        }
175               
176        exit (0);
177}
Note: See TracBrowser for help on using the repository browser.