Beispiel #1
0
/**
 * @brief Destroy a stream.
 * @protected
 *
 * @warning This function must only be used to implement a subclass of
 * @ref SquashObject.  Each subclass should implement a *_destroy
 * function which should perform any operations needed to destroy
 * their own data and chain up to the *_destroy function of the base
 * class, eventually invoking ::squash_object_destroy.  Invoking this
 * function in any other context is likely to cause a memory leak or
 * crash.  If you are not creating a subclass, you should be calling
 * @ref squash_object_unref instead.
 *
 * @param stream The stream.
 *
 * @see squash_object_destroy
 */
void
squash_stream_destroy (void* stream) {
    SquashStream* s;

    assert (stream != NULL);

    s = (SquashStream*) stream;

    if (SQUASH_UNLIKELY(s->priv != NULL)) {
        SquashStreamPrivate* priv = (SquashStreamPrivate*) s->priv;

        if (!priv->finished) {
            squash_stream_send_to_thread (s, SQUASH_OPERATION_TERMINATE);
        }
        cnd_destroy (&(priv->request_cnd));
        cnd_destroy (&(priv->result_cnd));
        mtx_destroy (&(priv->io_mtx));

        squash_free (s->priv);
    }

    if (s->destroy_user_data != NULL && s->user_data != NULL) {
        s->destroy_user_data (s->user_data);
    }

    if (s->options != NULL) {
        s->options = squash_object_unref (s->options);
    }

    squash_object_destroy (stream);
}
Beispiel #2
0
bool RWMutex::destroy()
{
	if(initialized)
	{
		if(mtx_lock(&mtxExclusiveAccess) != thrd_success)
			return false;

		if(mtx_lock(&mtxSharedAccessCompleted) != thrd_success)
		{
			mtx_unlock(&mtxExclusiveAccess);
			return false;
		}
		if(nExclusiveAccessCount > 0
			|| nSharedAccessCount > nCompletedSharedAccessCount)
		{
			// Busy
			int j = 27;
		}

		mtx_unlock(&mtxSharedAccessCompleted);
		mtx_unlock(&mtxExclusiveAccess);

		cnd_destroy(&cndSharedAccessCompleted);
		mtx_destroy(&mtxSharedAccessCompleted);
		mtx_destroy(&mtxExclusiveAccess);

		initialized = false;
	}
	return true;
}
Beispiel #3
0
/*
====================
TFile_StartServer
====================
*/
void TFile_StartServer( void ) {
	server_message_t message;

	if ( !server_initialized ) {
		T_FatalError( "TFile_StartServer: Server is not initialized" );
	}

	if ( server_running ) {
		T_FatalError( "TFile_StartServer: Server is already running" );
	}

	cnd_init( &server_condition );
	mtx_init( &server_mutex, mtx_plain );
	mtx_lock( &server_mutex );

	message.ip_socket = server_socket;
	message.ip6_socket = server_socket6;
	if ( thrd_create( &server_thread, ServerThread, &message ) != thrd_success ) {
		T_FatalError( "TFile_StartServer: Unable to create thread" );
	}

	cnd_wait( &server_condition, &server_mutex );

	mtx_destroy( &server_mutex );
	cnd_destroy( &server_condition );

	server_running = t_true;
}
int main(void)
{
	SetConsoleTitle(TITLE);

	mtx_init(&gmutex, mtx_plain);
	cnd_init(&gcond);

	init_timer();
	init_path();
	init_strings();
	init_server();
	initpackets();
	//test_map(1);
	thrd_create(&t1, initsocket, (void*)0);
	thrd_create(&t2, commands, (void*)0);
	server_loop();
	destroy_server();
	mtx_destroy(&gmutex);
	cnd_destroy(&gcond);
#if _DEBUG
	VLDReportLeaks();
#endif

	exit(TRUE);// Exit program
}
Beispiel #5
0
void util_ringbuffer_destroy( struct util_ringbuffer *ring )
{
   cnd_destroy(&ring->change);
   mtx_destroy(&ring->mutex);
   FREE(ring->buf);
   FREE(ring);
}
void Condition_Deinit(COND_HANDLE handle)
{
    // Codes_SRS_CONDITION_18_007: [ Condition_Deinit will not fail if handle is NULL ]
    if (handle != NULL)
    {
        // Codes_SRS_CONDITION_18_009: [ Condition_Deinit will deallocate handle if it is not NULL 
        cnd_t* cond = (cnd_t*)handle;
        cnd_destroy(cond);
        free(cond);
    }
}
Beispiel #7
0
void rd_kafka_timers_destroy (rd_kafka_timers_t *rkts) {
        rd_kafka_timer_t *rtmr;

        rd_kafka_assert(NULL, TAILQ_EMPTY(&rkts->rkts_timers));

        while ((rtmr = TAILQ_FIRST(&rkts->rkts_timers)))
                rd_kafka_timer_stop(rkts, rtmr, 0);
        rd_kafka_assert(rkts->rkts_rk, TAILQ_EMPTY(&rkts->rkts_timers));

        cnd_destroy(&rkts->rkts_cond);
        mtx_destroy(&rkts->rkts_lock);
}
Beispiel #8
0
void db_worker_stop() {
    if (!db_enabled) {
        return;
    }
    mtx_lock(&mtx);
    ring_put_exit(&ring);
    cnd_signal(&cnd);
    mtx_unlock(&mtx);
    thrd_join(thrd, NULL);
    cnd_destroy(&cnd);
    mtx_destroy(&mtx);
    ring_free(&ring);
}
Beispiel #9
0
static void do_test_apis (rd_kafka_type_t cltype) {
        rd_kafka_t *rk;
        char errstr[512];
        rd_kafka_queue_t *mainq, *backgroundq;
        rd_kafka_conf_t *conf;

        mtx_init(&last_event_lock, mtx_plain);
        cnd_init(&last_event_cnd);

        do_test_unclean_destroy(cltype, 0/*tempq*/);
        do_test_unclean_destroy(cltype, 1/*mainq*/);

        test_conf_init(&conf, NULL, 0);
        /* Remove brokers, if any, since this is a local test and we
         * rely on the controller not being found. */
        test_conf_set(conf, "bootstrap.servers", "");
        test_conf_set(conf, "socket.timeout.ms", MY_SOCKET_TIMEOUT_MS_STR);
        /* For use with the background queue */
        rd_kafka_conf_set_background_event_cb(conf, background_event_cb);

        rk = rd_kafka_new(cltype, conf, errstr, sizeof(errstr));
        TEST_ASSERT(rk, "kafka_new(%d): %s", cltype, errstr);

        mainq = rd_kafka_queue_get_main(rk);
        backgroundq = rd_kafka_queue_get_background(rk);

        do_test_options(rk);

        do_test_CreateTopics("temp queue, no options", rk, NULL, 0, 0);
        do_test_CreateTopics("temp queue, no options, background_event_cb",
                             rk, backgroundq, 1, 0);
        do_test_CreateTopics("temp queue, options", rk, NULL, 0, 1);
        do_test_CreateTopics("main queue, options", rk, mainq, 0, 1);

        do_test_DeleteTopics("temp queue, no options", rk, NULL, 0);
        do_test_DeleteTopics("temp queue, options", rk, NULL, 1);
        do_test_DeleteTopics("main queue, options", rk, mainq, 1);

        do_test_mix(rk, mainq);

        do_test_configs(rk, mainq);

        rd_kafka_queue_destroy(backgroundq);
        rd_kafka_queue_destroy(mainq);

        rd_kafka_destroy(rk);

        mtx_destroy(&last_event_lock);
        cnd_destroy(&last_event_cnd);

}
Beispiel #10
0
/**
 * Destroy a queue. refcnt must be at zero.
 */
void rd_kafka_q_destroy_final (rd_kafka_q_t *rkq) {

        mtx_lock(&rkq->rkq_lock);
        rd_kafka_q_fwd_set0(rkq, NULL, 0/*no-lock*/);
        rd_kafka_q_disable0(rkq, 0/*no-lock*/);
        rd_kafka_q_purge0(rkq, 0/*no-lock*/);
	assert(!rkq->rkq_fwdq);
        mtx_unlock(&rkq->rkq_lock);
	mtx_destroy(&rkq->rkq_lock);
	cnd_destroy(&rkq->rkq_cond);

        if (rkq->rkq_flags & RD_KAFKA_Q_F_ALLOCATED)
                rd_free(rkq);
}
Beispiel #11
0
int main(int argc, char** argv)
{
  int res;

  mtx_init(&gMutex, mtx_plain);
  cnd_init(&gCond);

  res = tests_run(unit_tests, argc, argv);

  mtx_destroy(&gMutex);
  cnd_destroy(&gCond);

  return res;
}
Beispiel #12
0
/* This is the main program (i.e. the main thread) */
int main(void)
{
  /* Initialization... */
  mtx_init(&gMutex, mtx_plain);
  cnd_init(&gCond);

  /* Test 1: thread arguments & return values */
  printf("PART I: Thread arguments & return values\n");
  {
    thrd_t t1, t2, t3, t4;
    int id1, id2, id3, id4, ret1, ret2, ret3, ret4;

    /* Start a bunch of child threads */
    id1 = 1;
    thrd_create(&t1, ThreadIDs, (void*)&id1);
    thrd_join(t1, &ret1);
    printf(" Thread 1 returned %d.\n", ret1);
    id2 = 2;
    thrd_create(&t2, ThreadIDs, (void*)&id2);
    thrd_join(t2, &ret2);
    printf(" Thread 2 returned %d.\n", ret2);
    id3 = 3;
    thrd_create(&t3, ThreadIDs, (void*)&id3);
    thrd_join(t3, &ret3);
    printf(" Thread 3 returned %d.\n", ret3);
    id4 = 4;
    thrd_create(&t4, ThreadIDs, (void*)&id4);
    thrd_join(t4, &ret4);
    printf(" Thread 4 returned %d.\n", ret4);
  }

  /* Test 2: compile time thread local storage */
  printf("PART II: Compile time thread local storage\n");
#ifndef NO_CT_TLS
  {
    thrd_t t1;

    /* Clear the TLS variable (it should keep this value after all threads are
       finished). */
    gLocalVar = 1;
    printf(" Main gLocalVar is %d.\n", gLocalVar);

    /* Start a child thread that modifies gLocalVar */
    thrd_create(&t1, ThreadTLS, NULL);
    thrd_join(t1, NULL);

    /* Check if the TLS variable has changed */
    if(gLocalVar == 1)
      printf(" Main gLocalID was not changed by the child thread - OK!\n");
    else
      printf(" Main gLocalID was changed by the child thread - FAIL!\n");
  }
#else
  printf(" Compile time TLS is not supported on this platform...\n");
#endif

  /* Test 3: mutex locking */
  printf("PART III: Mutex locking (100 threads x 10000 iterations)\n");
  {
    thrd_t t[100];
    int i;

    /* Clear the global counter. */
    gCount = 0;

    /* Start a bunch of child threads */
    for (i = 0; i < 100; ++ i)
    {
      thrd_create(&t[i], ThreadLock, NULL);
    }

    /* Wait for the threads to finish */
    for (i = 0; i < 100; ++ i)
    {
      thrd_join(t[i], NULL);
    }

    /* Check the global count */
    printf(" gCount = %d\n", gCount);
  }

  /* Test 4: condition variable */
  printf("PART IV: Condition variable (40 + 1 threads)\n");
  {
    thrd_t t1, t[40];
    int i;

    /* Set the global counter to the number of threads to run. */
    gCount = 40;

    /* Start the waiting thread (it will wait for gCount to reach zero). */
    thrd_create(&t1, ThreadCondition2, NULL);

    /* Start a bunch of child threads (these will decrease gCount by 1 when they
       finish) */
    for (i = 0; i < 40; ++ i)
    {
      thrd_create(&t[i], ThreadCondition1, NULL);
    }

    /* Wait for the waiting thread to finish */
    thrd_join(t1, NULL);

    /* Wait for the other threads to finish */
    for (i = 0; i < 40; ++ i)
    {
      thrd_join(t[i], NULL);
    }
  }

  /* Test 5: yield */
  printf("PART V: Yield (40 + 1 threads)\n");
  {
    thrd_t t[40];
    int i;

    /* Start a bunch of child threads */
    for (i = 0; i < 40; ++ i)
    {
      thrd_create(&t[i], ThreadYield, NULL);
    }

    /* Yield... */
    thrd_yield();

    /* Wait for the threads to finish */
    for (i = 0; i < 40; ++ i)
    {
      thrd_join(t[i], NULL);
    }
  }

  /* Test 6: Sleep */
  printf("PART VI: Sleep (10 x 100 ms)\n");
  {
    int i;
    struct timespec ts;

    printf(" Sleeping");
    fflush(stdout);
    for (i = 0; i < 10; ++ i)
    {
      /* Calculate current time + 100ms */
      clock_gettime(TIME_UTC, &ts);
      ts.tv_nsec += 100000000;
      if (ts.tv_nsec >= 1000000000)
      {
        ts.tv_sec++;
        ts.tv_nsec -= 1000000000;
      }

      /* Sleep... */
      thrd_sleep(&ts, NULL);

      printf(".");
      fflush(stdout);
    }
    printf("\n");
  }

  /* Test 7: Time */
  printf("PART VII: Current time (UTC), three times\n");
  {
    struct timespec ts;
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
    clock_gettime(TIME_UTC, &ts);
    printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
  }

  /* FIXME: Implement some more tests for the TinyCThread API... */

  /* Finalization... */
  mtx_destroy(&gMutex);
  cnd_destroy(&gCond);

  return 0;
}
Beispiel #13
0
/**
 * @brief Test that Metadata requests are retried properly when
 *        timing out due to high broker rtt.
 */
static void do_test_low_socket_timeout (const char *topic) {
        rd_kafka_t *rk;
        rd_kafka_conf_t *conf;
        rd_kafka_topic_t *rkt;
        rd_kafka_resp_err_t err;
        const struct rd_kafka_metadata *md;
        int res;

        mtx_init(&ctrl.lock, mtx_plain);
        cnd_init(&ctrl.cnd);

        TEST_SAY("Test Metadata request retries on timeout\n");

        test_conf_init(&conf, NULL, 60);
        test_conf_set(conf, "socket.timeout.ms", "1000");
        test_conf_set(conf, "socket.max.fails", "12345");
        test_conf_set(conf, "retry.backoff.ms", "5000");
        /* Avoid api version requests (with their own timeout) to get in
         * the way of our test */
        test_conf_set(conf, "api.version.request", "false");
        test_socket_enable(conf);
        test_curr->connect_cb = connect_cb;
        test_curr->is_fatal_cb = is_fatal_cb;

        rk = test_create_handle(RD_KAFKA_PRODUCER, conf);
        rkt = test_create_producer_topic(rk, topic, NULL);

        TEST_SAY("Waiting for sockem connect..\n");
        mtx_lock(&ctrl.lock);
        while (!ctrl.skm)
                cnd_wait(&ctrl.cnd, &ctrl.lock);
        mtx_unlock(&ctrl.lock);

        TEST_SAY("Connected, fire off a undelayed metadata() to "
                 "make sure connection is up\n");

        err = rd_kafka_metadata(rk, 0, rkt, &md, tmout_multip(2000));
        TEST_ASSERT(!err, "metadata(undelayed) failed: %s",
                    rd_kafka_err2str(err));
        rd_kafka_metadata_destroy(md);

        if (thrd_create(&ctrl.thrd, ctrl_thrd_main, NULL) != thrd_success)
                TEST_FAIL("Failed to create sockem ctrl thread");

        set_delay(0, 3000); /* Takes effect immediately */

        /* After two retries, remove the delay, the third retry
         * should kick in and work. */
        set_delay(((1000 /*socket.timeout.ms*/ +
                    5000 /*retry.backoff.ms*/) * 2) - 2000, 0);

        TEST_SAY("Calling metadata() again which should succeed after "
                 "3 internal retries\n");
        /* Metadata should be returned after the third retry */
        err = rd_kafka_metadata(rk, 0, rkt, &md,
                                ((1000 /*socket.timeout.ms*/ +
                                  5000 /*retry.backoff.ms*/) * 2) + 5000);
        TEST_SAY("metadata() returned %s\n", rd_kafka_err2str(err));
        TEST_ASSERT(!err, "metadata(undelayed) failed: %s",
                    rd_kafka_err2str(err));
        rd_kafka_metadata_destroy(md);

        rd_kafka_topic_destroy(rkt);
        rd_kafka_destroy(rk);

        /* Join controller thread */
        mtx_lock(&ctrl.lock);
        ctrl.term = 1;
        mtx_unlock(&ctrl.lock);
        thrd_join(ctrl.thrd, &res);

        cnd_destroy(&ctrl.cnd);
        mtx_destroy(&ctrl.lock);
}
Beispiel #14
0
void stack10_Destructor(Stack10 *s){
	mtx_destroy(&s->mtx_);
	cnd_destroy(&s->cnd_push_);
	cnd_destroy(&s->cnd_pop_);
}
Beispiel #15
0
int main(int argc, char** argv)
{
    signal(SIGINT, IntHandler);

    Server serv;

    // Initialize the server
    InitConfig(&serv.conf, argv[1], argc, argv);
    
    if(!serv.conf.name) {
        fprintf(stderr, "Script doesn't specify a server name.\n");
        return 1;
    }

    if(!serv.conf.port) {
        fprintf(stderr, "Script doesn't specify a port.\n");
        return 1;
    }

	printf("Successfully configured server.\n"
		"Name: %s\n"
		"Port: %s\n"
		"Num Threads: %d\n"
		"Cycles Per Loop: %d\n"
		"Num Routes: %d\n", serv.conf.name, serv.conf.port, serv.conf.numThreads, serv.conf.cyclesPerLoop, sb_count(serv.conf.routes));

    InitList(&serv.clientQueue, sizeof(Sock));
    InitList(&serv.requestQueue, sizeof(ClientRequest));

    cnd_init(&serv.updateLoop);
    cnd_init(&serv.newConn);

    // The loop thread is responsible for initializing LoopData

    thrd_t loopThread, connThread;

    thrd_create(&loopThread, MainLoop, &serv);
    thrd_create(&connThread, ConnLoop, &serv);

    // Initialize winsock and listen for clients
    WSADATA wsaData;
    SOCKET listenSocket;

    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if(iResult != 0) {
        fprintf(stderr, "WSAStartup failed: %d\n", iResult);
        return 1;
    }

    struct addrinfo hints;
    
    ZeroMemory(&hints, sizeof(hints));

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    struct addrinfo* result;

    iResult = getaddrinfo(NULL, serv.conf.port, &hints, &result);
    if(iResult != 0) {
        fprintf(stderr, "getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    
    listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

    if(listenSocket == INVALID_SOCKET) {
        fprintf(stderr, "Error at socket(): %d\n", WSAGetLastError());
        freeaddrinfo(result); 
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);

    if(iResult == SOCKET_ERROR) {
        fprintf(stderr, "bind failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    if(listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
        fprintf(stderr, "listen failed with error: %d\n", WSAGetLastError());
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }
	
	while (KeepRunning) {
		SOCKET clientSocket = accept(listenSocket, NULL, NULL);

		if (clientSocket == INVALID_SOCKET) {
			fprintf(stderr, "accept failed: %d\n", WSAGetLastError());
            continue;
		}

		int iMode = 1;
        if (ioctlsocket(clientSocket, FIONBIO, &iMode) == SOCKET_ERROR) {
            fprintf(stderr, "ioctlsocket failed: %d\n", WSAGetLastError());
            continue;
        }

        Sock sock;

        InitSock(&sock, (void*)clientSocket);
        
        ListPushBack(&serv.clientQueue, &sock);

        cnd_signal(&serv.newConn);
	}

    WSACleanup();

    thrd_join(&connThread, NULL);
	thrd_join(&loopThread, NULL);

    cnd_destroy(&serv.newConn);
    cnd_destroy(&serv.updateLoop);

	DestroyList(&serv.requestQueue);
	DestroyList(&serv.clientQueue);

    DestroyConfig(&serv.conf);

	return 0;
}