控制Linux中信号量队列中的去队列顺序

时间:2013-11-16 08:46:17

标签: linux multithreading pthreads semaphore

我想实现代码,我想将几​​个“优先级数”分配给不同的线程。一些线程可能在同一个信号量上等待。假设线程在信号量S上排队,另一个线程在信号量S上执行sem_post。一旦执行sem_post,我希望信号量S队列中具有最高“优先级数”的线程获得对信号量的访问权限而不是任何其他线程。据我所知,没有直接的方法来实现这一点,因为将被选择用于访问的线程可以是队列的任何一个元素(并且不一定是FIFO等)。事实上,我尝试增加线程的pthread优先级,但我意识到它也不起作用。有人可以指导我如何在C. Thankyou中手动实现这种控制信号队列的设计。

3 个答案:

答案 0 :(得分:1)

我可以想到两种方法:

  • 使用condition variable“唤醒部分或全部服务员”,他们将自行解决优先权问题;或
  • 使用(实时)信号按优先顺序“唤醒单个特定服务员”

在每种情况下,信号量至少有mutex和一些簿记。如果低于零,则其绝对值是服务员的数量(例如,值== -3表示3个线程正在等待)。

条件变量方法

信号量跟踪任何给定优先级的服务员数量,以及任何给定优先级的释放的服务员数量。在伪C:

typedef struct priority_sem_s {
  int              value;     // if negative, abs(sem->value) == no. of waiting threads
  pthread_mutex_t  mutex;
  pthread_cond_t   cv;
  int              n_waiting[N_PRIORITIES];  // no. waiting (blocked) at each priority
  int              n_released[N_PRIORITIES]; // no. waiters released (unblocked) at each priority
} priosem_t;

void post(priosem_t *sem):
  lock(sem->mutex);
  sem->value++;

  if (sem->value <= 0 && prio_waiting_is_NOT_empty(sem)):
    // someone was waiting; release one of the highest prio
    int prio = fetch_highest_prio_waiting(sem);
    sem->prio_waiting[prio]--;
    sem->prio_released[prio]++;
    cond_broadcast(sem->cv, sem->mutex);

  unlock(sem->mutex);

void wait(priosem_t *sem, int prio):
  lock(sem->mutex);
  sem->value--;

  if (sem->value < 0):
    // get in line
    sem->prio_waiting[prio]++;
    while (sem->prio_released[prio] < 0):
      cond_wait(sem->cv, sem->mutex);
    // ok to leave
    sem->prio_released[prio]--;

  unlock(sem->mutex);

优点:可跨进程共享(在共享内存中实现)。

缺点:唤醒每个服务员只释放一个。 Martin James建议每个优先级使用一个条件变量,这将以更多同步原语为代价减少“不必要的”唤醒。

信号方法

使用sigsuspendrealtime signal与noop处理程序暂停和恢复服务员。在伪C:

typedef struct priority_sem_s {
  int              value;    // if negative, abs(value) == no. of waiting threads
  pthread_mutex_t  mutex;
  void            *waiting;  // ordered list of [priority, thread-id] pairs
} priosem_t;

void post(priosem_t *sem):
  lock(sem->mutex);
  sem->value++;

  if (sem->value <= 0 && waiting_queue_is_NOT_empty(sem)):
    pthread_t tid = pop_highest_prio_waiter(sem);
    pthread_kill(tid, SIGRTMIN+n);

  unlock(sem->mutex);

void wait(priosem_t *sem, int prio):
  // XXX --> PRECONDITION:  SIGRTMIN+n is SIG_BLOCK'd <-- XXX
  // XXX --> PRECONDITION:  SIGRTMIN+n has a no-op handler installed <-- XXX
  lock(sem->mutex);
  sem->value--;

  if (sem->value < 0):
    // get in line
    add_me_to_wait_list(sem, pthread_self(), prio);
    unlock(sem->mutex);
    sigsuspend(full_mask_except_sigrtmin_plus_n);
    return;  // OK!

  unlock(sem->mutex);

优势:概念上更简单;没有不必要的唤醒。

缺点:无法跨进程共享。必须选择或动态选择可用的实时信号(查找SIGDRFL处理的未屏蔽信号?)并尽早屏蔽。

答案 1 :(得分:0)

我认为您必须使用post()和wait(优先级)方法构建自己的'PrioritySemaphore',(PS),类。你需要一个互斥锁来保护内部数据,一个'totalCount'int和一个数组[priority]结构包含一个信号量供线程等待和'PriorityCount'int。

等待(优先级):锁定互斥锁。如果totalCount> 0,则将其解锁,解锁互斥锁并返回。如果totalCount = 0,则使用(priority)索引数组,包括PriorityCount,解锁互斥锁并等待信号量。

post():锁定互斥锁。如果totalCount = 0,请将解锁互斥锁并返回。如果totalCount> 0,则从最高优先级端迭代数组,查找非零PriorityCount。如果没有找到,请输入totalCount,解锁互斥锁并返回。如果找到非零的PriorityCount,则对其进行判断,以该优先级发信号通知信号量,解锁互斥锁并返回。

答案 2 :(得分:0)

我必须开发一种具有以下特征的信号灯结构:

  1. 有一个关键部分,最多Capacity个线程可以同时输入和执行。执行线程退出关键部分后;
  2. 当信号量达到最大容量且执​​行队列已满时:队列中的线程进入睡眠状态,并在其他线程退出关键部分时被唤醒;
  3. 执行队列具有FIFO语义;
  4. 有一个通知机制,用于通知等待线程其在队列中的位置;
  5. 只有进入关键部分的线程才被允许退出。

点1-2通常描述理论上的semaphore数据类型,而点3-4则要求其他行为/ API约束和功能。即使信号量经常被错误地表示为同步原语本身,也可以仅使用 mutex condition variable 原语来构建这种结构,这不会太令人惊讶。它遵循C ++ 11实现,该实现也可以移植到提供上述原语的任何语言/环境中。由于通知机制要求不使信号灯锁保持忙碌,因此该实现并非完全无关紧要。自定义优先级和优先级编辑尚未实现,因为我不需要类似于调度程序的功能,但也应该可以。

Semaphore.h

#pragma once

#include <condition_variable>
#include <mutex>
#include <thread>
#include <functional>
#include <list>

namespace usr
{
    typedef std::function<void(unsigned processIndex)> SemaphoreNotifier;

    class Semaphore;

    class SemaphoreToken final
    {
        friend class Semaphore;
    public:
        SemaphoreToken();
    private:
        SemaphoreToken(Semaphore &semaphore);
    private:
        void Invalidate();
    private:
        Semaphore *Parent;
        std::thread::id ThreadId;
    };

    class SemaphoreCounter final
    {
        friend class Semaphore;
    public:
        SemaphoreCounter();
    private:
        void Increment();
    public:
        unsigned GetCount() const { return m_count; }
    private:
        unsigned m_count;
    };

    class Semaphore final
    {
        class Process
        {
        public:
            Process(unsigned index);
        public:
            void Wait();
            void Set();
            void Decrement();
            void Detach();
        public:
            bool IsDetached() const { return m_detached; }
            unsigned GetIndex() const { return m_index; }
        private:
            std::mutex m_mutex;
            unsigned m_index;                   // Guarded by m_mutex
            bool m_detached;                    // Guarded by m_mutex
            std::unique_lock<std::mutex> m_lock;
            std::condition_variable m_cond;
        };
    public:
        Semaphore(unsigned capacity = 1);
    public:
        SemaphoreToken Enter();
        SemaphoreToken Enter(SemaphoreCounter &counter, unsigned &id);
        SemaphoreToken Enter(const SemaphoreNotifier &notifier);
        SemaphoreToken Enter(const SemaphoreNotifier &notifier, SemaphoreCounter &counter, unsigned &id);
        bool TryEnter(SemaphoreToken &token);
        bool TryEnter(SemaphoreCounter &counter, unsigned &id, SemaphoreToken &token);
        void Exit(SemaphoreToken &token);
    private:
        bool enter(bool tryEnter, const SemaphoreNotifier &notifier, SemaphoreCounter *counter, unsigned &id, SemaphoreToken &token);
    private:
        // Disable copy constructor and assign operator
        Semaphore(const Semaphore &);
        Semaphore & operator=(const Semaphore &);
    public:
        unsigned GetCapacity() const { return m_capacity; }
    private:
        mutable std::mutex m_mutex;
        unsigned m_capacity;
        unsigned m_leftCapacity;               // Guarded by m_mutex
        std::list<Process *> m_processes;      // Guarded by m_mutex
    };
}

Semaphore.cpp

#include "Semaphore.h"
#include <cassert>
#include <limits>
#include <algorithm>

using namespace std;
using namespace usr;

Semaphore::Semaphore(unsigned capacity)
{
    if (capacity == 0)
        throw runtime_error("Capacity must not be zero");

    m_capacity = capacity;
    m_leftCapacity = capacity;
}

SemaphoreToken Semaphore::Enter()
{
    unsigned id;
    SemaphoreToken token;
    enter(false, nullptr, nullptr, id, token);
    return token;
}

SemaphoreToken Semaphore::Enter(SemaphoreCounter &counter, unsigned &id)
{
    SemaphoreToken token;
    enter(false, nullptr, &counter, id, token);
    return token;
}

SemaphoreToken Semaphore::Enter(const SemaphoreNotifier &notifier)
{
    unsigned id;
    SemaphoreToken token;
    enter(false, notifier, nullptr, id, token);
    return token;
}

SemaphoreToken Semaphore::Enter(const SemaphoreNotifier &notifier,
    SemaphoreCounter &counter, unsigned &id)
{
    SemaphoreToken token;
    enter(false, notifier, &counter, id, token);
    return token;
}

bool Semaphore::TryEnter(SemaphoreToken &token)
{
    unsigned id;
    return enter(true, nullptr, nullptr, id, token);
}

bool Semaphore::TryEnter(SemaphoreCounter &counter, unsigned &id, SemaphoreToken &token)
{
    return enter(true, nullptr, &counter, id, token);
}

bool Semaphore::enter(bool tryEnter, const SemaphoreNotifier &notifier,
    SemaphoreCounter *counter, unsigned &id, SemaphoreToken &token)
{
    unique_lock<mutex> lock(m_mutex);
    if (counter != nullptr)
    {
        id = counter->GetCount();
        counter->Increment();
    }

    if (m_leftCapacity > 0)
    {
        // Semaphore is availabile without accessing queue
        assert(m_processes.size() == 0);
        m_leftCapacity--;
    }
    else
    {
        if (tryEnter)
            return false;

        Process process((unsigned)m_processes.size());
        unsigned previousIndex = numeric_limits<unsigned>::max();
        m_processes.push_back(&process);

        // Release semaphore unlock
        lock.unlock();

    NotifyAndWait:
        unsigned index = process.GetIndex();
        if (notifier != nullptr && index != 0 && index != previousIndex)
        {
            try
            {
                // Notify the caller on progress
                notifier(index);
            }
            catch (...)
            {
                // Retake Semaphore lock
                lock.lock();

                // Remove the failing process
                auto found = std::find(m_processes.begin(), m_processes.end(), &process);
                auto it = m_processes.erase(found);
                for (; it != m_processes.end(); it++)
                {
                    // Decrement following processes
                    auto &otherProcess = **it;
                    otherProcess.Decrement();
                    otherProcess.Set();
                }

                // Rethrow. NOTE: lock will be unlocked by RAII
                throw;
            }
            previousIndex = index;
        }

        process.Wait();
        if (!process.IsDetached())
            goto NotifyAndWait;
    }

    token = SemaphoreToken(*this);
    return true;
}

void Semaphore::Exit(SemaphoreToken &token)
{
    if (this != token.Parent || token.ThreadId != this_thread::get_id())
        throw runtime_error("Exit called from wrong semaphore or thread");

    {
        unique_lock<mutex> lock(m_mutex);
        if (m_processes.size() == 0)
        {
            m_leftCapacity++;
        }
        else
        {
            auto front = m_processes.front();
            m_processes.pop_front();
            front->Detach();
            front->Set();

            for (auto process : m_processes)
            {
                process->Decrement();
                process->Set();
            }
        }

        token.Invalidate();
    }
}

SemaphoreToken::SemaphoreToken() :
    Parent(nullptr)
{
}

SemaphoreToken::SemaphoreToken(usr::Semaphore &semaphore) :
    Parent(&semaphore),
    ThreadId(this_thread::get_id())
{
}

void SemaphoreToken::Invalidate()
{
    Parent = nullptr;
    ThreadId = thread::id();
}

SemaphoreCounter::SemaphoreCounter()
    : m_count(0)
{
}

void SemaphoreCounter::Increment()
{
    m_count++;
}

Semaphore::Process::Process(unsigned index) :
    m_index(index),
    m_detached(false),
    m_lock(m_mutex)
{
}

void Semaphore::Process::Wait()
{
    m_cond.wait(m_lock);
}

void Semaphore::Process::Set()
{
    m_cond.notify_one();
}

void Semaphore::Process::Decrement()
{
    unique_lock<mutex> lock(m_mutex);
    assert(m_index > 0);
    m_index--;
}

void Semaphore::Process::Detach()
{
    unique_lock<mutex> lock(m_mutex);
    assert(m_index == 0);
    m_detached = true;
}

我使用以下示例代码对其进行了测试:

SemaphoreCounter counter;
Semaphore semaphore(4);  // Up to 4 threads can execute simultaneously

vector<shared_ptr<thread>> threads;
int threadCount = 300;
for (int i = 0; i < threadCount; i++)
{
    threads.push_back(std::make_shared<thread>([&semaphore, &counter]
    {
        unsigned threadId;
        auto token = semaphore.Enter([&threadId](unsigned index) {
            cout << "Thread " << threadId << " has " << index << " processes ahead before execution" << endl;
        }, counter, threadId);

        cout << "EXECUTE Thread " << threadId << endl;
        std::this_thread::sleep_for(15ms);
        semaphore.Exit(token);
    }));
}

for (int i = 0; i < threadCount; i++)
    threads[i]->join();