Shinichiro Hamaji | 6bbf9e2 | 2016-01-19 18:59:13 +0900 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "thread.h" |
| 16 | |
Shinichiro Hamaji | 702befc | 2016-01-27 17:21:39 +0900 | [diff] [blame] | 17 | #include "log.h" |
Shinichiro Hamaji | 6bbf9e2 | 2016-01-19 18:59:13 +0900 | [diff] [blame] | 18 | |
Shinichiro Hamaji | 702befc | 2016-01-27 17:21:39 +0900 | [diff] [blame] | 19 | thread::thread(const fn_t& fn) { |
| 20 | if (pthread_create(&th_, NULL, &thread::Run, new fn_t(fn)) != 0) |
| 21 | PERROR("pthread_create"); |
| 22 | } |
Shinichiro Hamaji | 6bbf9e2 | 2016-01-19 18:59:13 +0900 | [diff] [blame] | 23 | |
Shinichiro Hamaji | 702befc | 2016-01-27 17:21:39 +0900 | [diff] [blame] | 24 | void thread::join() { |
| 25 | if (pthread_join(th_, NULL) != 0) |
| 26 | PERROR("pthread_join"); |
| 27 | } |
Shinichiro Hamaji | 6bbf9e2 | 2016-01-19 18:59:13 +0900 | [diff] [blame] | 28 | |
Shinichiro Hamaji | 702befc | 2016-01-27 17:21:39 +0900 | [diff] [blame] | 29 | void* thread::Run(void* p) { |
| 30 | fn_t* fn = static_cast<fn_t*>(p); |
| 31 | (*fn)(); |
| 32 | delete fn; |
| 33 | return NULL; |
Shinichiro Hamaji | 6bbf9e2 | 2016-01-19 18:59:13 +0900 | [diff] [blame] | 34 | } |