[fpc-devel] Linux thread priority mess (and possible implementation)

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Jun 24 11:06:23 CEST 2010


Hi,

Looking at the rtl/unix/cthread.pp unit, I noticed two $Warning lines sayin
that setting or getting thread priority is not implemented for any unix
system.  WHAT???

I then thought I would go ahead and do the implementation. Seeing that
cthreads using libc and POSIX implementation, there was lots of docs about
it on the net, but the deeper I went the more confused I got.

In summary, this info is what I gathered so far:

* Linux thread priority is VERY confusing (compared to Windows).
* You have Static and Dynamic thread priority.
* You also have various Thread Scheduling priorities (different from
  thread priorities).
* Thread priority values range from -15..15.
    15 being real-time
    0 being normal
    -15 being idle
  When this range is applied, I have no idea!
* Based on the scheduler used you also get other priority ranges.
    SCHED_OTHER has a range of 0 (no range)
    SCHED_FIFO (first in - first out) has a range of 1..99
    SCHED_RR (round robin) has a range of 1..99
* Dynamic priority does not apply to processes with a non-zero static
  priority. WTF, I'm lost again!
* Dynamic priorities have a range of -20..20
  Just to f*ck with you even more, this range is now switched around.
    -20 is real-time
    0 is normal
    20 is idle
* You can only set Dynamic priority with libc.setpriority() or libc.nice()
* A normal user can only use the Thread Scheduler SCHED_OTHER, all others
  require super user rights. So no real-time apps for standard users.
* NICE levels range from 4..-5


All this information comes from these URLs:

* Linux Thread Priority
  http://www.midnightbeach.com/Kylix.Sidebar.3.html

* Linux POSIX 1003.1c Thread Attribute Support
  http://www2.roguewave.com/support/docs/leif/sourcepro/html/threadspl/4-3.html

* Java thread priority > Linux priority
  http://www.javamex.com/tutorials/threads/priority_what.shtml

* Threads: Basic theory and libraries
  http://www.cs.cf.ac.uk/Dave/C/node29.html#SECTION0029414000000000000000

   NOTE: the last one shows an example implementation for thread priority.
   I tried to translate that into Object Pascal or the cthreads
   implementation shown below


I have created a Thread Manager demo that can use various Synchronization
Methods and Thread Priorities. But in all cases, the thread priority
doesn't seem to make any difference under Linux. I can see from the debug
lines (writeln statements below) that the thread priorities are being set
(seems to use the -20..20 range but wrong way round), but they all execute
at the same speed on my Linux quad core system.

   http://opensoft.homeip.net/~graemeg/thread_manager.png


Can anybody make heads or tails about thread priority under Linux (or any
unix system for that matter). Is it possible to have various thread
priorities or not under Linux? I use multi-threading a lot in my code, and
assumed thread priority was implemented under FPC Linux, but looking at the
original cthreads.pp unit, it clearly is not (for any unix platform).

Is the following code correct?


---------------[ rtl/unix/cthreads.pp ]-------------------------------
  function  CThreadSetPriority (threadHandle : TThreadID; Prio: longint):
boolean;
    var
      param: sched_param;
      ret: integer;
      priority: integer;
    begin
      {.$Warning ThreadSetPriority needs to be implemented}
      writeln('DEBUG:  CThreadSetPriority  priority=', Prio);
      param.__sched_priority := Prio;
      priority := 0;   { SCHED_OTHER is always used }
      ret := pthread_setschedparam(pthread_t(threadHandle), priority, @param);
      Result := (ret = 0);
      writeln('DEBUG:  CThreadSetPriority - DONE');
    end;


  function  CThreadGetPriority (threadHandle : TThreadID): Integer;
    var
      param: sched_param;
      ret: integer;
      priority: longint;
    begin
      {.$Warning ThreadGetPriority needs to be implemented}
      writeln('DEBUG:  CThreadGetPriority ');
      priority := 0;  { SCHED_OTHER is always used }
      ret := pthread_getschedparam (pthread_t(threadHandle), @priority,
@param);
      Result := param.__sched_priority;
      writeln('DEBUG:  CThreadGetPriority (priority=', Result, ') - DONE');
    end;

-----------------------------[ end ]-----------------------


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/




More information about the fpc-devel mailing list