Пример #1
0
/*
 * Creates a new semaphore with initial value
 */
void createSem(semaphore * s, int value) {
    s->value = value;
    s->mutex = 1;

    s->cvar = st_cond_new();
    if(s->cvar == NULL) {
        perror("Error creating semaphore: ");
        exit(1);
    }
}
Пример #2
0
hoxResult
hoxDbClient::initialize( const char* szHost,
                         int         nPort )
{
    const char* FNAME = "hoxDbClient::initialize";
    hoxResult result = hoxRC_UNKNOWN;

    hoxLog(LOG_INFO, "%s: ENTER. [%s:%d]", FNAME, szHost, nPort);

    if ( s_bInitialized )
    {
        hoxLog(LOG_DEBUG, "%s: The module has already initialized.", FNAME);
        return hoxRC_OK;
    }

    s_mutex = st_mutex_new();  // ... to provide exclusive assess

    /* Open a "shared" client socket. */
    s_nfd = _open_client_socket( szHost, nPort );
    if ( s_nfd == NULL )
    {
        hoxLog(LOG_ERROR, "%s: Failed to open a client socket.", FNAME);
        return hoxRC_OK;
    }

    s_bInitialized = true;

    /* Send an "HELLO" request to make sure that the DB Agent is OK. */
    result = send_HELLO();
    if ( result != hoxRC_OK )
    {
        hoxLog(LOG_ERROR, "%s: Failed to send HELLO request.", FNAME);
        return hoxRC_ERR;
    }

    /* Start a "write" thread to help avoiding blocking
     * handlers of client requests.
     */

    s_writeCond = st_cond_new();

    s_writeThread = st_thread_create( _handle_db_write,
                                      ( void * ) NULL,
                                      1 /* joinable */,
                                      0 /* stack-size */ );
    if ( s_writeThread == NULL )
    {
        hoxLog(LOG_ERROR, "%s: Failed to create write DB thread.", FNAME);
        return hoxRC_ERR;
    }

    hoxLog(LOG_DEBUG, "%s: END. (OK)", FNAME);
    return hoxRC_OK;
}
Пример #3
0
/*
 * Asynchronous DNS host name resolution. This program creates one
 * ST thread for each host name (specified as command line arguments).
 * All threads do host name resolution concurrently.
 */
int main(int argc, char *argv[])
{
  if (st_init() < 0) {
    perror("st_init");
    exit(1);
  }
  condition = st_cond_new();
  mutex = st_mutex_new();

  int maxQueueP = 15;
  int maxQueueC = 10;
  st_thread_t p[6],c[6];

  int i = 0;
  for (; i < maxQueueP; ++i)
  {
    if (  (p[i] = st_thread_create(producer,i,1,0) ) == NULL) {
      perror("st_thread_create producer failed");
      exit(1);
    }
  }

  i = 0;
  for (; i < maxQueueC; ++i)
  {
    if ( (c[i] = st_thread_create(consumer,i,1,0) ) == NULL) {
      perror("st_thread_create consumer failed");
      exit(1);
    }
  }
  

  i = 0;
  for (; i < maxQueueP; ++i)
  {
    st_thread_join(p[i],0);
  }
  i = 0;
  for (; i < maxQueueC; ++i)
  {
    st_thread_join(c[i],0);
  }

  

  /* NOTREACHED */
  return 1;
}
Пример #4
0
_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg,
			       int joinable, int stk_size)
{
  _st_thread_t *thread;
  _st_stack_t *stack;
  void **ptds;
  char *sp;
#ifdef __ia64__
  char *bsp;
#endif

  /* Adjust stack size */
  if (stk_size == 0)
    stk_size = ST_DEFAULT_STACK_SIZE;
  stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE;
  stack = _st_stack_new(stk_size);
  if (!stack)
    return NULL;

  /* Allocate thread object and per-thread data off the stack */
#if defined (MD_STACK_GROWS_DOWN)
  sp = stack->stk_top;
#ifdef __ia64__
  /*
   * The stack segment is split in the middle. The upper half is used
   * as backing store for the register stack which grows upward.
   * The lower half is used for the traditional memory stack which
   * grows downward. Both stacks start in the middle and grow outward
   * from each other.
   */
  sp -= (stk_size >> 1);
  bsp = sp;
  /* Make register stack 64-byte aligned */
  if ((unsigned long)bsp & 0x3f)
    bsp = bsp + (0x40 - ((unsigned long)bsp & 0x3f));
  stack->bsp = bsp + _ST_STACK_PAD_SIZE;
#endif
  sp = sp - (ST_KEYS_MAX * sizeof(void *));
  ptds = (void **) sp;
  sp = sp - sizeof(_st_thread_t);
  thread = (_st_thread_t *) sp;

  /* Make stack 64-byte aligned */
  if ((unsigned long)sp & 0x3f)
    sp = sp - ((unsigned long)sp & 0x3f);
  stack->sp = sp - _ST_STACK_PAD_SIZE;
#elif defined (MD_STACK_GROWS_UP)
  sp = stack->stk_bottom;
  thread = (_st_thread_t *) sp;
  sp = sp + sizeof(_st_thread_t);
  ptds = (void **) sp;
  sp = sp + (ST_KEYS_MAX * sizeof(void *));

  /* Make stack 64-byte aligned */
  if ((unsigned long)sp & 0x3f)
    sp = sp + (0x40 - ((unsigned long)sp & 0x3f));
  stack->sp = sp + _ST_STACK_PAD_SIZE;
#else
#error Unknown OS
#endif

  memset(thread, 0, sizeof(_st_thread_t));
  memset(ptds, 0, ST_KEYS_MAX * sizeof(void *));

  /* Initialize thread */
  thread->private_data = ptds;
  thread->stack = stack;
  thread->start = start;
  thread->arg = arg;

#ifndef __ia64__
  _ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main);
#else
  _ST_INIT_CONTEXT(thread, stack->sp, stack->bsp, _st_thread_main);
#endif

  /* If thread is joinable, allocate a termination condition variable */
  if (joinable) {
    thread->term = st_cond_new();
    if (thread->term == NULL) {
      _st_stack_free(thread->stack);
      return NULL;
    }
  }

  /* Make thread runnable */
  thread->state = _ST_ST_RUNNABLE;
  _st_active_count++;
  _ST_ADD_RUNQ(thread);
#ifdef DEBUG
  _ST_ADD_THREADQ(thread);
#endif

  return thread;
}
Пример #5
0
extern "C" st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg,
	int joinable, int stk_size)
{
	st_thread_t *thread;
	st_stack_t *stack;
	void **ptds;
	char *sp;

	/* Adjust stack size */
	if (stk_size == 0)
		stk_size = ST_DEFAULT_STACK_SIZE;
	stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE;
	stack = _st_stack_new(stk_size);
	if (!stack)
		return NULL;

	/* Allocate thread object and per-thread data off the stack */
#if defined (MD_STACK_GROWS_DOWN)
	sp = stack->stk_top;
	sp = sp - (ST_KEYS_MAX * sizeof(void *));
	ptds = (void **) sp;
	sp = sp - sizeof(st_thread_t);
	thread = (st_thread_t *) sp;

	/* Make stack 64-byte aligned */
	if ((unsigned long)sp & 0x3f)
		sp = sp - ((unsigned long)sp & 0x3f);
	stack->sp = sp - _ST_STACK_PAD_SIZE;
#elif defined (MD_STACK_GROWS_UP)
	sp = stack->stk_bottom;
	thread = (st_thread_t *)sp;
	sp = sp + sizeof(st_thread_t);
	ptds = (void **)sp;
	sp = sp + (ST_KEYS_MAX * sizeof(void *));

	/* Make stack 64-byte aligned */
	if ((unsigned long)sp & 0x3f)
		sp = sp + (0x40 - ((unsigned long)sp & 0x3f));
	stack->sp = sp + _ST_STACK_PAD_SIZE;
#else
#error Unknown OS
#endif

	memset(thread, 0, sizeof(st_thread_t));
	memset(ptds, 0, ST_KEYS_MAX * sizeof(void *));

	/* Initialize thread */
	thread->private_data = ptds;
	thread->stack = stack;
	thread->start = start;
	thread->arg = arg;

	_ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main);

	/* If thread is joinable, allocate a termination condition variable */
	if (joinable) {
		thread->term = st_cond_new();
		if (thread->term == NULL) {
			_st_stack_free(thread->stack);
			return NULL;
		}
	}

	if (!thread->context_switch)
	{
		thread->context_switch = (st_context_switch_t*)calloc(1, sizeof(st_context_switch_t));
		thread->context_switch->thread = thread;
	}


	/* Make thread runnable */
	thread->state = _ST_ST_RUNNABLE;
	_st_active_count++;
	_ST_ADD_RUNQ(thread);

	return thread;
}
Пример #6
0
	server_base_t()
		:_ref(1)
	{
		_notify_cond = st_cond_new();
	}
Пример #7
0
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "timer.h"
#include "test_cases.h"

long workingNum = 0;

st_cond_t cond = st_cond_new();

static void* doneTask(void* arg) {
	int rc = 0;
	--workingNum;
	if ((rc = st_cond_signal(cond)) != 0) {
		std::cout << "st_cond_signal " << rc << " " << strerror(rc) << std::endl;
		return NULL;
	}
	return NULL;
}

// context switching
void task2(void* arg) {
	for (long i = 0; i < 1000000; ++i) {
		//st_sleep(0);