Beispiel #1
0
my_bool my_thread_global_init(void)
{
  if (pthread_key_create(&THR_KEY_mysys,free))
  {
    fprintf(stderr,"Can't initialize threads: error %d\n",errno);
    exit(1);
  }
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  pthread_mutexattr_init(&my_fast_mutexattr);
  pthread_mutexattr_setkind_np(&my_fast_mutexattr,PTHREAD_MUTEX_ADAPTIVE_NP);
#endif
#ifdef PPTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  pthread_mutexattr_init(&my_errchk_mutexattr);
  pthread_mutexattr_setkind_np(&my_errchk_mutexattr,
			       PTHREAD_MUTEX_ERRORCHECK_NP);
#endif
  THR_KEY_mysys_initialized= TRUE;
#ifdef HAVE_OPENSSL
  pthread_mutex_init(&LOCK_ssl_config,MY_MUTEX_INIT_FAST);
#endif
  pthread_mutex_init(&THR_LOCK_malloc,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_net,MY_MUTEX_INIT_FAST);
#ifdef _WIN32
  /* win_pthread_init(); */
#endif
#ifndef HAVE_LOCALTIME_R
  pthread_mutex_init(&LOCK_localtime_r,MY_MUTEX_INIT_SLOW);
#endif
  return my_thread_init();
}
Beispiel #2
0
SDL_mutex *
SDL_CreateMutex(void)
{
    SDL_mutex *mutex;
    pthread_mutexattr_t attr;

    /* Allocate the structure */
    mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
    if (mutex) {
        pthread_mutexattr_init(&attr);
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
        pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
#else
        /* No extra attributes necessary */
#endif
        if (pthread_mutex_init(&mutex->id, &attr) != 0) {
            SDL_SetError("pthread_mutex_init() failed");
            SDL_free(mutex);
            mutex = NULL;
        }
    } else {
        SDL_OutOfMemory();
    }
    return (mutex);
}
Beispiel #3
0
int almtx_init(almtx_t *mtx, int type)
{
    int ret;

    if(!mtx) return althrd_error;
    if((type&~(almtx_recursive|almtx_timed)) != 0)
        return althrd_error;

    type &= ~almtx_timed;
    if(type == almtx_plain)
        ret = pthread_mutex_init(mtx, NULL);
    else
    {
        pthread_mutexattr_t attr;

        ret = pthread_mutexattr_init(&attr);
        if(ret) return althrd_error;

        if(type == almtx_recursive)
        {
            ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#ifdef HAVE_PTHREAD_MUTEXATTR_SETKIND_NP
            if(ret != 0)
                ret = pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE);
#endif
        }
        else
            ret = 1;
        if(ret == 0)
            ret = pthread_mutex_init(mtx, &attr);
        pthread_mutexattr_destroy(&attr);
    }
    return ret ? althrd_error : althrd_success;
}
Beispiel #4
0
void InitThreads (int wantthreads, size_t needstack)
{
	pthread_mutexattr_t	mattrib;

	if (needstack != 0)
		stacksiz = needstack;
	else	stacksiz = DEFAULT_STACKSIZ;

	numthreads = wantthreads;
	if (numthreads < 0)
		numthreads = 4;
	if (numthreads < 1)
		numthreads = 1;
	if (numthreads > MAX_THREADS)
		numthreads = MAX_THREADS;

	printf ("Setup for %d threads, 0x%x stack size\n",
			numthreads, (unsigned int)stacksiz);
	if (numthreads <= 1)
		return;

	my_mutex = (pthread_mutex_t *) SafeMalloc (sizeof(*my_mutex));
	if (pthread_mutexattr_create (&mattrib) == -1)
		COM_Error ("pthread_mutex_attr_create failed");
	if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
		COM_Error ("pthread_mutexattr_setkind_np failed");
	if (pthread_mutex_init (my_mutex, mattrib) == -1)
		COM_Error ("pthread_mutex_init failed");
}
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void ThreadSetupLock(void)
{
	pthread_mutexattr_t mattrib;

	Log_Print("pthread multi-threading\n");

	if (!my_mutex)
	{
		my_mutex = GetMemory(sizeof(*my_mutex));
		if (pthread_mutexattr_create (&mattrib) == -1)
			Error ("pthread_mutex_attr_create failed");
		if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
			Error ("pthread_mutexattr_setkind_np failed");
		if (pthread_mutex_init (my_mutex, mattrib) == -1)
			Error ("pthread_mutex_init failed");
	}

	if (pthread_attr_create (&attrib) == -1)
		Error ("pthread_attr_create failed");
	if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
		Error ("pthread_attr_setstacksize failed");

	threaded = true;
	currentnumthreads = 0;
	currentthreadid = 0;
} //end of the function ThreadInitLock
Beispiel #6
0
hashtable ht_new(unsigned int size, 
                 unsigned int (*hash)(const void *key),
                 boolean (*isequal)(const void *key1, 
                                    const void *key2),
                 /*@null@*/ void (*free_keyval)(void *key)) {
  hashtable ht = malloc(sizeof(struct hashtable_struct));
#ifdef _REENTRANT
  pthread_mutexattr_t attrib;
#endif
  assert(ht!=NULL);
  ht->table = (hashentry **)malloc(sizeof(hashentry *)*size);
  assert(ht->table!=NULL);
  memset(ht->table, 0, size * sizeof(hashentry *));   
  ht->table_size = size;
  ht->hash = hash;
  ht->isequal = isequal;
  ht->free_keyval = free_keyval;
#ifdef _REENTRANT
  (void)pthread_mutexattr_init(&attrib);
  (void)pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE_NP);
  (void)pthread_mutex_init(&ht->mutex, &attrib);
  (void)pthread_mutexattr_destroy(&attrib);
#endif
  return(ht);
}
void* XMLPlatformUtils::makeMutex(MemoryManager* manager)
{
    MutexHolderType* const  holder = new (manager) MutexHolderType;

    pthread_mutexattr_t     attr;

#if defined(XML_USE_DCE)
    pthread_mutexattr_create(&attr);
    pthread_mutexattr_setkind_np(&attr, MUTEX_RECURSIVE_NP);
    if (pthread_mutex_init(&holder->fInstance, attr))
    {
        delete holder;

        panic(PanicHandler::Panic_MutexErr);
    }
    pthread_mutexattr_delete(&attr);
#else
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    if (pthread_mutex_init(&holder->fInstance, &attr))
    {
        delete holder;

        panic(PanicHandler::Panic_MutexErr);
    }
    pthread_mutexattr_destroy(&attr);
#endif

    return holder;
}
Beispiel #8
0
void* XMLPlatformUtils::makeMutex()
{
    pthread_mutex_t* mutex = new pthread_mutex_t;
    pthread_mutexattr_t*  attr = new pthread_mutexattr_t;

#if defined(XML_USE_DCE)
    pthread_mutexattr_create(attr);
    pthread_mutexattr_setkind_np(attr, MUTEX_RECURSIVE_NP);
    if (pthread_mutex_init(mutex, *attr))
    {
        ThrowXMLwithMemMgr(XMLPlatformUtilsException,
                 XMLExcepts::Mutex_CouldNotCreate, fgMemoryManager);
    }
    pthread_mutexattr_delete(attr);
#else
    pthread_mutexattr_init(attr);
    pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
    if (pthread_mutex_init(mutex, attr))
    {
        ThrowXMLwithMemMgr(XMLPlatformUtilsException,
                 XMLExcepts::Mutex_CouldNotCreate, fgMemoryManager);
    }
    pthread_mutexattr_destroy(attr);
#endif
    delete attr;

    return (void*) mutex;
}
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void RunThreadsOn(int workcnt, qboolean showpacifier, void(*func)(int))
{
	int		i;
	pthread_t	work_threads[MAX_THREADS];
	pthread_addr_t	status;
	pthread_attr_t	attrib;
	pthread_mutexattr_t	mattrib;
	int		start, end;

	Log_Print("pthread multi-threading\n");

	start = I_FloatTime ();
	dispatch = 0;
	workcount = workcnt;
	oldf = -1;
	pacifier = showpacifier;
	threaded = true;

	if (numthreads < 1 || numthreads > MAX_THREADS) numthreads = 1;

	if (pacifier)
		setbuf (stdout, NULL);

	if (!my_mutex)
	{
		my_mutex = GetMemory(sizeof(*my_mutex));
		if (pthread_mutexattr_create (&mattrib) == -1)
			Error ("pthread_mutex_attr_create failed");
		if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
			Error ("pthread_mutexattr_setkind_np failed");
		if (pthread_mutex_init (my_mutex, mattrib) == -1)
			Error ("pthread_mutex_init failed");
	}

	if (pthread_attr_create (&attrib) == -1)
		Error ("pthread_attr_create failed");
	if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
		Error ("pthread_attr_setstacksize failed");
	
	for (i=0 ; i<numthreads ; i++)
	{
  		if (pthread_create(&work_threads[i], attrib
		, (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
			Error ("pthread_create failed");
	}
		
	for (i=0 ; i<numthreads ; i++)
	{
		if (pthread_join (work_threads[i], &status) == -1)
			Error ("pthread_join failed");
	}

	threaded = false;

	end = I_FloatTime ();
	if (pacifier)
		printf (" (%i)\n", end-start);
} //end of the function RunThreadsOn
Beispiel #10
0
mutex::mutex()
{
	pthread_mutexattr_t attrib;
	pthread_mutexattr_init(&attrib);
	#ifdef linux_pthreads
		pthread_mutexattr_setkind_np(&attrib,PTHREAD_MUTEX_ERRORCHECK_NP);
	#endif
	pthread_mutex_init(&mymid,&attrib);
//	owner = 0;
};
Beispiel #11
0
/*
=============
_RunThreadsOn
=============
*/
void _RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
{
	int		i;
	pthread_t	work_threads[MAX_THREADS];
	pthread_addr_t	status;
	pthread_attr_t	attrib;
	pthread_mutexattr_t	mattrib;
	int		start, end;

	start = I_FloatTime ();
	dispatch = 0;
	workcount = workcnt;
	oldf = -1;
	pacifier = showpacifier;

	if (pacifier)
		setbuf (stdout, NULL);

	if (!my_mutex)
	{
		my_mutex = safe_malloc (sizeof(*my_mutex));
		if (pthread_mutexattr_create (&mattrib) == -1)
			Error ("pthread_mutex_attr_create failed");
		if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
			Error ("pthread_mutexattr_setkind_np failed");
		if (pthread_mutex_init (my_mutex, mattrib) == -1)
			Error ("pthread_mutex_init failed");
	}

	if (pthread_attr_create (&attrib) == -1)
		Error ("pthread_attr_create failed");
	if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
		Error ("pthread_attr_setstacksize failed");
	
	for (i=0 ; i<numthreads ; i++)
	{
  		if (pthread_create(&work_threads[i], attrib
		, (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
			Error ("pthread_create failed");
	}
		
	for (i=0 ; i<numthreads ; i++)
	{
		if (pthread_join (work_threads[i], &status) == -1)
			Error ("pthread_join failed");
	}

	end = I_FloatTime ();
	if (pacifier)
		Sys_Printf (" (%i)\n", end-start);
}
Beispiel #12
0
/*****************************************************************************
 * vlc_mutex_init: initialize a mutex
 *****************************************************************************/
int vlc_mutex_init( vlc_mutex_t *p_mutex )
{
#if defined( LIBVLC_USE_PTHREAD )
    pthread_mutexattr_t attr;
    int                 i_result;

    pthread_mutexattr_init( &attr );

# ifndef NDEBUG
    /* Create error-checking mutex to detect problems more easily. */
#  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
#  else
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
#  endif
# endif
    i_result = pthread_mutex_init( p_mutex, &attr );
    pthread_mutexattr_destroy( &attr );
    return i_result;
#elif defined( UNDER_CE )
    InitializeCriticalSection( &p_mutex->csection );
    return 0;

#elif defined( WIN32 )
    *p_mutex = CreateMutex( NULL, FALSE, NULL );
    return (*p_mutex != NULL) ? 0 : ENOMEM;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    /* check the arguments and whether it's already been initialized */
    if( p_mutex == NULL )
    {
        return B_BAD_VALUE;
    }

    if( p_mutex->init == 9999 )
    {
        return EALREADY;
    }

    p_mutex->lock = create_sem( 1, "BeMutex" );
    if( p_mutex->lock < B_NO_ERROR )
    {
        return( -1 );
    }

    p_mutex->init = 9999;
    return B_OK;

#endif
}
Beispiel #13
0
/*****************************************************************************
 * vlc_mutex_init: initialize a recursive mutex (Do not use)
 *****************************************************************************/
void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
{
    pthread_mutexattr_t attr;

    pthread_mutexattr_init( &attr );
#if defined (__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ < 6)
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
#else
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
#endif
    if( pthread_mutex_init( p_mutex, &attr ) )
        abort();
    pthread_mutexattr_destroy( &attr );
}
Beispiel #14
0
void InitThreads (void)
{
#ifdef __alpha
	pthread_mutexattr_t	mattrib;

	my_mutex = malloc (sizeof(*my_mutex));
	if (pthread_mutexattr_create (&mattrib) == -1)
		Error ("pthread_mutex_attr_create failed");
	if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
		Error ("pthread_mutexattr_setkind_np failed");
	if (pthread_mutex_init (my_mutex, mattrib) == -1)
		Error ("pthread_mutex_init failed");
#endif
}
Beispiel #15
0
my_bool my_thread_global_init(void)
{
  if (pthread_key_create(&THR_KEY_mysys,free))
  {
    fprintf(stderr,"Can't initialize threads: error %d\n",errno);
    exit(1);
  }
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
  pthread_mutexattr_init(&my_fast_mutexattr);
  pthread_mutexattr_setkind_np(&my_fast_mutexattr,PTHREAD_MUTEX_ADAPTIVE_NP);
#endif
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
  pthread_mutexattr_init(&my_errchk_mutexattr);
  pthread_mutexattr_setkind_np(&my_errchk_mutexattr,
			       PTHREAD_MUTEX_ERRORCHECK_NP);
#endif

  pthread_mutex_init(&THR_LOCK_malloc,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_open,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_keycache,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_lock,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_isam,MY_MUTEX_INIT_SLOW);
  pthread_mutex_init(&THR_LOCK_myisam,MY_MUTEX_INIT_SLOW);
  pthread_mutex_init(&THR_LOCK_heap,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_net,MY_MUTEX_INIT_FAST);
  pthread_mutex_init(&THR_LOCK_charset,MY_MUTEX_INIT_FAST);
#if defined( __WIN__) || defined(OS2)
  win_pthread_init();
#endif
#ifndef HAVE_LOCALTIME_R
  pthread_mutex_init(&LOCK_localtime_r,MY_MUTEX_INIT_SLOW);
#endif
#ifndef HAVE_GETHOSTBYNAME_R
  pthread_mutex_init(&LOCK_gethostbyname_r,MY_MUTEX_INIT_SLOW);
#endif
  return my_thread_init();
}
Beispiel #16
0
mutex_t *MutexAlloc(void)
{
	pthread_mutex_t	*my_mutex;
	pthread_mutexattr_t	mattrib;

	if (numthreads == 1)
		return NULL;
	my_mutex = malloc (sizeof(*my_mutex));
	if (pthread_mutexattr_create (&mattrib) == -1)
		Error ("pthread_mutex_attr_create failed");
	if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
		Error ("pthread_mutexattr_setkind_np failed");
	if (pthread_mutex_init (my_mutex, mattrib) == -1)
		Error ("pthread_mutex_init failed");
	return (void *) my_mutex;
}
Beispiel #17
0
ACL_mutex * ACL_CreateMutex()
{
    ACL_mutex *mutex;
    pthread_mutexattr_t attr;
    
    mutex=(ACL_mutex *)malloc(sizeof(ACL_mutex));
    if(NULL!=mutex)
    {
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
        if(pthread_mutex_init(&mutex->id, &attr)!=0)
        {
            free(mutex);
            mutex=NULL;
        }
    }
    return mutex;
}
Beispiel #18
0
/*****************************************************************************
 * vlc_mutex_init: initialize a mutex
 *****************************************************************************/
void vlc_mutex_init( vlc_mutex_t *p_mutex )
{
    pthread_mutexattr_t attr;

    if( pthread_mutexattr_init( &attr ) )
        abort();
#ifndef NDEBUG
    /* Create error-checking mutex to detect problems more easily. */
# if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
# else
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
# endif
#endif
    if( pthread_mutex_init( p_mutex, &attr ) )
        abort();
    pthread_mutexattr_destroy( &attr );
}
Beispiel #19
0
SDL_mutex *SDL_CreateMutex (void)
{
#if defined(_WIN32)
  SDL_mutex *mutex;

  /* Allocate mutex memory */
  mutex = (SDL_mutex *)malloc(sizeof(*mutex));
  if ( mutex ) {
    /* Create the mutex, with initial value signaled */
    mutex->id = CreateMutex(NULL, FALSE, NULL);
    if ( ! mutex->id ) {
      CLog::Log(LOGERROR, "Couldn't create mutex");
      free(mutex);
      mutex = NULL;
    }
  } else {
    CLog::Log(LOGERROR, "OutOfMemory");
  }
  return(mutex);
#else
  SDL_mutex *mutex;
  pthread_mutexattr_t attr;

  /* Allocate the structure */
  mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
  if ( mutex ) {
    pthread_mutexattr_init(&attr);
#if defined(XBMC_PTHREAD_MUTEX_RECURSIVE)
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#elif defined(XBMC_PTHREAD_MUTEX_RECURSIVE_NP)
    pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif
		if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
      CLog::Log(LOGERROR, "pthread_mutex_init() failed");
      free(mutex);
      mutex = NULL;
    }
  } else {
    CLog::Log(LOGERROR, "OutOfMemory");
  }
  return(mutex);
#endif
}
Beispiel #20
0
void InitializeCriticalSection(CRITICAL_SECTION *cs)
{
    pthread_mutexattr_t attrib;
    int ret;

    ret = pthread_mutexattr_init(&attrib);
    assert(ret == 0);

    ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
#ifdef HAVE_PTHREAD_NP_H
    if(ret != 0)
        ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
#endif
    assert(ret == 0);
    ret = pthread_mutex_init(cs, &attrib);
    assert(ret == 0);

    pthread_mutexattr_destroy(&attrib);
}
Beispiel #21
0
int
createMutex(Mutex_t& handle)
{
	pthread_mutexattr_t attr;
	int res = pthread_mutexattr_create(&attr);
	assert(res == 0);
	if (res != 0)
	{
		return -1;
	}

#if defined(BLOCXX_HAVE_PTHREAD_MUTEXATTR_SETTYPE)
	res = pthread_mutexattr_setkind_np(&attr, MUTEX_RECURSIVE_NP);
	assert(res == 0);
	if (res != 0)
	{
		pthread_mutexattr_delete(&attr);
		return -1;
	}
#endif

	res = pthread_mutex_init(&handle.mutex, attr);

	pthread_mutexattr_delete(&attr);
	if (res != 0)
	{
		return -1;
	}

#if !defined(BLOCXX_HAVE_PTHREAD_MUTEXATTR_SETTYPE)
	res = pthread_cond_init(&handle.unlocked, PTHREAD_COND_ATTR_DEFAULT);
	if (res != 0)
	{
		pthread_mutex_destroy(&handle.mutex);
		return -1;
	}

	handle.valid_id = false;
	handle.count = 0;
#endif
	return 0;
}
int main(){
    static const int threadNb = 100;
    int tids[threadNb] ;

    pthread_t threads[threadNb];
    //mutex initialization with non-default attribute
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
    pthread_mutex_init(&pseudoListMutex, &attr);
    pthread_mutexattr_destroy(&attr);
    for(int i=0; i<threadNb; i++){
        tids[i] = i;
        pthread_create(&threads[i], NULL, &threadFun, &tids[i]);
    }
    for(int i=0; i<threadNb; i++)
        pthread_join(threads[i], NULL);

    return 0;
}
Beispiel #23
0
/*****************************************************************************
 * vlc_mutex_init: initialize a recursive mutex (Do not use)
 *****************************************************************************/
int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
{
#if defined( LIBVLC_USE_PTHREAD )
    pthread_mutexattr_t attr;
    int                 i_result;

    pthread_mutexattr_init( &attr );
#  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
#  else
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
#  endif
    i_result = pthread_mutex_init( p_mutex, &attr );
    pthread_mutexattr_destroy( &attr );
    return( i_result );
#elif defined( WIN32 )
    /* Create mutex returns a recursive mutex */
    *p_mutex = CreateMutex( 0, FALSE, 0 );
    return (*p_mutex != NULL) ? 0 : ENOMEM;
#else
# error Unimplemented!
#endif
}
Beispiel #24
0
void FNI_InflateObject(JNIEnv *env, jobject wrapped_obj) {
  struct oobj *obj = FNI_UNWRAP_MASKED(wrapped_obj);
  FLEX_MUTEX_LOCK(&global_inflate_mutex);
  /* be careful in case someone inflates this guy while our back is turned */
  if (!FNI_IS_INFLATED(wrapped_obj)) {
    /* all data in inflated_oobj is managed manually, so we can use malloc */
    struct inflated_oobj *infl = 
#if defined(WITH_TRANSACTIONS) && defined(BDW_CONSERVATIVE_GC)
#ifdef WITH_GC_STATS
      GC_malloc_uncollectable_stats
#else
      GC_malloc_uncollectable /* transactions stores version info here */
#endif
#else
	malloc
#endif
      (sizeof(*infl));
#if (!defined(WITH_TRANSACTIONS)) || (!defined(BDW_CONSERVATIVE_GC))
    INCREMENT_MEM_STATS(sizeof(*infl));
#endif
    /* initialize infl */
    memset(infl, 0, sizeof(*infl));
#ifndef WITH_HASHLOCK_SHRINK
    infl->hashcode = HASHCODE_MASK(obj->hashunion.hashcode);
#endif /* !WITH_HASHLOCK_SHRINK */
#if WITH_HEAVY_THREADS || WITH_PTH_THREADS || WITH_USER_THREADS
# ifdef ERROR_CHECKING_LOCKS
    /* error checking locks are slower, but catch more bugs (maybe) */
    { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr);
      pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
      pthread_mutex_init(&(infl->mutex), &attr);
      pthread_mutexattr_destroy(&attr);
    }
# else /* !ERROR_CHECKING_LOCKS */
    pthread_mutex_init(&(infl->mutex), NULL);
# endif /* ERROR_CHECKING_LOCKS || !ERROR_CHECKING_LOCKS */
    pthread_cond_init(&(infl->cond), NULL);
    pthread_rwlock_init(&(infl->jni_data_lock), NULL);
#endif
#ifndef WITH_HASHLOCK_SHRINK
#ifndef WITH_DYNAMIC_WB
    obj->hashunion.inflated = infl;
#else /* WITH_DYNAMIC_WB */
    obj->hashunion.inflated = (struct inflated_oobj *) 
	((ptroff_t) infl | (obj->hashunion.hashcode & 2));
#endif /* WITH_DYNAMIC_WB */
#else /* WITH_HASHLOCK_SHRINK */
    infl_table_set(INFL_LOCK, obj, infl, NULL);
#endif /* WITH_HASHLOCK_SHRINK */
    assert(FNI_IS_INFLATED(wrapped_obj));
#ifdef WITH_PRECISE_GC 
#if defined(WITH_REALTIME_JAVA) && defined(WITH_NOHEAP_SUPPORT)
    /* Can't inflate a heap reference in a NoHeapRealtimeThread */
    assert((!(((ptroff_t)FNI_UNWRAP(wrapped_obj))&1))||
	   (!((struct FNI_Thread_State*)env)->noheap));
    if (((ptroff_t)FNI_UNWRAP(wrapped_obj))&1)  /* register only if in heap */
#endif
      precise_register_inflated_obj(obj, 
#ifdef WITH_REALTIME_JAVA
				    (void (*)(jobject_unwrapped, ptroff_t))
#endif
				    deflate_object);
#elif defined(BDW_CONSERVATIVE_GC)
    /* register finalizer to deallocate inflated_oobj on gc */
    if (GC_base(obj)!=NULL) {// skip if this is not a heap-allocated object
        GC_register_finalizer_no_order(GC_base(obj), deflate_object,
			      (GC_PTR) ((void*)obj-(void*)GC_base(obj)),
			      &(infl->old_finalizer),
			      &(infl->old_client_data));
    } 
#endif
#ifdef WITH_REALTIME_JAVA
# ifdef BDW_CONSERVATIVE_GC /* XXX this test may be reversed? see v1.29 XXX */
    if (GC_base(obj)!=NULL) /* skip if this is not a heap-allocated object */
# endif /* BDW_CONSERVATIVE_GC */
    RTJ_register_finalizer(wrapped_obj, deflate_object); 
#endif
  }
  FLEX_MUTEX_UNLOCK(&global_inflate_mutex);
}
Beispiel #25
0
uintptr_t _createmutexex( SECURITY_ATTRIBUTES *sa, const char *name, unsigned flags, unsigned access )
{
    //NOTE: Windows critical sections are recursive
    //NOTE: posix recursive mutexes don't return error code

    int rc;
    pthread_mutex_t *pm;
    pthread_mutexattr_t mattr;
    int fd_mutex = -1;
    char shmname[MAX_PATH+1];
    size_t namelen = 0;
    bool opened = false;
    uintptr_t hndl;

    if ( name && *name ) {
        namelen = strlen (name);

        if ( 1+namelen > NAME_MAX ) {
            _setlasterror( ERROR_BAD_ARGUMENTS );
            return (uintptr_t)NULL;
        }

        shmname[0] = '/';
        strcpy (shmname+1, name);

        //try to open mutex first
        fd_mutex = shm_open (shmname, O_RDWR, S_IRUSR|S_IWUSR);
        if ( fd_mutex != -1 ) {
            struct flock fl;
            fl.l_type   = F_WRLCK;	//note: check only
            fl.l_whence = SEEK_SET;
            fl.l_start  = 0;
            fl.l_len    = 0;
            fl.l_pid    = getpid ();

            rc = fcntl (fd_mutex, F_GETLK, &fl);
            if (!(rc == 0 && fl.l_type == F_RDLCK)) {
                //semaphore is unused or other error
                ftruncate (fd_mutex, sizeof(pthread_mutex_t));
            } else {
                _setlasterror( ERROR_ALREADY_EXISTS );
                opened = true;
            }
        } else {
            //create new semaphore
            fd_mutex = shm_open (shmname, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
            if ( fd_mutex == -1 ) {
                _setlasterror( get_win_error (errno) );
                return (uintptr_t)NULL;
            }
            ftruncate (fd_mutex, sizeof(pthread_mutex_t));
        }

        void *addr_mutex = mmap (0, sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd_mutex, 0);
        if (addr_mutex == MAP_FAILED) {
            _setlasterror( get_win_error (errno) );
            close (fd_mutex);
            if (!opened) shm_unlink (shmname);
            return (uintptr_t)NULL;
        }

        //add file open check
        struct flock fl;
        fl.l_type   = F_RDLCK;
        fl.l_whence = SEEK_SET;
        fl.l_start  = 0;
        fl.l_len    = 0;
        fl.l_pid    = getpid ();
        fcntl (fd_mutex, F_SETLK, &fl);

        pm = (pthread_mutex_t *)addr_mutex;
    }
    else
    {
        pm = (pthread_mutex_t *) malloc (sizeof(pthread_mutex_t));
        if ( !pm ) {
            _setlasterror( get_win_error (errno) );	//ERROR_NOT_ENOUGH_MEMORY
            return (uintptr_t)NULL;
        }
    }

    if (!opened) {
        //initialize mutex
        pthread_mutexattr_init (&mattr);
        if ( name && *name )
            pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED);
        else
            pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_PRIVATE);
#if defined (__linux__) || defined (__bsd__)
        pthread_mutexattr_setkind_np (&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
#else
        pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE);
#endif
        rc = pthread_mutex_init (pm, &mattr);
        pthread_mutexattr_destroy (&mattr);

        if ( rc != 0 ) {
            _setlasterror( get_win_error (rc) );
            if ( name && *name ) {
                munmap ((void*)pm, sizeof(pthread_mutex_t));
                shm_unlink (shmname);
                close (fd_mutex);
            } else
                free (pm);
            return (uintptr_t)NULL;
        }
    }

    size_t datalen = sizeof(MUTEXOBJDATA) + (name ? namelen+2 : 0);
    MUTEXOBJDATA* data;
    data = (MUTEXOBJDATA*)malloc (datalen);
    if (!data) {
        _setlasterror( get_win_error (rc) );
        if ( name && *name ) {
            munmap ((void*)pm, sizeof(pthread_mutex_t));
            if (!opened) shm_unlink (shmname);
            close (fd_mutex);
        } else
            free (pm);
        return (uintptr_t)NULL;
    }
    data->fdmutex = fd_mutex;
    strcpy (data->name, (name && *name ? shmname : ""));

    //(flags && CREATE_MUTEX_INITIAL_OWNER)
    hndl = _createhandle (-1, 0, &mutexkrnlobj, &pm, sizeof(void*), data, datalen);
    if ( !hndl ) {
        _setlasterror( get_win_error (errno) );
        pthread_mutex_destroy (pm);		//destroy created mutex
        if ( name && *name ) {
            munmap ((void*)pm, sizeof(pthread_mutex_t));
            if (!opened) shm_unlink (shmname);
            close (fd_mutex);
        } else
            free (pm);

        free (data);
        return (uintptr_t)NULL;
    }

    free (data);
    return hndl;
}
Beispiel #26
0
static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
  int f;
  struct stream_priv_s* p = (struct stream_priv_s*)opts;
//printf("open_s called for stream_Sagetv\n");fflush(stdout);
  if(mode != STREAM_READ)
    return STREAM_UNSUPORTED;

  if(!p->host) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "We need an host name (ex: stv://server.net/cdda://5)\n");
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }
  if(!p->url || strlen(p->url) == 0) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "We need a remote url (ex: mpst://server.net/cdda://5)\n");
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }

  stream->priv = p;
  if (!OpenConnection(stream))
  {
	  stream->priv = 0;
	  m_struct_free(&stream_opts,opts);
	  return STREAM_ERROR;
  }
	stream->flags = STREAM_SEEK;

#ifndef WIN32
	pthread_mutexattr_t attr;
	p->mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	if (p->mutex)
	{
		pthread_mutexattr_init(&attr);
#ifdef CONFIG_DARWIN
		pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
#else
		pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif
		if (pthread_mutex_init(p->mutex, &attr) != 0)
		{
			free(p->mutex);
			p->mutex = NULL;
printf("MUTEX creation failed for pthread\n");
		}
	}
#else
	p->mutex = (LPCRITICAL_SECTION) malloc(sizeof(CRITICAL_SECTION));
	InitializeCriticalSection(p->mutex);
#endif
	stream->end_pos = size(stream, NULL);

	if (stream->activeFileFlag)
		p->actualSize = 0;
	else
		p->actualSize = stream->end_pos;

	stream->fill_buffer = fill_buffer;
	stream->control = control;
	stream->size = size;
	stream->seek = seek;
	stream->close = close_s;
    stream->type = STREAMTYPE_FILE;
	
	return STREAM_OK;

}
Beispiel #27
0
/*
==================
CalcPortalVis
==================
*/
void CalcPortalVis (void)
{
	int		i;

// fastvis just uses mightsee for a very loose bound
	if (fastvis)
	{
		for (i=0 ; i<numportals*2 ; i++)
		{
			portals[i].visbits = portals[i].mightsee;
			portals[i].status = stat_done;
		}
		return;
	}
	
	leafon = 0;
	
#ifdef __alpha
{
	pthread_t	work_threads[MAX_THREADS];
	pthread_addr_t	status;
	pthread_attr_t	attrib;
	pthread_mutexattr_t	mattrib;
	int		i;
	
	my_mutex = malloc (sizeof(*my_mutex));
	if (pthread_mutexattr_create (&mattrib) == -1)
		Error ("pthread_mutex_attr_create failed");
	if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
		Error ("pthread_mutexattr_setkind_np failed");
	if (pthread_mutex_init (my_mutex, mattrib) == -1)
		Error ("pthread_mutex_init failed");

	if (pthread_attr_create (&attrib) == -1)
		Error ("pthread_attr_create failed");
	if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
		Error ("pthread_attr_setstacksize failed");
	
	for (i=0 ; i<numthreads ; i++)
	{
  		if (pthread_create(&work_threads[i], attrib
		, LeafThread, (pthread_addr_t)i) == -1)
			Error ("pthread_create failed");
	}
		
	for (i=0 ; i<numthreads ; i++)
	{
		if (pthread_join (work_threads[i], &status) == -1)
			Error ("pthread_join failed");
	}

	if (pthread_mutex_destroy (my_mutex) == -1)
			Error ("pthread_mutex_destroy failed");
}
#else
	LeafThread (0);
#endif

	if (verbose)
	{
		printf ("portalcheck: %i  portaltest: %i  portalpass: %i\n",c_portalcheck, c_portaltest, c_portalpass);
		printf ("c_vistest: %i  c_mighttest: %i\n",c_vistest, c_mighttest);
	}

}
/*
 *  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;
}