Clean up and fix semaphores and threading

This commit is contained in:
def 2017-08-31 19:13:55 +02:00
parent cd386488e2
commit 705349f2e5
6 changed files with 33 additions and 60 deletions

View file

@ -526,7 +526,9 @@ void *thread_init(void (*threadfunc)(void *), void *u)
void thread_wait(void *thread)
{
#if defined(CONF_FAMILY_UNIX)
pthread_join((pthread_t)thread, NULL);
int result = pthread_join((pthread_t)thread, NULL);
if(result != 0)
dbg_msg("thread", "!! %d", result);
#elif defined(CONF_FAMILY_WINDOWS)
WaitForSingleObject((HANDLE)thread, INFINITE);
#else
@ -534,16 +536,6 @@ void thread_wait(void *thread)
#endif
}
void thread_destroy(void *thread)
{
#if defined(CONF_FAMILY_UNIX)
void *r = 0;
pthread_join((pthread_t)thread, &r);
#else
/*#error not implemented*/
#endif
}
void thread_yield()
{
#if defined(CONF_FAMILY_UNIX)
@ -647,12 +639,7 @@ void lock_unlock(LOCK lock)
#endif
}
#if defined(CONF_FAMILY_UNIX) && !defined(CONF_PLATFORM_MACOSX)
void sphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
void sphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
void sphore_signal(SEMAPHORE *sem) { sem_post(sem); }
void sphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
#elif defined(CONF_FAMILY_WINDOWS)
#if defined(CONF_FAMILY_WINDOWS)
void sphore_init(SEMAPHORE *sem) { *sem = CreateSemaphore(0, 0, 10000, 0); }
void sphore_wait(SEMAPHORE *sem) { WaitForSingleObject((HANDLE)*sem, INFINITE); }
void sphore_signal(SEMAPHORE *sem) { ReleaseSemaphore((HANDLE)*sem, 1, NULL); }
@ -666,9 +653,18 @@ void sphore_init(SEMAPHORE *sem)
}
void sphore_wait(SEMAPHORE *sem) { sem_wait(*sem); }
void sphore_signal(SEMAPHORE *sem) { sem_post(*sem); }
void sphore_destroy(SEMAPHORE *sem) { sem_close(*sem); }
#else
#error not implemented on this platform
void sphore_destroy(SEMAPHORE *sem)
{
char aBuf[64];
sem_close(*sem);
str_format(aBuf, sizeof(aBuf), "/%d-ddphore-%p", pid(), (void *)sem);
sem_unlink(aBuf);
}
#elif defined(CONF_FAMILY_UNIX)
void sphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); }
void sphore_wait(SEMAPHORE *sem) { sem_wait(sem); }
void sphore_signal(SEMAPHORE *sem) { sem_post(sem); }
void sphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); }
#endif
static int new_tick = -1;

View file

@ -387,18 +387,9 @@ void *thread_init(void (*threadfunc)(void *), void *user);
*/
void thread_wait(void *thread);
/*
Function: thread_destroy
Destroys a thread.
Parameters:
thread - Thread to destroy.
*/
void thread_destroy(void *thread);
/*
Function: thread_yeild
Yeild the current threads execution slice.
Yield the current threads execution slice.
*/
void thread_yield();
@ -425,15 +416,14 @@ void lock_unlock(LOCK lock);
/* Group: Semaphores */
#if defined(CONF_FAMILY_UNIX)
#include <semaphore.h>
#if defined(CONF_PLATFORM_MACOSX)
typedef sem_t* SEMAPHORE;
#else
typedef sem_t SEMAPHORE;
#endif
#elif defined(CONF_FAMILY_WINDOWS)
#if defined(CONF_FAMILY_WINDOWS)
typedef void* SEMAPHORE;
#elif defined(CONF_PLATFORM_MACOSX)
#include <semaphore.h>
typedef sem_t* SEMAPHORE;
#elif defined(CONF_FAMILY_UNIX)
#include <semaphore.h>
typedef sem_t SEMAPHORE;
#else
#error not implemented on this platform
#endif
@ -1274,9 +1264,7 @@ void swap_endian(void *data, unsigned elem_size, unsigned num);
typedef void (*DBG_LOGGER)(const char *line);
void dbg_logger(DBG_LOGGER logger);
#if !defined(CONF_PLATFORM_MACOSX)
void dbg_enable_threaded();
#endif
void dbg_logger_stdout();
void dbg_logger_debugger();
void dbg_logger_file(const char *filename);

View file

@ -61,21 +61,15 @@
#error missing atomic implementation for this compiler
#endif
#if defined(CONF_PLATFORM_MACOSX)
/*
use semaphore provided by SDL on macosx
*/
#else
class semaphore
{
class semaphore
{
SEMAPHORE sem;
public:
public:
semaphore() { sphore_init(&sem); }
~semaphore() { sphore_destroy(&sem); }
void wait() { sphore_wait(&sem); }
void signal() { sphore_signal(&sem); }
};
#endif
};
class lock
{

View file

@ -80,7 +80,6 @@ void CGraphicsBackend_Threaded::StopProcessor()
m_Shutdown = true;
m_Activity.signal();
thread_wait(m_pThread);
thread_destroy(m_pThread);
}
void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer)

View file

@ -60,7 +60,6 @@ void CFetcher::QueueAdd(CFetchTask *pTask, const char *pUrl, const char *pDest,
if(!m_pThHandle)
{
m_pThHandle = thread_init(&FetcherThread, this);
thread_detach(m_pThHandle);
}
if(!m_pFirst)

View file

@ -17,10 +17,7 @@ CJobPool::~CJobPool()
{
m_Shutdown = true;
for(int i = 0; i < m_NumThreads; i++)
{
thread_wait(m_apThreads[i]);
thread_destroy(m_apThreads[i]);
}
lock_destroy(m_Lock);
}