Esempio n. 1
0
void *easy_atomic_mthread_op_start(void *args)
{
    easy_atomic_mt_op_args_t *p = (easy_atomic_mt_op_args_t *)args;
    int                     i;

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic_add(&p->int64_add, 1);
        easy_atomic_add(&p->int64_sum, 1);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic_add_return(&p->int64_add_return, 1);
        easy_atomic_add_return(&p->int64_sum, 1);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic_inc(&p->int64_inc);
        easy_atomic_inc(&p->int64_sum);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic_dec(&p->int64_dec);
        easy_atomic_dec(&p->int64_sum);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic32_add(&p->int32_add, 1);
        easy_atomic32_add(&p->int32_sum, 1);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic32_add_return(&p->int32_add_return, 1);
        easy_atomic32_add_return(&p->int32_sum, 1);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic32_inc(&p->int32_inc);
        easy_atomic32_inc(&p->int32_sum);
    }

    for(i = 0; i < p->loop_count; i++) {
        easy_atomic32_dec(&p->int32_dec);
        easy_atomic32_dec(&p->int32_sum);
    }

    int                     j = 0;
    int                     t = 0;
    easy_atomic_t           v;

    for(j = 0; j < p->loop_count; j++) {
        do {
            v = p->int64_set;
            t = easy_atomic_cmp_set(&p->int64_set, v, v + 1);
        } while(t == 0);

        easy_atomic_add(&p->int64_set_sum, t);
    }

    return (void *)NULL;
}
Esempio n. 2
0
    int BasePacketStreamer::encode_handler(easy_request_t *r, void *packet)
    {
      BasePacket* bp = (BasePacket*)packet;
      int32_t pcode = bp->getPCode();
      int64_t length = bp->length();
      TBSYS_LOG(DEBUG, "encode packet, pcode=%d, length=%ld, chid=%d", pcode, length, bp->getChannelId());
      if (EASY_TYPE_CLIENT == r->ms->c->type)
      {
        uint32_t chid = ((easy_session_t*)r->ms)->packet_id;
        bp->setChannelId(chid);
      }
      else
      {
        if (((easy_message_t*)r->ms)->status == EASY_MESG_DESTROY)
        {
          return EASY_ERROR;
        }

        if (r->retcode == EASY_ABORT)
        {
          r->ms->c->pool->ref--;
          easy_atomic_dec(&r->ms->pool->ref);
          r->retcode = EASY_OK;
        }
      }

      easy_list_t list;
      easy_list_init(&list);
      Stream output(r->ms->pool, &list);

      output.reserve(TFS_PACKET_HEADER_V1_SIZE + bp->length());
      output.set_int32(TFS_PACKET_FLAG_V1);
      char* len_pos = output.get_free();  // length() may not equal real data length
      output.set_int32(bp->length());
      output.set_int16(bp->getPCode());
      output.set_int16(TFS_PACKET_VERSION_V2);
      output.set_int64(bp->getChannelId());
      output.set_int32(0); // crc, place holder

      if (TFS_SUCCESS != bp->serialize(output))
      {
        return EASY_ERROR;
      }

      // use real length to replace header length
      if (bp->length() != output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE)
      {
        // help to detect serialize problem
        TBSYS_LOG(DEBUG, "length()=%ld not equals to serialize()=%ld",
            bp->length(), output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE);
        int64_t pos = 0;
        Serialization::set_int32(len_pos, INT_SIZE, pos, output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE);
      }

      easy_request_addbuf_list(r, &list);

      return EASY_OK;
    }
 int ObUpdateCallback::process(easy_request_t* r)
 {
   int ret = EASY_OK;
   if (NULL == r)
   {
     TBSYS_LOG(ERROR, "request is empty, r = %p", r);
     ret = EASY_BREAK;
   }
   else if (NULL == r->ipacket)
   {
     TBSYS_LOG(ERROR, "request is empty, r->ipacket = %p", r->ipacket);
     ret = EASY_BREAK;
   }
   else
   {
     ObUpdateServer *server = reinterpret_cast<ObUpdateServer*>(r->ms->c->handler->user_data);
     ObPacket *req = reinterpret_cast<ObPacket*>(r->ipacket);
     req->set_request(r);
     r->ms->c->pool->ref ++;
     easy_atomic_inc(&r->ms->pool->ref);
     easy_pool_set_lock(r->ms->pool);
     ret = server->handlePacket(req);
     if (OB_SUCCESS == ret)
     {
       // enqueue success
       ret = EASY_AGAIN;
     }
     else if (OB_ENQUEUE_FAILED == ret)
     {
       TBSYS_LOG(WARN, "can not push packet(src is %s, pcode is %u) to packet queue", 
                 inet_ntoa_r(r->ms->c->addr), req->get_packet_code());
       r->ms->c->pool->ref --;
       easy_atomic_dec(&r->ms->pool->ref);
       ret = EASY_OK;
     }
     else /* OB_ERROR */
     {
       ret = EASY_AGAIN;
     }
   }
   return ret;
 }