Commit 65016304 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Add tor_cond_init/uninit

parent e8652481
Loading
Loading
Loading
Loading
+6 −11
Original line number Diff line number Diff line
@@ -148,28 +148,23 @@ tor_get_thread_id(void)

/* Conditions. */

/** Return a newly allocated condition, with nobody waiting on it. */
tor_cond_t *
tor_cond_new(void)
int
tor_cond_init(tor_cond_t *cond)
{
  tor_cond_t *cond = tor_malloc_zero(sizeof(tor_cond_t));
  memset(cond, 0, sizeof(tor_cond_t));
  if (pthread_cond_init(&cond->cond, NULL)) {
    tor_free(cond);
    return NULL;
    return -1;
  }
  return cond;
  return 0;
}
/** Release all resources held by <b>cond</b>. */
void
tor_cond_free(tor_cond_t *cond)
tor_cond_uninit(tor_cond_t *cond)
{
  if (!cond)
    return;
  if (pthread_cond_destroy(&cond->cond)) {
    log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
    return;
  }
  tor_free(cond);
}
/** Wait until one of the tor_cond_signal functions is called on <b>cond</b>.
 * All waiters on the condition must wait holding the same <b>mutex</b>.
+17 −0
Original line number Diff line number Diff line
@@ -24,6 +24,23 @@ tor_mutex_free(tor_mutex_t *m)
  tor_free(m);
}

tor_cond_t *
tor_cond_new(void)
{
  tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  if (tor_cond_init(cond)<0)
    tor_free(cond);
  return cond;
}
void
tor_cond_free(tor_cond_t *c)
{
  if (!c)
    return;
  tor_cond_uninit(c);
  tor_free(c);
}

/** Identity of the "main" thread */
static unsigned long main_thread_id = -1;

+2 −0
Original line number Diff line number Diff line
@@ -74,6 +74,8 @@ typedef struct tor_cond_t {

tor_cond_t *tor_cond_new(void);
void tor_cond_free(tor_cond_t *cond);
int tor_cond_init(tor_cond_t *cond);
void tor_cond_uninit(tor_cond_t *cond);
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
                  const struct timeval *tv);
void tor_cond_signal_one(tor_cond_t *cond);
+7 −12
Original line number Diff line number Diff line
@@ -70,30 +70,25 @@ tor_get_thread_id(void)
  return (unsigned long)GetCurrentThreadId();
}

tor_cond_t *
tor_cond_new(void)
int
tor_cond_init(tor_cond_t *cond)
{
  tor_cond_t *cond = tor_malloc(sizeof(tor_cond_t));
  memset(cond, 0, sizeof(tor_cond_t));
  if (InitializeCriticalSectionAndSpinCount(&cond->lock, SPIN_COUNT)==0) {
    tor_free(cond);
    return NULL;
    return -1;
  }
  if ((cond->event = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) {
    DeleteCriticalSection(&cond->lock);
    tor_free(cond);
    return NULL;
    return -1;
  }
  cond->n_waiting = cond->n_to_wake = cond->generation = 0;
  return cond;
  return 0;
}
void
tor_cond_free(tor_cond_t *cond)
tor_cond_uninit(tor_cond_t *cond)
{
  if (!cond)
    return;
  DeleteCriticalSection(&cond->lock);
  CloseHandle(cond->event);
  mm_free(cond);
}

static void