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 CURRENT_THREAD_ID pthread_self()
61 #endif
62 #elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
63 #include <process.h>
64 #define LOCK(mutex) EnterCriticalSection(&(mutex))
65 #define UNLOCK(mutex) LeaveCriticalSection(&(mutex))
66 #define MUTEX(mutex) CRITICAL_SECTION (mutex)
67 #define MUTEX_SIZE (sizeof(CRITICAL_SECTION))
68 #define INIT_MUTEX(mutex) InitializeCriticalSection(&(mutex))
69 #define TINI_MUTEX(mutex) DeleteCriticalSection(&(mutex))
70 #define TSIGNAL(cond) WakeAllConditionVariable(&(cond))
71 #define WAIT(cond,mutex) SleepConditionVariableCS(&(cond),&(mutex),INFINITE);
72 #define COND(cond) CONDITION_VARIABLE (cond)
73 #define INIT_COND(cond) InitializeConditionVariable(&(cond));
74 #define TINI_COND(cond)
75 #define IF_PTHREADS(x)
76 #define THREAD_ROUTINE_RETURN_TYPE void
77 #define THREAD_ROUTINE_RETURN_VALUE
78 #define THREAD_SLEEP_MS(ms) Sleep(ms)
79 #define THREAD_JOIN(thread) WaitForSingleObject((HANDLE)thread, INFINITE)
80 #define CURRENT_THREAD_ID GetCurrentThreadId()
81 #else
82 #define LOCK(mutex)
83 #define UNLOCK(mutex)
84 #define MUTEX(mutex)
85 #define INIT_MUTEX(mutex)
86 #define TINI_MUTEX(mutex)
87 #define TSIGNAL(cond)
88 #define WAIT(cond,mutex) this_is_unsupported
89 #define COND(cond)
90 #define INIT_COND(cond)
91 #define TINI_COND(cond)
92 #define IF_PTHREADS(x)
93 #endif
94 
95 #endif /* _RFB_COMMON_THREADING_H */