File: bxthread.h

package info (click to toggle)
bochs 2.6.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,176 kB
  • sloc: cpp: 248,327; ansic: 16,767; sh: 8,265; makefile: 4,890; yacc: 1,317; asm: 385; perl: 359; lex: 308; csh: 3
file content (101 lines) | stat: -rw-r--r-- 3,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/////////////////////////////////////////////////////////////////////////
// $Id: bxthread.h 13298 2017-09-16 22:01:49Z vruppert $
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2017  The Bochs Project
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
/////////////////////////////////////////////////////////////////////////

#ifndef BX_THREAD_H
#define BX_THREAD_H

// Bochs multi-threading support

#if BX_WITH_SDL || BX_WITH_SDL2

#include <SDL_mutex.h>
#include <SDL_timer.h>
#include <SDL_thread.h>

#define BX_THREAD_VAR(name) SDL_Thread* (name) = NULL
#define BX_THREAD_FUNC(name,arg) static int name(void* arg)
#define BX_THREAD_EXIT return 0
#if BX_WITH_SDL2
#define BX_THREAD_CREATE(name,arg,var) do { var = SDL_CreateThread(name, #name, (void*)arg); } while (0)
#define BX_THREAD_KILL(var) SDL_DetachThread(var)
#else
#define BX_THREAD_CREATE(name,arg,var) do { var = SDL_CreateThread(name, (void*)arg); } while (0)
#define BX_THREAD_KILL(var) SDL_KillThread(var)
#endif
#define BX_LOCK(mutex) SDL_LockMutex(mutex)
#define BX_UNLOCK(mutex) SDL_UnlockMutex(mutex)
#define BX_MUTEX(mutex) SDL_mutex (*mutex)
#define BX_INIT_MUTEX(mutex) mutex = SDL_CreateMutex()
#define BX_FINI_MUTEX(mutex) SDL_DestroyMutex(mutex)
#define BX_MSLEEP(val) SDL_Delay(val)

#elif defined(WIN32)

#define BX_THREAD_VAR(name) HANDLE (name)
#define BX_THREAD_FUNC(name,arg) DWORD WINAPI name(LPVOID arg)
#define BX_THREAD_EXIT return 0
#define BX_THREAD_CREATE(name,arg,var) do { var = CreateThread(NULL, 0, name, arg, 0, NULL); } while (0)
#define BX_THREAD_KILL(var) TerminateThread(var, 0)
#define BX_LOCK(mutex) EnterCriticalSection(&(mutex))
#define BX_UNLOCK(mutex) LeaveCriticalSection(&(mutex))
#define BX_MUTEX(mutex) CRITICAL_SECTION (mutex)
#define BX_INIT_MUTEX(mutex) InitializeCriticalSection(&(mutex))
#define BX_FINI_MUTEX(mutex) DeleteCriticalSection(&(mutex))
#define BX_MSLEEP(val) Sleep(val)

#else

#include <pthread.h>

#define BX_THREAD_VAR(name) pthread_t (name)
#define BX_THREAD_FUNC(name,arg) void name(void* arg)
#define BX_THREAD_EXIT pthread_exit(NULL)
#define BX_THREAD_CREATE(name,arg,var) \
    pthread_create(&(var), NULL, (void *(*)(void *))&(name), arg)
#define BX_THREAD_KILL(var) pthread_cancel(var); pthread_join(var, NULL)
#define BX_LOCK(mutex) pthread_mutex_lock(&(mutex));
#define BX_UNLOCK(mutex) pthread_mutex_unlock(&(mutex));
#define BX_MUTEX(mutex) pthread_mutex_t (mutex)
#define BX_INIT_MUTEX(mutex) pthread_mutex_init(&(mutex),NULL)
#define BX_FINI_MUTEX(mutex) pthread_mutex_destroy(&(mutex))
#define BX_MSLEEP(val) usleep(val*1000)

#endif

typedef struct
{
#if BX_WITH_SDL || BX_WITH_SDL2
  SDL_cond *cond;
  SDL_mutex *lock;  
#elif defined(WIN32)
  HANDLE event;
#else
  pthread_cond_t cond;
  pthread_mutex_t lock;
#endif
} bx_thread_event_t;

void BOCHSAPI_MSVCONLY bx_create_event(bx_thread_event_t *thread_ev);
void BOCHSAPI_MSVCONLY bx_destroy_event(bx_thread_event_t *thread_ev);
void BOCHSAPI_MSVCONLY bx_set_event(bx_thread_event_t *thread_ev);
bx_bool BOCHSAPI_MSVCONLY bx_wait_for_event(bx_thread_event_t *thread_ev);

#endif