Changeset 1098


Ignore:
Timestamp:
Nov 30, 2011, 2:43:29 AM (9 years ago)
Author:
sam
Message:

core: replace the Condition class with a nicer Queue object that is
implemented using a cond_var on Linux, but will use something else on
Windows.

Location:
trunk/src/thread
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/thread/thread.h

    r1093 r1098  
    3232};
    3333
    34 class Condition : public ConditionBase
     34class Queue : public QueueBase
    3535{
    3636public:
    37     Condition() : ConditionBase() {}
     37    Queue() : QueueBase() {}
    3838};
    3939
  • trunk/src/thread/threadbase.h

    r1095 r1098  
    1818
    1919#if defined __linux__ || defined __native_client__
     20#   include <cstring>
    2021#   include <pthread.h>
    2122#elif defined _WIN32
     
    7576};
    7677
    77 class ConditionBase
     78class QueueBase
    7879{
    7980public:
    80     ConditionBase()
     81    QueueBase()
    8182    {
    8283#if defined __linux__ || defined __native_client__
     84        memset(m_values, 0, sizeof(m_values));
     85        m_waiting = 0;
     86        m_size = 0;
    8387        pthread_mutex_init(&m_mutex, NULL);
    8488        pthread_cond_init(&m_cond, NULL);
     
    8690    }
    8791
    88     ~ConditionBase()
     92    ~QueueBase()
    8993    {
    9094#if defined __linux__ || defined __native_client__
     
    9498    }
    9599
    96     void Acquire()
     100    void Push(int value)
    97101    {
    98102#if defined __linux__ || defined __native_client__
    99103        pthread_mutex_lock(&m_mutex);
    100 #endif
    101     }
    102 
    103     void Release()
    104     {
    105 #if defined __linux__ || defined __native_client__
     104        m_values[m_size] = value;
     105        m_size++;
     106        if (m_waiting)
     107            pthread_cond_signal(&m_cond);
    106108        pthread_mutex_unlock(&m_mutex);
    107109#endif
    108110    }
    109111
    110     void Wait()
     112    int Pop()
    111113    {
    112114#if defined __linux__ || defined __native_client__
    113         pthread_cond_wait(&m_cond, &m_mutex);
    114 #endif
    115     }
    116 
    117     void Notify()
    118     {
    119 #if defined __linux__ || defined __native_client__
    120         pthread_cond_signal(&m_cond);
     115        pthread_mutex_lock(&m_mutex);
     116        /* Loop until there is something in the queue. Be careful, we
     117         * could get woken up but another thread may have eaten the
     118         * message in the meantime. */
     119        while (!m_size)
     120        {
     121            m_waiting++;
     122            pthread_cond_wait(&m_cond, &m_mutex);
     123            m_waiting--;
     124        }
     125        m_size--;
     126        int ret = m_values[0];
     127        memmove(m_values, m_values + 1, m_size * sizeof(m_values[0]));
     128        pthread_mutex_unlock(&m_mutex);
     129        return ret;
    121130#endif
    122131    }
     
    124133private:
    125134#if defined __linux__ || defined __native_client__
     135    int m_values[100];
     136    size_t m_waiting, m_size;
    126137    pthread_mutex_t m_mutex;
    127138    pthread_cond_t m_cond;
     
    135146    {
    136147#if defined __linux__ || defined __native_client__
    137         pthread_create(&m_thread, NULL, fn, data);
     148        /* Set the joinable attribute for systems who don't play nice */
     149        pthread_attr_t attr;
     150        pthread_attr_init(&attr);
     151        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
     152        pthread_create(&m_thread, &attr, fn, data);
    138153#elif defined _WIN32
    139         m_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fn, data, 0, &m_tid);
     154        m_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fn,
     155                                data, 0, &m_tid);
    140156#endif
    141157    }
Note: See TracChangeset for help on using the changeset viewer.