Esempio n. 1
0
static LoggingItem *createItem(
    const char *_file, const char *_function,
    int _line, LogLevel_t _level, int _type)
{
    LoggingItem *item = new LoggingItem(
        _file, _function, _line, _level, _type);

    malloc_count.ref();

#if DEBUG_MEMORY
    int val = item_count.fetchAndAddRelaxed(1) + 1;
    if (val == 0)
        memory_time.start();
    max_count = (val > max_count) ? val : max_count;
    if (memory_time.elapsed() > 1000)
    {
        cout<<"current memory usage: "
            <<val<<" * "<<sizeof(LoggingItem)<<endl;
        cout<<"max memory usage: "
            <<max_count<<" * "<<sizeof(LoggingItem)<<endl;
        cout<<"malloc count: "<<(int)malloc_count<<endl;
        memory_time.start();
    }
#else
    item_count.ref();
#endif

    return item;
}
int recursiveResult(int level)
{
    count.ref();
    if (--level > 0) {
        QFuture<int> f1 = run(recursiveResult, level);
        QFuture<int> f2 = run(recursiveResult, level);
        return f1.result() + f2.result();
    }
    return 1;
}
void recursiveRun(int level)
{
    count.ref();
    if (--level > 0) {
        QFuture<void> f1 = run(recursiveRun, level);
        QFuture<void> f2 = run(recursiveRun, level);
        f1.waitForFinished();
        f2.waitForFinished();
    }
}
Esempio n. 4
0
            void run()
            {
                testsTurn.release();

                threadsTurn.acquire();
                QVERIFY(!readWriteLock.tryLockForRead());
                testsTurn.release();

                threadsTurn.acquire();
                QVERIFY(readWriteLock.tryLockForRead());
                lockCount.ref();
                QVERIFY(readWriteLock.tryLockForRead());
                lockCount.ref();
                lockCount.deref();
                readWriteLock.unlock();
                lockCount.deref();
                readWriteLock.unlock();
                testsTurn.release();

                threadsTurn.acquire();
                QTime timer;
                timer.start();
                QVERIFY(!readWriteLock.tryLockForRead(1000));
                QVERIFY(timer.elapsed() >= 1000);
                testsTurn.release();

                threadsTurn.acquire();
                timer.start();
                QVERIFY(readWriteLock.tryLockForRead(1000));
                QVERIFY(timer.elapsed() <= 1000);
                lockCount.ref();
                QVERIFY(readWriteLock.tryLockForRead(1000));
                lockCount.ref();
                lockCount.deref();
                readWriteLock.unlock();
                lockCount.deref();
                readWriteLock.unlock();
                testsTurn.release();

                threadsTurn.acquire();
            }
Esempio n. 5
0
void QEglFSX11Hooks::platformInit()
{
    m_display = XOpenDisplay(0);
    if (!m_display)
        qFatal("Could not open display");

    XSetEventQueueOwner(m_display, XCBOwnsEventQueue);
    m_connection = XGetXCBConnection(m_display);

    running.ref();

    xcb_screen_iterator_t it = xcb_setup_roots_iterator(xcb_get_setup(m_connection));

    m_connectionEventListener = xcb_generate_id(m_connection);
    xcb_create_window(m_connection, XCB_COPY_FROM_PARENT,
                      m_connectionEventListener, it.data->root,
                      0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
                      it.data->root_visual, 0, 0);

    m_eventReader = new EventReader(this);
    m_eventReader->start();
}
Esempio n. 6
0
    void Create(void)
    {
        Destroy();

        if (!gLocalContext->FlagIsSet(Torc::AdminThread))
        {
            gAdminRunLoop = CFRunLoopGetMain();
            return;
        }

        m_createdThread = true;
        gAdminRunLoopRunning.ref();

        m_thread = new TorcOSXCallbackThread();
        m_thread->start();

        int count = 0;
        while (count++ < 10 && !m_thread->isRunning())
            QThread::msleep(count < 2 ? 10 : 100);

        if (!m_thread->isRunning())
            LOG(VB_GENERAL, LOG_WARNING, "OS X callback thread not started yet!");
    }
 void addRef() { ref.ref(); }
 void run()
 {
     quint64 i = 0;
     while (t.elapsed() < one_minute) {
         i++;
         uint nb = (i * 9 + lockCount.load() * 13) % threadCount;
         QMutexLocker locker(&mutex[nb]);
         if (sentinel[nb].load()) errorCount.ref();
         if (sentinel[nb].fetchAndAddRelaxed(5)) errorCount.ref();
         if (!sentinel[nb].testAndSetRelaxed(5, 0)) errorCount.ref();
         if (sentinel[nb].load()) errorCount.ref();
         lockCount.ref();
         nb = (nb * 17 + i * 5 + lockCount.load() * 3) % threadCount;
         if (mutex[nb].tryLock()) {
             if (sentinel[nb].load()) errorCount.ref();
             if (sentinel[nb].fetchAndAddRelaxed(16)) errorCount.ref();
             if (!sentinel[nb].testAndSetRelaxed(16, 0)) errorCount.ref();
             if (sentinel[nb].load()) errorCount.ref();
             lockCount.ref();
             mutex[nb].unlock();
         }
         nb = (nb * 15 + i * 47 + lockCount.load() * 31) % threadCount;
         if (mutex[nb].tryLock(2)) {
             if (sentinel[nb].load()) errorCount.ref();
             if (sentinel[nb].fetchAndAddRelaxed(53)) errorCount.ref();
             if (!sentinel[nb].testAndSetRelaxed(53, 0)) errorCount.ref();
             if (sentinel[nb].load()) errorCount.ref();
             lockCount.ref();
             mutex[nb].unlock();
         }
     }
 }
Esempio n. 9
0
 void run()
 {
     semaphore.acquire();
     count.ref();
 }
Esempio n. 10
0
 public: void run()
 {
     count.ref();
 }
Esempio n. 11
0
void tst_QReadWriteLock::tryWriteLock()
{
    {
        QReadWriteLock rwlock;
        QVERIFY(rwlock.tryLockForWrite());
        rwlock.unlock();
        QVERIFY(rwlock.tryLockForWrite());
        rwlock.unlock();

        rwlock.lockForWrite();
        QVERIFY(!rwlock.tryLockForWrite());
        QVERIFY(!rwlock.tryLockForWrite());
        rwlock.unlock();

        rwlock.lockForRead();
        QVERIFY(!rwlock.tryLockForWrite());
        rwlock.unlock();
    }

    {
        QReadWriteLock rwlock(QReadWriteLock::Recursive);
        QVERIFY(rwlock.tryLockForWrite());
        rwlock.unlock();
        QVERIFY(rwlock.tryLockForWrite());
        rwlock.unlock();

        rwlock.lockForWrite();
        QVERIFY(rwlock.tryLockForWrite());
        QVERIFY(rwlock.tryLockForWrite());
        rwlock.unlock();
        rwlock.unlock();
        rwlock.unlock();

        rwlock.lockForRead();
        QVERIFY(!rwlock.tryLockForWrite());
        rwlock.unlock();
    }

    // functionality test
    {
        class Thread : public QThread
        {
        public:
            Thread() : failureCount(0) { }
            void run()
            {
                testsTurn.release();

                threadsTurn.acquire();
                if (readWriteLock.tryLockForWrite())
                    failureCount++;
                testsTurn.release();

                threadsTurn.acquire();
                if (!readWriteLock.tryLockForWrite())
                    failureCount++;
                if (!lockCount.testAndSetRelaxed(0, 1))
                    failureCount++;
                if (!lockCount.testAndSetRelaxed(1, 0))
                    failureCount++;
                readWriteLock.unlock();
                testsTurn.release();

                threadsTurn.acquire();
                if (readWriteLock.tryLockForWrite(1000))
                    failureCount++;
                testsTurn.release();

                threadsTurn.acquire();
                if (!readWriteLock.tryLockForWrite(1000))
                    failureCount++;
                if (!lockCount.testAndSetRelaxed(0, 1))
                    failureCount++;
                if (!lockCount.testAndSetRelaxed(1, 0))
                    failureCount++;
                readWriteLock.unlock();
                testsTurn.release();

                threadsTurn.acquire();
            }

            int failureCount;
        };

        Thread thread;
        thread.start();

        testsTurn.acquire();
        readWriteLock.lockForRead();
        lockCount.ref();
        threadsTurn.release();

        testsTurn.acquire();
        lockCount.deref();
        readWriteLock.unlock();
        threadsTurn.release();

        testsTurn.acquire();
        readWriteLock.lockForRead();
        lockCount.ref();
        threadsTurn.release();

        testsTurn.acquire();
        lockCount.deref();
        readWriteLock.unlock();
        threadsTurn.release();

        // stop thread
        testsTurn.acquire();
        threadsTurn.release();
        thread.wait();

        QCOMPARE(thread.failureCount, 0);
    }
}
Esempio n. 12
0
void LogForwardThread::forwardMessage(LogMessage *msg)
{
#ifdef DUMP_PACKET
    QList<QByteArray>::const_iterator it = msg->begin();
    int i = 0;
    for (; it != msg->end(); ++it, i++)
    {
        QByteArray buf = *it;
        cout << i << ":\t" << buf.size() << endl << "\t"
             << buf.toHex().constData() << endl << "\t"
             << buf.constData() << endl;
    }
#endif

    msgsSinceHeartbeat.ref();

    // First section is the client id
    QByteArray clientBa = msg->first();
    QString clientId = QString(clientBa.toHex());

    QByteArray json     = msg->at(1);

    if (json.size() == 0)
    {
        // This is either a ping response or a first gasp
        logClientMapMutex.lock();
        LoggerListItem *logItem = logClientMap.value(clientId, NULL);
        logClientMapMutex.unlock();
        if (!logItem)
        {
            // Send an initial ping so the client knows we are in the house
            emit pingClient(clientId);
        }
        else
        {
            // cout << "pong " << clientId.toLocal8Bit().constData() << endl;
            loggingGetTimeStamp(&logItem->epoch, NULL);
        }
        return;
    }

    QMutexLocker lock(&logClientMapMutex);
    LoggerListItem *logItem = logClientMap.value(clientId, NULL);

    // cout << "msg  " << clientId.toLocal8Bit().constData() << endl;
    if (logItem)
    {
        loggingGetTimeStamp(&logItem->epoch, NULL);
    }
    else
    {
        LoggingItem *item = LoggingItem::create(json);

        logClientCount.ref();
        LOG(VB_FILE, LOG_DEBUG, QString("New Logging Client: ID: %1 (#%2)")
            .arg(clientId).arg(logClientCount.fetchAndAddOrdered(0)));

#ifndef NOLOGSERVER
        // TODO FIXME This is not thread-safe!
        if (logClientCount.fetchAndAddOrdered(0) > 1 && m_shutdownTimer &&
            m_shutdownTimer->isActive())
        {
            LOG(VB_GENERAL, LOG_INFO, "Aborting shutdown timer");
            m_shutdownTimer->stop();
        }
#endif

        QMutexLocker lock2(&loggerMapMutex);
        QMutexLocker lock3(&logRevClientMapMutex);

        // Need to find or create the loggers
        LoggerList *loggers = new LoggerList;
        LoggerBase *logger;

        // FileLogger from logFile
        QString logfile = item->logFile();
        logfile.detach();
        if (!logfile.isEmpty())
        {
            logger = FileLogger::create(logfile, lock2.mutex());

            ClientList *clients = logRevClientMap.value(logger);

            if (clients)
                clients->insert(0, clientId);

            if (logger && loggers)
                loggers->insert(0, logger);
        }

#ifndef _WIN32
        // SyslogLogger from facility
        int facility = item->facility();
#if CONFIG_SYSTEMD_JOURNAL
        if ((facility > 0) || (facility == SYSTEMD_JOURNAL_FACILITY))
        {
            logger = SyslogLogger::create(lock2.mutex(), facility > 0);
#else
        if (facility > 0)
        {
            logger = SyslogLogger::create(lock2.mutex());
#endif

            ClientList *clients = logRevClientMap.value(logger);

            if (clients)
                clients->insert(0, clientId);

            if (logger && loggers)
                loggers->insert(0, logger);
        }
#endif

        // DatabaseLogger from table
        QString table = item->table();
        if (!table.isEmpty())
        {
            logger = DatabaseLogger::create(table, lock2.mutex());

            ClientList *clients = logRevClientMap.value(logger);

            if (clients)
                clients->insert(0, clientId);

            if (logger && loggers)
                loggers->insert(0, logger);
        }

        logItem = new LoggerListItem;
        loggingGetTimeStamp(&logItem->epoch, NULL);
        logItem->list = loggers;
        logClientMap.insert(clientId, logItem);

        item->DecrRef();
    }

#ifndef NOLOGSERVER
    m_zmqPubSock->sendMessage(*msg);
#else
    if (logItem && logItem->list && !logItem->list->isEmpty())
    {
        LoggerList::iterator it = logItem->list->begin();
        LoggingItem *item = LoggingItem::create(json);
        if (!item)
            return;
        for (; it != logItem->list->end(); ++it)
        {
            (*it)->logmsg(item);
        }
        item->DecrRef();
    }
#endif
}

/// \brief Stop the thread by setting the abort flag
void LogForwardThread::stop(void)
{
    m_aborted = true;
}