aboutsummaryrefslogtreecommitdiff
path: root/profiling/pthread_everywhere.h
blob: 7e12d66ba8669ff9f609bd0d78c44e4ec407512d (plain)
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
// Copyright 2017 The Gemmlowp Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// pthread_everywhere.h: Either includes <pthread.h> or implements a
// subset of pthread functionality on top of C++11 <thread> for portability.

#ifndef GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_
#define GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_

#include "pthread_everywhere.h"

#ifndef _WIN32
#define GEMMLOWP_USE_PTHREAD
#endif

#if defined GEMMLOWP_USE_PTHREAD
#include <pthread.h>
#else
// Implement a small subset of pthread on top of C++11 threads.
// The function signatures differ from true pthread functions in two ways:
//  - True pthread functions return int error codes, ours return void.
//    Rationale: the c++11 <thread> equivalent functions return void
//    and use exceptions to report errors; we don't want to deal with
//    exceptions in this code, so we couldn't meaningfully return errors
//    in the polyfill. Also, the gemmlowp code using these pthread functions
//    never checks their return values anyway.
//  - True pthread *_create/*_init functions take pointers to 'attribute'
//    structs; ours take nullptr_t. That is because gemmlowp always passes
//    nullptr at the moment, so any support we would code for non-null
//    attribs would be unused.
#include <thread>
#include <mutex>
#include <condition_variable>
#include <cstddef>
namespace gemmlowp {
using pthread_t = std::thread*;
using pthread_mutex_t = std::mutex*;
using pthread_cond_t = std::condition_variable*;
inline void pthread_create(pthread_t* thread, std::nullptr_t, 
  void *(*start_routine) (void *), void *arg) {
  *thread = new std::thread(start_routine, arg);
}
inline void pthread_join(pthread_t thread, std::nullptr_t) {
  thread->join();
}
inline void pthread_mutex_init(pthread_mutex_t *mutex, std::nullptr_t) {
  *mutex = new std::mutex;
}
inline void pthread_mutex_lock(pthread_mutex_t* mutex) {
  (*mutex)->lock();
}
inline void pthread_mutex_unlock(pthread_mutex_t* mutex) {
  (*mutex)->unlock();
}
inline void pthread_mutex_destroy(pthread_mutex_t *mutex) {
  delete *mutex;
}
inline void pthread_cond_init(pthread_cond_t *cond, std::nullptr_t) {
  *cond = new std::condition_variable;
}
inline void pthread_cond_signal(pthread_cond_t* cond) {
  (*cond)->notify_one();
}
inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
  std::unique_lock<std::mutex> lock(**mutex, std::adopt_lock);
  (*cond)->wait(lock);
  // detach lock from mutex so when we leave this conext
  // the lock is not released
  lock.release();
}
inline void pthread_cond_destroy(pthread_cond_t *cond) {
  delete *cond;
}
}  // end namespace gemmlowp
#endif

#endif  // GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_