LibVNCServer/LibVNCClient
threading.h
Go to the documentation of this file.
1/*
2 * LibVNCServer/LibVNCClient common platform threading defines and includes.
3 *
4 * Copyright (C) 2020 Christian Beier <dontmind@freeshell.org>
5 *
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this software; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
22#ifndef _RFB_COMMON_THREADING_H
23#define _RFB_COMMON_THREADING_H
24
25#include <rfb/rfbconfig.h>
26
27#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
28#include <pthread.h>
29#if 0 /* debugging */
30#define LOCK(mutex) (rfbLog("%s:%d LOCK(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_lock(&(mutex)))
31#define UNLOCK(mutex) (rfbLog("%s:%d UNLOCK(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_unlock(&(mutex)))
32#define MUTEX(mutex) pthread_mutex_t (mutex)
33#define INIT_MUTEX(mutex) (rfbLog("%s:%d INIT_MUTEX(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_init(&(mutex),NULL))
34#define TINI_MUTEX(mutex) (rfbLog("%s:%d TINI_MUTEX(%s)\n",__FILE__,__LINE__,#mutex), pthread_mutex_destroy(&(mutex)))
35#define TSIGNAL(cond) (rfbLog("%s:%d TSIGNAL(%s)\n",__FILE__,__LINE__,#cond), pthread_cond_signal(&(cond)))
36#define WAIT(cond,mutex) (rfbLog("%s:%d WAIT(%s,%s)\n",__FILE__,__LINE__,#cond,#mutex), pthread_cond_wait(&(cond),&(mutex)))
37#define COND(cond) pthread_cond_t (cond)
38#define INIT_COND(cond) (rfbLog("%s:%d INIT_COND(%s)\n",__FILE__,__LINE__,#cond), pthread_cond_init(&(cond),NULL))
39#define TINI_COND(cond) (rfbLog("%s:%d TINI_COND(%s)\n",__FILE__,__LINE__,#cond), pthread_cond_destroy(&(cond)))
40#define IF_PTHREADS(x) x
41#else
42#if !NONETWORK
43#define LOCK(mutex) pthread_mutex_lock(&(mutex))
44#define UNLOCK(mutex) pthread_mutex_unlock(&(mutex))
45#endif
46#define MUTEX(mutex) pthread_mutex_t (mutex)
47#define MUTEX_SIZE (sizeof(pthread_mutex_t))
48#define INIT_MUTEX(mutex) pthread_mutex_init(&(mutex),NULL)
49#define TINI_MUTEX(mutex) pthread_mutex_destroy(&(mutex))
50#define TSIGNAL(cond) pthread_cond_signal(&(cond))
51#define WAIT(cond,mutex) pthread_cond_wait(&(cond),&(mutex))
52#define COND(cond) pthread_cond_t (cond)
53#define INIT_COND(cond) pthread_cond_init(&(cond),NULL)
54#define TINI_COND(cond) pthread_cond_destroy(&(cond))
55#define IF_PTHREADS(x) x
56#define THREAD_ROUTINE_RETURN_TYPE void*
57#define THREAD_ROUTINE_RETURN_VALUE NULL
58#define THREAD_SLEEP_MS(ms) usleep(ms*1000)
59#define THREAD_JOIN(thread) pthread_join(thread, NULL)
60#define THREAD_DETACH(thread) pthread_detach(thread)
61#define CURRENT_THREAD_ID pthread_self()
62#endif
63#elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
64#include <process.h>
65#define LOCK(mutex) EnterCriticalSection(&(mutex))
66#define UNLOCK(mutex) LeaveCriticalSection(&(mutex))
67#define MUTEX(mutex) CRITICAL_SECTION (mutex)
68#define MUTEX_SIZE (sizeof(CRITICAL_SECTION))
69#define INIT_MUTEX(mutex) InitializeCriticalSection(&(mutex))
70#define TINI_MUTEX(mutex) DeleteCriticalSection(&(mutex))
71#define TSIGNAL(cond) WakeAllConditionVariable(&(cond))
72#define WAIT(cond,mutex) SleepConditionVariableCS(&(cond),&(mutex),INFINITE);
73#define COND(cond) CONDITION_VARIABLE (cond)
74#define INIT_COND(cond) InitializeConditionVariable(&(cond));
75#define TINI_COND(cond)
76#define IF_PTHREADS(x)
77#define THREAD_ROUTINE_RETURN_TYPE void
78#define THREAD_ROUTINE_RETURN_VALUE
79#define THREAD_SLEEP_MS(ms) Sleep(ms)
80#define THREAD_JOIN(thread) WaitForSingleObject((HANDLE)thread, INFINITE)
81#define THREAD_DETACH(thread) CloseHandle((HANDLE)thread)
82#define CURRENT_THREAD_ID GetCurrentThreadId()
83#else
84#define LOCK(mutex)
85#define UNLOCK(mutex)
86#define MUTEX(mutex)
87#define INIT_MUTEX(mutex)
88#define TINI_MUTEX(mutex)
89#define TSIGNAL(cond)
90#define WAIT(cond,mutex) this_is_unsupported
91#define COND(cond)
92#define INIT_COND(cond)
93#define TINI_COND(cond)
94#define IF_PTHREADS(x)
95#endif
96
97#endif /* _RFB_COMMON_THREADING_H */