File: threading.h

package info (click to toggle)
libvpx 1.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,932 kB
  • sloc: ansic: 252,377; cpp: 115,241; asm: 22,233; sh: 5,291; python: 4,391; perl: 2,010; makefile: 431
file content (110 lines) | stat: -rw-r--r-- 2,910 bytes parent folder | download | duplicates (26)
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
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef VPX_VP8_COMMON_THREADING_H_
#define VPX_VP8_COMMON_THREADING_H_

#include "./vpx_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#if CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD

#if defined(_WIN32) && !HAVE_PTHREAD_H
/* Win32 */
#include <windows.h>
#else
/* pthreads */
#ifdef __APPLE__
#include <mach/mach_init.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#include <time.h>
#include <unistd.h>
#else
#include <semaphore.h>
#endif
#endif

/* Synchronization macros: Win32 and Pthreads */
#if defined(_WIN32) && !HAVE_PTHREAD_H
#define vp8_sem_t HANDLE
#define vp8_sem_init(sem, pshared, value) \
  (int)((*sem = CreateSemaphore(NULL, value, 32768, NULL)) == NULL)
#define vp8_sem_wait(sem) \
  (int)(WAIT_OBJECT_0 != WaitForSingleObject(*sem, INFINITE))
#define vp8_sem_post(sem) ReleaseSemaphore(*sem, 1, NULL)
#define vp8_sem_destroy(sem) \
  if (*sem) ((int)(CloseHandle(*sem)) == TRUE)
#define thread_sleep(nms) Sleep(nms)

#else

#ifdef __APPLE__
#define vp8_sem_t semaphore_t
#define vp8_sem_init(sem, pshared, value) \
  semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, value)
#define vp8_sem_wait(sem) semaphore_wait(*sem)
#define vp8_sem_post(sem) semaphore_signal(*sem)
#define vp8_sem_destroy(sem) semaphore_destroy(mach_task_self(), *sem)
#else
#include <errno.h>
#include <unistd.h>
#include <sched.h>
#define vp8_sem_t sem_t
#define vp8_sem_init sem_init
static INLINE int vp8_sem_wait(vp8_sem_t *sem) {
  int ret;
  while ((ret = sem_wait(sem)) == -1 && errno == EINTR) {
  }
  return ret;
}
#define vp8_sem_post sem_post
#define vp8_sem_destroy sem_destroy
#endif /* __APPLE__ */
/* Not Windows. Assume pthreads */

/* thread_sleep implementation: yield unless Linux/Unix. */
#if defined(__unix__) || defined(__APPLE__)
#define thread_sleep(nms)
/* {struct timespec ts;ts.tv_sec=0;
    ts.tv_nsec = 1000*nms;nanosleep(&ts, NULL);} */
#else
#define thread_sleep(nms) sched_yield();
#endif /* __unix__ || __APPLE__ */

#endif

#if VPX_ARCH_X86 || VPX_ARCH_X86_64
#include "vpx_ports/x86.h"
#else
#define x86_pause_hint()
#endif

#include "vpx_util/vpx_atomics.h"

static INLINE void vp8_atomic_spin_wait(
    int mb_col, const vpx_atomic_int *last_row_current_mb_col,
    const int nsync) {
  while (mb_col > (vpx_atomic_load_acquire(last_row_current_mb_col) - nsync)) {
    x86_pause_hint();
    thread_sleep(0);
  }
}

#endif /* CONFIG_OS_SUPPORT && CONFIG_MULTITHREAD */

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // VPX_VP8_COMMON_THREADING_H_