0

I'm working on an avionics OS (thread layer) and I'm looking for an optimal solution regarding the following (simplified) requirement: "Threads waiting for [various objects] are queued in priority order. For the same priority, threads are also queued in FIFO order".

[various objects] are e.g. semaphores.

I thought I could build such a waiting list using a classical linked list, which makes insertion/sorting relatively fast and easy, and which perfectly fits with expected usage (one thread goes in waiting state at a time). But I am working on a bare metal target and I don't have any libc support, thus I have no malloc (which is very useful for linked list!).

For sorting threads by priority I usually use binary heaps (http://en.wikipedia.org/wiki/Binary_heap) which is very efficient, but it can't be used here because "FIFO order" can not be managed this way.

Of course, I can do it with more classical sorting algorithms, but they are usually time-consuming, even for one insertion, because a lot of array elements may be moved at each insertion.

So I wonder if an appropriate algorithm exists... maybe a kind of improved binary heap?... Or a “static” linked list?... Or maybe the best thing is an allocator algorithm associated with a linked list?...

For information: - the total number of threads is limited to 128, so memory need is always finite and can be known/reserved at compile time. - I have a limited quantity or RAM, so I can hardly do constructions such a binary heap sorted by priority pointing on FIFOs (naturally ordered by arrival time)…

I'd really appreciate any idea and fresh look regarding this problem.

Thanks !

2 Answers2

0

Probably you need a stable in-place sort - it will maintain relative order of items after sorting by priority, satisfying your FIFO requirement.

Pick anything from list in wiki, for example in-place merge sort, block sort and tim sort are both in-place and stable: http://en.wikipedia.org/wiki/Sorting_algorithm

Regarding memory allocation and linked lists - maybe you can implement your own malloc? You can allocate fixed size heap (128 * thread information size), and then use index of each block as a pointer. So real pointer to object will be (heap start address) + index * (block size). And then implement sorting as you normally would, but with indexes instead of pointers.

Another idea is to separate FIFO requirement from priority queue requirement, and sort containers with queues of same-priority items - but this would require dynamic list allocation and larger heap.

Alexander
  • 4,153
  • 1
  • 24
  • 37
  • Thanks Alexander for pointing me a few tips. I've read tons of things about sorting algorithms since 2 weeks and I've just understood that "stable" is a major keyword. Shame on me. – user2010402 Jun 25 '14 at 15:47
  • The drawback with wiki is that algorithms mainly apply on array of unsorted data. From my side, what I want is inserting a new data inside an array already sorted. Of course I can add the new element and apply a complete sorting algorithm, but it often seems to be a big operation for a simple thing... – user2010402 Jun 25 '14 at 16:00
0

The standard technique for this problem is the Bentley-Saxe priority queue. I would describe it in detail here, along with some tips for how to implement it in-place with minimal memory requirements, but anything I said would just be reiterating Pat Morin's excellent answer over on the CS Theory StackExchange: Pat Morin's answer to "Is there a stable heap?" on cstheory.SE

Community
  • 1
  • 1
quintopia
  • 121
  • 5