summaryrefslogtreecommitdiff
path: root/ACE/tests/Priority_Task_Test.cpp
blob: 1f61d9fb598e01e606604200ad6c0469ed2bbd42 (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

//=============================================================================
/**
 *  @file    Priority_Task_Test.cpp
 *
 *    This is a simple test to illustrate the priority mechanism of
 *    ACE Tasks.  The test requires no options, but the -d option
 *    enables LM_DEBUG output.
 *
 *
 *  @author Carlos O'Ryan <coryan@cs.wustl.edu>
 */
//=============================================================================


#include "test_config.h"
#include "ace/Task.h"
#include "ace/Sched_Params.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_string.h"

static const ACE_TCHAR *usage = ACE_TEXT ("usage: %s [-d]\n");

#if defined (ACE_HAS_THREADS)

/**
 * @class Priority_Task
 *
 * @brief A simple Task that runs itself a different priorities.
 *
 * This task uses the void* argument on open to run the svc()
 * method at a different priority. The point is execise the thread
 * priority features of ACE.
 */
class Priority_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
  /// The constructor
  Priority_Task (void);

  //FUZZ: disable check_for_lack_ACE_OS
  /**
   * Receives the priority and run svc() on a separate thread at that
   * priority.
   *FUZZ: enable check_for_lack_ACE_OS
   */
  int open (void *);

  /// Runs on a separate thread an checks the priority.
  int svc (void);

  /// Returns 1 if priority was set properly, 0 otherwise.
  int succeeded (void) { return error_ == 0; }

private:
  int priority_;
  u_int error_;
};

Priority_Task::Priority_Task (void)
  : ACE_Task<ACE_MT_SYNCH> (ACE_Thread_Manager::instance ()),
    priority_ (0),
    error_ (0)
{
}

int
Priority_Task::open (void *arg)
{
  this->priority_ = *(int *) arg;

  long flags = THR_NEW_LWP;

  // To get FIFO scheduling with PTHREADS.
  ACE_SET_BITS (flags,
                THR_SCHED_FIFO);

  // Become an active object.
  if (this->activate (flags,
                      1,
                      0,
                      this->priority_) == -1)
    {
      // On Linux, for example, only the superuser can set the policy
      // to other than ACE_SCHED_OTHER.  But with ACE_SCHED_OTHER,
      // there is only one thread priority value, for example, 0.  So,
      // let the superuser run an interesting test, but for other
      // users use the minimum ACE_SCHED_OTHER thread priority.

      long fallback_priority =
        ACE_Sched_Params::priority_min (ACE_SCHED_OTHER,
                                        ACE_SCOPE_THREAD);

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) task activation at priority %d with ")
                  ACE_TEXT ("flags 0x%X failed; retry at priority %d with ")
                  ACE_TEXT ("flags 0x%X (errno is %d%p)\n"),
                  this->priority_,
                  flags,
                  fallback_priority,
                  THR_NEW_LWP,
                  ACE_ERRNO_GET,
                  ACE_TEXT ("")));

      flags = THR_NEW_LWP;
      this->priority_ = fallback_priority;

      if (this->activate (flags,
                          1,
                          1,
                          this->priority_) == -1)
        {
          if (ACE_OS::last_error () == EPERM)
            ACE_ERROR_RETURN ((LM_INFO,
                               ACE_TEXT ("Insufficient privilege to run this test.\n")),
                              -1);
          else
            ACE_DEBUG ((LM_ERROR,
                        ACE_TEXT ("(%t) task activation at priority %d failed, ")
                        ACE_TEXT ("exiting!\n%a"),
                        this->priority_,
                        -1));
        }
    }
  return 0;
}

int
Priority_Task::svc (void)
{
  ACE_hthread_t thr_handle;
  ACE_Thread::self (thr_handle);
  int prio;

  if (ACE_Thread::getprio (thr_handle, prio) == -1)
  {
    if (errno == ENOTSUP)
    {
      ACE_DEBUG((LM_DEBUG,
                 ACE_TEXT ("getprior not supported on this platform\n")
               ));
      return 0;
    }
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("getprio failed")),
                      -1);
  }

  if (prio == this->priority_)
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%t) actual prio of %d equals desired priority\n"),
                prio));
  else
    {
      ACE_DEBUG ((LM_ERROR,
                  ACE_TEXT ("(%t) actual prio = %d, desired priority_ = %d!\n"),
                  prio,
                  this->priority_));
      ++error_;
    }

  return 0;
}

#endif /* ACE_HAS_THREADS */

int
run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("Priority_Task_Test"));

  if (argc <= 1)
    // Disable LM_DEBUG messages.
    ACE_Log_Msg::instance ()->priority_mask
      (ACE_Log_Msg::instance ()->priority_mask () &~ LM_DEBUG);
  else if (argc == 2)
    {
      if (ACE_OS::strcmp (argv[1],
                          ACE_TEXT ("-d")) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           usage,
                           argv [0]),
                          -1);
      // else -d option: don't disable LM_DEBUG messages
    }
  else
    ACE_ERROR_RETURN ((LM_ERROR,
                       usage,
                       argv [0]),
                      -1);

  int status = 0;

#if defined (ACE_HAS_THREADS)

  Priority_Task tasks[ACE_MAX_ITERATIONS];

  size_t i;

  // Spawn off ACE_MAX_ITERATIONS of tasks, passing each one their
  // iteration number as their priority.

  // NOTE: on Solaris, for example, this requests the min FIFO
  // priority.  But, this test doesn't use the Realtime scheduling
  // class.  The FIFO priorities are used because they're all
  // nonnegative.

  ACE_Sched_Priority_Iterator priority (ACE_SCHED_FIFO,
                                        ACE_SCOPE_THREAD);

  for (i = 0; i < ACE_MAX_ITERATIONS; i++)
    {
      int p = priority.priority ();
      if (tasks[i].open ((void *) &p) == -1)
        break; // Out of enclosing loop.

      // If there are more priorities get the next one...
      if (priority.more ())
        priority.next ();
    }

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) %d tasks spawned, wait for them to exit . . .\n"),
              ACE_MAX_ITERATIONS));

  // Wait for all tasks to exit.
  ACE_Thread_Manager::instance ()->wait ();

  for (i = 0; i < ACE_MAX_ITERATIONS; i++)
    if (!tasks[i].succeeded ())
      {
        ++status;
        break;
      }

#else
  ACE_ERROR ((LM_DEBUG,
              ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */

  // Re-enable LM_DEBUG messages.
  ACE_Log_Msg::instance ()->priority_mask
    (ACE_Log_Msg::instance ()->priority_mask () | LM_DEBUG);

  ACE_END_TEST;
  return status;
}