예제 #1
0
Thread
CreateThread_SDL (ThreadFunction func, void *data, SDWORD stackSize
#ifdef NAMED_SYNCHRO
		  , const char *name
#endif
	)
{
	TrueThread thread;
	struct ThreadStartInfo *startInfo;
	
	thread = (struct _thread *) HMalloc (sizeof *thread);
#ifdef NAMED_SYNCHRO
	thread->name = name;
#endif
#ifdef PROFILE_THREADS
	thread->startTime = GetTimeCounter ();
#endif

	thread->localData = CreateThreadLocal ();

	startInfo = (struct ThreadStartInfo *) HMalloc (sizeof (*startInfo));
	startInfo->func = func;
	startInfo->data = data;
	startInfo->sem = SDL_CreateSemaphore (0);
	startInfo->thread = thread;
	
	thread->native = SDL_CreateThread (ThreadHelper, (void *) startInfo);
	if (!(thread->native))
	{
		DestroyThreadLocal (thread->localData);
		HFree (startInfo);
		HFree (thread);
		return NULL;
	}
	// The responsibility to free 'startInfo' and 'thread' is now by the new
	// thread.
	
	QueueThread (thread);

#ifdef DEBUG_THREADS
#if 0	
	log_add (log_Debug, "Thread '%s' created.", ThreadName (thread));
	fflush (stderr);
#endif
#endif

	// Signal to the new thread that the thread structure is ready
	// and it can begin to use it.
	SDL_SemPost (startInfo->sem);

	(void) stackSize;  /* Satisfying compiler (unused parameter) */
	return thread;
}
예제 #2
0
Thread
CreateThread_PT (ThreadFunction func, void *data, SDWORD stackSize
#ifdef NAMED_SYNCHRO
		  , const char *name
#endif
	)
{
	TrueThread thread;
	struct ThreadStartInfo *startInfo;
	pthread_attr_t attr;

	
	//log_add (log_Debug, "CreateThread_PT '%s'", name);
	
	thread = (struct _thread *) HMalloc (sizeof *thread);
#ifdef NAMED_SYNCHRO
	thread->name = name;
#endif

	thread->localData = CreateThreadLocal ();

	startInfo = (struct ThreadStartInfo *) HMalloc (sizeof (*startInfo));
	startInfo->func = func;
	startInfo->data = data;
	if (sem_init(&startInfo->sem, 0, 0) < 0)
	{
		log_add (log_Fatal, "createthread seminit fail");
		exit(EXIT_FAILURE);
	}
	startInfo->thread = thread;
		
	pthread_attr_init(&attr);
 	if (pthread_attr_setstacksize(&attr, 75000))
 	{
 		log_add (log_Debug, "pthread stacksize fail");
 	}
	if (pthread_create(&thread->native, &attr, ThreadHelper, (void *)startInfo))
	{
		log_add (log_Debug, "pthread create fail");
		DestroyThreadLocal (thread->localData);
		HFree (startInfo);
		HFree (thread);
		return NULL;
	}
	// The responsibility to free 'startInfo' and 'thread' is now by the new
	// thread.
	
	QueueThread (thread);

#ifdef DEBUG_THREADS
//#if 0	
	log_add (log_Debug, "Thread '%s' created.", thread->name);
	fflush (stderr);
//#endif
#endif

	// Signal to the new thread that the thread structure is ready
	// and it can begin to use it.
	if (sem_post (&startInfo->sem))
	{
		log_add(log_Fatal, "CreateThread sem_post fail");
		exit(EXIT_FAILURE);
	}

	(void) stackSize;  /* Satisfying compiler (unused parameter) */
	return thread;
}