public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: Tristan Savatier <tristan@mpegtv.com>
To: pthreads-win32@sourceware.cygnus.com
Cc: rpj@ise.canberra.edu.au, Bok <bok@bok.net>
Subject: bug report (with fix): pthread_setschedparam() fails withWindows-CE 3.0 (Pocket PC)
Date: Tue, 23 May 2000 01:16:00 -0000	[thread overview]
Message-ID: <392A3EE3.37BBD811@mpegtv.com> (raw)

I just discovered a bug in pthread_win32 that caused
pthread_setschedparam() to fail systematically with
Windows-CE 3.0 (dubbed Pocket PC by microsoft).

The bug stems from the following piece of code that seems
perfectly correct at first sight:

====================================================
int pthread_setschedparam(pthread_t thread, int policy,
			  const struct sched_param *param)
{
[...]

  /* Validate priority level. */
  if (param->sched_priority < sched_get_priority_min(policy) ||
      param->sched_priority > sched_get_priority_max(policy))
    {
      return EINVAL;
    }

[...]
}

int sched_get_priority_min(int policy)
{
  if (policy < SCHED_MIN || policy > SCHED_MAX)
    {
      return EINVAL;
    }

  /* This is independent of scheduling policy in Win32. */
  return THREAD_PRIORITY_LOWEST;
}

int sched_get_priority_max(int policy)
{
  if (policy < SCHED_MIN || policy > SCHED_MAX)
    {
      return EINVAL;
    }

  /* This is independent of scheduling policy in Win32. */
  return THREAD_PRIORITY_HIGHEST;
}

====================================================

On Windows98, THREAD_PRIORITY_LOWEST is (-2) and 
THREAD_PRIORITY_HIGHEST is 2, and everything works just fine.

On WinCE 3.0, it so happen that THREAD_PRIORITY_LOWEST is 5
and THREAD_PRIORITY_HIGHEST is 1 (yes, I know, it is funny:
highest priority use smaller numbers) and the following happens:

sched_get_priority_min() returns 5
sched_get_priority_max() returns 1

and of course:

pthread_setschedparam() always fails to validate priority
level and returns EINVAL!!!

I believe that the correct fix is something like:

====================================================

int sched_get_priority_min(int policy)
{
  if (policy < SCHED_MIN || policy > SCHED_MAX)
    {
      return EINVAL;
    }

  /* This is independent of scheduling policy in Win32. */
  return MIN(THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_HIGHEST);
}

int sched_get_priority_max(int policy)
{
  if (policy < SCHED_MIN || policy > SCHED_MAX)
    {
      return EINVAL;
    }

  /* This is independent of scheduling policy in Win32. */
  return MAX(THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_HIGHEST);
}

====================================================

Of course the question remains whether it is 'legal' or
not to have an inverted priority, where the highest priority
is the smalest value.

The book "Multithreaded Programming with Pthread" by Bill Lewis & al.
says (p. 77):

<< POSIX gives no advice on how to use the
priority levels provided.  All you know is that for any given
policy, the priority level must be between 
sched_get_priority_min(policy) and
sched_get_priority_max(policy). >>

It does not say if sched_get_priority_min(policy) is always
assumed to be (numerically) smaller than
sched_get_priority_max(policy), but I assume that it is the case,
and obviously the posix_win32 implementation of
pthread_setschedparam() makes the same assumption.


-- 
Regards, -- Tristan Savatier (President, MpegTV LLC)

MpegTV: http://www.mpegtv.com - Tel: (415) 864 6466
                                Fax: (415) 704 3224

             reply	other threads:[~2000-05-23  1:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-23  1:16 Tristan Savatier [this message]
2000-05-23  1:54 ` (more) bug report (with fix): pthread_setschedparam() fails with Windows-CE " Tristan Savatier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=392A3EE3.37BBD811@mpegtv.com \
    --to=tristan@mpegtv.com \
    --cc=bok@bok.net \
    --cc=pthreads-win32@sourceware.cygnus.com \
    --cc=rpj@ise.canberra.edu.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).