예제 #1
0
void Barber::run() {
    while (customersLeft.tryAcquire()) {
        l1.lock();
        buffer[in] = ID;
        in = (in + 1) % numBarbers;
        l1.unlock();
        barberReady->release(); // signal availability
        customerReady[ID].acquire(); // wait for customer to be sitted
        barberDone[ID].release(); // signal that hair is done
    }
}
예제 #2
0
void tst_QSemaphore::tryAcquireWithTimeoutStarvation()
{
    class Thread : public QThread
    {
    public:
        QSemaphore startup;
        QSemaphore *semaphore;
        int amountToConsume, timeout;

        void run()
        {
            startup.release();
            forever {
                if (!semaphore->tryAcquire(amountToConsume, timeout))
                    break;
                semaphore->release(amountToConsume);
            }
        }
static PyObject *meth_QSemaphore_tryAcquire(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds)
{
    PyObject *sipParseErr = NULL;

    {
        int a0 = 1;
        QSemaphore *sipCpp;

        static const char *sipKwdList[] = {
            sipName_n,
        };

        if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, sipKwdList, NULL, "B|i", &sipSelf, sipType_QSemaphore, &sipCpp, &a0))
        {
            bool sipRes;

            Py_BEGIN_ALLOW_THREADS
            sipRes = sipCpp->tryAcquire(a0);
            Py_END_ALLOW_THREADS

            return PyBool_FromLong(sipRes);
        }
    }
예제 #4
0
void SqlwriteThread_buffer::run()
{
    QString databaseseq;

    QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL",databaseseq.setNum(m_index));
    database.setHostName(m_hostname);
    database.setDatabaseName(m_databasename);
    database.setUserName(m_usrname);
    database.setPassword(m_psword);

    if (!database.open())
    {
        database.close();
        qDebug()<<trUtf8("数据库打不开")<<endl;
        return ;
    }
    else
    {
        qDebug()<<trUtf8("线程内数据已经打开")<<endl;
    }


    /*
    QString i2c;
    QFile timelog("timelog"+i2c.setNum(m_index)+".txt");
    if (!timelog.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
    {
        qDebug()<<trUtf8("LOG日志没有打开")<<endl;
        return;
    }
    timelog.close();
    */

    QSqlQuery sqlquery(database);
    //    QTime timerecord;
    //    timerecord.start();
    qDebug()<<"sqltest thread runing...";

    mysqldata *data=g_buffer.data();
    mysqldata tempdata;
    bool flag = true;
    while(g_rIndex < DATABLOCKSIZE)
    {
        mutex.lock();
        if(usedBuffer.tryAcquire())
        {            
            flag = false;
            int index=g_rIndex;
            ++g_rIndex;
            qDebug()<<QThread::currentThreadId()<<trUtf8("线程读写第")
                    <<index<<trUtf8("块数据,now数据缓存区内存有 ")<<usedBuffer.available()+1<<" blocks!";

            mutex.unlock();     //先锁住rindex,如果能获取得到资源,就取走该资源。rndex指向该资源。
            tempdata = data[index % 10];

            //qDebug()<<"-";//
            freeBuffer.release();
        }
        else
        {
            mutex.unlock();
            continue;
        }

        int range = tempdata.datavector.size();
        for(int i=0;i <= range/SQLLINE ; ++i)		//i要改成大数,同range
        {
            QString sqlstatement = "";
            int statementnum =( (i+1)*SQLLINE > range ) ? range - i*SQLLINE: SQLLINE;
            sqlstatement="insert into InsertTest("
                    "id,"
                    "taskseq,"
                    "protocoltype ,"
                    "threadnumintask,"
                    "idinthread,"
                    "framesize,"
                    "DestMac,"
                    "SourMac,"
                    "TypeorLength,"
                    "Data,"
                    "Upperlayer) VALUES";


            for(int j=0; j < statementnum; j++)
            {
                sqlstatement += "("+tempdata.idvector.at(i*SQLLINE + j);
                sqlstatement += "," + tempdata.taskseqvector.at(i*SQLLINE + j);
                sqlstatement += ", '" + tempdata.typevector.at(i*SQLLINE + j)+"'";
                sqlstatement += "," + tempdata.threadnumvector.at(i*SQLLINE + j);
                sqlstatement += "," + tempdata.idinthreadvector.at(i*SQLLINE + j);
                sqlstatement += "," + tempdata.framesizevector.at(i*SQLLINE + j);
                sqlstatement += ", '" + tempdata.DestMacvector.at(i*SQLLINE + j)+"'";
                sqlstatement += ", '" + tempdata.SourMacvector.at(i*SQLLINE + j)+"'";
                sqlstatement += ", '" + tempdata.TypeorLengthvector.at(i*SQLLINE + j)+"'";
                sqlstatement += ", '" + tempdata.datavector.at(i*SQLLINE + j)+"'";
                sqlstatement += ", '" + tempdata.uplayervector.at(i*SQLLINE + j)+"')";
                if( j == statementnum -1) sqlstatement += ";";
                else sqlstatement += ",";
            }
            if( statementnum != 0 )
            {
                if(!sqlquery.exec(sqlstatement))
                {
                    qDebug()<<"block--"<<i<<"\t\n"<<sqlquery.lastError().databaseText();
                    qDebug()<<trUtf8("发生语句错误")<<sqlstatement;
                    return;
                }

            }
            else
            {
                break;
            }
        }


    }




    //    timelog.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    //    QTextStream logout(&timelog);
    //    logout << "Insert "<<m_range<<" Lines to SQL need time : " << timerecord.elapsed() << "(ms)\n";
    //    timelog.close();


    database.close();
    qDebug()<<m_index<<"_thread run over!"<<endl;
}
예제 #5
0
void tst_QSemaphore::tryAcquireWithTimeout()
{
    QFETCH(int, timeout);

    // timers are not guaranteed to be accurate down to the last millisecond,
    // so we permit the elapsed times to be up to this far from the expected value.
    int fuzz = 50;

    QSemaphore semaphore;
    QElapsedTimer time;

#define FUZZYCOMPARE(a,e) \
    do { \
        int a1 = a; \
        int e1 = e; \
        QVERIFY2(qAbs(a1-e1) < fuzz, \
            qPrintable(QString("(%1=%2) is more than %3 milliseconds different from (%4=%5)") \
                        .arg(#a).arg(a1).arg(fuzz).arg(#e).arg(e1))); \
    } while (0)

    QCOMPARE(semaphore.available(), 0);

    semaphore.release();
    QCOMPARE(semaphore.available(), 1);
    time.start();
    QVERIFY(!semaphore.tryAcquire(2, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 1);

    semaphore.release();
    QCOMPARE(semaphore.available(), 2);
    time.start();
    QVERIFY(!semaphore.tryAcquire(3, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 2);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 12);
    time.start();
    QVERIFY(!semaphore.tryAcquire(100, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 12);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 22);
    time.start();
    QVERIFY(!semaphore.tryAcquire(100, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 22);

    time.start();
    QVERIFY(semaphore.tryAcquire(1, timeout));
    FUZZYCOMPARE(time.elapsed(), 0);
    QCOMPARE(semaphore.available(), 21);

    time.start();
    QVERIFY(semaphore.tryAcquire(1, timeout));
    FUZZYCOMPARE(time.elapsed(), 0);
    QCOMPARE(semaphore.available(), 20);

    time.start();
    QVERIFY(semaphore.tryAcquire(10, timeout));
    FUZZYCOMPARE(time.elapsed(), 0);
    QCOMPARE(semaphore.available(), 10);

    time.start();
    QVERIFY(semaphore.tryAcquire(10, timeout));
    FUZZYCOMPARE(time.elapsed(), 0);
    QCOMPARE(semaphore.available(), 0);

    // should not be able to acquire more
    time.start();
    QVERIFY(!semaphore.tryAcquire(1, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(1, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(10, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(10, timeout));
    FUZZYCOMPARE(time.elapsed(), timeout);
    QCOMPARE(semaphore.available(), 0);

#undef FUZZYCOMPARE
}
예제 #6
0
void tst_QSemaphore::tryAcquire()
{
    QSemaphore semaphore;

    QCOMPARE(semaphore.available(), 0);

    semaphore.release();
    QCOMPARE(semaphore.available(), 1);
    QVERIFY(!semaphore.tryAcquire(2));
    QCOMPARE(semaphore.available(), 1);

    semaphore.release();
    QCOMPARE(semaphore.available(), 2);
    QVERIFY(!semaphore.tryAcquire(3));
    QCOMPARE(semaphore.available(), 2);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 12);
    QVERIFY(!semaphore.tryAcquire(100));
    QCOMPARE(semaphore.available(), 12);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 22);
    QVERIFY(!semaphore.tryAcquire(100));
    QCOMPARE(semaphore.available(), 22);

    QVERIFY(semaphore.tryAcquire());
    QCOMPARE(semaphore.available(), 21);

    QVERIFY(semaphore.tryAcquire());
    QCOMPARE(semaphore.available(), 20);

    QVERIFY(semaphore.tryAcquire(10));
    QCOMPARE(semaphore.available(), 10);

    QVERIFY(semaphore.tryAcquire(10));
    QCOMPARE(semaphore.available(), 0);

    // should not be able to acquire more
    QVERIFY(!semaphore.tryAcquire());
    QCOMPARE(semaphore.available(), 0);

    QVERIFY(!semaphore.tryAcquire());
    QCOMPARE(semaphore.available(), 0);

    QVERIFY(!semaphore.tryAcquire(10));
    QCOMPARE(semaphore.available(), 0);

    QVERIFY(!semaphore.tryAcquire(10));
    QCOMPARE(semaphore.available(), 0);
}
예제 #7
0
void tst_QSemaphore::tryAcquireWithTimeout()
{
    QFETCH(int, timeout);

    QSemaphore semaphore;
    QTime time;


    QCOMPARE(semaphore.available(), 0);

    semaphore.release();
    QCOMPARE(semaphore.available(), 1);
    time.start();
    QVERIFY(!semaphore.tryAcquire(2, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 1);

    semaphore.release();
    QCOMPARE(semaphore.available(), 2);
    time.start();
    QVERIFY(!semaphore.tryAcquire(3, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 2);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 12);
    time.start();
    QVERIFY(!semaphore.tryAcquire(100, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 12);

    semaphore.release(10);
    QCOMPARE(semaphore.available(), 22);
    time.start();
    QVERIFY(!semaphore.tryAcquire(100, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 22);

    time.start();
    QVERIFY(semaphore.tryAcquire(1, timeout));
    QVERIFY(time.elapsed() <= timeout);
    QCOMPARE(semaphore.available(), 21);

    time.start();
    QVERIFY(semaphore.tryAcquire(1, timeout));
    QVERIFY(time.elapsed() <= timeout);
    QCOMPARE(semaphore.available(), 20);

    time.start();
    QVERIFY(semaphore.tryAcquire(10, timeout));
    QVERIFY(time.elapsed() <= timeout);
    QCOMPARE(semaphore.available(), 10);

    time.start();
    QVERIFY(semaphore.tryAcquire(10, timeout));
    QVERIFY(time.elapsed() <= timeout);
    QCOMPARE(semaphore.available(), 0);

    // should not be able to acquire more
    time.start();
    QVERIFY(!semaphore.tryAcquire(1, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(1, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(10, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 0);

    time.start();
    QVERIFY(!semaphore.tryAcquire(10, timeout));
    QVERIFY(time.elapsed() >= timeout);
    QCOMPARE(semaphore.available(), 0);
}
예제 #8
0
void SearchThread::run()
{
  running = true;


  /*  search in cache */
  char *searchString = strdup((char *) this->searchTerms.toUtf8 ().constData ());
  SimpleVector < char *>*tokens = tokenizeString (searchString);
  int numSearchTerms = tokens->size ();
  int pos;

  bool locked = semCache.tryAcquire(1,1000);
  for (pos = 0; pos < nbCache; pos++)
  {

	  if(address!="" && address !="ALL" && Cache[pos].host != address) continue;
      // check if this is a hash-only search
      char hashOnlySearch = false;
      const char *hashStart = "hash_";
      char *hashString = (char *)malloc(41);
      char *pointerToHashStart = strstr (searchString, hashStart);

      if (pointerToHashStart != NULL)
      {

    	  // search contains a hash-only search.
    	  // extract hash, and ignore any other search terms
    	  char *pointerToHash = &(pointerToHashStart[strlen (hashStart)]);

    	  // hash should be at most 40 characters long
    	  int numRead = sscanf (pointerToHash, "%40s", hashString);
    	  if (numRead == 1)
    	  {
    		  delete[]searchString;
    		  searchString = hashString;
    		  hashOnlySearch = true;
    	  }
    	  else
    	  {
    		  free(hashString);
    	  }
      }
      else
      {
    	  free(hashString);
      }

      char hitAll = true;

      if (!hashOnlySearch)
      {

    	  // check each term
    	  for (int j = 0; j < numSearchTerms && hitAll; j++)
    	  {
    		  char *term = *(tokens->getElement (j));
    		  char * tmpstr = stringLocateIgnoreCase (Cache[pos].name, term);
    		  if (tmpstr == NULL)
    		  {

    			  // missed this term
    			  hitAll = false;
    		  }
    		  else delete [] tmpstr;
    	  }
      }
      else
      {
    	  char *hashTerm = *(tokens->getElement (0));
    	  if (strcasecmp (hashString, hashTerm) != 0)
    	  {
    		  hitAll = false;
    	  }
      }
      if (hitAll)
      {
            QTextCodec *codecutf=QTextCodec::codecForName("utf8");
            QTextCodec *codeclat=QTextCodec::codecForName("iso8859-1");
            QString qstr;
	    // try to convert string from utf8
            qstr=codecutf->toUnicode(Cache[pos].name);
	    // test if conversion is reversible
            if(strcmp(Cache[pos].name,qstr.toUtf8().data()))
	      // if not reversible, it's not utf8, convert from iso8859 :
              qstr=codeclat->toUnicode(Cache[pos].name);
	  QApplication::postEvent (eventReceiver,
				   new SearchResultEvent (qstr,
							  Cache[pos].size,
							  QString::fromLatin1
							  (Cache[pos].hash),
							  QString::fromLatin1
							  (Cache[pos].host)));
      }
  }
  if(locked) semCache.release(1);
  for (int i = 0; i < numSearchTerms; i++)
  {
      delete[](*(tokens->getElement (i)));
  }
  delete tokens;
  free( searchString );

  MuteLayer::muteLayerSearch ((char *) this->searchTerms.
			      toLatin1 ().constData (),
			      SearchThread::muteSearchHandler, this, 3000,
			      (char *) this->address.
			      toLatin1 ().constData ());



}