blob: 97332080c50fbaeaeb3cda4ab5fae36ac9565b0b [file] [log] [blame]
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +09001// 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 Hamaji702befc2016-01-27 17:21:39 +090017#include "log.h"
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +090018
Shinichiro Hamaji702befc2016-01-27 17:21:39 +090019thread::thread(const fn_t& fn) {
20 if (pthread_create(&th_, NULL, &thread::Run, new fn_t(fn)) != 0)
21 PERROR("pthread_create");
22}
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +090023
Shinichiro Hamaji702befc2016-01-27 17:21:39 +090024void thread::join() {
25 if (pthread_join(th_, NULL) != 0)
26 PERROR("pthread_join");
27}
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +090028
Shinichiro Hamaji702befc2016-01-27 17:21:39 +090029void* thread::Run(void* p) {
30 fn_t* fn = static_cast<fn_t*>(p);
31 (*fn)();
32 delete fn;
33 return NULL;
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +090034}