File: threading.cpp

package info (click to toggle)
x265 4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,408 kB
  • sloc: asm: 187,063; cpp: 118,996; ansic: 741; makefile: 146; sh: 91; python: 11
file content (162 lines) | stat: -rw-r--r-- 3,607 bytes parent folder | download | duplicates (4)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*****************************************************************************
 * Copyright (C) 2013-2020 MulticoreWare, Inc
 *
 * Authors: Steve Borho <steve@borho.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at license @ x265.com
 *****************************************************************************/

#include "common.h"
#include "threading.h"
#include "cpu.h"

namespace X265_NS {
// x265 private namespace

#if X265_ARCH_X86 && !defined(X86_64) && ENABLE_ASSEMBLY && defined(__GNUC__)
extern "C" intptr_t PFX(stack_align)(void (*func)(), ...);
#define STACK_ALIGN(func, ...) PFX(stack_align)((void (*)())func, __VA_ARGS__)
#else
#define STACK_ALIGN(func, ...) func(__VA_ARGS__)
#endif

#if NO_ATOMICS
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

int no_atomic_or(int* ptr, int mask)
{ 
    pthread_mutex_lock(&g_mutex);
    int ret = *ptr;
    *ptr |= mask;
    pthread_mutex_unlock(&g_mutex);
    return ret;
}

int no_atomic_and(int* ptr, int mask)
{
    pthread_mutex_lock(&g_mutex);
    int ret = *ptr;
    *ptr &= mask;
    pthread_mutex_unlock(&g_mutex);
    return ret;
}

int no_atomic_inc(int* ptr)
{
    pthread_mutex_lock(&g_mutex);
    *ptr += 1;
    int ret = *ptr;
    pthread_mutex_unlock(&g_mutex);
    return ret;
}

int no_atomic_dec(int* ptr)
{
    pthread_mutex_lock(&g_mutex);
    *ptr -= 1;
    int ret = *ptr;
    pthread_mutex_unlock(&g_mutex);
    return ret;
}

int no_atomic_add(int* ptr, int val)
{
    pthread_mutex_lock(&g_mutex);
    *ptr += val;
    int ret = *ptr;
    pthread_mutex_unlock(&g_mutex);
    return ret;
}
#endif

/* C shim for forced stack alignment */
static void stackAlignMain(Thread *instance)
{
    // defer processing to the virtual function implemented in the derived class
    instance->threadMain();
}

#if _WIN32

static DWORD WINAPI ThreadShim(Thread *instance)
{
    STACK_ALIGN(stackAlignMain, instance);

    return 0;
}

bool Thread::start()
{
    DWORD threadId;

    thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadShim, this, 0, &threadId);

    return threadId > 0;
}

void Thread::stop()
{
    if (thread)
        WaitForSingleObject(thread, INFINITE);
}

Thread::~Thread()
{
    if (thread)
        CloseHandle(thread);
}

#else /* POSIX / pthreads */

static void *ThreadShim(void *opaque)
{
    // defer processing to the virtual function implemented in the derived class
    Thread *instance = reinterpret_cast<Thread *>(opaque);

    STACK_ALIGN(stackAlignMain, instance);

    return NULL;
}

bool Thread::start()
{
    if (pthread_create(&thread, NULL, ThreadShim, this))
    {
        thread = 0;
        return false;
    }

    return true;
}

void Thread::stop()
{
    if (thread)
        pthread_join(thread, NULL);
}

Thread::~Thread() {}

#endif // if _WIN32

Thread::Thread()
{
    thread = 0;
}

}