thread_t *
thread_allocate ()
{
  thread_t *thr;
  thr = (thread_t *) dk_alloc (sizeof (thread_t));
  memset (thr, 0, sizeof (thread_t));
  thr->thr_sem = semaphore_allocate (0);
  thr->thr_schedule_sem = semaphore_allocate (0);

  return thr;
}
static thread_t *
thread_alloc ()
{
  thread_t *thr;

  thr = (thread_t *) dk_alloc (sizeof (thread_t));
  memset (thr, 0, sizeof (thread_t));
  thr->thr_status = RUNNABLE;
  thr->thr_handle = dk_alloc (sizeof (pthread_t));
  thr->thr_cv = _alloc_cv ();
  thr->thr_sem = semaphore_allocate (0);
  thr->thr_schedule_sem = semaphore_allocate (0);

  return thr;
}
/*
 *  The main thread must call this function to convert itself into a thread.
 */
thread_t *
thread_initial (unsigned long stack_size)
{
  if (_main_thread)
    return _main_thread;
  else
    {
      NEW_VARZ (thread_t, thr);
      _main_thread = thr;
      thr->thr_status = RUNNING;
      thr->thr_sem = semaphore_allocate (0);
      thr->thr_schedule_sem = semaphore_allocate (0);
      _thread_init_attributes (thr);
      thread_set_priority (thr, NORMAL_PRIORITY);
      return thr;
    }
}
Example #4
0
int
mts_trx_enlist (lock_trx_t * lt, caddr_t tr_cookie, unsigned long len)
{
  if (!local_rm)
    {
      return 1;
    }
  DOUBLE_LOCK (trx_import, enlist);
  if (!local_rm->rm)
    {
      RELEASE_OBJECT (local_rm);
      return 0;
    }
  try
  {
    HRESULT hr =
	DtcGetTransactionManager (0, 0, __uuidof (ITransactionImport),
	0, 0, 0, (void **) &local_rm->trx_import);
    MTS_THROW_ASSERT (hr, "Get Transaction Import");
  }
  catch (const mts_error & err)
  {
    err.dump ();
    RELEASE_OBJECT (local_rm);
    return err.get_errcode ();
  }
  RELEASE_OBJECT (local_rm);
enlist:
  try
  {
    auto_interface < ITransaction > itrx;
    tp_data_t *tpd;
    HRESULT hr = local_rm->trx_import->Import (len,
	(BYTE *) tr_cookie,
	(IID *) & __uuidof (ITransaction),
	(void **) &itrx.get ());
    MTS_THROW_ASSERT (hr, "Import transaction");

    hr = mts_trx_enlist_loc (lt->lt_client, itrx.get ());
    MTS_THROW_ASSERT (hr, "Enlist local transaction");
    tpd = (tp_data_t *) dk_alloc (sizeof (tp_data_t));
    memset (tpd, 0, sizeof (tp_data_t));
    tpd->cli_tp_enlisted = CONNECTION_PREPARED;
    tpd->cli_tp_trx = itrx.release ();
    tpd->cli_tp_sem2 = semaphore_allocate (0);
    lt->lt_client->cli_tp_data = tpd;
    lt->lt_2pc._2pc_type = tpd->cli_trx_type = TP_MTS_TYPE;
#ifdef MSDTC_DEBUG
    lt->lt_in_mts = 1;
#endif
  }
  catch (const mts_error & err)
  {
    err.dump ();
    return err.get_errcode ();
  }
  return 0;
};
dk_mutex_t *
mutex_allocate (void)
{
  NEW_VARZ (dk_mutex_t, mtx);
  mtx->mtx_handle = semaphore_allocate (1);
#ifdef MTX_DEBUG
  mtx->mtx_owner = NULL;
  dk_set_push (&all_mtxs, (void*)mtx);
#endif
  return mtx;
}
int main(int argc, const char* argv[]) {
	int segment_id;
	int semaphore_id;
	char* shared_memory;

	// Pick a key for the semaphore set
	const int semaphore_key = 6969;

	if (argc != 2) {
		printf("Usage: client segment-key\n");
		exit(1);
	}

	segment_id = shmget(atoi(argv[1]), getpagesize(), 0666);

	if (segment_id < 0) {
		perror("Could not get segment");
		exit(1);
	}

	semaphore_id = semaphore_allocate(key);

	if (semaphore_id < 0) {
		perror("Error allocating semaphore!");
		exit(1);
	}

	if (semaphore_initialize(semaphore_id) < 0) {
		perror("Error intiializing semaphore!");
		exit(1);
	}

	shared_memory = shmat(segment_id, NULL, 0);

	if (shared_memory < (char*)0) {
		perror("Could not attach segment");
		exit(1);
	}


	semaphore_wait(semaphore_id);
	printf("%s\n", shared_memory);
	semaphore_post(semaphore_id);

	*shared_memory = '*';

	shmdt(shared_memory);

	semaphore_deallocate(key);

	return 0;
}
/*
 *  The main thread must call this function to convert itself into a fiber.
 */
thread_t *
thread_initial (unsigned long stack_size)
{
  static unsigned int marker = THREAD_STACK_MARKER;

  if (_current_fiber)
    return _current_fiber;
  else
    {
      NEW_VARZ (thread_t, thr);

      assert (_current_fiber == NULL);
      _main_thread = _current_fiber = thr;

      _sched_init ();

      if (stack_size == 0)
	stack_size = MAIN_STACK_SIZE;

#if (SIZEOF_VOID_P == 8)
      stack_size *= 2;
#endif
#if defined (__x86_64 ) && defined (SOLARIS)
  /*GK: the LDAP on that platform requires that */
  stack_size *= 2;
#endif

      thr->thr_stack_marker = &marker;
      thr->thr_sem = semaphore_allocate (0);
      thr->thr_schedule_sem = semaphore_allocate (0);
      thread_set_priority (thr, NORMAL_PRIORITY);
      _thread_init_attributes (thr);
      _fiber_for_thread (thr, stack_size);

      _fiber_status (thr, RUNNING);

      return thr;
    }
}
/*
 *  The main thread must call this function to convert itself into a thread.
 */
thread_t *
thread_initial (unsigned long stack_size)
{
  int rc;
  thread_t *thr = NULL;

  if (_main_thread)
    return _main_thread;

  /*
   *  Initialize pthread key
   */
#ifndef OLD_PTHREADS
  rc = pthread_key_create (&_key_current, NULL);
#else
  rc = pthread_keycreate (&_key_current, NULL);
#endif
  CKRET (rc);

  /*
   *  Start off with a value of NULL
   */
  rc = pthread_setspecific (_key_current, NULL);
  CKRET (rc);

  /*
   *  Initialize default thread/mutex attributes
   */
#ifndef OLD_PTHREADS
  /* attribute for thread creation */
  rc = pthread_attr_init (&_thread_attr);
  CKRET (rc);

  /* attribute for mutex creation */
  rc = pthread_mutexattr_init (&_mutex_attr);
  CKRET (rc);
#else
  rc = pthread_attr_create (&_thread_attr);
  CKRET (rc);

  rc = pthread_mutexattr_create (&_mutex_attr);
  CKRET (rc);
#endif

#if defined (PTHREAD_PROCESS_PRIVATE) && !defined(oldlinux) && !defined(__FreeBSD__)
  rc = pthread_mutexattr_setpshared (&_mutex_attr, PTHREAD_PROCESS_PRIVATE);
  CKRET (rc);
#endif

#if defined (MUTEX_FAST_NP) && !defined (_AIX)
  rc = pthread_mutexattr_setkind_np (&_mutex_attr, MUTEX_FAST_NP);
  CKRET (rc);
#endif

#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  rc = pthread_mutexattr_settype (&_mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP);
  CKRET (rc);
#endif

  /*
   *  Allocate a thread structure
   */
  thr = (thread_t *) dk_alloc (sizeof (thread_t));
  memset (thr, 0, sizeof (thread_t));

  assert (_main_thread == NULL);
  _main_thread = thr;

  _sched_init ();

  if (stack_size == 0)
    stack_size = MAIN_STACK_SIZE;

#if (SIZEOF_VOID_P == 8)
  stack_size *= 2;
#endif
#if defined (__x86_64 ) && defined (SOLARIS)
  /*GK: the LDAP on that platform requires that */
  stack_size *= 2;
#endif


  stack_size = ((stack_size / 8192) + 1) * 8192;

  thr->thr_stack_size = stack_size;
  thr->thr_status = RUNNING;
  thr->thr_cv = _alloc_cv ();
  thr->thr_sem = semaphore_allocate (0);
  thr->thr_schedule_sem = semaphore_allocate (0);
  if (thr->thr_cv == NULL)
    goto failed;
  _thread_init_attributes (thr);
  thread_set_priority (thr, NORMAL_PRIORITY);

  rc = pthread_setspecific (_key_current, thr);
  CKRET (rc);

  return thr;

failed:
  if (thr)
    {
      _thread_free_attributes (thr);
      dk_free (thr, sizeof (thread_t));
    }
  return NULL;
}