Example #1
0
File: app.c Project: Juul/gnupg
/* Lock the reader SLOT.  This function shall be used right before
   calling any of the actual application functions to serialize access
   to the reader.  We do this always even if the reader is not
   actually used.  This allows an actual connection to assume that it
   never shares a reader (while performing one command).  Returns 0 on
   success; only then the unlock_reader function must be called after
   returning from the handler. */
static gpg_error_t
lock_reader (int slot, ctrl_t ctrl)
{
  int res;

  if (slot < 0 || slot >= DIM (lock_table))
    return gpg_error (slot<0? GPG_ERR_INV_VALUE : GPG_ERR_RESOURCE_LIMIT);

  if (!lock_table[slot].initialized)
    {
      res = npth_mutex_init (&lock_table[slot].lock, NULL);
      if (res)
        {
          log_error ("error initializing mutex: %s\n", strerror (res));
          return gpg_error_from_errno (res);
        }
      lock_table[slot].initialized = 1;
      lock_table[slot].app = NULL;
      lock_table[slot].last_app = NULL;
    }

  res = npth_mutex_lock (&lock_table[slot].lock);
  if (res)
    {
      log_error ("failed to acquire APP lock for slot %d: %s\n",
                 slot, strerror (res));
      return gpg_error_from_errno (res);
    }

  apdu_set_progress_cb (slot, print_progress_line, ctrl);

  return 0;
}
Example #2
0
/* This function must be called once to initialize this module. It
   has to be done before a second thread is spawned.  */
void
initialize_module_cache (void)
{
  int err;

  err = npth_mutex_init (&encryption_lock, NULL);

  if (err)
    log_fatal ("error initializing cache module: %s\n", strerror (err));
}
Example #3
0
File: t-thread.c Project: 1587/npth
int
main (int argc, char *argv[])
{
  int rc;
  npth_attr_t tattr;
  int state;
  npth_t tid1, tid2;
  void *retval;

  if (argc >= 2 && !strcmp (argv[1], "--verbose"))
    opt_verbose = 1;

  rc = npth_init ();
  fail_if_err (rc);

  rc = npth_mutex_init (&counter_mutex, NULL);
  fail_if_err (rc);

  rc = npth_attr_init (&tattr);
  fail_if_err (rc);
  rc = npth_attr_getdetachstate (&tattr, &state);
  fail_if_err (rc);
  if ( state != NPTH_CREATE_JOINABLE )
    fail_msg ("new tattr is not joinable");

  info_msg ("creating thread-one");
  rc = npth_create (&tid1, &tattr, thread_one, NULL);
  fail_if_err (rc);
  npth_setname_np (tid1, "thread-one");

  info_msg ("creating thread-two");
  rc = npth_create (&tid2, &tattr, thread_two, NULL);
  fail_if_err (rc);
  npth_setname_np (tid2, "thread-two");

  rc = npth_attr_destroy (&tattr);
  fail_if_err (rc);

  info_msg ("waiting for thread-one to terminate");
  rc = npth_join (tid1, &retval);
  fail_if_err (rc);
  if (retval != (void*)4711)
    fail_msg ("thread-one returned an unexpected value");

  info_msg ("waiting for thread-two to terminate");
  rc = npth_join (tid2, &retval);
  fail_if_err (rc);
  if (retval != (void*)4722)
    fail_msg ("thread-two returned an unexpected value");

  if (counter != 100)
    fail_msg ("counter value not as expected");

  return 0;
}
Example #4
0
/* This function must be called once to initialize this module.  This
   has to be done before a second thread is spawned.  We can't do the
   static initialization because Pth emulation code might not be able
   to do a static init; in particular, it is not possible for W32. */
void
initialize_module_call_pinentry (void)
{
  static int initialized;

  if (!initialized)
    {
      if (npth_mutex_init (&entry_lock, NULL))
        initialized = 1;
    }
}
Example #5
0
/* This function must be called once to initialize this module.  This
   has to be done before a second thread is spawned.  We can't do the
   static initialization because NPth emulation code might not be able
   to do a static init; in particular, it is not possible for W32. */
void
initialize_module_call_scd (void)
{
  static int initialized;
  int err;

  if (!initialized)
    {
      err = npth_mutex_init (&start_scd_lock, NULL);
      if (err)
	log_fatal ("error initializing mutex: %s\n", strerror (err));
      initialized = 1;
    }
}
Example #6
0
File: t-mutex.c Project: 1587/npth
int
main (int argc, char *argv[])
{
  int rc;
  npth_mutex_t mutex;

  rc = npth_init ();
  fail_if_err (rc);

  rc = npth_mutex_init (&mutex, NULL);
  fail_if_err (rc);
  rc = npth_mutex_lock (&mutex);
  fail_if_err (rc);
  rc = npth_mutex_unlock (&mutex);
  fail_if_err (rc);

  return 0;
}