int MessageQueue::EnqueueCallback(int id, const char * payload, bool blocking) { //Enqueued messages are held in list (callbacks) //Callbacks are created here and sent to clients in next poll int result = 0; bool sameThread = poll_TID == ::getThreadId(); bool waitForResult = blocking; if (sameThread && waitForResult) { log_info("WARNING: Blocking callback in poll thread is unsupported and will be set to nonblocking %ld\nPayload: %s\n", poll_TID, payload); waitForResult = false; } pthread_mutex_lock(&mut); callback cb = callback(); InitCallback(&cb, id, waitForResult, payload); queue.push_back(cb); if (waitForResult) { debug("wait called"); pthread_cond_wait(&con, &mut); //wait for the signal with con as condition variable debug("wait done signal received"); //vector::push_back copies our object hence we need to find the actual object in vector. //mayby a vector of pointers should be used instead //then again this happens for blocking calls only and is not a bigg issue. for (std::vector<callback>::iterator it = queue.begin() ; it != queue.end();++it) { if(it->id == id){ it->status = CBS_COMPLETE; result = it->ret; break; } } } pthread_mutex_unlock(&mut); return result; }
Server::Server(boost::asio::io_service& asyncservice, const string& address, const string& port) : m_endPoint(boost::asio::ip::address::from_string(address), atoi(port.c_str())) , m_tcpserver(asyncservice, m_endPoint) , m_dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2)) , m_codec(std::bind(&ProtobufDispatcher::HandleMessage, &m_dispatcher, std::placeholders::_1, std::placeholders::_2)) { m_mutex.lock(); InitCallback(); InitSubscribe(); //建立redis连接 m_redis.InitRedis(); //输出服务器信息 std::cout << "server run at " << m_endPoint.address().to_string()<< " : " <<m_endPoint.port() << std::endl; //向redis注册服务器; m_strServerSocket = m_endPoint.address().to_string(); m_strServerSocket += ':'; char buf[10] = { 0 }; _itoa_s(m_endPoint.port(), buf, sizeof(buf), 10); m_strServerSocket += buf; m_redis.ZADD("serverRank", 100, m_strServerSocket); //订阅消息; string subscribeCommand("SUBSCRIBE"); auto itBegin = g_mapSubscribe.begin(); while (itBegin != g_mapSubscribe.end()) { subscribeCommand += ' '; subscribeCommand += itBegin->first; ++itBegin; } std::cout << subscribeCommand << std::endl; std::thread subscribeThread(&CRedis::Subscribe, subscribeCommand.c_str(), ReidsSubscribeSync); subscribeThread.detach(); m_mutex.unlock(); }
NS_IMETHODIMP nsAsyncRedirectVerifyHelper::Run() { /* If the channel got canceled after it fired AsyncOnChannelRedirect * and before we got here, mostly because docloader load has been canceled, * we must completely ignore this notification and prevent any further * notification. */ if (IsOldChannelCanceled()) { ExplicitCallback(NS_BINDING_ABORTED); return NS_OK; } // First, the global observer NS_ASSERTION(gIOService, "Must have an IO service at this point"); LOG(("nsAsyncRedirectVerifyHelper::Run() calling gIOService...")); nsresult rv = gIOService->AsyncOnChannelRedirect(mOldChan, mNewChan, mFlags, this); if (NS_FAILED(rv)) { ExplicitCallback(rv); return NS_OK; } // Now, the per-channel observers nsCOMPtr<nsIChannelEventSink> sink; NS_QueryNotificationCallbacks(mOldChan, sink); if (sink) { LOG(("nsAsyncRedirectVerifyHelper::Run() calling sink...")); rv = DelegateOnChannelRedirect(sink, mOldChan, mNewChan, mFlags); } // All invocations to AsyncOnChannelRedirect has been done - call // InitCallback() to flag this InitCallback(); return NS_OK; }
/* Normal and exotic debug output */ void InitCallback_Exotic( void* aCB, uInt16 aVersion ) { InitCallback ( aCB, aVersion, CB_PurePrintf, CB_PurePrintf ); } /* InitCallback_Exotic */
/* Normal debug output */ void InitCallback_Pure ( void* aCB, uInt16 aVersion ) { InitCallback ( aCB, aVersion, CB_PurePrintf, NULL ); } /* InitCallback_Pure */
gint main(gint argc, char** argv) { gtk_init(&argc, &argv); ball = g_slice_new(balls); paddle = g_slice_new(paddles); white = g_slice_new(GdkGC); black = g_slice_new(GdkGC); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request(window, DISPLAY_WIDTH, DISPLAY_HEIGHT); gtk_window_set_resizable(GTK_WINDOW(window), false); gtk_window_set_title(GTK_WINDOW(window), "GTK Pong"); draw = gtk_drawing_area_new(); gtk_container_add(GTK_CONTAINER(window), draw); InitCallback(); gtk_widget_show_all(window); pixmap = gdk_pixmap_new(draw->window, draw->allocation.width, draw->allocation.height, -1); black = draw->style->black_gc; white = draw->style->white_gc; GtkWidget *ballimagetmp = gtk_image_new_from_file("data/chromium.png"); ballimage = gtk_image_get_pixbuf(GTK_IMAGE(ballimagetmp)); BallInfo(); paddle->width = 100; paddle->height = 10; paddle->y = DISPLAY_HEIGHT - paddle->height; paddle->x = (DISPLAY_WIDTH / 2) - (paddle->width / 2); paddle->speed = paddle->width / 2; if (DISPLAY_WIDTH < paddle->width) { printf("Paddle too big"); return 2; } if (DISPLAY_WIDTH < ball->width) { printf("Ball too big"); return 2; } if (DISPLAY_HEIGHT < paddle->height + ball->height) { printf("Height of paddle and ball too big"); return 2; } ball->x = UniformRandom(0, DISPLAY_WIDTH - ball->width); //Initial position ball->y = UniformRandom(0, (DISPLAY_HEIGHT - ball->height - paddle->height) / 2); ball->dx = UniformRandom(3, 7); //Initial speed/direction of travel ball->dy = UniformRandom(3, 7); if (UniformRandom(1, 2) == 1)//Allow negative values initially ball->dx *= -1; if (UniformRandom(1, 2) == 1) ball->dy *= -1; Fill(white, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT); DrawPaddle(true); DrawBall(true); #ifdef DEBUG ball->dx = 0; ball->dy = 0; #else for (g_usleep(2 * G_USEC_PER_SEC); g_main_context_iteration(NULL, 0);) ; #endif while (true) { DrawBall(false); gint i; for (i = 0; i < ABS(ball->dx) || i < ABS(ball->dy); ++i) {//Move ball if (i < ABS(ball->dx)) { ball->x += ABS(ball->dx) / ball->dx; if (!checkPos()) { ball->dx *= -1; ball->x += 2 * ABS(ball->dx) / ball->dx; } } if (i < ABS(ball->dy)) { ball->y += ABS(ball->dy) / ball->dy; if (!checkPos()) { ball->dy *= -1; ball->y += 2 * ABS(ball->dy) / ball->dy; } } } DrawBall(true); for (g_usleep(20000); g_main_context_iteration(NULL, 0);) ; } return 0; }