xref: /aosp_15_r20/external/gemmlowp/profiling/pthread_everywhere.h (revision 5f39d1b313f0528e11bae88b3029b54b9e1033e7)
1*5f39d1b3SJooyung Han // Copyright 2017 The Gemmlowp Authors. All Rights Reserved.
2*5f39d1b3SJooyung Han //
3*5f39d1b3SJooyung Han // Licensed under the Apache License, Version 2.0 (the "License");
4*5f39d1b3SJooyung Han // you may not use this file except in compliance with the License.
5*5f39d1b3SJooyung Han // You may obtain a copy of the License at
6*5f39d1b3SJooyung Han //
7*5f39d1b3SJooyung Han //     http://www.apache.org/licenses/LICENSE-2.0
8*5f39d1b3SJooyung Han //
9*5f39d1b3SJooyung Han // Unless required by applicable law or agreed to in writing, software
10*5f39d1b3SJooyung Han // distributed under the License is distributed on an "AS IS" BASIS,
11*5f39d1b3SJooyung Han // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5f39d1b3SJooyung Han // See the License for the specific language governing permissions and
13*5f39d1b3SJooyung Han // limitations under the License.
14*5f39d1b3SJooyung Han 
15*5f39d1b3SJooyung Han // pthread_everywhere.h: Either includes <pthread.h> or implements a
16*5f39d1b3SJooyung Han // subset of pthread functionality on top of C++11 <thread> for portability.
17*5f39d1b3SJooyung Han 
18*5f39d1b3SJooyung Han #ifndef GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_
19*5f39d1b3SJooyung Han #define GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_
20*5f39d1b3SJooyung Han 
21*5f39d1b3SJooyung Han #ifndef _WIN32
22*5f39d1b3SJooyung Han #define GEMMLOWP_USE_PTHREAD
23*5f39d1b3SJooyung Han #endif
24*5f39d1b3SJooyung Han 
25*5f39d1b3SJooyung Han #if defined GEMMLOWP_USE_PTHREAD
26*5f39d1b3SJooyung Han #include <pthread.h>
27*5f39d1b3SJooyung Han #else
28*5f39d1b3SJooyung Han // Implement a small subset of pthread on top of C++11 threads.
29*5f39d1b3SJooyung Han // The function signatures differ from true pthread functions in two ways:
30*5f39d1b3SJooyung Han //  - True pthread functions return int error codes, ours return void.
31*5f39d1b3SJooyung Han //    Rationale: the c++11 <thread> equivalent functions return void
32*5f39d1b3SJooyung Han //    and use exceptions to report errors; we don't want to deal with
33*5f39d1b3SJooyung Han //    exceptions in this code, so we couldn't meaningfully return errors
34*5f39d1b3SJooyung Han //    in the polyfill. Also, the gemmlowp code using these pthread functions
35*5f39d1b3SJooyung Han //    never checks their return values anyway.
36*5f39d1b3SJooyung Han //  - True pthread *_create/*_init functions take pointers to 'attribute'
37*5f39d1b3SJooyung Han //    structs; ours take nullptr_t. That is because gemmlowp always passes
38*5f39d1b3SJooyung Han //    nullptr at the moment, so any support we would code for non-null
39*5f39d1b3SJooyung Han //    attribs would be unused.
40*5f39d1b3SJooyung Han #include <condition_variable>
41*5f39d1b3SJooyung Han #include <cstddef>
42*5f39d1b3SJooyung Han #include <mutex>
43*5f39d1b3SJooyung Han #include <thread>
44*5f39d1b3SJooyung Han namespace gemmlowp {
45*5f39d1b3SJooyung Han using pthread_t = std::thread *;
46*5f39d1b3SJooyung Han using pthread_mutex_t = std::mutex *;
47*5f39d1b3SJooyung Han using pthread_cond_t = std::condition_variable *;
pthread_create(pthread_t * thread,std::nullptr_t,void * (* start_routine)(void *),void * arg)48*5f39d1b3SJooyung Han inline void pthread_create(pthread_t *thread, std::nullptr_t,
49*5f39d1b3SJooyung Han                            void *(*start_routine)(void *), void *arg) {
50*5f39d1b3SJooyung Han   *thread = new std::thread(start_routine, arg);
51*5f39d1b3SJooyung Han }
pthread_join(pthread_t thread,std::nullptr_t)52*5f39d1b3SJooyung Han inline void pthread_join(pthread_t thread, std::nullptr_t) { thread->join(); }
pthread_mutex_init(pthread_mutex_t * mutex,std::nullptr_t)53*5f39d1b3SJooyung Han inline void pthread_mutex_init(pthread_mutex_t *mutex, std::nullptr_t) {
54*5f39d1b3SJooyung Han   *mutex = new std::mutex;
55*5f39d1b3SJooyung Han }
pthread_mutex_lock(pthread_mutex_t * mutex)56*5f39d1b3SJooyung Han inline void pthread_mutex_lock(pthread_mutex_t *mutex) { (*mutex)->lock(); }
pthread_mutex_unlock(pthread_mutex_t * mutex)57*5f39d1b3SJooyung Han inline void pthread_mutex_unlock(pthread_mutex_t *mutex) { (*mutex)->unlock(); }
pthread_mutex_destroy(pthread_mutex_t * mutex)58*5f39d1b3SJooyung Han inline void pthread_mutex_destroy(pthread_mutex_t *mutex) { delete *mutex; }
pthread_cond_init(pthread_cond_t * cond,std::nullptr_t)59*5f39d1b3SJooyung Han inline void pthread_cond_init(pthread_cond_t *cond, std::nullptr_t) {
60*5f39d1b3SJooyung Han   *cond = new std::condition_variable;
61*5f39d1b3SJooyung Han }
pthread_cond_signal(pthread_cond_t * cond)62*5f39d1b3SJooyung Han inline void pthread_cond_signal(pthread_cond_t *cond) { (*cond)->notify_one(); }
pthread_cond_broadcast(pthread_cond_t * cond)63*5f39d1b3SJooyung Han inline void pthread_cond_broadcast(pthread_cond_t *cond) {
64*5f39d1b3SJooyung Han   (*cond)->notify_all();
65*5f39d1b3SJooyung Han }
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)66*5f39d1b3SJooyung Han inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
67*5f39d1b3SJooyung Han   std::unique_lock<std::mutex> lock(**mutex, std::adopt_lock);
68*5f39d1b3SJooyung Han   (*cond)->wait(lock);
69*5f39d1b3SJooyung Han   // detach lock from mutex so when we leave this conext
70*5f39d1b3SJooyung Han   // the lock is not released
71*5f39d1b3SJooyung Han   lock.release();
72*5f39d1b3SJooyung Han }
pthread_cond_destroy(pthread_cond_t * cond)73*5f39d1b3SJooyung Han inline void pthread_cond_destroy(pthread_cond_t *cond) { delete *cond; }
74*5f39d1b3SJooyung Han }  // end namespace gemmlowp
75*5f39d1b3SJooyung Han #endif
76*5f39d1b3SJooyung Han 
77*5f39d1b3SJooyung Han #endif  // GEMMLOWP_PROFILING_PTHREAD_EVERYWHERE_H_
78