Ignore:
Timestamp:
Nov 30, 2011, 7:31:34 PM (9 years ago)
Author:
gary
Message:

core: implement Queue on Win32 and on the PS3.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/platform/ps3/threadbase.h

    r1097 r1101  
    5353};
    5454
    55 class ConditionBase
     55class QueueBase
    5656{
    5757public:
    58     ConditionBase()
     58    QueueBase()
    5959    {
     60        m_start = m_count = 0;
     61        m_poppers = m_pushers = 0;
     62
     63        sys_lwmutex_attribute_t mattr;
     64        sys_lwmutex_attribute_initialize(mattr);
     65        sys_lwmutex_create(&m_mutex, &mattr);
     66
     67        sys_lwcond_attribute_t cattr;
     68        sys_lwcond_attribute_initialize(cattr);
     69        sys_lwcond_create(&m_empty_cond, &m_mutex, &cattr);
     70        sys_lwcond_create(&m_full_cond, &m_mutex, &cattr);
    6071    }
    6172
    62     ~ConditionBase()
     73    ~QueueBase()
    6374    {
     75        while (sys_lwcond_destroy(&m_empty_cond) == EBUSY)
     76            ;
     77        while (sys_lwcond_destroy(&m_full_cond) == EBUSY)
     78            ;
     79        while (sys_lwmutex_destroy(&m_mutex) == EBUSY)
     80            ;
    6481    }
    6582
    66     void Acquire()
     83    void Push(int value)
    6784    {
     85        /* FIXME: this is a copy of the pthread implementation, but we
     86         * should really use libsync2 instead. */
     87        sys_lwmutex_lock(&m_mutex, 0);
     88        m_pushers++;
     89        while (m_count == CAPACITY)
     90            sys_lwcond_wait(&m_full_cond, 0);
     91        m_pushers--;
     92        m_values[(m_start + m_count) % CAPACITY] = value;
     93        m_count++;
     94        if (m_poppers)
     95            sys_lwcond_signal(&m_empty_cond);
     96        sys_lwmutex_unlock(&m_mutex);
    6897    }
    6998
    70     void Release()
     99    int Pop()
    71100    {
    72     }
    73 
    74     void Wait()
    75     {
    76     }
    77 
    78     void Notify()
    79     {
     101        sys_lwmutex_lock(&m_mutex, 0);
     102        m_poppers++;
     103        while (m_count == 0)
     104            sys_lwcond_wait(&m_empty_cond, 0);
     105        m_poppers--;
     106        int ret = m_values[m_start];
     107        m_start = (m_start + 1) % CAPACITY;
     108        m_count--;
     109        if (m_pushers)
     110            sys_lwcond_signal(&m_full_cond);
     111        sys_lwmutex_unlock(&m_mutex);
     112        return ret;
    80113    }
    81114
    82115private:
     116    static size_t const CAPACITY = 100;
     117    int m_values[CAPACITY];
     118    size_t m_start, m_count;
     119    size_t m_poppers, m_pushers;
     120    sys_lwmutex_t m_mutex;
     121    sys_lwcond_t m_empty_cond, m_full_cond;
    83122};
    84123
Note: See TracChangeset for help on using the changeset viewer.